. pci driver now returns devices, even when they have been pci_reserve()d
[minix3.git] / lib / ip / socket.c
blobc08ded1bc65f5b5d81d59ee072d78a3558317ad8
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <sys/socket.h>
8 #include <net/netlib.h>
9 #include <netinet/in.h>
11 #define DEBUG 0
13 static int _tcp_socket(int protocol);
14 static int _udp_socket(int protocol);
16 int socket(int domain, int type, int protocol)
18 #if DEBUG
19 fprintf(stderr, "socket: domain %d, type %d, protocol %d\n",
20 domain, type, protocol);
21 #endif
22 if (domain != AF_INET)
24 #if DEBUG
25 fprintf(stderr, "socket: bad domain %d\n", domain);
26 #endif
27 errno= EAFNOSUPPORT;
28 return -1;
30 if (type == SOCK_STREAM)
31 return _tcp_socket(protocol);
33 if (type == SOCK_DGRAM)
34 return _udp_socket(protocol);
36 #if DEBUG
37 fprintf(stderr, "socket: nothing for domain %d, type %d, protocol %d\n",
38 domain, type, protocol);
39 #endif
40 errno= EPROTOTYPE;
41 return -1;
44 static int _tcp_socket(int protocol)
46 int fd;
47 if (protocol != 0 && protocol != IPPROTO_TCP)
49 #if DEBUG
50 fprintf(stderr, "socket(tcp): bad protocol %d\n", protocol);
51 #endif
52 errno= EPROTONOSUPPORT;
53 return -1;
55 fd= open(TCP_DEVICE, O_RDWR);
56 return fd;
59 static int _udp_socket(int protocol)
61 int r, fd, t_errno;
62 struct sockaddr_in sin;
64 if (protocol != 0 && protocol != IPPROTO_UDP)
66 #if DEBUG
67 fprintf(stderr, "socket(udp): bad protocol %d\n", protocol);
68 #endif
69 errno= EPROTONOSUPPORT;
70 return -1;
72 fd= open(UDP_DEVICE, O_RDWR);
73 if (fd == -1)
74 return fd;
76 /* Bind is implict for UDP sockets? */
77 sin.sin_family= AF_INET;
78 sin.sin_addr.s_addr= INADDR_ANY;
79 sin.sin_port= 0;
80 r= bind(fd, (struct sockaddr *)&sin, sizeof(sin));
81 if (r != 0)
83 t_errno= errno;
84 close(fd);
85 errno= t_errno;
86 return -1;
88 return fd;