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/>.
22 #include <net/socket.h>
28 extern unsigned int fd_count
;
30 socket_t
*socket_getbyfd (int fd
)
33 for (socket
= socket_list
.next
; socket
!= &socket_list
; socket
= socket
->next
) {
41 int socket (int family
, int type
, int protocol
)
45 /* alloc and init context */
46 sock
= (socket_t
*) kmalloc (sizeof (socket_t
));
48 sock
->fd
= fd_count
++;
49 sock
->family
= family
;
51 sock
->protocol
= protocol
;
53 switch (sock
->family
) {
60 sock
->fd
= net_proto_tcp_socket ();
63 sock
->fd
= ips_socket ();
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
));
79 fd
->prev
= fd_list
.prev
;
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);
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;
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
);
115 switch (sock
->family
) {
123 return net_proto_tcp_connect (fd
, addr
);
125 return ips_connect (fd
, addr
);
131 int send (int fd
, char *msg
, size_t size
, int flags
)
133 socket_t
*sock
= socket_getbyfd (fd
);
138 DPRINT ("send (%d) -> %s\n", fd
, msg
);
140 switch (sock
->family
) {
148 return net_proto_tcp_send (fd
, msg
, size
);
150 return ips_send (fd
, msg
, size
);
156 int recv (int fd
, char *msg
, size_t size
, int flags
)
158 socket_t
*sock
= socket_getbyfd (fd
);
163 DPRINT ("recv (%d)\n", fd
);
165 switch (sock
->family
) {
173 return net_proto_tcp_recv (fd
, msg
, size
);
175 return ips_recv (fd
, msg
, size
);
181 int bind (int fd
, sockaddr_in
*addr
, socklen_t len
)
183 socket_t
*sock
= socket_getbyfd (fd
);
188 DPRINT ("bind (%d)\n", fd
);
190 switch (sock
->family
) {
201 return ips_bind (fd
, addr
);
207 int listen (int fd
, int backlog
)
209 socket_t
*sock
= socket_getbyfd (fd
);
214 DPRINT ("listen (%d)\n", fd
);
216 switch (sock
->family
) {
227 return ips_listen (fd
, backlog
);
233 int accept (int fd
, sockaddr_in
*addr
, socklen_t
*addrlen
)
235 socket_t
*servsock
= socket_getbyfd (fd
);
240 DPRINT ("accept (%d)\n", fd
);
244 switch (servsock
->family
) {
255 client
= ips_accept (fd
, addr
, addrlen
);
264 /* alloc and init context */
265 sock
= (socket_t
*) kmalloc (sizeof (socket_t
));
269 sock
->family
= servsock
->family
;
270 sock
->type
= servsock
->type
;
271 sock
->protocol
= servsock
->protocol
;
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
;
294 socket_t
*sock
= socket_getbyfd (fd
);
299 DPRINT ("close (%d)\n", fd
);
301 switch (sock
->family
) {
309 return net_proto_tcp_close (fd
);
311 return ips_close (fd
);
317 int sfcntl (int fd
, int cmd
, long arg
)
319 socket_t
*sock
= socket_getbyfd (fd
);
324 DPRINT ("fcntl (%d)\n", fd
);
326 switch (sock
->family
) {
334 return net_proto_tcp_fcntl (fd
, cmd
, arg
);
336 return ips_fcntl (fd
);
340 unsigned int init_socket ()
342 socket_list
.next
= &socket_list
;
343 socket_list
.prev
= &socket_list
;