6 #include <sys/socket.h>
8 #include <net/netlib.h>
9 #include <netinet/in.h>
13 static int _tcp_socket(int protocol
);
14 static int _udp_socket(int protocol
);
16 int socket(int domain
, int type
, int protocol
)
19 fprintf(stderr
, "socket: domain %d, type %d, protocol %d\n",
20 domain
, type
, protocol
);
22 if (domain
!= AF_INET
)
25 fprintf(stderr
, "socket: bad domain %d\n", domain
);
30 if (type
== SOCK_STREAM
)
31 return _tcp_socket(protocol
);
33 if (type
== SOCK_DGRAM
)
34 return _udp_socket(protocol
);
37 fprintf(stderr
, "socket: nothing for domain %d, type %d, protocol %d\n",
38 domain
, type
, protocol
);
44 static int _tcp_socket(int protocol
)
47 if (protocol
!= 0 && protocol
!= IPPROTO_TCP
)
50 fprintf(stderr
, "socket(tcp): bad protocol %d\n", protocol
);
52 errno
= EPROTONOSUPPORT
;
55 fd
= open(TCP_DEVICE
, O_RDWR
);
59 static int _udp_socket(int protocol
)
62 struct sockaddr_in sin
;
64 if (protocol
!= 0 && protocol
!= IPPROTO_UDP
)
67 fprintf(stderr
, "socket(udp): bad protocol %d\n", protocol
);
69 errno
= EPROTONOSUPPORT
;
72 fd
= open(UDP_DEVICE
, O_RDWR
);
76 /* Bind is implict for UDP sockets? */
77 sin
.sin_family
= AF_INET
;
78 sin
.sin_addr
.s_addr
= INADDR_ANY
;
80 r
= bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
));