1 /*
2  * Socket wrapper functions.
3  * These could all go into separate files, so only the ones needed cause
4  * the corresponding function to be added to the executable.  If sockets
5  * are a library (SVR4) this might make a difference (?), but if sockets
6  * are in the kernel (BSD) it doesn't matter.
7  *
8  * These wrapper functions also use the same prototypes as POSIX.1g,
9  * which might differ from many implementations (i.e., POSIX.1g specifies
10  * the fourth argument to getsockopt() as "void *", not "char *").
11  *
12  * If your system's headers are not correct [i.e., the Solaris 2.5
13  * <sys/socket.h> omits the "const" from the second argument to both
14  * bind() and connect()], you'll get warnings of the form:
15  *warning: passing arg 2 of `bind' discards `const' from pointer target type
16  *warning: passing arg 2 of `connect' discards `const' from pointer target type
17  */
18 
19 #include	"unp.h"
20 
21 int
Accept(int fd,struct sockaddr * sa,socklen_t * salenptr)22 Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
23 {
24 	int		n;
25 
26 again:
27 	if ( (n = accept(fd, sa, salenptr)) < 0) {
28 #ifdef	EPROTO
29 		if (errno == EPROTO || errno == ECONNABORTED)
30 #else
31 		if (errno == ECONNABORTED)
32 #endif
33 			goto again;
34 		else
35 			err_sys("accept error");
36 	}
37 	return(n);
38 }
39 
40 void
Bind(int fd,const struct sockaddr * sa,socklen_t salen)41 Bind(int fd, const struct sockaddr *sa, socklen_t salen)
42 {
43 	if (bind(fd, sa, salen) < 0)
44 		err_sys("bind error");
45 }
46 
47 void
Connect(int fd,const struct sockaddr * sa,socklen_t salen)48 Connect(int fd, const struct sockaddr *sa, socklen_t salen)
49 {
50 	if (connect(fd, sa, salen) < 0)
51 		err_sys("connect error");
52 }
53 
54 void
Getpeername(int fd,struct sockaddr * sa,socklen_t * salenptr)55 Getpeername(int fd, struct sockaddr *sa, socklen_t *salenptr)
56 {
57 	if (getpeername(fd, sa, salenptr) < 0)
58 		err_sys("getpeername error");
59 }
60 
61 void
Getsockname(int fd,struct sockaddr * sa,socklen_t * salenptr)62 Getsockname(int fd, struct sockaddr *sa, socklen_t *salenptr)
63 {
64 	if (getsockname(fd, sa, salenptr) < 0)
65 		err_sys("getsockname error");
66 }
67 
68 void
Getsockopt(int fd,int level,int optname,void * optval,socklen_t * optlenptr)69 Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
70 {
71 	if (getsockopt(fd, level, optname, optval, optlenptr) < 0)
72 		err_sys("getsockopt error");
73 }
74 
75 int
Isfdtype(int fd,int fdtype)76 Isfdtype(int fd, int fdtype)
77 {
78 	int		n;
79 
80 	if ( (n = isfdtype(fd, fdtype)) < 0)
81 		err_sys("isfdtype error");
82 	return(n);
83 }
84 
85 /* include Listen */
86 void
Listen(int fd,int backlog)87 Listen(int fd, int backlog)
88 {
89 	char	*ptr;
90 
91 		/*4can override 2nd argument with environment variable */
92 	if ( (ptr = getenv("LISTENQ")) != NULL)
93 		backlog = atoi(ptr);
94 
95 	if (listen(fd, backlog) < 0)
96 		err_sys("listen error");
97 }
98 /* end Listen */
99 
100 ssize_t
Recv(int fd,void * ptr,size_t nbytes,int flags)101 Recv(int fd, void *ptr, size_t nbytes, int flags)
102 {
103 	ssize_t		n;
104 
105 	if ( (n = recv(fd, ptr, nbytes, flags)) < 0)
106 		err_sys("recv error");
107 	return(n);
108 }
109 
110 ssize_t
Recvfrom(int fd,void * ptr,size_t nbytes,int flags,struct sockaddr * sa,socklen_t * salenptr)111 Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
112 		 struct sockaddr *sa, socklen_t *salenptr)
113 {
114 	ssize_t		n;
115 
116 	if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
117 		err_sys("recvfrom error");
118 	return(n);
119 }
120 
121 ssize_t
Recvmsg(int fd,struct msghdr * msg,int flags)122 Recvmsg(int fd, struct msghdr *msg, int flags)
123 {
124 	ssize_t		n;
125 
126 	if ( (n = recvmsg(fd, msg, flags)) < 0)
127 		err_sys("recvmsg error");
128 	return(n);
129 }
130 
131 int
Select(int nfds,fd_set * readfds,fd_set * writefds,fd_set * exceptfds,struct timeval * timeout)132 Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
133        struct timeval *timeout)
134 {
135 	int		n;
136 
137 	if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)
138 		err_sys("select error");
139 	return(n);		/* can return 0 on timeout */
140 }
141 
142 void
Send(int fd,const void * ptr,size_t nbytes,int flags)143 Send(int fd, const void *ptr, size_t nbytes, int flags)
144 {
145 	if (send(fd, ptr, nbytes, flags) != nbytes)
146 		err_sys("send error");
147 }
148 
149 void
Sendto(int fd,const void * ptr,size_t nbytes,int flags,const struct sockaddr * sa,socklen_t salen)150 Sendto(int fd, const void *ptr, size_t nbytes, int flags,
151 	   const struct sockaddr *sa, socklen_t salen)
152 {
153 	if (sendto(fd, ptr, nbytes, flags, sa, salen) != nbytes)
154 		err_sys("sendto error");
155 }
156 
157 void
Sendmsg(int fd,const struct msghdr * msg,int flags)158 Sendmsg(int fd, const struct msghdr *msg, int flags)
159 {
160 	int			i;
161 	ssize_t		nbytes;
162 
163 	nbytes = 0;	/* must first figure out what return value should be */
164 	for (i = 0; i < msg->msg_iovlen; i++)
165 		nbytes += msg->msg_iov[i].iov_len;
166 
167 	if (sendmsg(fd, msg, flags) != nbytes)
168 		err_sys("sendmsg error");
169 }
170 
171 void
Setsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)172 Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
173 {
174 	if (setsockopt(fd, level, optname, optval, optlen) < 0)
175 		err_sys("setsockopt error");
176 }
177 
178 void
Shutdown(int fd,int how)179 Shutdown(int fd, int how)
180 {
181 	if (shutdown(fd, how) < 0)
182 		err_sys("shutdown error");
183 }
184 
185 int
Sockatmark(int fd)186 Sockatmark(int fd)
187 {
188 	int		n;
189 
190 	if ( (n = sockatmark(fd)) < 0)
191 		err_sys("sockatmark error");
192 	return(n);
193 }
194 
195 /* include Socket */
196 int
Socket(int family,int type,int protocol)197 Socket(int family, int type, int protocol)
198 {
199 	int		n;
200 
201 	if ( (n = socket(family, type, protocol)) < 0)
202 		err_sys("socket error");
203 	return(n);
204 }
205 /* end Socket */
206 
207 void
Socketpair(int family,int type,int protocol,int * fd)208 Socketpair(int family, int type, int protocol, int *fd)
209 {
210 	int		n;
211 
212 	if ( (n = socketpair(family, type, protocol, fd)) < 0)
213 		err_sys("socketpair error");
214 }
215