Subversion Repositories Kolibri OS

Rev

Rev 115 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 115 Rev 261
Line 14... Line 14...
14
;;         Added a timer per socket to allow delays when rx window ;;
14
;;         Added a timer per socket to allow delays when rx window ;;
15
;;         gets below 1KB                                          ;;
15
;;         gets below 1KB                                          ;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line -... Line 17...
-
 
17
 
17
 
18
 
18
 
19
 
19
;*******************************************************************
20
;*******************************************************************
20
;   Interface
21
;   Interface
21
;      The interfaces defined in ETHERNET.INC plus:
22
;      The interfaces defined in ETHERNET.INC plus:
Line 25... Line 26...
25
;      app_socket_handler
26
;      app_socket_handler
26
;      checksum
27
;      checksum
27
;
28
;
28
;*******************************************************************
29
;*******************************************************************
Line -... Line 30...
-
 
30
 
-
 
31
uglobal
-
 
32
StackCounters:
-
 
33
  dumped_rx_count:    dd  0
-
 
34
  arp_tx_count:       dd  0
-
 
35
  arp_rx_count:       dd  0
-
 
36
  ip_rx_count:        dd  0
-
 
37
  ip_tx_count:        dd  0
Line -... Line 38...
-
 
38
endg
-
 
39
 
-
 
40
; socket buffers
Line 29... Line -...
29
 
-
 
30
 
-
 
31
 
-
 
32
;
-
 
33
;   IP Packet after reception - Normal IP packet format
-
 
34
;
-
 
35
;    0                   1                   2                   3
-
 
36
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-
 
37
;
-
 
38
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
39
;0  |Version|  IHL  |Type of Service|       Total Length            |
-
 
40
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
41
;4  |         Identification        |Flags|      Fragment Offset    |
-
 
42
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
43
;8  |  Time to Live |    Protocol   |         Header Checksum       |
-
 
44
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
45
;12 |                       Source Address                          |
-
 
46
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
47
;16 |                    Destination Address                        |
-
 
48
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
49
;   |      Data                                                     |
-
 
50
;   +-+-+-..........                                               -+
-
 
51
 
-
 
52
 
-
 
53
;   TCP Payload ( Data field in IP datagram )
-
 
54
;
-
 
55
;    0                   1                   2                   3
-
 
56
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-
 
57
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41
SOCKETBUFFSIZE     equ        4096  ; state + config + buffer.
58
;20 |          Source Port          |       Destination Port        |
-
 
59
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
60
;24 |                        Sequence Number                        |
-
 
61
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
62
;28 |                    Acknowledgment Number                      |
-
 
63
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
64
;32 |  Data |           |U|A|P|R|S|F|                               |
-
 
65
;   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
-
 
66
;   |       |           |G|K|H|T|N|N|                               |
-
 
67
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
68
;36 |           Checksum            |         Urgent Pointer        |
-
 
69
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
70
;40 |                    Options                    |    Padding    |
-
 
71
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
72
;   |                             data
-
 
73
 
-
 
74
 
-
 
75
;
-
 
76
;   UDP Payload ( Data field in IP datagram )
-
 
77
;
-
 
78
;    0                   1                   2                   3
-
 
79
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-
 
80
;
-
 
81
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
82
;   |       Source Port             |      Destination Port         |
-
 
83
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
84
;   | Length ( UDP Header + Data )  |           Checksum            |
-
 
85
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
86
;   |       UDP Data                                                |
-
 
87
;   +-+-+-..........                                               -+
-
 
88
;
-
 
89
 
-
 
90
 
-
 
91
;
-
 
92
;  Socket Descriptor + Buffer
-
 
93
;
-
 
94
;    0                   1                   2                   3
-
 
95
;    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
-
 
96
;
-
 
97
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
98
;   |                    Status ( of this buffer )                  |
-
 
99
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
100
;   |  Application Process ID                                       |
-
 
101
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
102
;   |                  Local IP Address                             |
-
 
103
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
104
;   | Local IP Port                 | Unused ( set to 0 )           |
-
 
105
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
106
;   |                  Remote IP Address                            |
-
 
107
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
108
;   | Remote IP Port                | Unused ( set to 0 )           |
-
 
109
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
110
; 24|   Rx Data Count                                   INTEL format|
-
 
111
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
112
; 28|                 TCB STATE                         INTEL format|
-
 
113
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
114
; 32|   TCB Timer (seconds)                             INTEL format|
-
 
115
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
116
; 36| ISS (Inital Sequence # used by this connection )   INET format|
-
 
117
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
118
; 40| IRS ( Inital Receive Sequence # )                  INET format|
-
 
119
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
120
; 44| SND.UNA  Seq # of unack'ed sent packets            INET format|
-
 
121
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
122
; 48| SND.NXT  Next send seq # to use                    INET format|
-
 
123
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
124
; 52| SND.WND  Send window                               INET format|
-
 
125
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
126
; 56| RCV.NXT  Next expected receive sequence #          INET format|
-
 
127
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
128
; 60| RCV.WND  Receive window                            INET format|
-
 
129
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
130
; 64| SEG.LEN  Segment length                           INTEL format|
-
 
131
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
132
; 68| SEG.WND  Segment window                           INTEL format|
-
 
133
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
134
; 72| Retransmit queue # NOW WINDOW SIZE TIMER          INTEL format|
-
 
135
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
-
 
136
; 76|       RX Data                                                 |
-
 
137
;   +-+-+-..........                                               -+
-
 
138
 
-
 
139
 
-
 
140
 
-
 
141
; IP protocol numbers
-
 
142
PROTOCOL_ICMP     equ      1
-
 
Line 143... Line 42...
143
PROTOCOL_TCP      equ      6
42
SOCKETHEADERSIZE   equ        76    ; thus 4096 - SOCKETHEADERSIZE bytes data
144
PROTOCOL_UDP      equ      17
43
 
145
 
44
NUM_SOCKETS        equ        16    ; Number of open sockets supported. Was 20
146
 
45
 
147
; TIPBUFF status values
46
; IPBUFF status values
Line 148... Line 47...
148
BUFF_EMPTY         equ     0
47
BUFF_EMPTY         equ     0
Line 149... Line -...
149
BUFF_RX_FULL       equ     1
-
 
150
BUFF_ALLOCATED     equ     2
-
 
151
BUFF_TX_FULL       equ     3
-
 
152
 
-
 
153
NUM_IPBUFFERS      equ     20    ; buffers allocated for TX/RX
-
 
154
 
-
 
155
SOCK_EMPTY         equ     0        ; socket not in use
-
 
156
SOCK_OPEN          equ     1        ; open issued, but no data sent
-
 
157
 
-
 
158
; TCP opening modes
-
 
159
SOCKET_PASSIVE     equ     0
-
 
160
SOCKET_ACTIVE      equ     1
-
 
161
 
-
 
162
; TCP TCB states
-
 
163
TCB_LISTEN         equ        1
-
 
164
TCB_SYN_SENT       equ        2
-
 
165
TCB_SYN_RECEIVED   equ        3
-
 
166
TCB_ESTABLISHED    equ        4
-
 
167
TCB_FIN_WAIT_1     equ        5
-
 
168
TCB_FIN_WAIT_2     equ        6
-
 
169
TCB_CLOSE_WAIT     equ        7
-
 
170
TCB_CLOSING        equ        8
-
 
171
TCB_LAST_ACK       equ        9
-
 
172
TCB_TIME_WAIT      equ        10
-
 
173
TCB_CLOSED         equ        11
-
 
174
 
-
 
175
TWOMSL              equ     10      ; # of secs to wait before closing socket
-
 
176
 
-
 
177
; socket buffers
-
 
178
SOCKETBUFFSIZE     equ        4096  ; state + config + buffer.
48
BUFF_RX_FULL       equ     1
179
SOCKETHEADERSIZE   equ        76    ; thus 4096 - SOCKETHEADERSIZE bytes data
49
BUFF_ALLOCATED     equ     2
180
 
50
BUFF_TX_FULL       equ     3
181
NUM_SOCKETS        equ        16    ; Number of open sockets supported. Was 20
51
 
182
 
52
NUM_IPBUFFERS      equ     20    ; buffers allocated for TX/RX
Line 183... Line 53...
183
 
53
 
184
NUMQUEUES          equ        4
54
NUMQUEUES          equ        4
185
EMPTY_QUEUE        equ        0
55
EMPTY_QUEUE        equ        0
186
IPIN_QUEUE         equ        1
56
IPIN_QUEUE         equ        1
187
IPOUT_QUEUE        equ        2
-
 
188
NET1OUT_QUEUE      equ        3
-
 
Line 189... Line 57...
189
 
57
IPOUT_QUEUE        equ        2
190
NO_BUFFER          equ        0xFFFF
58
NET1OUT_QUEUE      equ        3
191
IPBUFFSIZE         equ        1500                ; MTU of an ethernet packet
59
 
Line 206... Line 74...
206
stack_data           equ   0x704000
74
stack_data           equ   0x704000
207
stack_data_end       equ   0x71ffff
75
stack_data_end       equ   0x71ffff
Line 208... Line 76...
208
 
76
 
209
; 32 bit word
77
; 32 bit word
-
 
78
stack_config         equ   stack_data
210
stack_config         equ   stack_data
79
 
211
; 32 bit word - IP Address in network format
80
; 32 bit word - IP Address in network format
212
stack_ip             equ   stack_data + 4
-
 
213
; 1 byte. 0 == inactive, 1 = active
-
 
-
 
81
stack_ip             equ   stack_data + 4
214
slip_active          equ   stack_data + 8       ; no longer used
82
 
215
; 1 byte. 0 == inactive, 1 = active
83
; 1 byte. 0 == inactive, 1 = active
216
ethernet_active      equ   stack_data + 9
-
 
217
unused               equ   stack_data + 10
-
 
218
;  word. Buffer number, -1 if none
-
 
219
rx_buff_ptr          equ   stack_data + 12
-
 
220
; dword. Buffer number, -1 if none
-
 
221
tx_buff_ptr          equ   stack_data + 16
84
ethernet_active      equ   stack_data + 9
222
; byte.
-
 
223
slip_rx_state        equ   stack_data + 20      ; no longer used
85
 
224
; byte
-
 
225
slip_tx_state        equ   stack_data + 21      ; no longer used
-
 
226
; dword. Index into data
-
 
227
rx_data_ptr          equ   stack_data + 22
86
 
228
; dword. Index into data
-
 
229
tx_data_ptr          equ   stack_data + 26
-
 
230
; word. Count of bytes to send
-
 
-
 
87
; TODO :: empty memory area
231
tx_msg_len           equ   stack_data + 30
88
 
232
; Address of selected socket
89
; Address of selected socket
233
sktAddr              equ   stack_data + 32
90
sktAddr              equ   stack_data + 32
234
; Parameter to checksum routine - data ptr
91
; Parameter to checksum routine - data ptr
235
checkAdd1            equ   stack_data + 36
92
checkAdd1            equ   stack_data + 36
Line 248... Line 105...
248
; receive and transmit IP buffer allocation
105
; receive and transmit IP buffer allocation
249
sockets              equ   stack_data + 62
106
sockets              equ   stack_data + 62
250
Next_free2           equ   sockets + (SOCKETBUFFSIZE * NUM_SOCKETS)
107
Next_free2           equ   sockets + (SOCKETBUFFSIZE * NUM_SOCKETS)
251
; 1560 byte buffer for rx / tx ethernet packets
108
; 1560 byte buffer for rx / tx ethernet packets
252
Ether_buffer         equ   Next_free2
109
Ether_buffer         equ   Next_free2
253
Next_free3           equ   Ether_buffer + 1560
110
Next_free3           equ   Ether_buffer + 1518
254
last_1sTick          equ   Next_free3
111
last_1sTick	     equ   Next_free3
255
IPbuffs              equ   Next_free3 + 1
112
IPbuffs              equ   Next_free3 + 1
256
queues               equ   IPbuffs + ( NUM_IPBUFFERS * IPBUFFSIZE )
113
queues               equ   IPbuffs + ( NUM_IPBUFFERS * IPBUFFSIZE )
257
queueList            equ   queues + (2 * NUMQUEUES)
114
queueList            equ   queues + (2 * NUMQUEUES)
258
last_1hsTick         equ   queueList + ( 2 * NUMQUEUEENTRIES )
115
last_1hsTick         equ   queueList + ( 2 * NUMQUEUEENTRIES )
Line 265... Line 122...
265
 
122
 
266
resendQ             equ     0x770000
123
resendQ             equ     0x770000
Line -... Line 124...
-
 
124
resendBuffer        equ     resendQ + ( 4 * NUMRESENDENTRIES ) ; for TCP
-
 
125
 
-
 
126
 
-
 
127
; simple macro for memory set operation
-
 
128
macro _memset_dw adr,value,amount
-
 
129
{
-
 
130
	mov 	edi, adr
-
 
131
	mov 	ecx, amount
-
 
132
	if value = 0
-
 
133
		xor	eax, eax
-
 
134
	else
-
 
135
		mov	eax, value
-
 
136
	end if
-
 
137
	cld
-
 
138
	rep 	stosd
-
 
139
}
-
 
140
 
-
 
141
 
-
 
142
; Below, the main network layer source code is included
-
 
143
;
-
 
144
include "queue.inc"
-
 
145
include "eth_drv/ethernet.inc"
-
 
146
include "ip.inc"
-
 
147
include "icmp.inc"
-
 
148
include "tcp.inc"
267
resendBuffer        equ     resendQ + ( 4 * NUMRESENDENTRIES ) ; for TCP
149
include "udp.inc"
268
 
150
include "socket.inc"
269
 
151
 
270
;***************************************************************************
152
;***************************************************************************
271
;   Function
153
;   Function
Line 276... Line 158...
276
;       on startup, the stack is inactive, and consumes no resources
158
;       on startup, the stack is inactive, and consumes no resources
277
;       This is a kernel function, called prior to the OS main loop
159
;       This is a kernel function, called prior to the OS main loop
278
;       in set_variables
160
;       in set_variables
279
;
161
;
280
;***************************************************************************
162
;***************************************************************************
281
stack_init:
-
 
282
    xor     eax,eax
-
 
283
    mov     edi,stack_data_start
-
 
284
    mov     ecx,0x20000 / 4  ; Assume that we have 128KB of data
-
 
285
    cld
-
 
286
    rep     stosd
-
 
287
 
-
 
288
    ; Initialise TCP resend queue data structures
-
 
289
    mov     eax, 0xFFFFFFFF
-
 
290
    mov     edi, resendQ
-
 
291
    mov     ecx, NUMRESENDENTRIES  ; 1 dword per entry
-
 
292
    cld
-
 
293
    rep     stosd
-
 
294
 
-
 
295
 
-
 
296
    mov     eax, 0xFFFFFFFF
-
 
297
    mov     [rx_buff_ptr], eax
-
 
298
    mov     [tx_buff_ptr], eax
-
 
Line 299... Line -...
299
 
-
 
300
    ; Put in some defaults : slip, 0x3f8, 4, ip=192.168.1.22
-
 
301
    ; Saves me entering them each boot up when debugging
163
 
302
    mov     eax, 0x03f80401
164
stack_init:
303
    mov     [stack_config], eax
165
	; Init two address spaces with default values
304
    mov     eax, 0xc801a8c0
166
	_memset_dw	stack_data_start, 0, 0x20000/4
Line -... Line 167...
-
 
167
	_memset_dw	resendQ, 0xFFFFFFFF, NUMRESENDENTRIES
305
    mov     [stack_ip], eax
168
 
Line 306... Line 169...
306
 
169
	; Queries initialization
307
    call    queueInit
170
	call	queueInit
308
 
171
 
309
    ; The following block sets up the 1s timer
172
	; The following block sets up the 1s timer
310
    mov     al,0x0
173
	mov	al, 0x0
311
    out     0x70,al
-
 
312
    in      al,0x71
174
	out	0x70, al
Line 313... Line 175...
313
    mov     [last_1sTick], al
175
	in	al, 0x71
Line 348... Line 210...
348
    cmp     al, [last_1sTick]
210
    cmp     al, [last_1sTick]
349
    je      sh_exit
211
    je      sh_exit
Line 350... Line 212...
350
 
212
 
Line 351... Line 213...
351
    mov     [last_1sTick], al
213
    mov     [last_1sTick], al
352
 
214
 
Line 353... Line 215...
353
    call    arp_timer
215
    stdcall arp_table_manager, ARP_TABLE_TIMER, 0, 0 
354
    call    tcp_tcb_handler
216
    call    tcp_tcb_handler
Line -... Line 217...
-
 
217
 
-
 
218
sh_exit:
-
 
219
    ret
-
 
220
 
-
 
221
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
222
;; Checksum [by Johnny_B]
-
 
223
;;  IN:
-
 
224
;;    buf_ptr=POINTER to buffer
-
 
225
;;    buf_size=SIZE of buffer
-
 
226
;;  OUT:
-
 
227
;;    AX=16-bit checksum
Line -... Line 228...
-
 
228
;;              Saves all used registers
-
 
229
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
230
proc checksum_jb stdcall uses ebx esi ecx,\
-
 
231
     buf_ptr:DWORD, buf_size:DWORD
-
 
232
 
-
 
233
    xor     eax, eax
-
 
234
    xor     ebx, ebx  ;accumulator
-
 
235
    mov     esi, dword[buf_ptr]
-
 
236
    mov     ecx, dword[buf_size]
Line -... Line 237...
-
 
237
    shr     ecx, 1  ; ecx=ecx/2
-
 
238
    jnc     @f      ; if CF==0 then size is even number
-
 
239
    mov     bh, byte[esi + ecx*2]
-
 
240
  @@:
-
 
241
    cld
Line 355... Line -...
355
 
-
 
356
sh_exit:
-
 
357
    ret
-
 
358
 
-
 
359
 
-
 
360
 
-
 
361
 
-
 
362
;***************************************************************************
-
 
363
;   Function
-
 
364
;      is_localport_unused
-
 
365
;
-
 
366
;   Description
-
 
367
;         scans through all the active sockets , looking to see if the
-
 
368
;      port number specified in bx is in use as a localport number.
242
 
369
;      This is useful when you want a to generate a unique local port
243
  .loop:
370
;      number.
244
    lodsw   ;eax=word[esi],esi=esi+2
371
;          On return, eax = 1 for free, 0 for in use
-
 
372
;
-
 
373
;***************************************************************************
-
 
374
is_localport_unused:
-
 
375
    mov     al, bh
-
 
376
    mov     ah, bl
-
 
377
    mov     bx, ax
-
 
378
 
-
 
379
    mov     edx, SOCKETBUFFSIZE * NUM_SOCKETS
-
 
380
    mov     ecx, NUM_SOCKETS
-
 
381
    mov     eax, 0                    ; Assume the return value is 'in use'
-
 
382
 
-
 
383
ilu1:
-
 
384
    sub     edx, SOCKETBUFFSIZE
-
 
385
    cmp     [edx + sockets + 12], bx
-
 
386
    loopnz  ilu1                  ; Return back if the socket is occupied
-
 
387
 
-
 
388
    jz      ilu_exit
-
 
389
    inc     eax                         ; return port not in use
-
 
390
 
-
 
391
ilu_exit:
-
 
392
    ret
-
 
393
 
-
 
394
 
-
 
395
 
-
 
396
;***************************************************************************
-
 
397
;   Function
-
 
398
;      get_free_socket
-
 
399
;
-
 
400
;   Description
-
 
401
;
-
 
402
;***************************************************************************
-
 
403
get_free_socket:
-
 
404
    push    ecx
-
 
405
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
-
 
406
    mov     ecx, NUM_SOCKETS
245
    xchg    ah,al    ;cause must be a net byte-order
407
 
-
 
408
gfs1:
-
 
Line 409... Line -...
409
    sub     eax, SOCKETBUFFSIZE
-
 
410
    cmp     [eax + sockets], dword SOCK_EMPTY
246
    add     ebx, eax
411
    loopnz  gfs1                  ; Return back if the socket is occupied
-
 
412
    mov     eax, ecx
247
    loop    .loop
Line 413... Line 248...
413
    pop     ecx
248
 
414
    jz      gfs_exit
249
    mov     eax, ebx
415
    mov     eax, 0xFFFFFFFF
250
    shr     eax, 16
416
 
251
    add     ax, bx
Line 428... Line 263...
428
;       Dont break anything; Most registers are used by the caller
263
;       Dont break anything; Most registers are used by the caller
429
;       This code is derived from the 'C' source, cksum.c, in the book
264
;       This code is derived from the 'C' source, cksum.c, in the book
430
;       Internetworking with TCP/IP Volume II by D.E. Comer
265
;       Internetworking with TCP/IP Volume II by D.E. Comer
431
;
266
;
432
;***************************************************************************
267
;***************************************************************************
-
 
268
 
-
 
269
 
433
checksum:
270
checksum:
434
    pusha
271
    pusha
Line 435... Line 272...
435
 
272
 
436
    xor     edx, edx                  ; edx is the accumulative checksum
273
    xor     edx, edx                  ; edx is the accumulative checksum
Line 508... Line 345...
508
;***************************************************************************
345
;***************************************************************************
509
;   Function
346
;   Function
510
;      app_stack_handler
347
;      app_stack_handler
511
;
348
;
512
;   Description
349
;   Description
513
;       This is an application service, called by int 0x40 fn 52
350
;       This is an application service, called by int 0x40, function 52
514
;       It provides application access to the network interface layer
351
;       It provides application access to the network interface layer
515
;
352
;
516
;***************************************************************************
353
;***************************************************************************
517
app_stack_handler:
354
app_stack_handler:
518
    cmp     eax, 0
355
    cmp     eax, 0
519
    jnz     not0
356
    jnz     not0
520
    ; Read the configuartion word
357
    ; Read the configuration word
521
    mov     eax, [stack_config]
358
    mov     eax, [stack_config]
522
    ret
359
    ret
Line 523... Line 360...
523
 
360
 
524
not0:
361
not0:
Line 570... Line 407...
570
    jnz     not3
407
    jnz     not3
571
    ; write the IP Address
408
    ; write the IP Address
572
    mov     [stack_ip], ebx
409
    mov     [stack_ip], ebx
573
    ret
410
    ret
Line 574... Line -...
574
 
-
 
575
not3:
-
 
576
    cmp     eax, 4
-
 
577
    jnz     not4
-
 
578
    ; Enabled the slip driver on the comm port
411
 
579
    ; slip removed
-
 
580
    ret
-
 
581
 
-
 
582
not4:
-
 
583
    cmp     eax, 5
-
 
584
    jnz     not5
-
 
585
    ; Disable the slip driver on the comm port
-
 
Line 586... Line 412...
586
    ; slip removed
412
;old functions was deleted
587
 
413
 
588
not5:
414
not3:
Line 589... Line 415...
589
    cmp     eax, 6
415
    cmp     eax, 6
590
    jnz     not6
416
    jnz     not6
Line 651... Line 477...
651
    mov     eax, [dns_ip]
477
    mov     eax, [dns_ip]
652
    ret
478
    ret
Line 653... Line 479...
653
 
479
 
654
not13:
480
not13:
655
    cmp     eax, 14
481
    cmp     eax, 14
Line 656... Line 482...
656
    jnz     stack_driver_end
482
    jnz     not14
657
 
483
 
Line 658... Line 484...
658
    ; write the dns IP Address
484
    ; write the dns IP Address
Line -... Line 485...
-
 
485
    mov     [dns_ip], ebx
-
 
486
 
-
 
487
    ret       
-
 
488
    
-
 
489
;
-
 
490
not14:
-
 
491
	cmp	eax, 15
-
 
492
	jnz	not15
-
 
493
 
-
 
494
    ; in ebx we need 4 to read the last 2 bytes
-
 
495
	cmp	ebx, dword 4
-
 
496
	je	read
-
 
497
    
-
 
498
    ; or we need 0 to read the first 4 bytes
-
 
499
	cmp	ebx, dword 0
-
 
500
	jnz	param_error
-
 
501
 
-
 
502
    ; read MAC, returned (in mirrored byte order) in eax
-
 
503
read:
-
 
504
	mov	eax, [node_addr + ebx]
-
 
505
	jmp	@f
-
 
506
    
-
 
507
param_error:	
-
 
508
	mov	eax, -1     ; params not accepted
-
 
509
@@:
-
 
510
	ret
-
 
511
 
-
 
512
    
-
 
513
; 0 -> arp_probe
-
 
514
; 1 -> arp_announce
-
 
515
; 2 -> arp_responce (not supported yet)
-
 
516
 
-
 
517
not15:					; ARP stuff
-
 
518
	cmp	eax, 16
-
 
519
	jnz	not16
-
 
520
	
-
 
521
	cmp	ebx, 0
-
 
522
	je	a_probe
-
 
523
		
-
 
524
	cmp	ebx, 1
-
 
525
	je	a_ann			; arp announce
-
 
526
		
-
 
527
;	cmp	ebx,2
-
 
528
;	jne	a_resp			; arp response
-
 
529
 
-
 
530
	jmp	param15_error
-
 
531
		
-
 
532
		
-
 
533
; arp probe, sender IP must be set to 0.0.0.0, target IP is set to address being probed
-
 
534
; ecx: pointer to target MAC, MAC should set to 0 by application
-
 
535
; edx: target IP
-
 
536
a_probe:
-
 
537
	push	dword [stack_ip]
-
 
538
	
-
 
539
	mov	edx, [stack_ip]
-
 
540
	mov	[stack_ip], dword 0
-
 
541
	mov	esi, ecx		; pointer to target MAC address	
-
 
542
	call	arp_request
-
 
543
	
-
 
544
	pop	dword [stack_ip]		
-
 
545
	jmp 	@f
-
 
546
	
-
 
547
; arp announce, sender IP must be set to target IP
-
 
548
; ecx: pointer to target MAC
-
 
549
a_ann:
-
 
550
	mov	edx, [stack_ip]
-
 
551
	mov	esi, ecx		; pointer to target MAC address
-
 
552
	call	arp_request
-
 
553
	jmp	@f
-
 
554
		
-
 
555
param15_error:
-
 
556
	mov	eax, -1
-
 
557
		
-
 
558
@@:
-
 
559
	ret
-
 
560
;
-
 
561
; modified by [smb]
-
 
562
 
-
 
563
;
-
 
564
; ARPTable manager interface
-
 
565
not16:
-
 
566
    cmp     eax, 17
-
 
567
    jnz     stack_driver_end
-
 
568
 
-
 
569
    ;see "proc arp_table_manager" for more details
-
 
570
    stdcall arp_table_manager,ebx,ecx,edx  ;Opcode,Index,Extra
659
    mov     [dns_ip], ebx
571
 
660
 
572
    ret
Line 661... Line 573...
661
    ret
573
;
662
 
574
 
663
stack_driver_end:
575
stack_driver_end:
664
    ret
576
	ret
665
 
577
 
666
 
578
 
667
 
579
 
668
;***************************************************************************
580
;***************************************************************************
669
;   Function
581
;   Function
670
;      app_socket_handler
582
;      app_socket_handler
671
;
583
;
Line 1078... Line 990...
1078
 
990
 
1079
sip_err_exit:
991
sip_err_exit:
1080
    mov     eax, 0xFFFFFFFF
992
    mov     eax, 0xFFFFFFFF
Line 1081... Line -...
1081
    ret
-
 
1082
 
-
 
1083
 
-
 
1084
 
-
 
1085
;***************************************************************************
-
 
1086
;   Function
-
 
1087
;      socket_open
-
 
1088
;
-
 
1089
;   Description
-
 
1090
;       find a free socket
-
 
1091
;       local port in ebx
-
 
1092
;       remote port in ecx
-
 
1093
;       remote ip in edx
-
 
1094
;       return socket # in eax, -1 if none available
-
 
1095
;
-
 
1096
;***************************************************************************
-
 
1097
socket_open:
-
 
1098
    call    get_free_socket
-
 
1099
 
-
 
1100
    cmp     eax, 0xFFFFFFFF
-
 
1101
    jz      so_exit
-
 
1102
 
-
 
1103
    ; ax holds the socket number that is free. Get real address
-
 
1104
    push    eax
-
 
1105
    shl     eax, 12
-
 
1106
    add     eax, sockets
-
 
1107
 
-
 
1108
    mov     [eax], dword SOCK_OPEN
-
 
1109
 
-
 
1110
    mov     [eax + 12], byte bh      ; Local port ( LS 16 bits )
-
 
1111
    mov     [eax + 13], byte bl      ; Local port ( LS 16 bits )
-
 
1112
    mov     ebx, [stack_ip]
-
 
1113
    mov     [eax + 8], ebx         ; Local IP
-
 
1114
    mov     [eax + 20], ch         ; Remote Port ( LS 16 bits )
-
 
1115
    mov     [eax + 21], cl         ; Remote Port ( LS 16 bits )
-
 
1116
    mov     [eax + 16], edx         ; Remote IP ( in Internet order )
-
 
1117
    mov     [eax + 24], dword 0      ; recieved data count
-
 
1118
 
-
 
1119
    mov     esi, [0x3010]
-
 
1120
    mov     ebx, [esi+TASKDATA.pid]
-
 
1121
    mov     [eax + 4], ebx         ; save the process ID
-
 
1122
    pop     eax      ; Get the socket number back, so we can return it
-
 
1123
 
-
 
1124
so_exit:
-
 
1125
    ret
-
 
1126
 
-
 
1127
 
-
 
1128
 
-
 
1129
;***************************************************************************
-
 
1130
;   Function
-
 
1131
;      socket_open_tcp
-
 
1132
;
-
 
1133
;   Description
-
 
1134
;       Opens a TCP socket in PASSIVE or ACTIVE mode
-
 
1135
;       find a free socket
-
 
1136
;       local port in ebx ( intel format )
-
 
1137
;       remote port in ecx ( intel format )
-
 
1138
;       remote ip in edx ( in Internet byte order )
-
 
1139
;       Socket open mode in esi  ( SOCKET_PASSIVE or SOCKET_ACTIVE )
-
 
1140
;       return socket # in eax, -1 if none available
-
 
1141
;
-
 
1142
;***************************************************************************
-
 
1143
socket_open_tcp:
-
 
1144
    call    get_free_socket
-
 
1145
 
-
 
1146
    cmp     eax, 0xFFFFFFFF
-
 
1147
    jz      so_exit
-
 
1148
 
-
 
1149
    ; ax holds the socket number that is free. Get real address
-
 
1150
    push    eax
-
 
1151
    shl     eax, 12
-
 
1152
    add     eax, sockets
-
 
1153
 
-
 
1154
    mov     [sktAddr], eax
-
 
1155
    mov     [eax], dword SOCK_OPEN
-
 
1156
 
-
 
1157
    ; TODO - check this works!
-
 
1158
    mov     [eax + 72], dword 0     ; Reset the window timer.
-
 
1159
 
-
 
1160
    mov     [eax + 12], byte bh      ; Local port ( LS 16 bits )
-
 
1161
    mov     [eax + 13], byte bl      ; Local port ( LS 16 bits )
-
 
1162
    mov     ebx, [stack_ip]
-
 
1163
    mov     [eax + 8], ebx         ; Local IP
-
 
1164
    mov     [eax + 20], ch         ; Remote Port ( LS 16 bits )
-
 
1165
    mov     [eax + 21], cl         ; Remote Port ( LS 16 bits )
-
 
1166
    mov     [eax + 16], edx         ; Remote IP ( in Internet order )
-
 
1167
    mov     [eax + 24], dword 0      ; recieved data count
-
 
1168
 
-
 
1169
    ; Now fill in TCB state
-
 
1170
    mov     ebx, TCB_LISTEN
-
 
1171
    cmp     esi, SOCKET_PASSIVE
-
 
1172
    jz      sot_001
-
 
1173
    mov     ebx, TCB_SYN_SENT
-
 
1174
 
-
 
1175
sot_001:
-
 
1176
    mov     [eax + 28], ebx            ; Indicate the state of the TCB
-
 
1177
 
-
 
1178
    mov     esi, [0x3010]
-
 
1179
    mov     ecx, [esi+TASKDATA.pid]
-
 
1180
    mov     [eax + 4], ecx         ; save the process ID
-
 
1181
 
-
 
1182
    cmp     ebx, TCB_LISTEN
-
 
1183
    je      sot_done
-
 
1184
 
-
 
1185
    ; Now, if we are in active mode, then we have to send a SYN to the specified remote port
-
 
1186
 
-
 
1187
 
-
 
1188
    mov     eax, EMPTY_QUEUE
-
 
1189
    call    dequeue
-
 
1190
    cmp     ax, NO_BUFFER
-
 
1191
    je      sot_done
-
 
1192
 
-
 
1193
    push    eax
-
 
1194
 
-
 
1195
    mov     bl, 0x02        ; SYN
-
 
1196
    mov     ecx, 0
-
 
1197
 
-
 
1198
    call    buildTCPPacket
-
 
1199
 
-
 
1200
    mov     eax, NET1OUT_QUEUE
-
 
1201
 
-
 
1202
    mov     edx, [stack_ip]
-
 
1203
    mov     ecx, [ sktAddr ]
-
 
1204
    mov     ecx, [ ecx + 16 ]
-
 
1205
    cmp     edx, ecx
-
 
1206
    jne     sot_notlocal
-
 
1207
    mov     eax, IPIN_QUEUE
-
 
1208
 
-
 
1209
sot_notlocal:
-
 
1210
       ; Send it.
-
 
1211
    pop     ebx
-
 
1212
    call    queue
-
 
1213
 
-
 
1214
    mov     esi, [sktAddr]
-
 
1215
 
-
 
1216
    ; increment SND.NXT in socket
-
 
1217
    add     esi, 48
-
 
1218
    call    inc_inet_esi
-
 
1219
 
-
 
1220
sot_done:
-
 
1221
    pop     eax      ; Get the socket number back, so we can return it
-
 
1222
 
-
 
1223
sot_exit:
-
 
1224
    ret
-
 
1225
 
-
 
1226
 
-
 
1227
 
-
 
1228
;***************************************************************************
-
 
1229
;   Function
-
 
1230
;      socket_close
-
 
1231
;
-
 
1232
;   Description
-
 
1233
;       socket # in ebx
-
 
1234
;       returns 0 for ok, -1 for socket not open (fail)
-
 
1235
;
-
 
1236
;***************************************************************************
-
 
1237
socket_close:
-
 
1238
    shl     ebx, 12
-
 
1239
    add     ebx, sockets
-
 
1240
    mov     eax, 0xFFFFFFFF         ; assume this operation will fail..
-
 
1241
    cmp     [ebx], dword SOCK_EMPTY
-
 
1242
    jz      sc_exit
-
 
1243
 
-
 
1244
    ; Clear the socket varaibles
-
 
1245
    xor     eax, eax
-
 
1246
    mov     edi,ebx
-
 
1247
    mov     ecx,SOCKETHEADERSIZE
-
 
1248
    cld
-
 
1249
    rep     stosb
-
 
1250
 
-
 
1251
sc_exit:
-
 
1252
    ret
-
 
1253
 
-
 
1254
 
-
 
1255
 
-
 
1256
;***************************************************************************
-
 
1257
;   Function
-
 
1258
;      socket_close_tcp
-
 
1259
;
-
 
1260
;   Description
-
 
1261
;       socket # in ebx
-
 
1262
;       returns 0 for ok, -1 for socket not open (fail)
-
 
1263
;
-
 
1264
;***************************************************************************
-
 
1265
socket_close_tcp:
-
 
1266
    ; first, remove any resend entries
-
 
1267
    pusha
-
 
1268
 
-
 
1269
    mov     esi, resendQ
-
 
1270
    mov     ecx, 0
-
 
1271
 
-
 
1272
sct001:
-
 
1273
    cmp     ecx, NUMRESENDENTRIES
-
 
1274
    je      sct003              ; None left
-
 
1275
    cmp     [esi], bl
-
 
1276
    je      sct002              ; found one
-
 
1277
    inc     ecx
-
 
1278
    add     esi, 4
-
 
1279
    jmp     sct001
-
 
1280
 
-
 
1281
sct002:
-
 
1282
    dec     dword [arp_rx_count] ; ************ TEST ONLY!
-
 
1283
 
-
 
1284
    mov     [esi], byte 0xFF
-
 
1285
    jmp     sct001
-
 
1286
 
-
 
1287
sct003:
-
 
1288
    popa
-
 
1289
 
-
 
1290
    shl     ebx, 12
-
 
1291
    add     ebx, sockets
-
 
1292
    mov     [sktAddr], ebx
-
 
1293
    mov     eax, 0xFFFFFFFF         ; assume this operation will fail..
-
 
1294
    cmp     [ebx], dword SOCK_EMPTY
-
 
1295
    jz      sct_exit
-
 
1296
 
-
 
1297
    ; Now construct the response, and queue for sending by IP
-
 
1298
    mov     eax, EMPTY_QUEUE
-
 
1299
    call    dequeue
-
 
1300
    cmp     ax, NO_BUFFER
-
 
1301
    je      stl_exit
-
 
1302
 
-
 
1303
    push    eax
-
 
1304
 
-
 
1305
    mov     bl, 0x11        ; FIN + ACK
-
 
1306
    mov     ecx, 0
-
 
1307
    mov     esi, 0
-
 
1308
 
-
 
1309
    call    buildTCPPacket
-
 
1310
 
-
 
1311
    mov     ebx, [sktAddr]
-
 
1312
 
-
 
1313
    ; increament SND.NXT in socket
-
 
1314
    mov     esi, 48
-
 
1315
    add     esi, ebx
-
 
1316
    call    inc_inet_esi
-
 
1317
 
-
 
1318
 
-
 
1319
    ; Get the socket state
-
 
1320
    mov     eax, [ebx + 28]
-
 
1321
    cmp     eax, TCB_LISTEN
-
 
1322
    je      destroyTCB
-
 
1323
    cmp     eax, TCB_SYN_SENT
-
 
1324
    je      destroyTCB
-
 
1325
    cmp     eax, TCB_SYN_RECEIVED
-
 
1326
    je      sct_finwait1
-
 
1327
    cmp     eax, TCB_ESTABLISHED
-
 
1328
    je      sct_finwait1
-
 
1329
 
-
 
1330
    ; assume CLOSE WAIT
-
 
1331
    ; Send a fin, then enter last-ack state
-
 
1332
    mov     eax, TCB_LAST_ACK
-
 
1333
    mov     [ebx + 28], eax
-
 
1334
    xor     eax, eax
-
 
1335
    jmp     sct_send
-
 
1336
 
-
 
1337
sct_finwait1:
-
 
1338
    ; Send a fin, then enter finwait2 state
-
 
1339
    mov     eax, TCB_FIN_WAIT_1
-
 
1340
    mov     [ebx + 28], eax
-
 
1341
    xor     eax, eax
-
 
1342
 
-
 
1343
sct_send:
-
 
1344
    mov     eax, NET1OUT_QUEUE
-
 
1345
 
-
 
1346
    mov     edx, [stack_ip]
-
 
1347
    mov     ecx, [ sktAddr ]
-
 
1348
    mov     ecx, [ ecx + 16 ]
-
 
1349
    cmp     edx, ecx
-
 
1350
    jne     sct_notlocal
-
 
1351
    mov     eax, IPIN_QUEUE
-
 
1352
 
-
 
1353
sct_notlocal:
-
 
1354
       ; Send it.
-
 
1355
    pop     ebx
-
 
1356
    call    queue
-
 
1357
    jmp     sct_exit
-
 
1358
 
-
 
1359
destroyTCB:
-
 
1360
    pop     eax
-
 
1361
    ; Clear the socket varaibles
-
 
1362
    xor     eax, eax
-
 
1363
    mov     edi,ebx
-
 
1364
    mov     ecx,SOCKETHEADERSIZE
-
 
1365
    cld
-
 
1366
    rep     stosb
-
 
1367
 
-
 
1368
sct_exit:
-
 
1369
    ret
-
 
1370
 
-
 
1371
 
-
 
1372
 
-
 
1373
;***************************************************************************
-
 
1374
;   Function
-
 
1375
;      socket_poll
-
 
1376
;
-
 
1377
;   Description
-
 
1378
;       socket # in ebx
-
 
1379
;       returns count in eax.
-
 
1380
;
-
 
1381
;***************************************************************************
-
 
1382
socket_poll:
-
 
1383
    shl     ebx, 12
-
 
1384
    add     ebx, sockets
-
 
1385
    mov     eax, [ebx + 24]
-
 
1386
 
-
 
1387
    ret
-
 
1388
 
-
 
1389
 
-
 
1390
 
-
 
1391
;***************************************************************************
-
 
1392
;   Function
-
 
1393
;      socket_status
-
 
1394
;
-
 
1395
;   Description
-
 
1396
;       socket # in ebx
-
 
1397
;       returns TCB state in eax.
-
 
1398
;
-
 
1399
;***************************************************************************
-
 
1400
socket_status:
-
 
1401
    shl     ebx, 12
-
 
1402
    add     ebx, sockets
-
 
1403
    mov     eax, [ebx + 28]
-
 
1404
 
-
 
1405
    ret
-
 
1406
 
-
 
1407
 
-
 
1408
 
-
 
1409
;***************************************************************************
-
 
1410
;   Function
-
 
1411
;      socket_read
-
 
1412
;
-
 
1413
;   Description
-
 
1414
;       socket # in ebx
-
 
1415
;       returns # of bytes remaining in eax, data in bl
-
 
1416
;
-
 
1417
;***************************************************************************
-
 
1418
socket_read:
-
 
1419
    shl     ebx, 12
-
 
1420
    add     ebx, sockets
-
 
1421
    mov     eax, [ebx + 24]         ; get count of bytes
-
 
1422
    mov     ecx,1
-
 
1423
    test    eax, eax
-
 
1424
    jz      sr2
-
 
1425
 
-
 
1426
    dec     eax
-
 
1427
    mov     esi, ebx            ; esi is address of socket
-
 
1428
    mov     [ebx + 24], eax         ; store new count
-
 
1429
    movzx   ebx, byte [ebx + SOCKETHEADERSIZE]  ; get the byte
-
 
1430
    add     esi, SOCKETHEADERSIZE
-
 
1431
    mov     edi, esi
-
 
1432
    inc     esi
-
 
1433
 
-
 
1434
    mov     ecx, (SOCKETBUFFSIZE - SOCKETHEADERSIZE) / 4
-
 
1435
    cld
-
 
1436
    rep     movsd
-
 
1437
    xor     ecx, ecx
-
 
1438
 
-
 
1439
sr1:
-
 
1440
    jmp     sor_exit
-
 
1441
 
-
 
1442
sr2:
-
 
1443
    xor     bl, bl
-
 
1444
 
-
 
1445
sor_exit:
-
 
1446
    ret
-
 
1447
 
-
 
1448
 
-
 
1449
 
-
 
1450
;***************************************************************************
-
 
1451
;   Function
-
 
1452
;      socket_write
-
 
1453
;
-
 
1454
;   Description
-
 
1455
;       socket in ebx
-
 
1456
;       # of bytes to write in ecx
-
 
1457
;       pointer to data in edx
-
 
1458
;       returns 0 in eax ok, -1 == failed ( invalid socket, or
-
 
1459
;       could not queue IP packet )
-
 
1460
;
-
 
1461
;***************************************************************************
-
 
1462
socket_write:
-
 
1463
    ; First, find the address of the socket descriptor
-
 
1464
    shl     ebx, 12
-
 
1465
    add     ebx, sockets         ; ebx = address of actual socket
-
 
1466
 
-
 
1467
    mov     eax, 0xFFFFFFFF
-
 
1468
    ; If the socket is invalid, return with an error code
-
 
1469
    cmp     [ebx], dword SOCK_EMPTY
-
 
1470
    je      sw_exit
-
 
1471
 
-
 
1472
 
-
 
1473
    mov     eax, EMPTY_QUEUE
-
 
1474
    call    dequeue
-
 
1475
    cmp     ax, NO_BUFFER
-
 
1476
    je      sw_exit
-
 
1477
 
-
 
1478
    ; Save the queue entry number
-
 
1479
    push    eax
-
 
1480
 
-
 
1481
    ; save the pointers to the data buffer & size
-
 
1482
    push    edx
-
 
1483
    push    ecx
-
 
1484
 
-
 
1485
    ; convert buffer pointer eax to the absolute address
-
 
1486
    mov     ecx, IPBUFFSIZE
-
 
1487
    mul     ecx
-
 
1488
    add     eax, IPbuffs
-
 
1489
 
-
 
1490
    mov     edx, eax
-
 
1491
 
-
 
1492
    ; So, ebx holds the socket ptr, edx holds the IPbuffer ptr
-
 
1493
 
-
 
1494
    ; Fill in the IP header ( some data is in the socket descriptor)
-
 
1495
    mov     eax, [ebx + 8]
-
 
1496
    mov     [edx + 12], eax      ; source IP
-
 
1497
    mov     eax, [ebx + 16]
-
 
1498
    mov     [edx + 16], eax      ; Destination IP
-
 
1499
 
-
 
1500
    mov     al, 0x45
-
 
1501
    mov     [edx], al         ; Version, IHL
-
 
1502
    xor     al, al
-
 
1503
    mov     [edx + 1], al     ; Type of service
-
 
1504
 
-
 
1505
    pop     eax                   ; Get the UDP data length
-
 
1506
    push    eax
-
 
1507
 
-
 
1508
    add     eax, 20 + 8           ; add IP header and UDP header lengths
-
 
1509
    mov     [edx + 2], ah
-
 
1510
    mov     [edx + 3], al
-
 
1511
    xor     al, al
-
 
1512
    mov     [edx + 4], al
-
 
1513
    mov     [edx + 5], al
-
 
1514
    mov     al, 0x40
-
 
1515
    mov     [edx + 6], al
-
 
1516
    xor     al, al
-
 
1517
    mov     [edx + 7], al
-
 
1518
    mov     al, 0x20
-
 
1519
    mov     [edx + 8], al
-
 
1520
    mov     al, 17
-
 
1521
    mov     [edx + 9], al
-
 
1522
 
-
 
1523
    ; Checksum left unfilled
-
 
1524
    xor     ax, ax
-
 
1525
    mov     [edx + 10], ax
-
 
1526
 
-
 
1527
    ; Fill in the UDP header ( some data is in the socket descriptor)
-
 
1528
    mov     ax, [ebx + 12]
-
 
1529
    mov     [edx + 20], ax
-
 
1530
 
-
 
1531
    mov     ax, [ebx + 20]
-
 
1532
    mov     [edx + 20 + 2], ax
-
 
1533
 
-
 
1534
    pop     eax
-
 
1535
    push    eax
-
 
1536
 
-
 
1537
    add     eax, 8
-
 
1538
    mov     [edx + 20 + 4], ah
-
 
1539
    mov     [edx + 20 + 5], al
-
 
1540
 
-
 
1541
    ; Checksum left unfilled
-
 
1542
    xor     ax, ax
-
 
1543
    mov     [edx + 20 + 6], ax
-
 
1544
 
-
 
1545
    pop     ecx                  ; count of bytes to send
-
 
1546
    mov     ebx, ecx            ; need the length later
-
 
1547
    pop     eax                  ; get callers ptr to data to send
-
 
1548
 
-
 
1549
    ; Get the address of the callers data
-
 
1550
    mov     edi,[0x3010]
-
 
1551
    add     edi,TASKDATA.mem_start
-
 
1552
    add     eax,[edi]
-
 
1553
    mov     esi, eax
-
 
1554
 
-
 
1555
    mov     edi, edx
-
 
1556
    add     edi, 28
-
 
1557
    cld
-
 
1558
    rep     movsb               ; copy the data across
-
 
1559
 
-
 
1560
    ; we have edx as IPbuffer ptr.
-
 
1561
    ; Fill in the UDP checksum
-
 
1562
    ; First, fill in pseudoheader
-
 
1563
    mov     eax, [edx + 12]
-
 
1564
    mov     [pseudoHeader], eax
-
 
1565
    mov     eax, [edx + 16]
-
 
1566
    mov     [pseudoHeader+4], eax
-
 
1567
    mov     ax, 0x1100            ; 0 + protocol
-
 
1568
    mov     [pseudoHeader+8], ax
-
 
1569
    add     ebx, 8
-
 
1570
    mov     eax, ebx
-
 
1571
    mov     [pseudoHeader+10], ah
-
 
1572
    mov     [pseudoHeader+11], al
-
 
1573
 
-
 
1574
    mov     eax, pseudoHeader
-
 
1575
    mov     [checkAdd1], eax
-
 
1576
    mov     [checkSize1], word 12
-
 
1577
    mov     eax, edx
-
 
1578
    add     eax, 20
-
 
1579
    mov     [checkAdd2], eax
-
 
1580
    mov     eax, ebx
-
 
1581
    mov     [checkSize2], ax      ; was eax!! mjh 8/7/02
-
 
1582
 
-
 
1583
    call    checksum
-
 
1584
 
-
 
1585
    ; store it in the UDP checksum ( in the correct order! )
-
 
1586
    mov     ax, [checkResult]
-
 
1587
 
-
 
1588
    ; If the UDP checksum computes to 0, we must make it 0xffff
-
 
1589
    ; (0 is reserved for 'not used')
-
 
1590
    cmp     ax, 0
-
 
1591
    jne     sw_001
-
 
1592
    mov     ax, 0xffff
-
 
1593
 
-
 
1594
sw_001:
-
 
1595
    mov     [edx + 20 + 6], ah
-
 
1596
    mov     [edx + 20 + 7], al
-
 
1597
 
-
 
1598
    ; Fill in the IP header checksum
-
 
1599
    mov     eax, edx
-
 
1600
    mov     [checkAdd1], eax
-
 
1601
    mov     [checkSize1], word 20
-
 
1602
    mov     [checkAdd2], dword 0
-
 
1603
    mov     [checkSize2], word 0
-
 
1604
 
-
 
1605
    call    checksum
-
 
1606
 
-
 
1607
    mov     ax, [checkResult]
-
 
1608
    mov     [edx + 10], ah
-
 
1609
    mov     [edx + 11], al
-
 
1610
 
-
 
1611
    ; Check destination IP address.
-
 
1612
    ; If it is the local host IP, route it back to IP_RX
-
 
1613
 
-
 
1614
    pop     ebx
-
 
1615
    mov     eax, NET1OUT_QUEUE
-
 
1616
 
-
 
1617
    mov     ecx, [ edx + 16]
-
 
1618
    mov     edx, [stack_ip]
-
 
1619
    cmp     edx, ecx
-
 
1620
    jne     sw_notlocal
-
 
1621
    mov     eax, IPIN_QUEUE
-
 
1622
 
-
 
1623
sw_notlocal:
-
 
1624
    ; Send it.
-
 
1625
    call    queue
-
 
1626
 
-
 
1627
    xor     eax, eax
-
 
1628
 
-
 
1629
sw_exit:
-
 
1630
    ret
-
 
1631
 
-
 
1632
 
-
 
1633
 
-
 
1634
;***************************************************************************
-
 
1635
;   Function
-
 
1636
;      socket_write_tcp
-
 
1637
;
-
 
1638
;   Description
-
 
1639
;       socket in ebx
-
 
1640
;       # of bytes to write in ecx
-
 
1641
;       pointer to data in edx
-
 
1642
;       returns 0 in eax ok, -1 == failed ( invalid socket, or
-
 
1643
;       could not queue IP packet )
-
 
1644
;
-
 
1645
;***************************************************************************
-
 
1646
socket_write_tcp:
-
 
1647
    ; First, find the address of the socket descriptor
-
 
1648
    shl     ebx, 12
-
 
1649
    add     ebx, sockets         ; ebx = address of actual socket
-
 
1650
 
-
 
1651
    mov     [sktAddr], ebx
-
 
1652
 
-
 
1653
    mov     eax, 0xFFFFFFFF
-
 
1654
    ; If the socket is invalid, return with an error code
-
 
1655
    cmp     [ebx], dword SOCK_EMPTY
-
 
1656
    je      swt_exit
-
 
1657
 
-
 
1658
    ; If the sockets window timer is nonzero, do not queue packet
-
 
1659
    ; TODO - done
-
 
1660
    cmp     [ebx + 72], dword 0
-
 
1661
    jne     swt_exit
-
 
1662
 
-
 
1663
    mov     eax, EMPTY_QUEUE
-
 
1664
    call    dequeue
-
 
1665
    cmp     ax, NO_BUFFER
-
 
1666
    je      swt_exit
-
 
1667
 
-
 
1668
    push    eax
-
 
1669
 
-
 
1670
    mov     bl, 0x10        ; ACK
-
 
1671
 
-
 
1672
    ; Get the address of the callers data
-
 
1673
    mov     edi,[0x3010]
-
 
1674
    add     edi,TASKDATA.mem_start
-
 
1675
    add     edx,[edi]
-
 
1676
    mov     esi, edx
-
 
1677
 
-
 
1678
    pop     eax
-
 
1679
    push    eax
-
 
1680
 
-
 
1681
    push    ecx
-
 
1682
    call    buildTCPPacket
-
 
1683
    pop     ecx
-
 
1684
 
-
 
1685
    ; Check destination IP address.
-
 
1686
    ; If it is the local host IP, route it back to IP_RX
-
 
1687
 
-
 
1688
    pop     ebx
-
 
1689
    push    ecx
-
 
1690
    mov     eax, NET1OUT_QUEUE
-
 
1691
 
-
 
1692
    mov     edx, [stack_ip]
-
 
1693
    mov     ecx, [ sktAddr ]
-
 
1694
    mov     ecx, [ ecx + 16 ]
-
 
1695
    cmp     edx, ecx
-
 
1696
    jne     swt_notlocal
-
 
1697
    mov     eax, IPIN_QUEUE
-
 
1698
 
-
 
1699
swt_notlocal:
-
 
1700
    pop     ecx
-
 
1701
 
-
 
1702
    push    ebx                 ; save ipbuffer number
-
 
1703
 
-
 
1704
    call    queue
-
 
1705
 
-
 
1706
    mov     esi, [sktAddr]
-
 
1707
 
-
 
1708
    ; increament SND.NXT in socket
-
 
1709
    ; Amount to increment by is in ecx
-
 
1710
    add     esi, 48
-
 
1711
    call    add_inet_esi
-
 
1712
 
-
 
1713
    pop     ebx
-
 
1714
 
-
 
1715
    ; Copy the IP buffer to a resend queue
-
 
1716
    ; If there isn't one, dont worry about it for now
-
 
1717
    mov     esi, resendQ
-
 
1718
    mov     ecx, 0
-
 
1719
 
-
 
1720
swt003:
-
 
1721
    cmp     ecx, NUMRESENDENTRIES
-
 
1722
    je      swt001              ; None found
-
 
1723
    cmp     [esi], byte 0xFF
-
 
1724
    je      swt002              ; found one
-
 
1725
    inc     ecx
-
 
1726
    add     esi, 4
-
 
1727
    jmp     swt003
-
 
1728
 
-
 
1729
swt002:
-
 
1730
    push    ebx
-
 
1731
 
-
 
1732
    ; OK, we have a buffer descriptor ptr in esi.
-
 
1733
    ; resend entry # in ecx
-
 
1734
    ;  Populate it
-
 
1735
    ;  socket #
-
 
1736
    ;  retries count
-
 
1737
    ;  retry time
-
 
1738
    ;  fill IP buffer associated with this descriptor
-
 
1739
 
-
 
1740
    mov     eax, [sktAddr]
-
 
1741
    sub     eax, sockets
-
 
1742
    shr     eax, 12             ; get skt #
-
 
1743
    mov     [esi], al
-
 
1744
    mov     [esi + 1], byte TCP_RETRIES
-
 
1745
    mov     [esi + 2], word TCP_TIMEOUT
-
 
1746
 
-
 
1747
    inc     ecx
-
 
1748
    ; Now get buffer location, and copy buffer across. argh! more copying,,
-
 
1749
    mov     edi, resendBuffer - IPBUFFSIZE
-
 
1750
swt002a:
-
 
1751
    add     edi, IPBUFFSIZE
-
 
1752
    loop    swt002a
-
 
1753
 
-
 
1754
    ; we have dest buffer location in edi
-
 
1755
    pop     eax
-
 
1756
    ; convert source buffer pointer eax to the absolute address
-
 
1757
    mov     ecx, IPBUFFSIZE
-
 
1758
    mul     ecx
-
 
1759
    add     eax, IPbuffs
-
 
1760
    mov     esi, eax
-
 
1761
 
-
 
1762
    ; do copy
-
 
1763
    mov     ecx, IPBUFFSIZE
-
 
1764
    cld
-
 
1765
    rep     movsb
-
 
1766
 
-
 
1767
    inc     dword [arp_rx_count] ; ************ TEST ONLY!
-
 
1768
 
-
 
1769
swt001:
-
 
1770
    xor     eax, eax
-
 
1771
 
-
 
1772
swt_exit:
-
 
1773
    ret
-
 
1774
 
-
 
1775
 
-
 
1776
 
-
 
1777
; Below, the main network layer source code is included
-
 
1778
;
-
 
1779
 
-
 
1780
include "queue.inc"
-
 
1781
include "ip.inc"
-
 
1782
include "tcp.inc"
-