Rev 4423 | Rev 5565 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4423 | Rev 5201 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2013. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2014. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
6 | ;; Part of the TCP/IP network stack for KolibriOS ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Written by hidnplayr@kolibrios.org, ;; |
8 | ;; Written by hidnplayr@kolibrios.org, ;; |
Line 13... | Line 13... | ||
13 | ;; GNU GENERAL PUBLIC LICENSE ;; |
13 | ;; GNU GENERAL PUBLIC LICENSE ;; |
14 | ;; Version 2, June 1991 ;; |
14 | ;; Version 2, June 1991 ;; |
15 | ;; ;; |
15 | ;; ;; |
16 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
16 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
Line 17... | Line 17... | ||
17 | 17 | ||
Line 18... | Line 18... | ||
18 | $Revision: 4423 $ |
18 | $Revision: 5201 $ |
Line 19... | Line 19... | ||
19 | 19 | ||
20 | struct SOCKET |
20 | struct SOCKET |
Line 291... | Line 291... | ||
291 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", ecx, edx, esi |
291 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_open: domain=%u type=%u protocol=%x ", ecx, edx, esi |
Line 292... | Line 292... | ||
292 | 292 | ||
293 | push ecx edx esi |
293 | push ecx edx esi |
294 | call SOCKET_alloc |
294 | call SOCKET_alloc |
- | 295 | pop esi edx ecx |
|
295 | pop esi edx ecx |
296 | test eax, eax |
Line 296... | Line 297... | ||
296 | jz .nobuffs |
297 | jz .nobuffs |
297 | 298 | ||
Line 695... | Line 696... | ||
695 | 696 | ||
Line 696... | Line 697... | ||
696 | or [eax + SOCKET.options], SO_NONBLOCK ; Mark the socket as non blocking, we dont want it to block any longer! |
697 | or [eax + SOCKET.options], SO_NONBLOCK ; Mark the socket as non blocking, we dont want it to block any longer! |
697 | 698 | ||
698 | test [eax + SOCKET.state], SS_BLOCKED ; Is the socket still in blocked state? |
699 | test [eax + SOCKET.state], SS_BLOCKED ; Is the socket still in blocked state? |
699 | jz @f |
700 | jz @f |
Line 700... | Line 701... | ||
700 | call SOCKET_notify.unblock ; Unblock it. |
701 | call SOCKET_notify ; Unblock it. |
701 | @@: |
702 | @@: |
Line 1190... | Line 1191... | ||
1190 | SOCKET_pair: |
1191 | SOCKET_pair: |
Line 1191... | Line 1192... | ||
1191 | 1192 | ||
Line 1192... | Line 1193... | ||
1192 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n" |
1193 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_pair\n" |
- | 1194 | ||
1193 | 1195 | call SOCKET_alloc |
|
1194 | call SOCKET_alloc |
1196 | test eax, eax |
Line 1195... | Line 1197... | ||
1195 | jz .nomem1 |
1197 | jz .nomem1 |
1196 | mov [esp+32], edi ; application's eax |
1198 | mov [esp+32], edi ; application's eax |
Line 1202... | Line 1204... | ||
1202 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_local |
1204 | mov [eax + SOCKET.rcv_proc], SOCKET_receive_local |
1203 | mov [eax + SOCKET.PID], 0 |
1205 | mov [eax + SOCKET.PID], 0 |
1204 | mov ebx, eax |
1206 | mov ebx, eax |
Line 1205... | Line 1207... | ||
1205 | 1207 | ||
- | 1208 | call SOCKET_alloc |
|
1206 | call SOCKET_alloc |
1209 | test eax, eax |
1207 | jz .nomem2 |
1210 | jz .nomem2 |
Line 1208... | Line 1211... | ||
1208 | mov [esp+20], edi ; application's ebx |
1211 | mov [esp+20], edi ; application's ebx |
1209 | 1212 | ||
Line 1218... | Line 1221... | ||
1218 | mov [eax + SOCKET.device], ebx |
1221 | mov [eax + SOCKET.device], ebx |
1219 | mov [ebx + SOCKET.device], eax |
1222 | mov [ebx + SOCKET.device], eax |
Line 1220... | Line 1223... | ||
1220 | 1223 | ||
1221 | lea eax, [eax + STREAM_SOCKET.rcv] |
1224 | lea eax, [eax + STREAM_SOCKET.rcv] |
- | 1225 | call SOCKET_ring_create |
|
- | 1226 | test eax, eax |
|
Line 1222... | Line 1227... | ||
1222 | call SOCKET_ring_create |
1227 | jz .nomem1 |
1223 | 1228 | ||
1224 | lea eax, [ebx + STREAM_SOCKET.rcv] |
1229 | lea eax, [ebx + STREAM_SOCKET.rcv] |
- | 1230 | call SOCKET_ring_create |
|
Line 1225... | Line 1231... | ||
1225 | call SOCKET_ring_create |
1231 | test eax, eax |
Line 1226... | Line 1232... | ||
1226 | pop eax |
1232 | jz .nomem2 |
1227 | 1233 | ||
Line 1463... | Line 1469... | ||
1463 | ret |
1469 | ret |
Line 1464... | Line 1470... | ||
1464 | 1470 | ||
1465 | 1471 | ||
1466 | ;-------------------------- |
1472 | ;-------------------------- |
- | 1473 | ; |
|
1467 | ; |
1474 | ; IN: eax = ptr to ring struct (just a buffer of the right size) |
1468 | ; eax = ptr to ring struct (just a buffer of the right size) |
1475 | ; OUT: eax = unchanged / 0 on error |
1469 | ; |
1476 | ; |
Line 1470... | Line 1477... | ||
1470 | align 4 |
1477 | align 4 |
1471 | SOCKET_ring_create: |
1478 | SOCKET_ring_create: |
Line 1472... | Line 1479... | ||
1472 | 1479 | ||
1473 | push esi |
1480 | push esi |
1474 | mov esi, eax |
1481 | mov esi, eax |
- | 1482 | ||
- | 1483 | push edx |
|
Line 1475... | Line 1484... | ||
1475 | 1484 | stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW |
|
Line 1476... | Line 1485... | ||
1476 | push edx |
1485 | pop edx |
1477 | stdcall create_ring_buffer, SOCKET_MAXDATA, PG_SW |
1486 | test eax, eax |
Line 1491... | Line 1500... | ||
1491 | add eax, SOCKET_MAXDATA |
1500 | add eax, SOCKET_MAXDATA |
1492 | mov [esi + RING_BUFFER.end_ptr], eax |
1501 | mov [esi + RING_BUFFER.end_ptr], eax |
1493 | mov eax, esi |
1502 | mov eax, esi |
1494 | pop esi |
1503 | pop esi |
Line -... | Line 1504... | ||
- | 1504 | ||
1495 | 1505 | .fail: |
|
Line 1496... | Line 1506... | ||
1496 | ret |
1506 | ret |
1497 | 1507 | ||
1498 | ;----------------------------------------------------------------- |
1508 | ;----------------------------------------------------------------- |
Line 1534... | Line 1544... | ||
1534 | add edi, ecx |
1544 | add edi, ecx |
1535 | cmp edi, [eax + RING_BUFFER.end_ptr] |
1545 | cmp edi, [eax + RING_BUFFER.end_ptr] |
1536 | jb @f |
1546 | jb @f |
1537 | sub edi, SOCKET_MAXDATA ; WRAP |
1547 | sub edi, SOCKET_MAXDATA ; WRAP |
1538 | @@: |
1548 | @@: |
- | 1549 | ||
1539 | mov [eax + RING_BUFFER.write_ptr], edi |
1550 | mov [eax + RING_BUFFER.write_ptr], edi |
1540 | pop edi |
1551 | pop edi |
Line 1541... | Line 1552... | ||
1541 | 1552 | ||
1542 | ; update size |
1553 | ; update size |
Line 1705... | Line 1716... | ||
1705 | align 4 |
1716 | align 4 |
1706 | SOCKET_block: |
1717 | SOCKET_block: |
Line 1707... | Line 1718... | ||
1707 | 1718 | ||
Line 1708... | Line -... | ||
1708 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax |
- | |
1709 | 1719 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: %x\n", eax |
|
- | 1720 | ||
- | 1721 | push eax |
|
1710 | pushf |
1722 | |
Line 1711... | Line 1723... | ||
1711 | push eax |
1723 | pushf |
1712 | cli |
1724 | cli |
Line 1722... | Line 1734... | ||
1722 | ; Remember the thread ID so we can wake it up again |
1734 | ; Remember the thread ID so we can wake it up again |
1723 | mov edx, [edx + TASKDATA.pid] |
1735 | mov edx, [edx + TASKDATA.pid] |
1724 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx |
1736 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: suspending thread: %u\n", edx |
1725 | mov [eax + SOCKET.TID], edx |
1737 | mov [eax + SOCKET.TID], edx |
1726 | pop edx |
1738 | pop edx |
- | 1739 | popf |
|
Line 1727... | Line 1740... | ||
1727 | 1740 | ||
1728 | call change_task |
1741 | call change_task |
1729 | pop eax |
- | |
Line 1730... | Line 1742... | ||
1730 | popf |
1742 | pop eax |
Line 1731... | Line 1743... | ||
1731 | 1743 | ||
Line 1732... | Line 1744... | ||
1732 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: continueing\n" |
1744 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_block: continuing\n" |
Line 1750... | Line 1762... | ||
1750 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax |
1762 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: %x\n", eax |
Line 1751... | Line 1763... | ||
1751 | 1763 | ||
1752 | call SOCKET_check |
1764 | call SOCKET_check |
Line 1753... | Line -... | ||
1753 | jz .error |
- | |
1754 | - | ||
1755 | test [eax + SOCKET.state], SS_BLOCKED |
- | |
1756 | jnz .unblock |
- | |
1757 | - | ||
1758 | ; test [eax + SOCKET.options], SO_NONBLOCK |
- | |
1759 | ; jz .error |
- | |
1760 | - | ||
1761 | push eax ecx esi |
- | |
1762 | - | ||
1763 | ; socket exists and is of non blocking type. |
- | |
1764 | ; We'll try to flag an event to the thread |
- | |
1765 | - | ||
1766 | mov eax, [eax + SOCKET.TID] |
- | |
1767 | test eax, eax |
- | |
1768 | jz .done |
1765 | jz .error |
1769 | mov ecx, 1 |
- | |
1770 | mov esi, TASK_DATA + TASKDATA.pid |
- | |
1771 | - | ||
1772 | .next_pid: |
- | |
1773 | cmp [esi], eax |
- | |
1774 | je .found_pid |
- | |
1775 | inc ecx |
- | |
1776 | add esi, 0x20 |
- | |
1777 | cmp ecx, [TASK_COUNT] |
- | |
1778 | jbe .next_pid |
- | |
1779 | ; PID not found, TODO: close socket! |
- | |
1780 | jmp .done |
- | |
1781 | - | ||
1782 | .found_pid: |
- | |
1783 | shl ecx, 8 |
- | |
1784 | or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK |
- | |
1785 | - | ||
1786 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", eax |
- | |
1787 | jmp .done |
- | |
1788 | 1766 | ||
1789 | .unblock: |
- | |
1790 | push eax ecx esi |
- | |
1791 | ; Clear the 'socket is blocked' flag |
- | |
1792 | and [eax + SOCKET.state], not SS_BLOCKED |
- | |
1793 | 1767 | ; Find the associated thread's TASK_DATA |
|
1794 | ; Find the thread's TASK_DATA |
1768 | push ebx ecx esi |
1795 | mov eax, [eax + SOCKET.TID] |
1769 | mov ebx, [eax + SOCKET.TID] |
1796 | test eax, eax |
1770 | test ebx, ebx |
1797 | jz .error |
1771 | jz .error2 |
1798 | xor ecx, ecx |
1772 | xor ecx, ecx |
1799 | inc ecx |
1773 | inc ecx |
1800 | mov esi, TASK_DATA |
1774 | mov esi, TASK_DATA |
1801 | .next: |
1775 | .next: |
1802 | cmp [esi + TASKDATA.pid], eax |
1776 | cmp [esi + TASKDATA.pid], ebx |
1803 | je .found |
1777 | je .found |
1804 | inc ecx |
1778 | inc ecx |
1805 | add esi, 0x20 |
1779 | add esi, 0x20 |
- | 1780 | cmp ecx, [TASK_COUNT] |
|
- | 1781 | jbe .next |
|
- | 1782 | ||
- | 1783 | .error2: |
|
1806 | cmp ecx, [TASK_COUNT] |
1784 | ; PID not found, TODO: close socket! |
- | 1785 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: error finding thread 0x%x !\n", ebx |
|
- | 1786 | pop esi ecx ebx |
|
- | 1787 | ret |
|
- | 1788 | ||
- | 1789 | .error: |
|
- | 1790 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_notify: invalid socket ptr: 0x%x !\n", eax |
|
1807 | jbe .next |
1791 | ret |
- | 1792 | ||
- | 1793 | .found: |
|
Line -... | Line 1794... | ||
- | 1794 | test [eax + SOCKET.state], SS_BLOCKED |
|
1808 | jmp .error |
1795 | jnz .un_block |
1809 | .found: |
1796 | |
1810 | 1797 | ; socket and thread exists and socket is of non blocking type. |
|
Line 1811... | Line -... | ||
1811 | ; Run the thread |
- | |
- | 1798 | ; We'll try to flag an event to the thread. |
|
1812 | mov [esi + TASKDATA.state], 0 ; Running |
1799 | shl ecx, 8 |
- | 1800 | or [ecx + SLOT_BASE + APPDATA.event_mask], EVENT_NETWORK |
|
Line -... | Line 1801... | ||
- | 1801 | ||
1813 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: Unblocked socket!\n" |
1802 | DEBUGF DEBUG_NETWORK_VERBOSE, "SOCKET_notify: poking thread %u!\n", eax |
- | 1803 | pop esi ecx ebx |
|
- | 1804 | ret |
|
- | 1805 | ||
- | 1806 | ||
- | 1807 | .un_block: |
|
- | 1808 | ; socket and thread exists and socket is of blocking type |
|
- | 1809 | ; We'll try to unblock it. |
|
1814 | 1810 | and [eax + SOCKET.state], not SS_BLOCKED ; Clear the 'socket is blocked' flag |
|
Line 1815... | Line 1811... | ||
1815 | .done: |
1811 | mov [esi + TASKDATA.state], 0 ; Run the thread |
1816 | pop esi ecx eax |
1812 | |
Line 1828... | Line 1824... | ||
1828 | ; put into beginning of list (which is a fastest way). |
1824 | ; put into beginning of list (which is a fastest way). |
1829 | ; |
1825 | ; |
1830 | ; IN: / |
1826 | ; IN: / |
1831 | ; OUT: eax = 0 on error, socket ptr otherwise |
1827 | ; OUT: eax = 0 on error, socket ptr otherwise |
1832 | ; edi = socket number |
1828 | ; edi = socket number |
1833 | ; ZF = cleared on error |
- | |
1834 | ; |
1829 | ; |
1835 | ;-------------------------------------------------------------------- |
1830 | ;-------------------------------------------------------------------- |
1836 | align 4 |
1831 | align 4 |
1837 | SOCKET_alloc: |
1832 | SOCKET_alloc: |
Line 1915... | Line 1910... | ||
1915 | call mutex_unlock |
1910 | call mutex_unlock |
1916 | popa |
1911 | popa |
1917 | @@: |
1912 | @@: |
Line 1918... | Line 1913... | ||
1918 | 1913 | ||
1919 | mov [net_sockets + SOCKET.NextPtr], eax |
- | |
Line 1920... | Line 1914... | ||
1920 | or eax, eax ; used to clear zero flag |
1914 | mov [net_sockets + SOCKET.NextPtr], eax |
1921 | 1915 | ||
1922 | pusha |
1916 | pusha |
1923 | mov ecx, socket_mutex |
1917 | mov ecx, socket_mutex |
Line 1966... | Line 1960... | ||
1966 | 1960 | ||
1967 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
1961 | cmp [eax + SOCKET.Protocol], IP_PROTO_TCP |
Line 1968... | Line 1962... | ||
1968 | jnz .no_tcp |
1962 | jnz .no_tcp |
- | 1963 | ||
- | 1964 | mov ebx, eax |
|
1969 | 1965 | cmp [ebx + STREAM_SOCKET.rcv.start_ptr], 0 |
|
- | 1966 | je @f |
|
- | 1967 | stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr] |
|
- | 1968 | @@: |
|
1970 | mov ebx, eax |
1969 | cmp [ebx + STREAM_SOCKET.snd.start_ptr], 0 |
- | 1970 | je @f |
|
1971 | stdcall kernel_free, [ebx + STREAM_SOCKET.rcv.start_ptr] |
1971 | stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr] |
1972 | stdcall kernel_free, [ebx + STREAM_SOCKET.snd.start_ptr] |
1972 | @@: |
Line 1973... | Line 1973... | ||
1973 | mov eax, ebx |
1973 | mov eax, ebx |
1974 | .no_tcp: |
1974 | .no_tcp: |
Line 2020... | Line 2020... | ||
2020 | 2020 | ||
2021 | ; Allocate new socket |
2021 | ; Allocate new socket |
2022 | push ebx |
2022 | push ebx |
2023 | call SOCKET_alloc |
2023 | call SOCKET_alloc |
- | 2024 | pop ebx |
|
2024 | pop ebx |
2025 | test eax, eax |
Line 2025... | Line 2026... | ||
2025 | jz .fail |
2026 | jz .fail |
2026 | 2027 | ||
2027 | push eax |
2028 | push eax |
Line 2099... | Line 2100... | ||
2099 | pusha |
2100 | pusha |
2100 | mov ecx, socket_mutex |
2101 | mov ecx, socket_mutex |
2101 | call mutex_unlock |
2102 | call mutex_unlock |
2102 | popa |
2103 | popa |
Line 2103... | Line 2104... | ||
2103 | 2104 | ||
- | 2105 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: socket %u not found!\n", eax |
|
2104 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: not found\n", eax |
2106 | DEBUGF DEBUG_NETWORK_ERROR, "SOCKET_num_to_ptr: caller = 0x%x\n", [esp] |
Line 2105... | Line 2107... | ||
2105 | ret |
2107 | ret |
2106 | 2108 | ||
Line 2208... | Line 2210... | ||
2208 | ; |
2210 | ; |
2209 | ;------------------------------------------------------ |
2211 | ;------------------------------------------------------ |
2210 | align 4 |
2212 | align 4 |
2211 | SOCKET_process_end: |
2213 | SOCKET_process_end: |
Line -... | Line 2214... | ||
- | 2214 | ||
- | 2215 | ret ; FIXME |
|
2212 | 2216 | ||
2213 | cmp [net_sockets + SOCKET.NextPtr], 0 ; Are there any active sockets at all? |
2217 | cmp [net_sockets + SOCKET.NextPtr], 0 ; Are there any active sockets at all? |
Line 2214... | Line 2218... | ||
2214 | je .quickret ; nope, exit immediately |
2218 | je .quickret ; nope, exit immediately |