| | 1 | /* This file is part of Whitix. |
| | 2 | * |
| | 3 | * Whitix is free software; you can redistribute it and/or modify |
| | 4 | * it under the terms of the GNU General Public License as published by |
| | 5 | * the Free Software Foundation; either version 2 of the License, or |
| | 6 | * (at your option) any later version. |
| | 7 | * |
| | 8 | * Whitix is distributed in the hope that it will be useful, |
| | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| | 11 | * GNU General Public License for more details. |
| | 12 | * |
| | 13 | * You should have received a copy of the GNU General Public License |
| | 14 | * along with Whitix; if not, write to the Free Software |
| | 15 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| | 16 | * |
| | 17 | */ |
| | 18 | |
| | 43 | struct Socket |
| | 44 | { |
| | 45 | int state; |
| | 46 | int family; |
| | 47 | int type; |
| | 48 | struct SocketAddr addr; |
| | 49 | struct SocketOps* socketOps; |
| | 50 | |
| | 51 | /* Since a server socket and server's children sockets are used for |
| | 52 | * entirely different purposes, we can optimize structure size by |
| | 53 | * using a union. |
| | 54 | */ |
| | 55 | union { |
| | 56 | struct { |
| | 57 | struct ListHead recvPackets, sendPackets; |
| | 58 | }; |
| | 59 | |
| | 60 | struct ListHead childSocks; |
| | 61 | }; |
| | 62 | struct ListHead next; |
| | 63 | struct Socket* parent; |
| | 64 | void* protoInfo; |
| | 65 | }; |
| | 66 | |
| | 67 | struct SocketBuffer |
| | 68 | { |
| | 69 | int length; |
| | 70 | int read; |
| | 71 | struct ListHead next; |
| | 72 | char data[1]; |
| | 73 | }; |
| | 74 | |
| | 75 | struct SocketOps |
| | 76 | { |
| | 77 | int (*bind)(struct Socket* socket, struct SocketAddr* addr, int length); |
| | 78 | int (*connect)(struct Socket* socket, struct SocketAddr* addr, int length); |
| | 79 | int (*create)(struct Socket* socket); |
| | 80 | int (*close)(struct Socket* socket); |
| | 81 | int (*listen)(struct Socket* socket, int backlog); |
| | 82 | int (*accept)(struct Socket* socket, struct SocketAddr* addr, int* length); |
| | 83 | int (*send)(struct Socket* socket, const void* buffer, int length, int flags); |
| | 84 | int (*receive)(struct Socket* socket, void* buffer, int length, int flags); |
| | 85 | }; |
| | 86 | |
| | 87 | /* Prototypes */ |
| | 88 | int SocketRegisterFamily(int family, struct SocketOps* ops); |
| | 89 | int SocketInit(); |
| | 90 | int SocketClose(struct Socket* socket); |
| | 91 | int SocketSetupChild(struct Socket* parent, struct Socket* child); |
| | 92 | int SocketDoAccept(struct Socket* server, struct Socket* client, struct Socket** child); |
| | 93 | struct SocketBuffer* SocketAllocateBuffer(const void* buffer, int length); |
| | 94 | int SocketDestroyBuffer(struct SocketBuffer* sockBuff); |
| | 95 | |
| 16 | | int SysSocketAccept(int fd, struct SocketAddr* addr, int length); |
| | 101 | int SysSocketAccept(int fd, struct SocketAddr* addr, int* length); |
| | 102 | int SysSocketSend(int fd, const void* buffer, int length, int flags); |
| | 103 | int SysSocketReceive(int fd, void* buffer, int length, int flags); |
| | 104 | int SysSocketIoCtl(int fd, unsigned long code, void* data); |
| | 105 | int SysSocketClose(int fd); |