aboutsummaryrefslogtreecommitdiff
path: root/src/inet.c
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-21 18:40:52 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-21 18:40:52 +0000
commit195069cf5f929f445b0ce20e531cd482d2559083 (patch)
tree6444d37c4ce32395428ba26ee0d16c0c372bd9da /src/inet.c
parente63f500d24ff0238425c9e13f220daf09a277ef5 (diff)
downloadluasocket-195069cf5f929f445b0ce20e531cd482d2559083.tar.gz
luasocket-195069cf5f929f445b0ce20e531cd482d2559083.tar.bz2
luasocket-195069cf5f929f445b0ce20e531cd482d2559083.zip
Fixed functions that return messages in ?socket.c.
Moved complexity of connect and accept there. Created a new options.c module to take care of options. Auxiliar.c is now cleaner.
Diffstat (limited to 'src/inet.c')
-rw-r--r--src/inet.c56
1 files changed, 18 insertions, 38 deletions
diff --git a/src/inet.c b/src/inet.c
index 6aea596..f6c2a6f 100644
--- a/src/inet.c
+++ b/src/inet.c
@@ -178,13 +178,21 @@ static void inet_pushresolved(lua_State *L, struct hostent *hp)
178} 178}
179 179
180/*-------------------------------------------------------------------------*\ 180/*-------------------------------------------------------------------------*\
181* Tries to create a new inet socket
182\*-------------------------------------------------------------------------*/
183const char *inet_trycreate(p_sock ps, int type)
184{
185 return sock_create(ps, AF_INET, type, 0);
186}
187
188/*-------------------------------------------------------------------------*\
181* Tries to connect to remote address (address, port) 189* Tries to connect to remote address (address, port)
182\*-------------------------------------------------------------------------*/ 190\*-------------------------------------------------------------------------*/
183const char *inet_tryconnect(p_sock ps, p_tm tm, const char *address, 191const char *inet_tryconnect(p_sock ps, const char *address,
184 unsigned short port) 192 unsigned short port, p_tm tm)
185{ 193{
186 struct sockaddr_in remote; 194 struct sockaddr_in remote;
187 int err; 195 const char *err;
188 memset(&remote, 0, sizeof(remote)); 196 memset(&remote, 0, sizeof(remote));
189 remote.sin_family = AF_INET; 197 remote.sin_family = AF_INET;
190 remote.sin_port = htons(port); 198 remote.sin_port = htons(port);
@@ -197,14 +205,9 @@ const char *inet_tryconnect(p_sock ps, p_tm tm, const char *address,
197 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr)); 205 memcpy(&remote.sin_addr, *addr, sizeof(struct in_addr));
198 } 206 }
199 } else remote.sin_family = AF_UNSPEC; 207 } else remote.sin_family = AF_UNSPEC;
200 do err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm_getretry(tm)); 208 err = sock_connect(ps, (SA *) &remote, sizeof(remote), tm);
201 while (err == IO_RETRY && tm_getretry(tm)); 209 if (err) sock_destroy(ps);
202 if (err != IO_DONE) { 210 return err;
203 sock_destroy(ps);
204 *ps = SOCK_INVALID;
205 if (err == IO_ERROR) return sock_connectstrerror();
206 else return io_strerror(err);
207 } else return NULL;
208} 211}
209 212
210/*-------------------------------------------------------------------------*\ 213/*-------------------------------------------------------------------------*\
@@ -214,6 +217,7 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
214 int backlog) 217 int backlog)
215{ 218{
216 struct sockaddr_in local; 219 struct sockaddr_in local;
220 const char *err;
217 memset(&local, 0, sizeof(local)); 221 memset(&local, 0, sizeof(local));
218 /* address is either wildcard or a valid ip address */ 222 /* address is either wildcard or a valid ip address */
219 local.sin_addr.s_addr = htonl(INADDR_ANY); 223 local.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -228,10 +232,10 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
228 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr)); 232 memcpy(&local.sin_addr, *addr, sizeof(struct in_addr));
229 } 233 }
230 sock_setblocking(ps); 234 sock_setblocking(ps);
231 if (sock_bind(ps, (SA *) &local, sizeof(local)) != IO_DONE) { 235 err = sock_bind(ps, (SA *) &local, sizeof(local));
236 if (err) {
232 sock_destroy(ps); 237 sock_destroy(ps);
233 *ps = SOCK_INVALID; 238 return err;
234 return sock_bindstrerror();
235 } else { 239 } else {
236 sock_setnonblocking(ps); 240 sock_setnonblocking(ps);
237 if (backlog >= 0) sock_listen(ps, backlog); 241 if (backlog >= 0) sock_listen(ps, backlog);
@@ -240,30 +244,6 @@ const char *inet_trybind(p_sock ps, const char *address, unsigned short port,
240} 244}
241 245
242/*-------------------------------------------------------------------------*\ 246/*-------------------------------------------------------------------------*\
243* Tries to create a new inet socket
244\*-------------------------------------------------------------------------*/
245const char *inet_trycreate(p_sock ps, int type)
246{
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 if (err == IO_RETRY) err = IO_TIMEOUT;
263 return io_strerror(err);
264}
265
266/*-------------------------------------------------------------------------*\
267* Some systems do not provide this so that we provide our own. It's not 247* Some systems do not provide this so that we provide our own. It's not
268* marvelously fast, but it works just fine. 248* marvelously fast, but it works just fine.
269\*-------------------------------------------------------------------------*/ 249\*-------------------------------------------------------------------------*/