Rev 1196 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1196 | Rev 1206 | ||
---|---|---|---|
Line 12... | Line 12... | ||
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
13 | ;; Version 2, June 1991 ;; |
13 | ;; Version 2, June 1991 ;; |
14 | ;; ;; |
14 | ;; ;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 16... | Line 16... | ||
16 | 16 | ||
Line 17... | Line 17... | ||
17 | $Revision: 1019 $ |
17 | $Revision: 1206 $ |
18 | 18 | ||
19 | align 4 |
19 | align 4 |
20 | struct SOCKET |
20 | struct SOCKET |
Line 25... | Line 25... | ||
25 | .Domain dd ? ; INET/UNIX/.. |
25 | .Domain dd ? ; INET/UNIX/.. |
26 | .Type dd ? ; RAW/UDP/TCP/... |
26 | .Type dd ? ; RAW/UDP/TCP/... |
27 | .Protocol dd ? ; ICMP/IPv4/ARP/ |
27 | .Protocol dd ? ; ICMP/IPv4/ARP/ |
28 | .LocalIP dd ? ; local IP address |
28 | .LocalIP dd ? ; local IP address |
29 | .RemoteIP dd ? ; remote IP address |
29 | .RemoteIP dd ? ; remote IP address |
30 | .LocalPort dw ? ; local port |
30 | .LocalPort dw ? ; local port (In INET byte order) |
31 | .RemotePort dw ? ; remote port |
31 | .RemotePort dw ? ; remote port (IN INET byte order |
32 | .OrigRemoteIP dd ? ; original remote IP address (used to reset to LISTEN state) |
32 | .OrigRemoteIP dd ? ; original remote IP address (used to reset to LISTEN state) |
33 | .OrigRemotePort dw ? ; original remote port (used to reset to LISTEN state) |
33 | .OrigRemotePort dw ? ; original remote port (used to reset to LISTEN state) |
34 | .rxDataCount dd ? ; rx data count |
34 | .rxDataCount dd ? ; rx data count |
35 | .TCBState dd ? ; TCB state |
35 | .TCBState dd ? ; TCB state |
36 | .TCBTimer dd ? ; TCB timer (seconds) |
36 | .TCBTimer dd ? ; TCB timer (seconds) |
Line 128... | Line 128... | ||
128 | ; type in edx |
128 | ; type in edx |
129 | ; protocol in esi |
129 | ; protocol in esi |
130 | ; OUT: eax is socket num, -1 on error |
130 | ; OUT: eax is socket num, -1 on error |
131 | ; |
131 | ; |
132 | ;----------------------------------------------- |
132 | ;----------------------------------------------- |
- | 133 | align 4 |
|
133 | socket_open: |
134 | socket_open: |
Line 134... | Line 135... | ||
134 | 135 | ||
Line 135... | Line 136... | ||
135 | DEBUGF 1,"socket_open: domain: %u, type: %u",ecx, edx |
136 | DEBUGF 1,"socket_open: domain: %u, type: %u",ecx, edx |
Line 160... | Line 161... | ||
160 | ; pointer to sockaddr struct in edx |
161 | ; pointer to sockaddr struct in edx |
161 | ; length of that struct in esi |
162 | ; length of that struct in esi |
162 | ; OUT: 0 on success |
163 | ; OUT: 0 on success |
163 | ; |
164 | ; |
164 | ;----------------------------------------------- |
165 | ;----------------------------------------------- |
165 | 166 | align 4 |
|
166 | socket_bind: |
167 | socket_bind: |
Line 167... | Line 168... | ||
167 | 168 | ||
Line 168... | Line 169... | ||
168 | DEBUGF 1,"Socket_bind: socknum: %u sockaddr: %x, length: %u, ",ecx,edx,esi |
169 | DEBUGF 1,"Socket_bind: socknum: %u sockaddr: %x, length: %u, ",ecx,edx,esi |
Line 173... | Line 174... | ||
173 | 174 | ||
174 | cmp esi, 2 |
175 | cmp esi, 2 |
Line 175... | Line 176... | ||
175 | jl s_error |
176 | jl s_error |
176 | - | ||
177 | cmp word [edx], AF_INET4 |
- | |
178 | je .af_inet4 |
177 | |
Line 179... | Line 178... | ||
179 | 178 | cmp word [edx], AF_INET4 |
|
Line 180... | Line 179... | ||
180 | jmp s_error |
179 | jne s_error |
181 | 180 | ||
Line -... | Line 181... | ||
- | 181 | .af_inet4: |
|
182 | .af_inet4: |
182 | |
183 | - | ||
184 | cmp esi, 6 |
183 | cmp esi, 6 |
185 | jl s_error |
184 | jl s_error |
186 | 185 | ||
187 | mov bx, word [edx + 2] |
- | |
188 | rol bx,8 ;;; |
- | |
189 | DEBUGF 1,"local port: %u ",bx |
- | |
190 | test bx, bx |
- | |
191 | jnz .check_only |
- | |
192 | - | ||
193 | mov bx , [last_UDP_port] |
- | |
194 | - | ||
195 | .find_port_loop: |
- | |
196 | inc bx |
- | |
197 | inc [last_UDP_port] |
- | |
198 | - | ||
199 | .check_only: |
- | |
200 | mov esi, net_sockets |
- | |
201 | - | ||
202 | .next_udp_socket: |
- | |
203 | mov esi, [esi + SOCKET.NextPtr] |
- | |
Line 204... | Line -... | ||
204 | or esi, esi |
- | |
205 | jz .udp_port_ok |
186 | mov ecx, [eax + SOCKET.Type] |
206 | - | ||
207 | cmp [esi + SOCKET.Type], IP_PROTO_UDP |
187 | mov bx, word [edx + 2] |
208 | jne .next_udp_socket |
188 | DEBUGF 1,"local port: %x ",bx |
- | 189 | test bx, bx |
|
Line 209... | Line -... | ||
209 | - | ||
210 | cmp [esi + SOCKET.LocalPort], bx |
190 | jz .find_free |
Line 211... | Line 191... | ||
211 | jne .next_udp_socket |
191 | |
- | 192 | call socket_check_port |
|
212 | 193 | test bx, bx |
|
Line 213... | Line 194... | ||
213 | cmp word [edx + 2], 0 |
194 | je s_error |
- | 195 | jmp .got_port |
|
214 | jne s_error |
196 | |
Line 215... | Line 197... | ||
215 | 197 | .find_free: |
|
216 | cmp bx, MAX_EPHEMERAL_PORT |
198 | |
Line 244... | Line 226... | ||
244 | ; length of that struct in esi |
226 | ; length of that struct in esi |
245 | ; OUT: 0 on success |
227 | ; OUT: 0 on success |
246 | ; |
228 | ; |
247 | ;----------------------------------------------- |
229 | ;----------------------------------------------- |
248 | align 4 |
230 | align 4 |
249 | - | ||
250 | socket_connect: |
231 | socket_connect: |
Line 251... | Line 232... | ||
251 | 232 | ||
Line 252... | Line 233... | ||
252 | DEBUGF 1,"Socket_connect: socknum: %u sockaddr: %x, length: %u,",ecx,edx,esi |
233 | DEBUGF 1,"Socket_connect: socknum: %u sockaddr: %x, length: %u,",ecx,edx,esi |
Line 272... | Line 253... | ||
272 | je .udp |
253 | je .udp |
Line 273... | Line 254... | ||
273 | 254 | ||
274 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
255 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
Line 275... | Line 256... | ||
275 | je .icmp |
256 | je .icmp |
276 | 257 | ||
Line 277... | Line 258... | ||
277 | ; cmp [eax + SOCKET.Type], IP_PROTO_TCP |
258 | cmp [eax + SOCKET.Type], IP_PROTO_TCP |
Line 278... | Line 259... | ||
278 | ; je .tcp |
259 | je .tcp |
Line 279... | Line 260... | ||
279 | 260 | ||
280 | jmp s_error |
- | |
281 | 261 | jmp s_error |
|
282 | .udp: |
- | |
283 | 262 | ||
Line 284... | Line 263... | ||
284 | mov bx , word [edx + 2] |
263 | .udp: |
285 | rol bx, 8 |
264 | |
286 | mov word [eax + SOCKET.RemotePort], bx |
- | |
287 | 265 | mov bx , word [edx + 2] |
|
Line 288... | Line 266... | ||
288 | DEBUGF 1,"remote port: %u ",bx |
266 | mov word [eax + SOCKET.RemotePort], bx |
289 | 267 | DEBUGF 1,"remote port: %x ",bx |
|
Line 393... | Line 371... | ||
393 | ; IN: socket number in ecx |
371 | ; IN: socket number in ecx |
394 | ; backlog in edx |
372 | ; backlog in edx |
395 | ; OUT: eax is socket num, -1 on error |
373 | ; OUT: eax is socket num, -1 on error |
396 | ; |
374 | ; |
397 | ;----------------------------------------------- |
375 | ;----------------------------------------------- |
398 | 376 | align 4 |
|
399 | socket_listen: |
377 | socket_listen: |
Line 400... | Line 378... | ||
400 | 378 | ||
Line 401... | Line 379... | ||
401 | DEBUGF 1,"Socket_listen: socknum: %u backlog: %u\n",ecx,edx |
379 | DEBUGF 1,"Socket_listen: socknum: %u backlog: %u\n",ecx,edx |
Line 430... | Line 408... | ||
430 | ; addr in edx |
408 | ; addr in edx |
431 | ; addrlen in esi |
409 | ; addrlen in esi |
432 | ; OUT: eax is socket num, -1 on error |
410 | ; OUT: eax is socket num, -1 on error |
433 | ; |
411 | ; |
434 | ;----------------------------------------------- |
412 | ;----------------------------------------------- |
435 | - | ||
436 | 413 | align 4 |
|
437 | socket_accept: |
414 | socket_accept: |
Line 438... | Line 415... | ||
438 | 415 | ||
Line 439... | Line 416... | ||
439 | DEBUGF 1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n",ecx,edx,esi |
416 | DEBUGF 1,"Socket_accept: socknum: %u sockaddr: %x, length: %u\n",ecx,edx,esi |
Line 477... | Line 454... | ||
477 | ; |
454 | ; |
478 | ; IN: socket number in ecx |
455 | ; IN: socket number in ecx |
479 | ; OUT: eax is socket num, -1 on error |
456 | ; OUT: eax is socket num, -1 on error |
480 | ; |
457 | ; |
481 | ;----------------------------------------------- |
458 | ;----------------------------------------------- |
482 | 459 | align 4 |
|
483 | socket_close: |
460 | socket_close: |
Line 484... | Line 461... | ||
484 | 461 | ||
Line 485... | Line 462... | ||
485 | DEBUGF 1,"Socket_close: socknum: %u\n",ecx |
462 | DEBUGF 1,"Socket_close: socknum: %u\n",ecx |
Line 493... | Line 470... | ||
493 | je .udp |
470 | je .udp |
Line 494... | Line 471... | ||
494 | 471 | ||
495 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
472 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
Line 496... | Line 473... | ||
496 | je .icmp |
473 | je .icmp |
497 | 474 | ||
Line 498... | Line 475... | ||
498 | ; cmp [eax + SOCKET.Type], IP_PROTO_TCP |
475 | cmp [eax + SOCKET.Type], IP_PROTO_TCP |
Line 499... | Line 476... | ||
499 | ; je .tcp |
476 | je .tcp |
Line 627... | Line 604... | ||
627 | ; addrlen in esi |
604 | ; addrlen in esi |
628 | ; flags in edi |
605 | ; flags in edi |
629 | ; OUT: eax is number of bytes copied, -1 on error |
606 | ; OUT: eax is number of bytes copied, -1 on error |
630 | ; |
607 | ; |
631 | ;----------------------------------------------- |
608 | ;----------------------------------------------- |
632 | 609 | align 4 |
|
633 | socket_recv: |
610 | socket_recv: |
Line 634... | Line 611... | ||
634 | 611 | ||
Line 635... | Line 612... | ||
635 | DEBUGF 1,"Socket_receive: socknum: %u sockaddr: %x, length: %u, flags: %x\n",ecx,edx,esi,edi |
612 | DEBUGF 1,"Socket_receive: socknum: %u sockaddr: %x, length: %u, flags: %x\n",ecx,edx,esi,edi |
Line 701... | Line 678... | ||
701 | ; |
678 | ; |
702 | ; SOCKET_send |
679 | ; SOCKET_send |
703 | ; |
680 | ; |
704 | ; |
681 | ; |
705 | ; IN: socket number in ecx |
682 | ; IN: socket number in ecx |
706 | ; addr in edx |
683 | ; pointer to data in edx |
707 | ; addrlen in esi |
684 | ; datalength in esi |
708 | ; flags in edi |
685 | ; flags in edi |
709 | ; OUT: -1 on error |
686 | ; OUT: -1 on error |
710 | ; |
687 | ; |
711 | ;----------------------------------------------- |
688 | ;----------------------------------------------- |
712 | 689 | align 4 |
|
713 | socket_send: |
690 | socket_send: |
Line 714... | Line 691... | ||
714 | 691 | ||
Line 715... | Line 692... | ||
715 | DEBUGF 1,"Socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x, ",ecx,edx,esi,edi |
692 | DEBUGF 1,"Socket_send: socknum: %u sockaddr: %x, length: %u, flags: %x, ",ecx,edx,esi,edi |
716 | 693 | ||
717 | stdcall net_socket_num_to_addr, ecx ; get real socket address |
694 | stdcall net_socket_num_to_addr, ecx ; get real socket address |
Line -... | Line 695... | ||
- | 695 | or eax, eax |
|
- | 696 | jz s_error |
|
- | 697 | ||
- | 698 | cmp word [eax + SOCKET.Domain], AF_INET4 |
|
- | 699 | je .af_inet4 |
|
- | 700 | ||
- | 701 | jmp s_error |
|
718 | or eax, eax |
702 | |
Line 719... | Line 703... | ||
719 | jz s_error |
703 | .af_inet4: |
720 | 704 | ||
Line 721... | Line 705... | ||
721 | DEBUGF 1,"Socket type:%u\n", [eax + SOCKET.Type]:4 |
705 | DEBUGF 1,"Socket type:%u\n", [eax + SOCKET.Type]:4 |
722 | 706 | ||
Line 723... | Line 707... | ||
723 | cmp [eax + SOCKET.Type], IP_PROTO_UDP |
707 | cmp [eax + SOCKET.Type], IP_PROTO_UDP |
724 | je .udp |
708 | je .udp |
Line 725... | Line 709... | ||
725 | 709 | ||
Line 726... | Line 710... | ||
726 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
710 | cmp [eax + SOCKET.Type], IP_PROTO_ICMP |
Line 727... | Line 711... | ||
727 | je .icmp |
711 | je .icmp |
- | 712 | ||
- | 713 | cmp [eax + SOCKET.Type], IP_PROTO_TCP |
|
- | 714 | je .tcp |
|
- | 715 | ||
- | 716 | jmp s_error |
|
- | 717 | ||
- | 718 | .udp: |
|
- | 719 | ||
- | 720 | DEBUGF 1,"type: UDP, " |
|
- | 721 | ||
- | 722 | cmp [eax + SOCKET.LocalPort],0 |
|
Line 728... | Line 723... | ||
728 | 723 | jne .port_ok |
|
729 | ; cmp [eax + SOCKET.Type], IP_PROTO_TCP |
724 | |
730 | ; je .tcp |
725 | mov ecx, [eax + SOCKET.Type] |
731 | 726 | call socket_find_port |
|
732 | jmp s_error |
- | |
733 | - | ||
734 | .udp: |
727 | test bx, bx |
735 | 728 | je s_error |
|
Line 736... | Line 729... | ||
736 | DEBUGF 1,"type: UDP\n" |
729 | mov [eax + SOCKET.LocalPort], bx |
Line 779... | Line 772... | ||
779 | ret |
772 | ret |
Line -... | Line 773... | ||
- | 773 | ||
- | 774 | ||
- | 775 | ||
- | 776 | ||
- | 777 | ;----------------------------------------------- |
|
- | 778 | ; |
|
- | 779 | ; SOCKET_find_free_port (local port) |
|
- | 780 | ; |
|
- | 781 | ; works with INET byte order |
|
- | 782 | ; |
|
- | 783 | ; IN: type in ecx (TCP/UDP) |
|
- | 784 | ; OUT: bx = 0 on error, portnumber otherwise |
|
- | 785 | ; |
|
- | 786 | ;----------------------------------------------- |
|
- | 787 | align 4 |
|
- | 788 | socket_find_port: |
|
- | 789 | ||
- | 790 | DEBUGF 1,"Socket_find_free_port, type: %u ",eax |
|
- | 791 | ||
- | 792 | cmp ecx, IP_PROTO_UDP |
|
- | 793 | je .udp |
|
- | 794 | ||
- | 795 | cmp ecx, IP_PROTO_TCP |
|
- | 796 | je .tcp |
|
- | 797 | ||
- | 798 | .udp: |
|
- | 799 | mov bx, [last_UDP_port] |
|
- | 800 | je .continue |
|
- | 801 | ||
- | 802 | .tcp: |
|
- | 803 | mov bx, [last_TCP_port] |
|
- | 804 | ||
- | 805 | ||
- | 806 | .continue: |
|
- | 807 | inc bx |
|
- | 808 | ||
- | 809 | .check_only: |
|
- | 810 | mov esi, net_sockets |
|
- | 811 | ||
- | 812 | .next_socket: |
|
- | 813 | mov esi, [esi + SOCKET.NextPtr] |
|
- | 814 | or esi, esi |
|
- | 815 | jz .port_ok |
|
- | 816 | ||
- | 817 | cmp [esi + SOCKET.Type], ecx |
|
- | 818 | jne .next_socket |
|
- | 819 | ||
- | 820 | rol bx, 8 |
|
- | 821 | cmp [esi + SOCKET.LocalPort], bx |
|
- | 822 | rol bx, 8 ; this doesnt change the zero flag, does it ? |
|
- | 823 | jne .next_socket |
|
- | 824 | ||
- | 825 | cmp bx, MAX_EPHEMERAL_PORT |
|
- | 826 | jle .continue |
|
- | 827 | ||
- | 828 | ; todo: WRAP! |
|
- | 829 | ; mov [last_UDP_port], MIN_EPHEMERAL_PORT |
|
- | 830 | .exit: |
|
- | 831 | xor ebx, ebx |
|
- | 832 | ||
- | 833 | .port_ok: |
|
- | 834 | rol bx, 8 |
|
- | 835 | ret |
|
- | 836 | ||
- | 837 | ;----------------------------------------------- |
|
- | 838 | ; |
|
- | 839 | ; SOCKET_check_port (local port) |
|
- | 840 | ; |
|
- | 841 | ; works with INET byte order |
|
- | 842 | ; |
|
- | 843 | ; IN: type in ecx (TCP/UDP) |
|
- | 844 | ; port to check in bx |
|
- | 845 | ; OUT: bx = 0 on error, unchanged otherwise |
|
- | 846 | ; |
|
- | 847 | ;----------------------------------------------- |
|
- | 848 | align 4 |
|
- | 849 | socket_check_port: |
|
- | 850 | mov esi, net_sockets |
|
- | 851 | ||
- | 852 | .next_socket: |
|
- | 853 | mov esi, [esi + SOCKET.NextPtr] |
|
- | 854 | or esi, esi |
|
- | 855 | jz .port_ok |
|
- | 856 | ||
- | 857 | cmp [esi + SOCKET.Type], ecx |
|
- | 858 | jne .next_socket |
|
- | 859 | ||
- | 860 | cmp [esi + SOCKET.LocalPort], bx |
|
- | 861 | jne .next_socket |
|
- | 862 | ||
- | 863 | xor ebx, ebx |
|
- | 864 | ||
- | 865 | .port_ok: |
|
- | 866 | ret |
|
- | 867 | ||
- | 868 | ||
- | 869 | ||
- | 870 | ;----------------------------------------------- |
|
- | 871 | ; |
|
- | 872 | ; SOCKET_internal_receiver |
|
- | 873 | ; |
|
- | 874 | ; Checks if any socket wants the received data |
|
- | 875 | ; If so, update the socket |
|
- | 876 | ; |
|
- | 877 | ; IN: eax = socket number |
|
- | 878 | ; ecx = number of bytes |
|
- | 879 | ; esi = pointer to beginning of data |
|
- | 880 | ; dx = Remote port (in INET byte order) |
|
- | 881 | ; edi = IP address of sender |
|
- | 882 | ; |
|
- | 883 | ; OUT: xxx |
|
- | 884 | ; |
|
- | 885 | ;----------------------------------------------- |
|
- | 886 | align 4 |
|
- | 887 | socket_internal_receiver: |
|
- | 888 | ||
- | 889 | DEBUGF 1,"internal socket receiver\n" |
|
- | 890 | ||
- | 891 | lea ebx, [eax + SOCKET.lock] |
|
- | 892 | call wait_mutex |
|
- | 893 | ||
- | 894 | mov [eax + SOCKET.RemotePort], dx ; update remote port number |
|
- | 895 | mov [eax + SOCKET.RemoteIP], edi |
|
- | 896 | ||
- | 897 | mov edx, [eax + SOCKET.rxDataCount] ; get # of bytes already in buffer |
|
- | 898 | DEBUGF 1,"bytes already in socket: %u ", edx |
|
- | 899 | ||
- | 900 | lea edi, [ecx + edx] ; check for buffer overflow |
|
- | 901 | cmp edi, SOCKETBUFFSIZE - SOCKETHEADERSIZE ; |
|
- | 902 | jg .dump ; |
|
- | 903 | ||
- | 904 | lea edi, [eax + SOCKET.rxData + edx] |
|
- | 905 | add [eax + SOCKET.rxDataCount], ecx ; increment the count of bytes in buffer |
|
- | 906 | DEBUGF 1,"adding %u bytes\n", ecx |
|
- | 907 | ||
- | 908 | ; copy the data across |
|
- | 909 | push cx |
|
- | 910 | shr ecx, 2 |
|
- | 911 | rep movsd |
|
- | 912 | pop cx |
|
- | 913 | and cx, 3 |
|
- | 914 | rep movsb |
|
- | 915 | ||
- | 916 | DEBUGF 1,"socket updated\n" |
|
- | 917 | ||
- | 918 | mov [eax + SOCKET.lock], 0 |
|
- | 919 | ||
- | 920 | ; flag an event to the application |
|
- | 921 | mov edx, [eax + SOCKET.PID] ; get socket owner PID |
|
- | 922 | mov ecx, 1 |
|
- | 923 | mov esi, TASK_DATA + TASKDATA.pid |
|
- | 924 | ||
- | 925 | .next_pid: |
|
- | 926 | cmp [esi], edx |
|
- | 927 | je .found_pid |
|
- | 928 | inc ecx |
|
- | 929 | add esi, 0x20 |
|
- | 930 | cmp ecx, [TASK_COUNT] |
|
- | 931 | jbe .next_pid |
|
- | 932 | ret |
|
- | 933 | ||
- | 934 | .found_pid: |
|
- | 935 | shl ecx, 8 |
|
- | 936 | or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK ; stack event |
|
- | 937 | mov [check_idle_semaphore], 200 |
|
- | 938 | ret |
|
- | 939 | ||
Line 780... | Line 940... | ||
780 | 940 | .dump: |