Port numbers for TCP and UDP must be given in Big endian order now.

git-svn-id: svn://kolibrios.org@2995 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2012-11-01 12:33:15 +00:00
parent 8e79162d05
commit 151ae82c14
16 changed files with 58 additions and 67 deletions

View File

@ -763,11 +763,11 @@ cmdPASV:
mov al, ',' mov al, ','
stosb stosb
; port ; port
movzx eax, byte [ebp + thread_data.datasock.sin_port + 1] movzx eax, byte [ebp + thread_data.datasock.sin_port]
call dword_to_ascii call dword_to_ascii
mov al, ',' mov al, ','
stosb stosb
movzx eax, byte [ebp + thread_data.datasock.sin_port] movzx eax, byte [ebp + thread_data.datasock.sin_port+1]
call dword_to_ascii call dword_to_ascii
; ')', 13, 10, 0 ; ')', 13, 10, 0
mov eax, ')' + 0x000a0d00 mov eax, ')' + 0x000a0d00
@ -843,13 +843,10 @@ cmdPORT:
; Now the same with portnumber ; Now the same with portnumber
inc esi inc esi
call ascii_to_byte call ascii_to_byte
mov bh, al mov byte[ebp + thread_data.datasock.sin_port], al
inc esi inc esi
call ascii_to_byte call ascii_to_byte
mov bl, al mov byte[ebp + thread_data.datasock.sin_port+1], al
; Save it in datasock too
mov [ebp + thread_data.datasock.sin_port], bx
; We will open the socket, but do not connect yet! ; We will open the socket, but do not connect yet!
mov [ebp + thread_data.datasock.sin_family], AF_INET4 mov [ebp + thread_data.datasock.sin_family], AF_INET4

View File

@ -126,8 +126,10 @@ start:
mov [serverip], ebx mov [serverip], ebx
invoke ini.get_int, path, str_ftpd, str_port, 21 invoke ini.get_int, path, str_ftpd, str_port, 21
xchg al, ah
mov [sockaddr1.port], ax mov [sockaddr1.port], ax
xchg al, ah
invoke con_printf, str1, eax invoke con_printf, str1, eax
add esp, 8 add esp, 8
@ -378,7 +380,7 @@ str_end db 'end', 0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 21 .port dw 0
.ip dd 0 .ip dd 0
rb 10 rb 10
.length = $ - sockaddr1 .length = $ - sockaddr1

View File

@ -689,7 +689,7 @@ lock xadd [DNSrequestID], eax ; atomically increment ID, get old value
push 0 push 0
push 0 ; sin_zero push 0 ; sin_zero
push esi ; sin_addr push esi ; sin_addr
push AF_INET4 + (53 shl 16) push AF_INET4 + (53 shl 24)
; sin_family and sin_port in network byte order ; sin_family and sin_port in network byte order
; 8c. Connect. ; 8c. Connect.
mcall 75, 4, , esp, sizeof.sockaddr_in mcall 75, 4, , esp, sizeof.sockaddr_in
@ -1204,7 +1204,7 @@ getaddrinfo._.generate_data: ;;
; Just copy port from input __gai_reqdata to output addrinfo. ; Just copy port from input __gai_reqdata to output addrinfo.
push edx push edx
mov edx, [ebx+__gai_reqdata.service] mov edx, [ebx+__gai_reqdata.service]
xchg dl, dh ; convert to network byte order xchg dl, dh ; convert to network byte order ;;;;; CHECKME
mov [edi+sizeof.addrinfo+sockaddr_in.sin_port], dx mov [edi+sizeof.addrinfo+sockaddr_in.sin_port], dx
pop edx pop edx
ret ret

View File

@ -29,7 +29,9 @@ use32
__DEBUG__ equ 1 ; enable/disable __DEBUG__ equ 1 ; enable/disable
__DEBUG_LEVEL__ equ 1 ; 1 = all, 2 = errors __DEBUG_LEVEL__ equ 1 ; 1 = all, 2 = errors
BUFFERSIZE equ 1024 BUFFERSIZE equ 1024
DEFAULTPORT equ 24800
include '../macros.inc' include '../macros.inc'
purge mov,add,sub purge mov,add,sub
@ -79,7 +81,8 @@ start:
test eax, eax test eax, eax
jnz error jnz error
invoke ini.get_int, path, str_remote, str_port, 24800 invoke ini.get_int, path, str_remote, str_port, DEFAULTPORT
xchg al, ah
mov [sockaddr1.port], ax mov [sockaddr1.port], ax
push str1 push str1
@ -337,7 +340,7 @@ mousestate dd 0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 24800 .port dw 0
.ip dd 192 + 168 shl 8 + 1 shl 16 + 115 shl 24 .ip dd 192 + 168 shl 8 + 1 shl 16 + 115 shl 24
rb 10 rb 10

View File

@ -135,7 +135,7 @@ hello db 'Hello world!',0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 23 .port dw 0x1700 ; 23
.ip dd 0 .ip dd 0
rb 10 rb 10
.length = $ - sockaddr1 .length = $ - sockaddr1

View File

@ -264,7 +264,7 @@ str10 db 'Push any key to continue.',0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 23 .port dw 0x1700 ; 23
.ip dd 0 .ip dd 0
rb 10 rb 10

View File

@ -141,27 +141,27 @@ key:
align 4 align 4
draw_window: draw_window:
mcall 12,1 mcall 12,1
mcall 0,(50*65536+400),(30*65536+180),0x34AABBCC,0x085080DD,str_title mcall 0, (50*65536+400), (30*65536+180), 0x34AABBCC, 0x085080DD, str_title
mcall 4,35*65536+10,0x80000000, str_server mcall 4, 35*65536+10, 0x80000000, str_server
mov ebx,5*65536+ 30 mov ebx, 5*65536+30
mov edx, str_source mov edx, str_source
mcall mcall
mov ebx,11*65536+ 50 mov ebx, 11*65536+50
mov edx, str_destination mov edx, str_destination
mcall mcall
mov ebx,47*65536+72 mov ebx, 47*65536+72
mov edx, str_mode mov edx, str_mode
mcall mcall
mov ebx,160*65536+72 mov ebx, 160*65536+72
mov edx, str_method mov edx, str_method
mcall mcall
mov ebx,270*65536+72 mov ebx, 270*65536+72
mov edx, str_blocksize mov edx, str_blocksize
mcall mcall
@ -451,7 +451,7 @@ done dd 0
sockaddr: sockaddr:
dw AF_INET4 dw AF_INET4
dw 69 dw 0x4500 ; 69
IP db 192,168,1,115 IP db 192,168,1,115
sockaddr_len = $ - sockaddr sockaddr_len = $ - sockaddr

View File

@ -238,7 +238,7 @@ pointerevent db 5 ; pointerevent
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 5900 .port dw 0x0c17 ; 5900
.ip dd 0 .ip dd 0
rb 10 rb 10

View File

@ -548,7 +548,7 @@ str_type db 'type',0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
dw 68 ; local port dw 68 shl 8 ; local port
dd 0 ; local IP dd 0 ; local IP
rb 10 rb 10
@ -557,7 +557,7 @@ sockaddr1:
sockaddr2: sockaddr2:
dw AF_INET4 dw AF_INET4
dw 67 ; destination port dw 67 shl 8 ; destination port
dd -1 ; destination IP dd -1 ; destination IP
rb 10 rb 10

View File

@ -375,7 +375,7 @@ IPv4_input: ; TODO: add IPv4
mov [esi + FRAGMENT_slot.ttl], 15 ; RFC recommends 15 secs as ttl mov [esi + FRAGMENT_slot.ttl], 15 ; RFC recommends 15 secs as ttl
mov ax, [edx + IPv4_header.Identification] mov ax, [edx + IPv4_header.Identification]
mov [esi + FRAGMENT_slot.id], ax mov [esi + FRAGMENT_slot.id], ax
mov eax,[edx + IPv4_header.SourceAddress] mov eax, [edx + IPv4_header.SourceAddress]
mov [esi + FRAGMENT_slot.SrcIP], eax mov [esi + FRAGMENT_slot.SrcIP], eax
mov eax, [edx + IPv4_header.DestinationAddress] mov eax, [edx + IPv4_header.DestinationAddress]
mov [esi + FRAGMENT_slot.DstIP], eax mov [esi + FRAGMENT_slot.DstIP], eax

View File

@ -44,15 +44,15 @@ ends
struct IP_SOCKET SOCKET struct IP_SOCKET SOCKET
LocalIP rd 4 LocalIP rd 4 ; network byte order
RemoteIP rd 4 RemoteIP rd 4 ; network byte order
ends ends
struct TCP_SOCKET IP_SOCKET struct TCP_SOCKET IP_SOCKET
LocalPort dw ? LocalPort dw ? ; network byte order
RemotePort dw ? RemotePort dw ? ; network byte order
t_state dd ? ; TCB state t_state dd ? ; TCB state
t_rxtshift db ? t_rxtshift db ?
@ -141,8 +141,8 @@ ends
struct UDP_SOCKET IP_SOCKET struct UDP_SOCKET IP_SOCKET
LocalPort dw ? LocalPort dw ? ; network byte order
RemotePort dw ? RemotePort dw ? ; network byte order
firstpacket db ? firstpacket db ?
ends ends
@ -213,6 +213,7 @@ macro SOCKET_init {
jb @r jb @r
cmp ax, MAX_EPHEMERAL_PORT cmp ax, MAX_EPHEMERAL_PORT
ja @r ja @r
xchg al, ah
mov [last_UDP_port], ax mov [last_UDP_port], ax
@@: @@:
@ -221,6 +222,7 @@ macro SOCKET_init {
jb @r jb @r
cmp ax, MAX_EPHEMERAL_PORT cmp ax, MAX_EPHEMERAL_PORT
ja @r ja @r
xchg al, ah
mov [last_TCP_port], ax mov [last_TCP_port], ax
} }
@ -446,7 +448,7 @@ SOCKET_bind:
@@: @@:
mov [eax + IP_SOCKET.LocalIP], ebx mov [eax + IP_SOCKET.LocalIP], ebx
mov bx, [edx + 2] ; Now fill in the port if it's still available mov bx, [edx + 2] ; Now fill in the local port if it's still available
call SOCKET_check_port call SOCKET_check_port
jz s_error ; ZF is set by socket_check_port, on error jz s_error ; ZF is set by socket_check_port, on error
@ -1106,11 +1108,6 @@ SOCKET_find_port:
cmp [eax + SOCKET.Protocol], IP_PROTO_TCP cmp [eax + SOCKET.Protocol], IP_PROTO_TCP
je .tcp je .tcp
jmp .error
.done:
mov [eax + UDP_SOCKET.LocalPort], bx
.error:
pop ecx esi ebx pop ecx esi ebx
ret ret
@ -1118,26 +1115,30 @@ SOCKET_find_port:
mov bx, [last_UDP_port] mov bx, [last_UDP_port]
call .findit call .findit
mov [last_UDP_port], bx mov [last_UDP_port], bx
jmp .done
pop ecx esi ebx
ret
.tcp: .tcp:
mov bx, [last_TCP_port] mov bx, [last_TCP_port]
call .findit call .findit
mov [last_TCP_port], bx mov [last_TCP_port], bx
jmp .done
pop ecx esi ebx
ret
.restart: .restart:
mov bx, MIN_EPHEMERAL_PORT mov bx, MIN_EPHEMERAL_PORT_N
.findit: .findit:
inc bx cmp bx, MAX_EPHEMERAL_PORT_N
je .restart
cmp bx, MAX_EPHEMERAL_PORT add bh, 1
jz .restart adc bl, 0
call SOCKET_check_port call SOCKET_check_port
jz .findit jz .findit
ret ret
@ -1150,9 +1151,9 @@ SOCKET_find_port:
; If the proposed port number is unused, it is filled in in the socket structure ; If the proposed port number is unused, it is filled in in the socket structure
; ;
; IN: eax = socket ptr (to find out if its a TCP/UDP socket) ; IN: eax = socket ptr (to find out if its a TCP/UDP socket)
; bx = proposed socket number ; bx = proposed socket number (network byte order)
; ;
; OUT: ZF = cleared on error ; OUT: ZF = set on error
; ;
;----------------------------------------------------------------- ;-----------------------------------------------------------------
align 4 align 4
@ -1182,10 +1183,9 @@ SOCKET_check_port:
ret ret
.port_ok: .port_ok:
DEBUGF 1,"local port %u is free\n", bx DEBUGF 1,"local port %x is free\n", bx ; FIXME: find a way to print big endian values with debugf
mov [eax + UDP_SOCKET.LocalPort], bx mov [eax + UDP_SOCKET.LocalPort], bx
or bx, bx ; set the zero-flag or bx, bx ; clear the zero-flag
ret ret

View File

@ -34,7 +34,9 @@ endg
MAX_NET_DEVICES = 16 MAX_NET_DEVICES = 16
MIN_EPHEMERAL_PORT = 49152 MIN_EPHEMERAL_PORT = 49152
MIN_EPHEMERAL_PORT_N = 0x00C0 ; same in Network byte order (FIXME)
MAX_EPHEMERAL_PORT = 61000 MAX_EPHEMERAL_PORT = 61000
MAX_EPHEMERAL_PORT_N = 0x48EE ; same in Network byte order (FIXME)
; Ethernet protocol numbers ; Ethernet protocol numbers
ETHER_ARP = 0x0608 ETHER_ARP = 0x0608

View File

@ -76,8 +76,6 @@ TCP_input:
ntohw [edx + TCP_header.Window] ntohw [edx + TCP_header.Window]
ntohw [edx + TCP_header.UrgentPointer] ntohw [edx + TCP_header.UrgentPointer]
ntohw [edx + TCP_header.SourcePort]
ntohw [edx + TCP_header.DestinationPort]
;------------------------ ;------------------------
; Find the socket pointer ; Find the socket pointer

View File

@ -425,10 +425,7 @@ TCP_send:
ntohd [esp] ntohd [esp]
push [eax + TCP_SOCKET.RemotePort] ; .DestinationPort dw ? push [eax + TCP_SOCKET.RemotePort] ; .DestinationPort dw ?
ntohw [esp]
push [eax + TCP_SOCKET.LocalPort] ; .SourcePort dw ? push [eax + TCP_SOCKET.LocalPort] ; .SourcePort dw ?
ntohw [esp]
push edi ; header size push edi ; header size

View File

@ -348,10 +348,8 @@ TCP_respond_socket:
; Fill in the TCP header by using the socket ptr ; Fill in the TCP header by using the socket ptr
mov ax, [esi + TCP_SOCKET.LocalPort] mov ax, [esi + TCP_SOCKET.LocalPort]
rol ax, 8
stosw stosw
mov ax, [esi + TCP_SOCKET.RemotePort] mov ax, [esi + TCP_SOCKET.RemotePort]
rol ax, 8
stosw stosw
mov eax, [esi + TCP_SOCKET.SND_NXT] mov eax, [esi + TCP_SOCKET.SND_NXT]
bswap eax bswap eax
@ -429,10 +427,8 @@ TCP_respond_segment:
; Fill in the TCP header by using a received segment ; Fill in the TCP header by using a received segment
mov ax, [esi + TCP_header.DestinationPort] mov ax, [esi + TCP_header.DestinationPort]
rol ax, 8
stosw stosw
mov ax, [esi + TCP_header.SourcePort] mov ax, [esi + TCP_header.SourcePort]
rol ax, 8
stosw stosw
mov eax, [esi + TCP_header.AckNumber] mov eax, [esi + TCP_header.AckNumber]
bswap eax bswap eax

View File

@ -131,10 +131,8 @@ UDP_input:
.no_checksum: .no_checksum:
DEBUGF 1,"UDP_input: checksum ok\n" DEBUGF 1,"UDP_input: checksum ok\n"
; Convert port numbers to intel format ; Convert length to little endian
rol [esi + UDP_header.DestinationPort], 8
rol [esi + UDP_header.SourcePort], 8
rol [esi + UDP_header.Length], 8 rol [esi + UDP_header.Length], 8
; Look for a socket where ; Look for a socket where
@ -198,7 +196,7 @@ UDP_input:
call mutex_lock call mutex_lock
popa popa
DEBUGF 1,"UDP_input: new remote port=%u\n", cx DEBUGF 1,"UDP_input: new remote port=%x\n", cx ; FIXME: find a way to print big endian values with debugf
mov [eax + UDP_SOCKET.RemotePort], cx mov [eax + UDP_SOCKET.RemotePort], cx
inc [eax + UDP_SOCKET.firstpacket] inc [eax + UDP_SOCKET.firstpacket]
@ -234,12 +232,10 @@ UDP_output:
DEBUGF 1,"UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi DEBUGF 1,"UDP_output: socket=%x bytes=%u data_ptr=%x\n", eax, ecx, esi
mov dx, [eax + UDP_SOCKET.RemotePort] mov dx, [eax + UDP_SOCKET.RemotePort]
DEBUGF 1,"UDP_output: remote port=%u, ", dx DEBUGF 1,"UDP_output: remote port=%x, ", dx ; FIXME: find a way to print big endian values with debugf
rol dx, 8
rol edx, 16 rol edx, 16
mov dx, [eax + UDP_SOCKET.LocalPort] mov dx, [eax + UDP_SOCKET.LocalPort]
DEBUGF 1,"local port=%u\n", dx DEBUGF 1,"local port=%x\n", dx
rol dx, 8
sub esp, 8 ; Data ptr and data size will be placed here sub esp, 8 ; Data ptr and data size will be placed here
push edx esi push edx esi