aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/src/inet.c b/src/inet.c
index 2334f53..80c488b 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -180,11 +180,11 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
180/*-------------------------------------------------------------------------*\ 180/*-------------------------------------------------------------------------*\
181* Tries to connect to remote address (address, port) 181* Tries to connect to remote address (address, port)
182\*-------------------------------------------------------------------------*/ 182\*-------------------------------------------------------------------------*/
183const char *inet_tryconnect(p_sock ps, const char *address, 183const char *inet_tryconnect(p_sock ps, p_tm tm, const char *address,
184 unsigned short port, int timeout) 184 unsigned short port)
185{ 185{
186 struct sockaddr_in remote; 186 struct sockaddr_in remote;
187 const char *err; 187 int err;
188 memset(&remote, 0, sizeof(remote)); 188 memset(&remote, 0, sizeof(remote));
189 remote.sin_family = AF_INET; 189 remote.sin_family = AF_INET;
190 remote.sin_port = htons(port); 190 remote.sin_port = htons(port);
@@ -197,14 +197,14 @@ const char *inet_tryconnect(p_sock ps, const char *address,
197 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); 197 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
198 } 198 }
199 } else remote.sin_family = AF_UNSPEC; 199 } else remote.sin_family = AF_UNSPEC;
200 err = sock_connect(ps, (SA *) &remote, sizeof(remote), timeout); 200 do err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm_getretry(tm));
201 if (err) { 201 while (err == IO_RETRY && tm_getretry(tm));
202 if (err != IO_DONE) {
202 sock_destroy(ps); 203 sock_destroy(ps);
203 *ps = SOCK_INVALID; 204 *ps = SOCK_INVALID;
204 return err; 205 if (err == IO_ERROR) return sock_connectstrerror();
205 } else { 206 else return io_strerror(err);
206 return NULL; 207 } else return NULL;
207 }
208} 208}
209 209
210/*-------------------------------------------------------------------------*\ 210/*-------------------------------------------------------------------------*\
@@ -214,7 +214,6 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
214 int backlog) 214 int backlog)
215{ 215{
216 struct sockaddr_in local; 216 struct sockaddr_in local;
217 const char *err;
218 memset(&local, 0, sizeof(local)); 217 memset(&local, 0, sizeof(local));
219 /* address is either wildcard or a valid ip address */ 218 /* address is either wildcard or a valid ip address */
220 local.sin_addr.s_addr = htonl(INADDR_ANY); 219 local.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -229,11 +228,10 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
229 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); 228 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
230 } 229 }
231 sock_setblocking(ps); 230 sock_setblocking(ps);
232 err = sock_bind(ps, (SA *) &local, sizeof(local)); 231 if (sock_bind(ps, (SA *) &local, sizeof(local)) != IO_DONE) {
233 if (err) {
234 sock_destroy(ps); 232 sock_destroy(ps);
235 *ps = SOCK_INVALID; 233 *ps = SOCK_INVALID;
236 return err; 234 return sock_bindstrerror();
237 } else { 235 } else {
238 sock_setnonblocking(ps); 236 sock_setnonblocking(ps);
239 if (backlog > 0) sock_listen(ps, backlog); 237 if (backlog > 0) sock_listen(ps, backlog);
@@ -246,7 +244,22 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
246\*-------------------------------------------------------------------------*/ 244\*-------------------------------------------------------------------------*/
247const char *inet_trycreate(p_sock ps, int type) 245const char *inet_trycreate(p_sock ps, int type)
248{ 246{
249 return sock_create(ps, AF_INET, type, 0); 247 if (sock_create(ps, AF_INET, type, 0) == IO_DONE) return NULL;
248 else return sock_createstrerror();
249}
250
251/*-------------------------------------------------------------------------*\
252* Tries to accept an inet socket
253\*-------------------------------------------------------------------------*/
254const char *inet_tryaccept(p_sock ps, p_tm tm, p_sock pc)
255{
256 struct sockaddr_in addr;
257 socklen_t addr_len = sizeof(addr);
258 int err;
259 /* loop until connection accepted or timeout happens */
260 do err = sock_accept(ps, pc, (SA *) &addr, &addr_len, tm_getretry(tm));
261 while (err == IO_RETRY && tm_getretry(tm) != 0);
262 return io_strerror(err);
250} 263}
251 264
252/*-------------------------------------------------------------------------*\ 265/*-------------------------------------------------------------------------*\