Kernel 0.5.8-r8; tcp protocol is finished from base and was merge with sockets, added...
[ZeXOS.git] / kernel / core / net / socket.c
blob27a50b4a1cb8f2ab4e2f402ea38d771c36a2b24c
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <string.h>
22 #include <net/socket.h>
23 #include <dev.h>
25 socket_t socket_list;
27 extern fd_t fd_list;
28 extern unsigned int fd_count;
30 socket_t *socket_getbyfd (int fd)
32 socket_t *socket;
33 for (socket = socket_list.next; socket != &socket_list; socket = socket->next) {
34 if (socket->fd == fd)
35 return socket;
38 return 0;
41 int socket (int family, int type, int protocol)
43 socket_t *sock;
45 /* alloc and init context */
46 sock = (socket_t *) kmalloc (sizeof (socket_t));
48 sock->fd = fd_count ++;
49 sock->family = family;
50 sock->type = type;
51 sock->protocol = protocol;
53 switch (sock->family) {
54 case AF_UNSPEC:
55 return -1;
56 case AF_UNIX:
58 return -1;
59 case AF_INET:
60 sock->fd = net_proto_tcp_socket ();
61 break;
62 case AF_RS232:
63 sock->fd = ips_socket ();
64 break;
67 /* add into list */
68 sock->next = &socket_list;
69 sock->prev = socket_list.prev;
70 sock->prev->next = sock;
71 sock->next->prev = sock;
73 fd_t *fd = (fd_t *) kmalloc (sizeof (fd_t));
75 fd->flags = FD_SOCK;
76 fd->id = sock->fd;
78 fd->next = &fd_list;
79 fd->prev = fd_list.prev;
80 fd->prev->next = fd;
81 fd->next->prev = fd;
83 return sock->fd;
86 hostent *gethostbyname (char *hostname)
88 unsigned l = strlen (hostname);
90 hostent *host = (hostent *) kmalloc (sizeof (hostent));
91 memset (host, 0, sizeof (hostent));
93 host->h_addr = (char *) kmalloc (sizeof (char) * (l + 2));
94 memset (host->h_addr, 0, l+1);
96 if (l < 32) {
97 memcpy (host->h_addr, hostname, l);
98 host->h_addr[l] = '\0';
101 /* +1 ? Yes, it contain zero character '\0' because memcpy */
102 host->h_length = l+1;
104 return host;
107 int connect (int fd, sockaddr_in *addr, socklen_t len)
109 DPRINT ("connect () -> socket: %d\n", fd);
110 socket_t *sock = socket_getbyfd (fd);
112 if (!sock)
113 return -1;
115 switch (sock->family) {
116 case AF_UNSPEC:
118 return -1;
119 case AF_UNIX:
121 return -1;
122 case AF_INET:
123 return net_proto_tcp_connect (fd, addr);
124 case AF_RS232:
125 return ips_connect (fd, addr);
128 return 1;
131 int send (int fd, char *msg, size_t size, int flags)
133 socket_t *sock = socket_getbyfd (fd);
135 if (!sock)
136 return -1;
138 DPRINT ("send (%d) -> %s\n", fd, msg);
140 switch (sock->family) {
141 case AF_UNSPEC:
143 return -1;
144 case AF_UNIX:
146 return -1;
147 case AF_INET:
148 return net_proto_tcp_send (fd, msg, size);
149 case AF_RS232:
150 return ips_send (fd, msg, size);
153 return 1;
156 int recv (int fd, char *msg, size_t size, int flags)
158 socket_t *sock = socket_getbyfd (fd);
160 if (!sock)
161 return -1;
163 DPRINT ("recv (%d)\n", fd);
165 switch (sock->family) {
166 case AF_UNSPEC:
168 return -1;
169 case AF_UNIX:
171 return -1;
172 case AF_INET:
173 return net_proto_tcp_recv (fd, msg, size);
174 case AF_RS232:
175 return ips_recv (fd, msg, size);
178 return 1;
181 int bind (int fd, sockaddr_in *addr, socklen_t len)
183 socket_t *sock = socket_getbyfd (fd);
185 if (!sock)
186 return -1;
188 DPRINT ("bind (%d)\n", fd);
190 switch (sock->family) {
191 case AF_UNSPEC:
193 return -1;
194 case AF_UNIX:
196 return -1;
197 case AF_INET:
199 return -1;
200 case AF_RS232:
201 return ips_bind (fd, addr);
204 return 1;
207 int listen (int fd, int backlog)
209 socket_t *sock = socket_getbyfd (fd);
211 if (!sock)
212 return -1;
214 DPRINT ("listen (%d)\n", fd);
216 switch (sock->family) {
217 case AF_UNSPEC:
219 return -1;
220 case AF_UNIX:
222 return -1;
223 case AF_INET:
225 return -1;
226 case AF_RS232:
227 return ips_listen (fd, backlog);
230 return 1;
233 int accept (int fd, sockaddr_in *addr, socklen_t *addrlen)
235 socket_t *servsock = socket_getbyfd (fd);
237 if (!servsock)
238 return -1;
240 DPRINT ("accept (%d)\n", fd);
242 int client = 0;
244 switch (servsock->family) {
245 case AF_UNSPEC:
247 return -1;
248 case AF_UNIX:
250 return -1;
251 case AF_INET:
253 return -1;
254 case AF_RS232:
255 client = ips_accept (fd, addr, addrlen);
256 break;
259 if (client <= 0)
260 return client;
262 socket_t *sock;
264 /* alloc and init context */
265 sock = (socket_t *) kmalloc (sizeof (socket_t));
267 fd_count ++;
268 sock->fd = client;
269 sock->family = servsock->family;
270 sock->type = servsock->type;
271 sock->protocol = servsock->protocol;
273 /* add into list */
274 sock->next = &socket_list;
275 sock->prev = socket_list.prev;
276 sock->prev->next = sock;
277 sock->next->prev = sock;
279 fd_t *fd_client = (fd_t *) kmalloc (sizeof (fd_t));
281 fd_client->flags = FD_SOCK;
282 fd_client->id = sock->fd;
284 fd_client->next = &fd_list;
285 fd_client->prev = fd_list.prev;
286 fd_client->prev->next = fd_client;
287 fd_client->next->prev = fd_client;
289 return client;
292 int sclose (int fd)
294 socket_t *sock = socket_getbyfd (fd);
296 if (!sock)
297 return -1;
299 DPRINT ("close (%d)\n", fd);
301 switch (sock->family) {
302 case AF_UNSPEC:
304 return -1;
305 case AF_UNIX:
307 return -1;
308 case AF_INET:
309 return net_proto_tcp_close (fd);
310 case AF_RS232:
311 return ips_close (fd);
314 return 1;
317 int sfcntl (int fd, int cmd, long arg)
319 socket_t *sock = socket_getbyfd (fd);
321 if (!sock)
322 return -1;
324 DPRINT ("fcntl (%d)\n", fd);
326 switch (sock->family) {
327 case AF_UNSPEC:
329 return -1;
330 case AF_UNIX:
332 return -1;
333 case AF_INET:
334 return net_proto_tcp_fcntl (fd, cmd, arg);
335 case AF_RS232:
336 return ips_fcntl (fd);
340 unsigned int init_socket ()
342 socket_list.next = &socket_list;
343 socket_list.prev = &socket_list;
345 fd_count = 0;
347 return 1;