source: trunk/minix/lib/ip/socket.c@ 12

Last change on this file since 12 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.6 KB
Line 
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>
7
8#include <net/netlib.h>
9#include <netinet/in.h>
10
11#define DEBUG 0
12
13static int _tcp_socket(int protocol);
14static int _udp_socket(int protocol);
15
16int socket(int domain, int type, int protocol)
17{
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)
23 {
24#if DEBUG
25 fprintf(stderr, "socket: bad domain %d\n", domain);
26#endif
27 errno= EAFNOSUPPORT;
28 return -1;
29 }
30 if (type == SOCK_STREAM)
31 return _tcp_socket(protocol);
32
33 if (type == SOCK_DGRAM)
34 return _udp_socket(protocol);
35
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;
42}
43
44static int _tcp_socket(int protocol)
45{
46 int fd;
47 if (protocol != 0 && protocol != IPPROTO_TCP)
48 {
49#if DEBUG
50 fprintf(stderr, "socket(tcp): bad protocol %d\n", protocol);
51#endif
52 errno= EPROTONOSUPPORT;
53 return -1;
54 }
55 fd= open(TCP_DEVICE, O_RDWR);
56 return fd;
57}
58
59static int _udp_socket(int protocol)
60{
61 int r, fd, t_errno;
62 struct sockaddr_in sin;
63
64 if (protocol != 0 && protocol != IPPROTO_UDP)
65 {
66#if DEBUG
67 fprintf(stderr, "socket(udp): bad protocol %d\n", protocol);
68#endif
69 errno= EPROTONOSUPPORT;
70 return -1;
71 }
72 fd= open(UDP_DEVICE, O_RDWR);
73 if (fd == -1)
74 return fd;
75
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)
82 {
83 t_errno= errno;
84 close(fd);
85 errno= t_errno;
86 return -1;
87 }
88 return fd;
89}
90
Note: See TracBrowser for help on using the repository browser.