diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-26 04:03:55 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-07-26 04:03:55 +0000 |
commit | cd994f306a67ea7b9ee0ba810d985114e097dd77 (patch) | |
tree | 54a44706f2523c4b53aebc8d4c5024113ab7cfe0 /src/unix.c | |
parent | c8b402e00442cd249397d4d33d2723a1f08a8108 (diff) | |
download | luasocket-cd994f306a67ea7b9ee0ba810d985114e097dd77.tar.gz luasocket-cd994f306a67ea7b9ee0ba810d985114e097dd77.tar.bz2 luasocket-cd994f306a67ea7b9ee0ba810d985114e097dd77.zip |
Gonna try my luck on windows...
Diffstat (limited to 'src/unix.c')
-rw-r--r-- | src/unix.c | 122 |
1 files changed, 61 insertions, 61 deletions
@@ -33,11 +33,11 @@ static int meth_getfd(lua_State *L); | |||
33 | static int meth_setfd(lua_State *L); | 33 | static int meth_setfd(lua_State *L); |
34 | static int meth_dirty(lua_State *L); | 34 | static int meth_dirty(lua_State *L); |
35 | 35 | ||
36 | static const char *unix_tryconnect(p_unix unix, const char *path); | 36 | static const char *unix_tryconnect(p_unix un, const char *path); |
37 | static const char *unix_trybind(p_unix unix, const char *path); | 37 | static const char *unix_trybind(p_unix un, const char *path); |
38 | 38 | ||
39 | /* unix object methods */ | 39 | /* unix object methods */ |
40 | static luaL_reg unix[] = { | 40 | static luaL_reg un[] = { |
41 | {"__gc", meth_close}, | 41 | {"__gc", meth_close}, |
42 | {"__tostring", aux_tostring}, | 42 | {"__tostring", aux_tostring}, |
43 | {"accept", meth_accept}, | 43 | {"accept", meth_accept}, |
@@ -77,9 +77,9 @@ static luaL_reg func[] = { | |||
77 | \*-------------------------------------------------------------------------*/ | 77 | \*-------------------------------------------------------------------------*/ |
78 | int unix_open(lua_State *L) { | 78 | int unix_open(lua_State *L) { |
79 | /* create classes */ | 79 | /* create classes */ |
80 | aux_newclass(L, "unix{master}", unix); | 80 | aux_newclass(L, "unix{master}", un); |
81 | aux_newclass(L, "unix{client}", unix); | 81 | aux_newclass(L, "unix{client}", un); |
82 | aux_newclass(L, "unix{server}", unix); | 82 | aux_newclass(L, "unix{server}", un); |
83 | /* create class groups */ | 83 | /* create class groups */ |
84 | aux_add2group(L, "unix{master}", "unix{any}"); | 84 | aux_add2group(L, "unix{master}", "unix{any}"); |
85 | aux_add2group(L, "unix{client}", "unix{any}"); | 85 | aux_add2group(L, "unix{client}", "unix{any}"); |
@@ -98,42 +98,42 @@ int unix_open(lua_State *L) { | |||
98 | * Just call buffered IO methods | 98 | * Just call buffered IO methods |
99 | \*-------------------------------------------------------------------------*/ | 99 | \*-------------------------------------------------------------------------*/ |
100 | static int meth_send(lua_State *L) { | 100 | static int meth_send(lua_State *L) { |
101 | p_unix unix = (p_unix) aux_checkclass(L, "unix{client}", 1); | 101 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); |
102 | return buf_meth_send(L, &unix->buf); | 102 | return buf_meth_send(L, &un->buf); |
103 | } | 103 | } |
104 | 104 | ||
105 | static int meth_receive(lua_State *L) { | 105 | static int meth_receive(lua_State *L) { |
106 | p_unix unix = (p_unix) aux_checkclass(L, "unix{client}", 1); | 106 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); |
107 | return buf_meth_receive(L, &unix->buf); | 107 | return buf_meth_receive(L, &un->buf); |
108 | } | 108 | } |
109 | 109 | ||
110 | /*-------------------------------------------------------------------------*\ | 110 | /*-------------------------------------------------------------------------*\ |
111 | * Just call option handler | 111 | * Just call option handler |
112 | \*-------------------------------------------------------------------------*/ | 112 | \*-------------------------------------------------------------------------*/ |
113 | static int meth_setoption(lua_State *L) { | 113 | static int meth_setoption(lua_State *L) { |
114 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 114 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
115 | return opt_meth_setoption(L, opt, &unix->sock); | 115 | return opt_meth_setoption(L, opt, &un->sock); |
116 | } | 116 | } |
117 | 117 | ||
118 | /*-------------------------------------------------------------------------*\ | 118 | /*-------------------------------------------------------------------------*\ |
119 | * Select support methods | 119 | * Select support methods |
120 | \*-------------------------------------------------------------------------*/ | 120 | \*-------------------------------------------------------------------------*/ |
121 | static int meth_getfd(lua_State *L) { | 121 | static int meth_getfd(lua_State *L) { |
122 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 122 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
123 | lua_pushnumber(L, (int) unix->sock); | 123 | lua_pushnumber(L, (int) un->sock); |
124 | return 1; | 124 | return 1; |
125 | } | 125 | } |
126 | 126 | ||
127 | /* this is very dangerous, but can be handy for those that are brave enough */ | 127 | /* this is very dangerous, but can be handy for those that are brave enough */ |
128 | static int meth_setfd(lua_State *L) { | 128 | static int meth_setfd(lua_State *L) { |
129 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 129 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
130 | unix->sock = (t_sock) luaL_checknumber(L, 2); | 130 | un->sock = (t_sock) luaL_checknumber(L, 2); |
131 | return 0; | 131 | return 0; |
132 | } | 132 | } |
133 | 133 | ||
134 | static int meth_dirty(lua_State *L) { | 134 | static int meth_dirty(lua_State *L) { |
135 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 135 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
136 | lua_pushboolean(L, !buf_isempty(&unix->buf)); | 136 | lua_pushboolean(L, !buf_isempty(&un->buf)); |
137 | return 1; | 137 | return 1; |
138 | } | 138 | } |
139 | 139 | ||
@@ -145,22 +145,22 @@ static int meth_accept(lua_State *L) { | |||
145 | p_unix server = (p_unix) aux_checkclass(L, "unix{server}", 1); | 145 | p_unix server = (p_unix) aux_checkclass(L, "unix{server}", 1); |
146 | p_tm tm = tm_markstart(&server->tm); | 146 | p_tm tm = tm_markstart(&server->tm); |
147 | t_sock sock; | 147 | t_sock sock; |
148 | const char *err = sock_accept(&server->sock, &sock, NULL, NULL, tm); | 148 | int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); |
149 | /* if successful, push client socket */ | 149 | /* if successful, push client socket */ |
150 | if (!err) { | 150 | if (err == IO_DONE) { |
151 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 151 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
152 | aux_setclass(L, "unix{client}", -1); | 152 | aux_setclass(L, "unix{client}", -1); |
153 | /* initialize structure fields */ | 153 | /* initialize structure fields */ |
154 | sock_setnonblocking(&sock); | 154 | sock_setnonblocking(&sock); |
155 | clnt->sock = sock; | 155 | clnt->sock = sock; |
156 | io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, | 156 | io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, |
157 | (p_geterr) sock_geterr, &clnt->sock); | 157 | (p_error) sock_ioerror, &clnt->sock); |
158 | tm_init(&clnt->tm, -1, -1); | 158 | tm_init(&clnt->tm, -1, -1); |
159 | buf_init(&clnt->buf, &clnt->io, &clnt->tm); | 159 | buf_init(&clnt->buf, &clnt->io, &clnt->tm); |
160 | return 1; | 160 | return 1; |
161 | } else { | 161 | } else { |
162 | lua_pushnil(L); | 162 | lua_pushnil(L); |
163 | lua_pushstring(L, err); | 163 | lua_pushstring(L, sock_strerror(err)); |
164 | return 2; | 164 | return 2; |
165 | } | 165 | } |
166 | } | 166 | } |
@@ -168,10 +168,10 @@ static int meth_accept(lua_State *L) { | |||
168 | /*-------------------------------------------------------------------------*\ | 168 | /*-------------------------------------------------------------------------*\ |
169 | * Binds an object to an address | 169 | * Binds an object to an address |
170 | \*-------------------------------------------------------------------------*/ | 170 | \*-------------------------------------------------------------------------*/ |
171 | static const char *unix_trybind(p_unix unix, const char *path) { | 171 | static const char *unix_trybind(p_unix un, const char *path) { |
172 | struct sockaddr_un local; | 172 | struct sockaddr_un local; |
173 | size_t len = strlen(path); | 173 | size_t len = strlen(path); |
174 | const char *err; | 174 | int err; |
175 | if (len >= sizeof(local.sun_path)) return "path too long"; | 175 | if (len >= sizeof(local.sun_path)) return "path too long"; |
176 | memset(&local, 0, sizeof(local)); | 176 | memset(&local, 0, sizeof(local)); |
177 | strcpy(local.sun_path, path); | 177 | strcpy(local.sun_path, path); |
@@ -179,20 +179,20 @@ static const char *unix_trybind(p_unix unix, const char *path) { | |||
179 | #ifdef UNIX_HAS_SUN_LEN | 179 | #ifdef UNIX_HAS_SUN_LEN |
180 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 180 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) |
181 | + len + 1; | 181 | + len + 1; |
182 | err = sock_bind(&unix->sock, (SA *) &local, local.sun_len); | 182 | err = sock_bind(&un->sock, (SA *) &local, local.sun_len); |
183 | 183 | ||
184 | #else | 184 | #else |
185 | err = sock_bind(&unix->sock, (SA *) &local, | 185 | err = sock_bind(&un->sock, (SA *) &local, |
186 | sizeof(local.sun_family) + len); | 186 | sizeof(local.sun_family) + len); |
187 | #endif | 187 | #endif |
188 | if (err) sock_destroy(&unix->sock); | 188 | if (err != IO_DONE) sock_destroy(&un->sock); |
189 | return err; | 189 | return sock_strerror(err); |
190 | } | 190 | } |
191 | 191 | ||
192 | static int meth_bind(lua_State *L) { | 192 | static int meth_bind(lua_State *L) { |
193 | p_unix unix = (p_unix) aux_checkclass(L, "unix{master}", 1); | 193 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); |
194 | const char *path = luaL_checkstring(L, 2); | 194 | const char *path = luaL_checkstring(L, 2); |
195 | const char *err = unix_trybind(unix, path); | 195 | const char *err = unix_trybind(un, path); |
196 | if (err) { | 196 | if (err) { |
197 | lua_pushnil(L); | 197 | lua_pushnil(L); |
198 | lua_pushstring(L, err); | 198 | lua_pushstring(L, err); |
@@ -205,33 +205,33 @@ static int meth_bind(lua_State *L) { | |||
205 | /*-------------------------------------------------------------------------*\ | 205 | /*-------------------------------------------------------------------------*\ |
206 | * Turns a master unix object into a client object. | 206 | * Turns a master unix object into a client object. |
207 | \*-------------------------------------------------------------------------*/ | 207 | \*-------------------------------------------------------------------------*/ |
208 | static const char *unix_tryconnect(p_unix unix, const char *path) | 208 | static const char *unix_tryconnect(p_unix un, const char *path) |
209 | { | 209 | { |
210 | struct sockaddr_un remote; | 210 | struct sockaddr_un remote; |
211 | const char *err; | 211 | int err; |
212 | size_t len = strlen(path); | 212 | size_t len = strlen(path); |
213 | if (len >= sizeof(remote.sun_path)) return "path too long"; | 213 | if (len >= sizeof(remote.sun_path)) return "path too long"; |
214 | memset(&remote, 0, sizeof(remote)); | 214 | memset(&remote, 0, sizeof(remote)); |
215 | strcpy(remote.sun_path, path); | 215 | strcpy(remote.sun_path, path); |
216 | remote.sun_family = AF_UNIX; | 216 | remote.sun_family = AF_UNIX; |
217 | tm_markstart(&unix->tm); | 217 | tm_markstart(&un->tm); |
218 | #ifdef UNIX_HAS_SUN_LEN | 218 | #ifdef UNIX_HAS_SUN_LEN |
219 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 219 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) |
220 | + len + 1; | 220 | + len + 1; |
221 | err = sock_connect(&unix->sock, (SA *) &remote, remote.sun_len, &unix->tm); | 221 | err = sock_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); |
222 | #else | 222 | #else |
223 | err = sock_connect(&unix->sock, (SA *) &remote, | 223 | err = sock_connect(&un->sock, (SA *) &remote, |
224 | sizeof(remote.sun_family) + len, &unix->tm); | 224 | sizeof(remote.sun_family) + len, &un->tm); |
225 | #endif | 225 | #endif |
226 | if (err) sock_destroy(&unix->sock); | 226 | if (err != IO_DONE) sock_destroy(&un->sock); |
227 | return err; | 227 | return sock_strerror(err); |
228 | } | 228 | } |
229 | 229 | ||
230 | static int meth_connect(lua_State *L) | 230 | static int meth_connect(lua_State *L) |
231 | { | 231 | { |
232 | p_unix unix = (p_unix) aux_checkclass(L, "unix{master}", 1); | 232 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); |
233 | const char *path = luaL_checkstring(L, 2); | 233 | const char *path = luaL_checkstring(L, 2); |
234 | const char *err = unix_tryconnect(unix, path); | 234 | const char *err = unix_tryconnect(un, path); |
235 | if (err) { | 235 | if (err) { |
236 | lua_pushnil(L); | 236 | lua_pushnil(L); |
237 | lua_pushstring(L, err); | 237 | lua_pushstring(L, err); |
@@ -248,8 +248,8 @@ static int meth_connect(lua_State *L) | |||
248 | \*-------------------------------------------------------------------------*/ | 248 | \*-------------------------------------------------------------------------*/ |
249 | static int meth_close(lua_State *L) | 249 | static int meth_close(lua_State *L) |
250 | { | 250 | { |
251 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 251 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
252 | sock_destroy(&unix->sock); | 252 | sock_destroy(&un->sock); |
253 | return 0; | 253 | return 0; |
254 | } | 254 | } |
255 | 255 | ||
@@ -258,12 +258,12 @@ static int meth_close(lua_State *L) | |||
258 | \*-------------------------------------------------------------------------*/ | 258 | \*-------------------------------------------------------------------------*/ |
259 | static int meth_listen(lua_State *L) | 259 | static int meth_listen(lua_State *L) |
260 | { | 260 | { |
261 | p_unix unix = (p_unix) aux_checkclass(L, "unix{master}", 1); | 261 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); |
262 | int backlog = (int) luaL_optnumber(L, 2, 32); | 262 | int backlog = (int) luaL_optnumber(L, 2, 32); |
263 | const char *err = sock_listen(&unix->sock, backlog); | 263 | int err = sock_listen(&un->sock, backlog); |
264 | if (err) { | 264 | if (err != IO_DONE) { |
265 | lua_pushnil(L); | 265 | lua_pushnil(L); |
266 | lua_pushstring(L, err); | 266 | lua_pushstring(L, sock_strerror(err)); |
267 | return 2; | 267 | return 2; |
268 | } | 268 | } |
269 | /* turn master object into a server object */ | 269 | /* turn master object into a server object */ |
@@ -277,20 +277,20 @@ static int meth_listen(lua_State *L) | |||
277 | \*-------------------------------------------------------------------------*/ | 277 | \*-------------------------------------------------------------------------*/ |
278 | static int meth_shutdown(lua_State *L) | 278 | static int meth_shutdown(lua_State *L) |
279 | { | 279 | { |
280 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{client}", 1); | 280 | p_unix un = (p_unix) aux_checkgroup(L, "unix{client}", 1); |
281 | const char *how = luaL_optstring(L, 2, "both"); | 281 | const char *how = luaL_optstring(L, 2, "both"); |
282 | switch (how[0]) { | 282 | switch (how[0]) { |
283 | case 'b': | 283 | case 'b': |
284 | if (strcmp(how, "both")) goto error; | 284 | if (strcmp(how, "both")) goto error; |
285 | sock_shutdown(&unix->sock, 2); | 285 | sock_shutdown(&un->sock, 2); |
286 | break; | 286 | break; |
287 | case 's': | 287 | case 's': |
288 | if (strcmp(how, "send")) goto error; | 288 | if (strcmp(how, "send")) goto error; |
289 | sock_shutdown(&unix->sock, 1); | 289 | sock_shutdown(&un->sock, 1); |
290 | break; | 290 | break; |
291 | case 'r': | 291 | case 'r': |
292 | if (strcmp(how, "receive")) goto error; | 292 | if (strcmp(how, "receive")) goto error; |
293 | sock_shutdown(&unix->sock, 0); | 293 | sock_shutdown(&un->sock, 0); |
294 | break; | 294 | break; |
295 | } | 295 | } |
296 | lua_pushnumber(L, 1); | 296 | lua_pushnumber(L, 1); |
@@ -304,8 +304,8 @@ error: | |||
304 | * Just call tm methods | 304 | * Just call tm methods |
305 | \*-------------------------------------------------------------------------*/ | 305 | \*-------------------------------------------------------------------------*/ |
306 | static int meth_settimeout(lua_State *L) { | 306 | static int meth_settimeout(lua_State *L) { |
307 | p_unix unix = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 307 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); |
308 | return tm_meth_settimeout(L, &unix->tm); | 308 | return tm_meth_settimeout(L, &un->tm); |
309 | } | 309 | } |
310 | 310 | ||
311 | /*=========================================================================*\ | 311 | /*=========================================================================*\ |
@@ -316,24 +316,24 @@ static int meth_settimeout(lua_State *L) { | |||
316 | \*-------------------------------------------------------------------------*/ | 316 | \*-------------------------------------------------------------------------*/ |
317 | static int global_create(lua_State *L) { | 317 | static int global_create(lua_State *L) { |
318 | t_sock sock; | 318 | t_sock sock; |
319 | const char *err = sock_create(&sock, AF_UNIX, SOCK_STREAM, 0); | 319 | int err = sock_create(&sock, AF_UNIX, SOCK_STREAM, 0); |
320 | /* try to allocate a system socket */ | 320 | /* try to allocate a system socket */ |
321 | if (!err) { | 321 | if (err == IO_DONE) { |
322 | /* allocate unix object */ | 322 | /* allocate unix object */ |
323 | p_unix unix = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 323 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
324 | /* set its type as master object */ | 324 | /* set its type as master object */ |
325 | aux_setclass(L, "unix{master}", -1); | 325 | aux_setclass(L, "unix{master}", -1); |
326 | /* initialize remaining structure fields */ | 326 | /* initialize remaining structure fields */ |
327 | sock_setnonblocking(&sock); | 327 | sock_setnonblocking(&sock); |
328 | unix->sock = sock; | 328 | un->sock = sock; |
329 | io_init(&unix->io, (p_send) sock_send, (p_recv) sock_recv, | 329 | io_init(&un->io, (p_send) sock_send, (p_recv) sock_recv, |
330 | (p_geterr) sock_geterr, &unix->sock); | 330 | (p_error) sock_ioerror, &un->sock); |
331 | tm_init(&unix->tm, -1, -1); | 331 | tm_init(&un->tm, -1, -1); |
332 | buf_init(&unix->buf, &unix->io, &unix->tm); | 332 | buf_init(&un->buf, &un->io, &un->tm); |
333 | return 1; | 333 | return 1; |
334 | } else { | 334 | } else { |
335 | lua_pushnil(L); | 335 | lua_pushnil(L); |
336 | lua_pushstring(L, err); | 336 | lua_pushstring(L, sock_strerror(err)); |
337 | return 2; | 337 | return 2; |
338 | } | 338 | } |
339 | } | 339 | } |