home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / ENGINE / IPASM.ASM < prev    next >
Encoding:
Assembly Source File  |  1991-07-23  |  10.6 KB  |  502 lines

  1. ;
  2. ;  TCP/IP support routines
  3. ;****************************************************************************
  4. ;*                                                                          *
  5. ;*                                                                          *
  6. ;*      part of NCSA Telnet                                                 *
  7. ;*      by Tim Krauskopf, VT100 by Gaige Paulsen, Tek by Aaron Contorer     *
  8. ;*                                                                          *
  9. ;*      National Center for Supercomputing Applications                     *
  10. ;*      152 Computing Applications Building                                 *
  11. ;*      605 E. Springfield Ave.                                             *
  12. ;*      Champaign, IL  61820                                                *
  13. ;*                                                                          *
  14. ;****************************************************************************
  15. ;
  16.     NAME    IPASM
  17. ;Microsoft EQU 1
  18. ;Lattice EQU 1
  19. ifndef Microsoft
  20.     ifndef Lattice
  21.         if2
  22.             %out
  23.             %out ERROR: You have to specify "/DMicrosoft" or "/DLattice" on the
  24.             %out        MASM command line to determine the type of assembly.
  25.             %out
  26.         endif
  27.         end
  28.     endif
  29. endif
  30. ;
  31. ;******************************************************************
  32. ;*
  33. ;*    We need to set up a stack for netsleep when we exit to DOS.
  34.  
  35. ;NEWSTACK SEGMENT PARA STACK 'STACK'
  36. ;    dw 2048 dup(?)
  37. ;STACKEND label far
  38. ;NEWSTACK    ends
  39.  
  40.  X   EQU     6
  41. ifdef Microsoft
  42. ;DGROUP  group _DATA
  43. ;_DATA segment public 'DATA'
  44. ;    assume DS:DGROUP
  45. .MODEL    LARGE
  46. .DATA
  47. else    
  48.     INCLUDE    DOS.MAC
  49.     SETX
  50.     DSEG
  51. endif
  52.  
  53. OLDSS dw 1 dup(?)
  54. OLDSP dw 1 dup(?)
  55.  
  56. extrn STKHQQ:word
  57. OLDSTKHQQ   dw ?
  58.  
  59. ;ifdef NOT_WORKING
  60. NEWSTACK dw 2048 dup(?)     ; define a stack for netsleep when we shell to DOS
  61. STACKEND label far
  62. ;endif
  63.  
  64. ifdef Microsoft
  65. ;_DATA ends
  66. ;
  67. ;_TEXT    segment public 'CODE'
  68. ;    assume CS:_TEXT
  69. .CODE
  70.     PUBLIC  _IPCHECK, _TCPCHECK, _MOVEBYTES, _LONGSWAP, _INTSWAP, _DBG
  71. else
  72.     ENDDS
  73.     PSEG
  74.     PUBLIC    IPCHECK,TCPCHECK,MOVEBYTES,LONGSWAP,INTSWAP,DBG
  75. endif    
  76. ;
  77. ;  Routines for general use by the communications programs
  78. ;
  79. ;
  80. ;************************************************************************
  81. ;  DBG
  82. ;  provides a synch point for debugging
  83. ;
  84. ifdef Microsoft
  85. _dbg     proc    far
  86. else
  87. dbg    proc    far
  88. endif
  89.     nop
  90.     nop
  91.     nop
  92.     ret
  93. ifdef Microsoft
  94. _dbg endp
  95. else
  96. dbg endp
  97. endif
  98. ;
  99. ;*************************************************************************
  100. ;  Internet header checksum
  101. ;    header checksum is calculated for a higher level program to verify
  102. ;
  103. ;  USAGE:  ipcheck((IPKT *)ptr,(int)len)
  104. ;
  105. ;  this proc knows that the IP header length is found in the first byte
  106. ;
  107. ifdef Microsoft
  108. _IPCHECK    PROC    FAR
  109. else
  110. IPCHECK    PROC    FAR
  111. endif
  112.     PUSH    BP
  113.     MOV        BP,SP
  114.     PUSH    DS
  115.     PUSH    ES
  116.     PUSH    SI
  117.     PUSH    DI
  118.  
  119.     MOV        AX,[BP+X+2]        ; ds for input data
  120.     MOV        DS,AX
  121.     MOV        SI,[BP+X]        ; pointer to data
  122.     MOV        CX,[BP+X+4]        ; count of words to test
  123.     XOR        BX,BX
  124.     CLC
  125. CHKSUM:
  126.     LODSW                    ; get next word
  127.     ADC        BX,AX            ; keep adding
  128.     LOOP    CHKSUM            ; til' done
  129.     ADC        BX,0            ; adds the carry bit in
  130. ;
  131.     NOT        BX                ; take one more 1-complement
  132.     MOV        AX,BX
  133.  
  134.     POP        DI
  135.     POP        SI
  136.     POP        ES
  137.     POP        DS
  138.     POP        BP
  139.     RET
  140. ifdef Microsoft
  141. _IPCHECK    ENDP
  142. else
  143. IPCHECK    ENDP
  144. endif
  145. ;
  146. ;  TCP checksum, has two parts, including support for a pseudo-header
  147. ;
  148. ;  usage:   tcpcheck(psptr,tcpptr,tcplen)
  149. ;            char *psptr,*tcpptr;  pointers to pseudo header and real header
  150. ;            int tcplen            length of tcp packet in checksum
  151. ;
  152. ifdef Microsoft
  153. _TCPCHECK    PROC    FAR
  154. else
  155. TCPCHECK    PROC    FAR
  156. endif
  157.     PUSH    BP
  158.     MOV        BP,SP
  159.     PUSH    DS
  160.     PUSH    ES
  161.     PUSH    SI
  162.     PUSH    DI
  163.  
  164.     MOV        AX,[BP+X+2]        ; ds for input data for pseudo-hdr
  165.     MOV        DS,AX
  166.     MOV        SI,[BP+X]        ; pointer to data
  167.     MOV        CX,6            ; length of p-hdr in words
  168.     XOR        BX,BX           ; clear to begin
  169.     CLC
  170. PCHKSUM:
  171.     LODSW                    ; get next word
  172.     ADC        BX,AX            ; keep adding
  173.     LOOP    PCHKSUM            ; til' done
  174.     ADC        BX,0            ; adds the carry bit in
  175. ;
  176. ; NOW THE REAL THING
  177. ;
  178.     MOV        AX,[BP+X+6]        ; ds of real stuff
  179.     MOV        DS,AX
  180.     MOV        SI,[BP+X+4]        ; pointer
  181.     MOV        CX,[BP+X+8]        ; count of bytes to test
  182.     MOV        DX,CX            ; keep a copy
  183.     SHR        CX,1            ; divide by two, round down
  184.     CLC
  185. RCHKSUM:
  186.     LODSW
  187.     ADC        BX,AX            ; add to previous running sum
  188.     LOOP    RCHKSUM    
  189.     ADC        BX,0            ; add the last carry in again
  190.     AND        DX,1            ; odd # of bytes?
  191.     JZ        NOTODD
  192.     LODSB                    ; get that last byte
  193.     XOR        AH,AH            ; clear the high portion
  194.     ADD        BX,AX            ; add the last one in
  195.     ADC        BX,0            ; add the carry in, too
  196. NOTODD:
  197.     NOT        BX                ; take one more 1-complement
  198.     MOV        AX,BX
  199.     POP        DI
  200.     POP        SI
  201.     POP        ES
  202.     POP        DS
  203.     POP        BP
  204.     RET
  205. ifdef Microsoft    
  206. _TCPCHECK    ENDP
  207. else
  208. TCPCHECK    ENDP
  209. endif
  210.  
  211. ;
  212. ;********************************************************************
  213. ;  New movebytes
  214. ;  Move an arbitrary number of bytes from one location to another.
  215. ;
  216. ;  Usage:
  217. ;  movebytes(to,from,count)
  218. ;   char *to,*from;
  219. ;   int16 count
  220. ;   moves < 64K from one 4 byte pointer to another.  Does not handle
  221. ;   overlap, but does copy quickly.
  222. ;
  223. ifdef Microsoft
  224. _MOVEBYTES    PROC    FAR
  225. else
  226. MOVEBYTES    PROC    FAR
  227. endif
  228.     PUSH    BP
  229.     MOV        BP,SP
  230.     PUSH    DS
  231.     PUSH    ES
  232.     PUSH    SI
  233.     PUSH    DI
  234.  
  235.     LES        DI,[BP+X]            ; WHERE TO PUT IT
  236.     LDS        SI,[BP+X+4]            ; WHERE TO GET IT
  237.     MOV        CX,[BP+X+8]            ; HOW MANY TO MOVE
  238.     SHR     CX,1                ; MAKE INTO A WORD COUNT
  239.     REP     MOVSW
  240.     ADC        CX,CX                ; GET THE ODD BYTE COUNT BACK
  241.     REP     MOVSB
  242.     POP        DI
  243.     POP        SI
  244.     POP        ES
  245.     POP        DS
  246.     POP        BP
  247.     RET
  248. ifdef Microsoft
  249. _MOVEBYTES    ENDP
  250. else
  251. MOVEBYTES    ENDP
  252. endif
  253.  
  254. ;
  255. ;*************************************************************************
  256. ;  longswap
  257. ;    swap the bytes of a long integer from PC
  258. ;  order (reverse) to in-order.  This will work both ways.
  259. ;  returns the new long value
  260. ;  usage:
  261. ;      l2 = longswap(l)
  262. ;    long l;
  263. ;
  264. ifdef Microsoft
  265. _LONGSWAP    PROC    FAR
  266.     PUSH    BP
  267.     MOV        BP,SP
  268.  
  269.     MOV        AX,[BP+X+2]        ; HIGH BYTES OF THE LONG INT
  270.     MOV        DX,[BP+X]        ; LOW BYTES OF THE LONG INT
  271. ;
  272. ;  GET THE DATA
  273. ;
  274.     XCHG    AH,AL            ; SWAP THEM, THESE ARE NOW LOW
  275.     XCHG    DH,DL            ; SWAP THE OTHERS
  276.     POP        BP
  277.     RET
  278. _LONGSWAP    ENDP
  279. else
  280. LONGSWAP    PROC    FAR
  281.     PUSH    BP
  282.     MOV        BP,SP
  283.     MOV        BX,[BP+X+2]        ; HIGH BYTES OF THE LONG INT
  284.     MOV        AX,[BP+X]        ; LOW BYTES OF THE LONG INT
  285. ;
  286. ;  GET THE DATA
  287. ;
  288.     XCHG    AH,AL            ; SWAP THEM, THESE ARE NOW LOW
  289.     XCHG    BH,BL            ; SWAP THE OTHERS
  290.     POP        BP
  291.     RET
  292. LONGSWAP    ENDP
  293. endif
  294. ;
  295. ;*************************************************************************
  296. ;  INTSWAP
  297. ;    swap the bytes of an integer, returns the swapped integer
  298. ;
  299. ;   usage:      i = intswap(i);
  300. ;
  301. ifdef Microsoft
  302. _INTSWAP    PROC    FAR
  303. else
  304. INTSWAP    PROC    FAR
  305. endif
  306.     MOV        BX,SP
  307.     MOV     AX,SS:[BX+4]
  308.     XCHG    AH,AL
  309.     RET
  310. ifdef Microsoft
  311. _INTSWAP    ENDP
  312. else
  313. INTSWAP    ENDP
  314. endif
  315.  
  316. ;
  317. ;**************************************************************************
  318. ;
  319. ;  Routines to install and deinstall a timer routine which calls
  320. ;  netsleep(0);
  321. ;  The timer is set to go off every 1/2 second to check for packets 
  322. ;  in the incoming packet buffer.  We use the user-hook into the system 
  323. ;  timer which occurs every 1/18th of a second.
  324. ;
  325. ;
  326. TIMEINT        EQU    4*1CH        ; User hook to timer int
  327.  
  328. ifdef Microsoft
  329. ifdef Watcom
  330.     EXTRN    netsleep_:FAR    ; C routine which gets called from handler
  331. else
  332.     EXTRN    _netsleep:FAR    ; C routine which gets called from handler
  333. endif
  334.     PUBLIC    _TINST,_TDEINST
  335. else    
  336.     EXTRN    netsleep:FAR    ; C routine which gets called from handler
  337.     PUBLIC    TINST,TDEINST
  338. endif    
  339. ;*************************************************************************
  340. ;
  341. ;  Take out the timer interrupt handler, restore previous value
  342. ;
  343. ifdef Microsoft
  344. _TDEINST    PROC    FAR
  345. else
  346. TDEINST    PROC    FAR
  347. endif
  348.     MOV        CX,CS:TIP        ; GET OLD IP FROM SAVE SPOT
  349.     MOV        DX,CS:TCS        ; GET OLD CS FROM SAVE SPOT
  350.     MOV        BX,TIMEINT        ; INTERRUPT IN TABLE FOR TIMER
  351.     PUSH    DS
  352.     XOR        AX,AX            ; SYSTEM INTERRUPT TABLE
  353.     MOV        DS,AX        
  354.     CLI
  355.     MOV        [BX],CX            ; STORE OLD IP INTO THE TABLE
  356.     INC        BX
  357.     INC        BX                ; MOVE POINTER IN INTERRUPT TABLE
  358.     MOV        [BX],DX            ; STORE OLD CS INTO THE TABLE
  359.     STI
  360.     POP        DS
  361.     RET
  362. ifdef Microsoft
  363. _TDEINST    ENDP
  364. else
  365. TDEINST    ENDP
  366. endif
  367. ;
  368. ;
  369. ;  install the timer interrupt handler, the handler is technically
  370. ;  part of this procedure.
  371. ;
  372. ifdef Microsoft
  373. _TINST    PROC    FAR
  374. else
  375. TINST    PROC    FAR
  376. endif
  377.     XOR        AX,AX
  378.     MOV        CS:TENTER,AL    ; CLEAR THIS FLAG
  379.     MOV        CS:TMYDS,DS        ; STORE FOR USE BY HANDLER
  380.     MOV        BX,TIMEINT        ; INTERRUPT IN TABLE FOR TIMER (1c)
  381.     PUSH    DS
  382.     XOR        AX,AX            ; SYSTEM INTERRUPT TABLE
  383.     MOV        DS,AX        
  384.     MOV        AX,OFFSET THAND    ; WHERE THE HANDLER IS
  385.     CLI
  386.     MOV        DX,[BX]            ; KEEP COPY OF THE IP
  387.     MOV        [BX],AX            ; STORE IP INTO THE TABLE
  388.     INC        BX
  389.     INC        BX                ; MOVE POINTER IN INTERRUPT TABLE
  390.     MOV        CX,[BX]            ; KEEP COPY OF THE CS, TOO
  391.     MOV        AX,CS
  392.     MOV        [BX],AX            ; STORE NEW CS INTO THE TABLE
  393.     STI
  394.     POP    DS
  395.     MOV    CS:TIP,DX            ; STORE THEM AWAY
  396.     MOV    CS:TCS,CX
  397.     RET
  398. ;
  399. ;  Code segment addressable data for keeping track of the interrupt handler
  400. ;  stuff
  401. ;
  402. TMYDS        DW    00H            ; THE DATA SEGMENT FOR THIS ASSEMBLY CODE
  403. TICNT        DB    0            ; COUNTER OF 1/18THS SEC
  404. TENTER         DB  00
  405. TIP          DW  00
  406. TCS          DW  00
  407. ;
  408. ;   The handler itself.
  409. ;
  410. THAND:                           ; not a public name, only handles ints
  411.     STI
  412.     PUSH    DS
  413.     PUSH     ES
  414.     PUSH    AX
  415.     PUSH    BX
  416.     PUSH    CX
  417.     PUSH    DX
  418.     PUSH    DI
  419.     PUSH    SI
  420.  
  421.     CLD                        ; ALL MOVES WILL BE FORWARD
  422.     MOV        AL,CS:TENTER
  423.     OR        AL,AL
  424.     JNZ        TIME2
  425.     MOV        AL,1
  426.     MOV     CS:TENTER,AL    ; SET FLAG TO INDICATE BUSY
  427.     INC        CS:TICNT
  428.     MOV        AL,CS:TICNT        ; COUNTER FOR US
  429.     AND        AL,7            ; SEE IF # MOD 8 = 0
  430.     JNZ        TSKIP            ; SKIP 7 OUT OF 8 TIMES
  431. ;
  432.     MOV        AL,60H            ; EOI FOR TIMER INT
  433.     OUT        20H,AL            ; LET LOWER INTERRUPTS IN
  434. ;
  435. ;  SET UP CORRECT DS
  436. ;
  437.     MOV        DS,CS:TMYDS        ; GET CORRECT DS
  438. ;
  439. ;  do we have to set up our own stack here?
  440. ;
  441.     MOV        AX,SS
  442.     MOV        OLDSS,AX
  443.     MOV        OLDSP,SP
  444.     CLI
  445. ;    MOV     AX,seg NEWSTACK
  446.     MOV     AX,seg STACKEND
  447.     MOV        SS,AX
  448. ;    MOV     SP,OFFSET STACKEND
  449.     MOV     SP,OFFSET DGROUP:STACKEND
  450.     mov     ax,STKHQQ
  451.     mov     OLDSTKHQQ,ax
  452.     mov     ax,offset DGROUP:NEWSTACK
  453.     mov     STKHQQ,ax
  454.     STI
  455.     XOR        AX,AX
  456.     PUSH     AX
  457. ifdef Microsoft
  458. ifdef Watcom
  459.     CALL netsleep_
  460. else
  461.     CALL _netsleep
  462. endif
  463. else    
  464.     CALL netsleep
  465. endif
  466.     POP        AX
  467.     CLI    
  468.     MOV        AX,OLDSS
  469.     MOV        SS,AX
  470.     MOV        SP,OLDSP
  471.     mov     ax,OLDSTKHQQ
  472.     mov     STKHQQ,ax
  473.     STI
  474. TSKIP:
  475.     XOR        AL,AL
  476.     MOV        CS:TENTER,AL    ; REENTER FLAG, DONE NOW
  477. TIME2:
  478.     POP     SI
  479.     POP        DI
  480.     POP        DX
  481.     POP        CX
  482.     POP        BX
  483.     POP        AX
  484.     POP        ES
  485.     POP        DS
  486. ;
  487. ;   forward to any other existing routines
  488. ;
  489.     JMP     DWORD PTR CS:TIP
  490. ifdef Microsoft
  491. _TINST        ENDP
  492. else
  493. TINST        ENDP
  494. endif
  495. ifdef Microsoft
  496. ;_TEXT ends
  497.  
  498. else
  499.     ENDPS
  500. endif
  501.     END
  502.