5 __weak_alias(socket
, _socket
)
13 #include <sys/socket.h>
14 #include <sys/ioc_net.h>
16 #include <net/netlib.h>
17 #include <netinet/in.h>
21 static int _tcp_socket(int protocol
);
22 static int _udp_socket(int protocol
);
23 static int _uds_socket(int type
, int protocol
);
25 int socket(int domain
, int type
, int protocol
)
28 fprintf(stderr
, "socket: domain %d, type %d, protocol %d\n",
29 domain
, type
, protocol
);
31 if (domain
!= AF_INET
&& domain
!= AF_UNIX
)
34 fprintf(stderr
, "socket: bad domain %d\n", domain
);
40 if (domain
== AF_UNIX
&& (type
== SOCK_STREAM
||
41 type
== SOCK_DGRAM
|| type
== SOCK_SEQPACKET
))
42 return _uds_socket(type
, protocol
);
44 if (domain
== AF_INET
&& type
== SOCK_STREAM
)
45 return _tcp_socket(protocol
);
47 if (domain
== AF_INET
&& type
== SOCK_DGRAM
)
48 return _udp_socket(protocol
);
51 fprintf(stderr
, "socket: nothing for domain %d, type %d, protocol %d\n",
52 domain
, type
, protocol
);
58 static int _tcp_socket(int protocol
)
61 if (protocol
!= 0 && protocol
!= IPPROTO_TCP
)
64 fprintf(stderr
, "socket(tcp): bad protocol %d\n", protocol
);
66 errno
= EPROTONOSUPPORT
;
69 fd
= open(TCP_DEVICE
, O_RDWR
);
73 static int _udp_socket(int protocol
)
76 struct sockaddr_in sin
;
78 if (protocol
!= 0 && protocol
!= IPPROTO_UDP
)
81 fprintf(stderr
, "socket(udp): bad protocol %d\n", protocol
);
83 errno
= EPROTONOSUPPORT
;
86 fd
= open(UDP_DEVICE
, O_RDWR
);
90 /* Bind is implict for UDP sockets? */
91 sin
.sin_family
= AF_INET
;
92 sin
.sin_addr
.s_addr
= INADDR_ANY
;
94 r
= bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
));
105 static int _uds_socket(int type
, int protocol
)
111 fprintf(stderr
, "socket(uds): bad protocol %d\n", protocol
);
113 errno
= EPROTONOSUPPORT
;
117 fd
= open(UDS_DEVICE
, O_RDWR
);
122 /* set the type for the socket via ioctl (SOCK_DGRAM,
123 * SOCK_STREAM, SOCK_SEQPACKET, etc)
125 r
= ioctl(fd
, NWIOSUDSTYPE
, &type
);
129 /* if that failed rollback socket creation */
133 /* return the error thrown by the call to ioctl */