Subversion Repositories Kolibri OS

Rev

Rev 379 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 379 Rev 380
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;;  UDP.INC                                                        ;;
3
;;  UDP.INC                                                        ;;
4
;;                                                                 ;;
4
;;                                                                 ;;
5
;;  UDP Processes for Menuet OS  TCP/IP stack                      ;;
5
;;  UDP Processes for Menuet OS  TCP/IP stack                      ;;
6
;;                                                                 ;;
6
;;                                                                 ;;
7
;;  Version 0.3  29 August 2002                                    ;;
7
;;  Version 0.3  29 August 2002                                    ;;
8
;;                                                                 ;;
8
;;                                                                 ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
9
;;  Copyright 2002 Mike Hibbett, mikeh@oceanfree.net               ;;
10
;;                                                                 ;;
10
;;                                                                 ;;
11
;;  See file COPYING for details                                   ;;
11
;;  See file COPYING for details                                   ;;
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
14
 
15
 
15
 
16
;*******************************************************************
16
;*******************************************************************
17
;   Interface
17
;   Interface
18
;
18
;
19
;       udp_rx      Handles received IP packets with the UDP protocol
19
;       udp_rx      Handles received IP packets with the UDP protocol
20
;
20
;
21
;*******************************************************************
21
;*******************************************************************
22
 
22
 
23
 
23
 
24
;
24
;
25
;   UDP Payload ( Data field in IP datagram )
25
;   UDP Payload ( Data field in IP datagram )
26
;
26
;
27
;    0                   1                   2                   3
27
;    0                   1                   2                   3
28
;    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
28
;    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
29
;
29
;
30
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31
;   |       Source Port             |      Destination Port         |
31
;   |       Source Port             |      Destination Port         |
32
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33
;   | Length ( UDP Header + Data )  |           Checksum            |
33
;   | Length ( UDP Header + Data )  |           Checksum            |
34
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34
;   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35
;   |       UDP Data                                                |
35
;   |       UDP Data                                                |
36
;   +-+-+-..........                                               -+
36
;   +-+-+-..........                                               -+
37
;
37
;
38
 
38
 
39
struc UDP_PACKET
39
struc UDP_PACKET
40
{  .SourcePort       dw  ?  ;+00
40
{  .SourcePort       dw  ?  ;+00
41
   .DestinationPort  dw  ?  ;+02
41
   .DestinationPort  dw  ?  ;+02
42
   .Length           dw  ?  ;+04 - Length of (UDP Header + Data)
42
   .Length           dw  ?  ;+04 - Length of (UDP Header + Data)
43
   .Checksum         dw  ?  ;+06
43
   .Checksum         dw  ?  ;+06
44
   .Data             db  ?  ;+08
44
   .Data             db  ?  ;+08
45
}
45
}
46
 
46
 
47
virtual at 0
47
virtual at 0
48
  UDP_PACKET UDP_PACKET
48
  UDP_PACKET UDP_PACKET
49
end virtual
49
end virtual
50
 
50
 
51
 
51
 
52
;***************************************************************************
52
;***************************************************************************
53
;   Function
53
;   Function
54
;      udp_rx  [by Johnny_B]
54
;      udp_rx  [by Johnny_B]
55
;
55
;
56
;   Description
56
;   Description
57
;       UDP protocol handler
57
;       UDP protocol handler
58
;       This is a kernel function, called by ip_rx
58
;       This is a kernel function, called by ip_rx
59
;       IP buffer address given in edx
59
;       IP buffer address given in edx
60
;          IP buffer number in eax
60
;          IP buffer number in eax
61
;          Free up (or re-use) IP buffer when finished
61
;          Free up (or re-use) IP buffer when finished
62
;
62
;
63
;***************************************************************************
63
;***************************************************************************
64
udp_rx:
64
udp_rx:
65
    push    eax
65
    push    eax
66
 
66
 
67
    ; First validate the header & checksum. Discard buffer if error
67
    ; First validate the header & checksum. Discard buffer if error
68
 
68
 
69
    ; Look for a socket where
69
    ; Look for a socket where
70
    ; IP Packet UDP Destination Port = local Port
70
    ; IP Packet UDP Destination Port = local Port
71
    ; IP Packet SA = Remote IP
71
    ; IP Packet SA = Remote IP
72
 
72
 
73
    movzx   ebx, word [edx + 22]   ; get the local port from
73
    movzx   ebx, word [edx + 22]   ; get the local port from
74
                                  ; the IP packet's UDP header
74
                                  ; the IP packet's UDP header
75
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
75
    mov     eax, SOCKETBUFFSIZE * NUM_SOCKETS
76
    mov     ecx, NUM_SOCKETS
76
    mov     ecx, NUM_SOCKETS
77
 
77
 
78
fs1:
78
fs1:
79
    sub     eax, SOCKETBUFFSIZE
79
    sub     eax, SOCKETBUFFSIZE
80
    cmp     [eax + sockets + 12], bx ; bx will hold the 'wrong' value,
80
    cmp     [eax + sockets + 12], bx ; bx will hold the 'wrong' value,
81
                                    ; but the comparision is correct
81
                                    ; but the comparision is correct
82
    loopnz  fs1                     ; Return back if no match
82
    loopnz  fs1                     ; Return back if no match
83
    jz      fs_done
83
    jz      fs_done
84
 
84
 
85
    ; No match, so exit
85
    ; No match, so exit
86
    jmp     udprx_001
86
    jmp     udprx_001
87
 
87
 
88
fs_done:
88
fs_done:
89
    ; For dhcp, we must allow any remote server to respond.
89
    ; For dhcp, we must allow any remote server to respond.
90
    ; I will accept the first incoming response to be the one
90
    ; I will accept the first incoming response to be the one
91
    ; I bind to, if the socket is opened with a destination IP address of
91
    ; I bind to, if the socket is opened with a destination IP address of
92
    ; 255.255.255.255
92
    ; 255.255.255.255
93
    mov     ebx, [eax + sockets + 16]
93
    mov     ebx, [eax + sockets + 16]
94
    cmp     ebx, 0xffffffff
94
    cmp     ebx, 0xffffffff
95
    je      udprx_002
95
    je      udprx_002
96
 
96
 
97
    mov     ebx, [edx + 12]    ; get the Source address from the IP packet
97
    mov     ebx, [edx + 12]    ; get the Source address from the IP packet
98
    cmp     [eax + sockets + 16], ebx
98
    cmp     [eax + sockets + 16], ebx
99
    jne     udprx_001          ; Quit if the source IP is not valid
99
    jne     udprx_001          ; Quit if the source IP is not valid
100
 
100
 
101
udprx_002:
101
udprx_002:
102
    ; OK - we have a valid UDP packet for this socket.
102
    ; OK - we have a valid UDP packet for this socket.
103
    ; First, update the sockets remote port number with the incoming msg
103
    ; First, update the sockets remote port number with the incoming msg
104
    ; - it will have changed
104
    ; - it will have changed
105
    ; from the original ( 69 normally ) to allow further connects
105
    ; from the original ( 69 normally ) to allow further connects
106
    movzx   ebx, word [edx + 20]      ; get the UDP source port
106
    movzx   ebx, word [edx + 20]      ; get the UDP source port
107
                                     ; ( was 69, now new )
107
                                     ; ( was 69, now new )
108
    mov     [eax + sockets + 20], bx
108
    mov     [eax + sockets + 20], bx
109
 
109
 
110
    ; Now, copy data to socket. We have socket address as [eax + sockets].
110
    ; Now, copy data to socket. We have socket address as [eax + sockets].
111
    ; We have IP packet in edx
111
    ; We have IP packet in edx
112
 
112
 
113
    ; get # of bytes in ecx
113
    ; get # of bytes in ecx
114
    movzx   ecx, byte [edx + 3]  ; total length of IP packet. Subtract
114
    movzx   ecx, byte [edx + 3]  ; total length of IP packet. Subtract
115
    mov     ch, byte [edx + 2]   ; 20 + 8 gives data length
115
    mov     ch, byte [edx + 2]   ; 20 + 8 gives data length
116
    sub     ecx, 28
116
    sub     ecx, 28
117
 
117
 
118
    mov     ebx, eax
118
    mov     ebx, eax
119
    add     ebx, sockets         ; ebx = address of actual socket
119
    add     ebx, sockets         ; ebx = address of actual socket
120
 
120
 
121
    mov     eax, [ebx+ 4]       ; get socket owner PID
121
    mov     eax, [ebx+ 4]       ; get socket owner PID
122
    push    eax
122
    push    eax
123
 
123
 
124
    mov     eax, [ebx + 24]      ; get # of bytes already in buffer
124
    mov     eax, [ebx + 24]      ; get # of bytes already in buffer
125
    add     [ebx + 24], ecx      ; increment the count of bytes in buffer
125
    add     [ebx + 24], ecx      ; increment the count of bytes in buffer
126
 
126
 
127
    ; point to the location to store the data
127
    ; point to the location to store the data
128
    add     ebx, eax
128
    add     ebx, eax
129
    add     ebx, SOCKETHEADERSIZE
129
    add     ebx, SOCKETHEADERSIZE
130
 
130
 
131
    ; ebx = location for first byte, ecx has count,
131
    ; ebx = location for first byte, ecx has count,
132
    ; edx points to data
132
    ; edx points to data
133
 
133
 
134
    add     edx, 28        ; edx now points to the data
134
    add     edx, 28        ; edx now points to the data
135
    mov     edi, ebx
135
    mov     edi, ebx
136
    mov     esi, edx
136
    mov     esi, edx
137
 
137
 
138
    cld
138
    cld
139
    rep     movsb          ; copy the data across
139
    rep     movsb          ; copy the data across
140
 
140
 
141
    ; flag an event to the application
141
    ; flag an event to the application
142
    pop     eax
142
    pop     eax
143
    mov     ecx,1
143
    mov     ecx,1
144
    mov     esi,TASK_DATA+TASKDATA.pid
144
    mov     esi,TASK_DATA+TASKDATA.pid
145
 
145
 
146
newsearch:
146
newsearch:
147
    cmp     [esi],eax
147
    cmp     [esi],eax
148
    je      foundPID
148
    je      foundPID
149
    inc     ecx
149
    inc     ecx
150
    add     esi,0x20
150
    add     esi,0x20
151
    cmp     ecx,[TASK_COUNT]
151
    cmp     ecx,[TASK_COUNT]
152
    jbe     newsearch
152
    jbe     newsearch
153
 
153
 
154
foundPID:
154
foundPID:
155
    shl     ecx,8
155
    shl     ecx,8
156
    or      dword [ecx+0x80000+APPDATA.event_mask],dword 10000000b ; stack event
156
    or      dword [ecx+SLOT_BASE+APPDATA.event_mask],dword 10000000b ; stack event
157
 
157
 
158
    mov     [check_idle_semaphore],200
158
    mov     [check_idle_semaphore],200
159
 
159
 
160
udprx_001:
160
udprx_001:
161
    pop     eax
161
    pop     eax
162
    call    freeBuff    ; Discard the packet
162
    call    freeBuff    ; Discard the packet
163
    ret
163
    ret