3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <net/socket.h>
29 extern unsigned int fd_count
;
31 socket_t
*socket_getbyfd (int fd
)
34 for (socket
= socket_list
.next
; socket
!= &socket_list
; socket
= socket
->next
) {
42 int socket (int family
, int type
, int protocol
)
46 /* alloc and init context */
47 sock
= (socket_t
*) kmalloc (sizeof (socket_t
));
49 sock
->fd
= fd_count
++;
50 sock
->family
= family
;
52 sock
->protocol
= protocol
;
54 switch (sock
->protocol
) {
56 switch (sock
->family
) {
63 sock
->fd
= net_proto_tcp_socket ();
66 sock
->fd
= ips_socket ();
71 switch (sock
->family
) {
78 sock
->fd
= net_proto_udp_socket ();
87 sock
->next
= &socket_list
;
88 sock
->prev
= socket_list
.prev
;
89 sock
->prev
->next
= sock
;
90 sock
->next
->prev
= sock
;
92 fd_t
*fd
= (fd_t
*) kmalloc (sizeof (fd_t
));
98 fd
->prev
= fd_list
.prev
;
105 hostent
*gethostbyname (char *hostname
)
109 unsigned l
= strlen (hostname
);
111 /* TODO: make better checking, that it is ip addresss */
113 if (hostname
[i
] == '.')
121 /* it is dns address */
123 target
= dns_cache_get (hostname
);
126 target
= dns_send_request (hostname
);
128 target
= net_proto_ip_convert (hostname
);
133 hostent
*host
= (hostent
*) kmalloc (sizeof (hostent
));
138 memset (host
, 0, sizeof (hostent
));
140 /* 4bytes - unsigned - for ipv4 */
143 host
->h_addr
= (char *) kmalloc (sizeof (char) * host
->h_length
);
150 memcpy (host
->h_addr
, &target
, host
->h_length
);
155 int connect (int fd
, sockaddr_in
*addr
, socklen_t len
)
157 DPRINT ("connect () -> socket: %d\n", fd
);
158 socket_t
*sock
= socket_getbyfd (fd
);
163 switch (sock
->protocol
) {
165 switch (sock
->family
) {
173 return net_proto_tcp_connect (fd
, addr
);
175 return ips_connect (fd
, addr
);
179 switch (sock
->family
) {
187 return net_proto_udp_connect (fd
, addr
);
198 int send (int fd
, char *msg
, size_t size
, int flags
)
200 socket_t
*sock
= socket_getbyfd (fd
);
205 DPRINT ("send (%d) -> %s\n", fd
, msg
);
207 switch (sock
->protocol
) {
209 switch (sock
->family
) {
217 return net_proto_tcp_send (fd
, msg
, size
);
219 return ips_send (fd
, msg
, size
);
223 switch (sock
->family
) {
231 return net_proto_udp_send (fd
, msg
, size
);
242 int sendto (int fd
, const void *msg
, size_t len
, int flags
, sockaddr_in
*to
, socklen_t tolen
)
244 socket_t
*sock
= socket_getbyfd (fd
);
249 DPRINT ("sendto (%d) -> %s\n", fd
, msg
);
251 switch (sock
->protocol
) {
253 switch (sock
->family
) {
261 return -1; // net_proto_tcp_sendto (fd, msg, len, to);
268 switch (sock
->family
) {
276 return net_proto_udp_sendto (fd
, msg
, len
, to
);
287 int recv (int fd
, char *msg
, size_t size
, int flags
)
289 socket_t
*sock
= socket_getbyfd (fd
);
294 DPRINT ("recv (%d)\n", fd
);
296 switch (sock
->protocol
) {
298 switch (sock
->family
) {
306 return net_proto_tcp_recv (fd
, msg
, size
);
308 return ips_recv (fd
, msg
, size
);
312 switch (sock
->family
) {
320 return net_proto_udp_recv (fd
, msg
, size
);
331 int recvfrom (int fd
, char *msg
, size_t size
, int flags
, sockaddr_in
*from
, socklen_t fromlen
)
333 socket_t
*sock
= socket_getbyfd (fd
);
338 DPRINT ("recvfrom (%d)\n", fd
);
340 switch (sock
->protocol
) {
342 switch (sock
->family
) {
351 return -1; //net_proto_tcp_recvfrom (fd, msg, size, from);
358 switch (sock
->family
) {
366 return net_proto_udp_recvfrom (fd
, msg
, size
, from
);
377 int bind (int fd
, sockaddr_in
*addr
, socklen_t len
)
379 socket_t
*sock
= socket_getbyfd (fd
);
384 DPRINT ("bind (%d)\n", fd
);
386 switch (sock
->protocol
) {
388 switch (sock
->family
) {
396 return net_proto_tcp_bind (fd
, addr
, len
);
398 return ips_bind (fd
, addr
);
402 switch (sock
->family
) {
410 return net_proto_udp_bind (fd
, addr
, len
);
420 int listen (int fd
, int backlog
)
422 socket_t
*sock
= socket_getbyfd (fd
);
427 DPRINT ("listen (%d)\n", fd
);
429 switch (sock
->family
) {
437 return net_proto_tcp_listen (fd
, backlog
);
439 return ips_listen (fd
, backlog
);
445 int accept (int fd
, sockaddr_in
*addr
, socklen_t
*addrlen
)
447 socket_t
*servsock
= socket_getbyfd (fd
);
452 DPRINT ("accept (%d)\n", fd
);
456 switch (servsock
->family
) {
464 client
= net_proto_tcp_accept (fd
, addr
, addrlen
);
467 client
= ips_accept (fd
, addr
, addrlen
);
476 /* alloc and init context */
477 sock
= (socket_t
*) kmalloc (sizeof (socket_t
));
481 sock
->family
= servsock
->family
;
482 sock
->type
= servsock
->type
;
483 sock
->protocol
= servsock
->protocol
;
486 sock
->next
= &socket_list
;
487 sock
->prev
= socket_list
.prev
;
488 sock
->prev
->next
= sock
;
489 sock
->next
->prev
= sock
;
491 fd_t
*fd_client
= (fd_t
*) kmalloc (sizeof (fd_t
));
493 fd_client
->flags
= FD_SOCK
;
494 fd_client
->id
= sock
->fd
;
496 fd_client
->next
= &fd_list
;
497 fd_client
->prev
= fd_list
.prev
;
498 fd_client
->prev
->next
= fd_client
;
499 fd_client
->next
->prev
= fd_client
;
506 socket_t
*sock
= socket_getbyfd (fd
);
511 DPRINT ("close (%d)\n", fd
);
513 switch (sock
->protocol
) {
515 switch (sock
->family
) {
523 return net_proto_tcp_close (fd
);
525 return ips_close (fd
);
529 switch (sock
->family
) {
537 return net_proto_udp_close (fd
);
548 int sfcntl (int fd
, int cmd
, long arg
)
550 socket_t
*sock
= socket_getbyfd (fd
);
555 DPRINT ("fcntl (%d)\n", fd
);
557 switch (sock
->family
) {
565 return net_proto_tcp_fcntl (fd
, cmd
, arg
);
567 return ips_fcntl (fd
);
571 unsigned int init_socket ()
573 socket_list
.next
= &socket_list
;
574 socket_list
.prev
= &socket_list
;