diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2006-03-13 07:16:39 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2006-03-13 07:16:39 +0000 |
commit | 6248b915cba040a3d04fedd100637e1465a43cc7 (patch) | |
tree | d87bde69c65934524b2102f47ac48cf28897bf80 /src/unix.c | |
parent | 00e74c304c7aa917fffd1e5b7adedfa05727333c (diff) | |
download | luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.tar.gz luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.tar.bz2 luasocket-6248b915cba040a3d04fedd100637e1465a43cc7.zip |
Fixing bugs...
Diffstat (limited to 'src/unix.c')
-rw-r--r-- | src/unix.c | 146 |
1 files changed, 78 insertions, 68 deletions
@@ -41,7 +41,7 @@ static const char *unix_trybind(p_unix un, const char *path); | |||
41 | /* unix object methods */ | 41 | /* unix object methods */ |
42 | static luaL_reg un[] = { | 42 | static luaL_reg un[] = { |
43 | {"__gc", meth_close}, | 43 | {"__gc", meth_close}, |
44 | {"__tostring", aux_tostring}, | 44 | {"__tostring", auxiliar_tostring}, |
45 | {"accept", meth_accept}, | 45 | {"accept", meth_accept}, |
46 | {"bind", meth_bind}, | 46 | {"bind", meth_bind}, |
47 | {"close", meth_close}, | 47 | {"close", meth_close}, |
@@ -70,20 +70,30 @@ static t_opt opt[] = { | |||
70 | {NULL, NULL} | 70 | {NULL, NULL} |
71 | }; | 71 | }; |
72 | 72 | ||
73 | /* our socket creation function */ | ||
74 | static luaL_reg func[] = { | ||
75 | {"unix", global_create}, | ||
76 | {NULL, NULL} | ||
77 | }; | ||
78 | |||
79 | |||
73 | /*-------------------------------------------------------------------------*\ | 80 | /*-------------------------------------------------------------------------*\ |
74 | * Initializes module | 81 | * Initializes module |
75 | \*-------------------------------------------------------------------------*/ | 82 | \*-------------------------------------------------------------------------*/ |
76 | int luaopen_socketunix(lua_State *L) { | 83 | int luaopen_socket_unix(lua_State *L) { |
77 | /* create classes */ | 84 | /* create classes */ |
78 | aux_newclass(L, "unix{master}", un); | 85 | auxiliar_newclass(L, "unix{master}", un); |
79 | aux_newclass(L, "unix{client}", un); | 86 | auxiliar_newclass(L, "unix{client}", un); |
80 | aux_newclass(L, "unix{server}", un); | 87 | auxiliar_newclass(L, "unix{server}", un); |
81 | /* create class groups */ | 88 | /* create class groups */ |
82 | aux_add2group(L, "unix{master}", "unix{any}"); | 89 | auxiliar_add2group(L, "unix{master}", "unix{any}"); |
83 | aux_add2group(L, "unix{client}", "unix{any}"); | 90 | auxiliar_add2group(L, "unix{client}", "unix{any}"); |
84 | aux_add2group(L, "unix{server}", "unix{any}"); | 91 | auxiliar_add2group(L, "unix{server}", "unix{any}"); |
85 | /* define library functions */ | 92 | /* make sure the function ends up in the package table */ |
86 | lua_pushcfunction(L, global_create); | 93 | luaL_openlib(L, "socket", func, 0); |
94 | /* return the function instead of the 'socket' table */ | ||
95 | lua_pushstring(L, "unix"); | ||
96 | lua_gettable(L, -2); | ||
87 | return 1; | 97 | return 1; |
88 | } | 98 | } |
89 | 99 | ||
@@ -94,30 +104,30 @@ int luaopen_socketunix(lua_State *L) { | |||
94 | * Just call buffered IO methods | 104 | * Just call buffered IO methods |
95 | \*-------------------------------------------------------------------------*/ | 105 | \*-------------------------------------------------------------------------*/ |
96 | static int meth_send(lua_State *L) { | 106 | static int meth_send(lua_State *L) { |
97 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 107 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
98 | return buf_meth_send(L, &un->buf); | 108 | return buffer_meth_send(L, &un->buf); |
99 | } | 109 | } |
100 | 110 | ||
101 | static int meth_receive(lua_State *L) { | 111 | static int meth_receive(lua_State *L) { |
102 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 112 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
103 | return buf_meth_receive(L, &un->buf); | 113 | return buffer_meth_receive(L, &un->buf); |
104 | } | 114 | } |
105 | 115 | ||
106 | static int meth_getstats(lua_State *L) { | 116 | static int meth_getstats(lua_State *L) { |
107 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 117 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
108 | return buf_meth_getstats(L, &un->buf); | 118 | return buffer_meth_getstats(L, &un->buf); |
109 | } | 119 | } |
110 | 120 | ||
111 | static int meth_setstats(lua_State *L) { | 121 | static int meth_setstats(lua_State *L) { |
112 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 122 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
113 | return buf_meth_setstats(L, &un->buf); | 123 | return buffer_meth_setstats(L, &un->buf); |
114 | } | 124 | } |
115 | 125 | ||
116 | /*-------------------------------------------------------------------------*\ | 126 | /*-------------------------------------------------------------------------*\ |
117 | * Just call option handler | 127 | * Just call option handler |
118 | \*-------------------------------------------------------------------------*/ | 128 | \*-------------------------------------------------------------------------*/ |
119 | static int meth_setoption(lua_State *L) { | 129 | static int meth_setoption(lua_State *L) { |
120 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 130 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
121 | return opt_meth_setoption(L, opt, &un->sock); | 131 | return opt_meth_setoption(L, opt, &un->sock); |
122 | } | 132 | } |
123 | 133 | ||
@@ -125,21 +135,21 @@ static int meth_setoption(lua_State *L) { | |||
125 | * Select support methods | 135 | * Select support methods |
126 | \*-------------------------------------------------------------------------*/ | 136 | \*-------------------------------------------------------------------------*/ |
127 | static int meth_getfd(lua_State *L) { | 137 | static int meth_getfd(lua_State *L) { |
128 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 138 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
129 | lua_pushnumber(L, (int) un->sock); | 139 | lua_pushnumber(L, (int) un->sock); |
130 | return 1; | 140 | return 1; |
131 | } | 141 | } |
132 | 142 | ||
133 | /* this is very dangerous, but can be handy for those that are brave enough */ | 143 | /* this is very dangerous, but can be handy for those that are brave enough */ |
134 | static int meth_setfd(lua_State *L) { | 144 | static int meth_setfd(lua_State *L) { |
135 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 145 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
136 | un->sock = (t_sock) luaL_checknumber(L, 2); | 146 | un->sock = (t_socket) luaL_checknumber(L, 2); |
137 | return 0; | 147 | return 0; |
138 | } | 148 | } |
139 | 149 | ||
140 | static int meth_dirty(lua_State *L) { | 150 | static int meth_dirty(lua_State *L) { |
141 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 151 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
142 | lua_pushboolean(L, !buf_isempty(&un->buf)); | 152 | lua_pushboolean(L, !buffer_isempty(&un->buf)); |
143 | return 1; | 153 | return 1; |
144 | } | 154 | } |
145 | 155 | ||
@@ -148,25 +158,25 @@ static int meth_dirty(lua_State *L) { | |||
148 | * server object | 158 | * server object |
149 | \*-------------------------------------------------------------------------*/ | 159 | \*-------------------------------------------------------------------------*/ |
150 | static int meth_accept(lua_State *L) { | 160 | static int meth_accept(lua_State *L) { |
151 | p_unix server = (p_unix) aux_checkclass(L, "unix{server}", 1); | 161 | p_unix server = (p_unix) auxiliar_checkclass(L, "unix{server}", 1); |
152 | p_tm tm = tm_markstart(&server->tm); | 162 | p_timeout tm = timeout_markstart(&server->tm); |
153 | t_sock sock; | 163 | t_socket sock; |
154 | int err = sock_accept(&server->sock, &sock, NULL, NULL, tm); | 164 | int err = socket_accept(&server->sock, &sock, NULL, NULL, tm); |
155 | /* if successful, push client socket */ | 165 | /* if successful, push client socket */ |
156 | if (err == IO_DONE) { | 166 | if (err == IO_DONE) { |
157 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 167 | p_unix clnt = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
158 | aux_setclass(L, "unix{client}", -1); | 168 | auxiliar_setclass(L, "unix{client}", -1); |
159 | /* initialize structure fields */ | 169 | /* initialize structure fields */ |
160 | sock_setnonblocking(&sock); | 170 | socket_setnonblocking(&sock); |
161 | clnt->sock = sock; | 171 | clnt->sock = sock; |
162 | io_init(&clnt->io, (p_send)sock_send, (p_recv)sock_recv, | 172 | io_init(&clnt->io, (p_send)socket_send, (p_recv)socket_recv, |
163 | (p_error) sock_ioerror, &clnt->sock); | 173 | (p_error) socket_ioerror, &clnt->sock); |
164 | tm_init(&clnt->tm, -1, -1); | 174 | timeout_init(&clnt->tm, -1, -1); |
165 | buf_init(&clnt->buf, &clnt->io, &clnt->tm); | 175 | buffer_init(&clnt->buf, &clnt->io, &clnt->tm); |
166 | return 1; | 176 | return 1; |
167 | } else { | 177 | } else { |
168 | lua_pushnil(L); | 178 | lua_pushnil(L); |
169 | lua_pushstring(L, sock_strerror(err)); | 179 | lua_pushstring(L, socket_strerror(err)); |
170 | return 2; | 180 | return 2; |
171 | } | 181 | } |
172 | } | 182 | } |
@@ -185,18 +195,18 @@ static const char *unix_trybind(p_unix un, const char *path) { | |||
185 | #ifdef UNIX_HAS_SUN_LEN | 195 | #ifdef UNIX_HAS_SUN_LEN |
186 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) | 196 | local.sun_len = sizeof(local.sun_family) + sizeof(local.sun_len) |
187 | + len + 1; | 197 | + len + 1; |
188 | err = sock_bind(&un->sock, (SA *) &local, local.sun_len); | 198 | err = socket_bind(&un->sock, (SA *) &local, local.sun_len); |
189 | 199 | ||
190 | #else | 200 | #else |
191 | err = sock_bind(&un->sock, (SA *) &local, | 201 | err = socket_bind(&un->sock, (SA *) &local, |
192 | sizeof(local.sun_family) + len); | 202 | sizeof(local.sun_family) + len); |
193 | #endif | 203 | #endif |
194 | if (err != IO_DONE) sock_destroy(&un->sock); | 204 | if (err != IO_DONE) socket_destroy(&un->sock); |
195 | return sock_strerror(err); | 205 | return socket_strerror(err); |
196 | } | 206 | } |
197 | 207 | ||
198 | static int meth_bind(lua_State *L) { | 208 | static int meth_bind(lua_State *L) { |
199 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 209 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
200 | const char *path = luaL_checkstring(L, 2); | 210 | const char *path = luaL_checkstring(L, 2); |
201 | const char *err = unix_trybind(un, path); | 211 | const char *err = unix_trybind(un, path); |
202 | if (err) { | 212 | if (err) { |
@@ -220,22 +230,22 @@ static const char *unix_tryconnect(p_unix un, const char *path) | |||
220 | memset(&remote, 0, sizeof(remote)); | 230 | memset(&remote, 0, sizeof(remote)); |
221 | strcpy(remote.sun_path, path); | 231 | strcpy(remote.sun_path, path); |
222 | remote.sun_family = AF_UNIX; | 232 | remote.sun_family = AF_UNIX; |
223 | tm_markstart(&un->tm); | 233 | timeout_markstart(&un->tm); |
224 | #ifdef UNIX_HAS_SUN_LEN | 234 | #ifdef UNIX_HAS_SUN_LEN |
225 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) | 235 | remote.sun_len = sizeof(remote.sun_family) + sizeof(remote.sun_len) |
226 | + len + 1; | 236 | + len + 1; |
227 | err = sock_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); | 237 | err = socket_connect(&un->sock, (SA *) &remote, remote.sun_len, &un->tm); |
228 | #else | 238 | #else |
229 | err = sock_connect(&un->sock, (SA *) &remote, | 239 | err = socket_connect(&un->sock, (SA *) &remote, |
230 | sizeof(remote.sun_family) + len, &un->tm); | 240 | sizeof(remote.sun_family) + len, &un->tm); |
231 | #endif | 241 | #endif |
232 | if (err != IO_DONE) sock_destroy(&un->sock); | 242 | if (err != IO_DONE) socket_destroy(&un->sock); |
233 | return sock_strerror(err); | 243 | return socket_strerror(err); |
234 | } | 244 | } |
235 | 245 | ||
236 | static int meth_connect(lua_State *L) | 246 | static int meth_connect(lua_State *L) |
237 | { | 247 | { |
238 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 248 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
239 | const char *path = luaL_checkstring(L, 2); | 249 | const char *path = luaL_checkstring(L, 2); |
240 | const char *err = unix_tryconnect(un, path); | 250 | const char *err = unix_tryconnect(un, path); |
241 | if (err) { | 251 | if (err) { |
@@ -244,7 +254,7 @@ static int meth_connect(lua_State *L) | |||
244 | return 2; | 254 | return 2; |
245 | } | 255 | } |
246 | /* turn master object into a client object */ | 256 | /* turn master object into a client object */ |
247 | aux_setclass(L, "unix{client}", 1); | 257 | auxiliar_setclass(L, "unix{client}", 1); |
248 | lua_pushnumber(L, 1); | 258 | lua_pushnumber(L, 1); |
249 | return 1; | 259 | return 1; |
250 | } | 260 | } |
@@ -254,8 +264,8 @@ static int meth_connect(lua_State *L) | |||
254 | \*-------------------------------------------------------------------------*/ | 264 | \*-------------------------------------------------------------------------*/ |
255 | static int meth_close(lua_State *L) | 265 | static int meth_close(lua_State *L) |
256 | { | 266 | { |
257 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 267 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
258 | sock_destroy(&un->sock); | 268 | socket_destroy(&un->sock); |
259 | lua_pushnumber(L, 1); | 269 | lua_pushnumber(L, 1); |
260 | return 1; | 270 | return 1; |
261 | } | 271 | } |
@@ -265,16 +275,16 @@ static int meth_close(lua_State *L) | |||
265 | \*-------------------------------------------------------------------------*/ | 275 | \*-------------------------------------------------------------------------*/ |
266 | static int meth_listen(lua_State *L) | 276 | static int meth_listen(lua_State *L) |
267 | { | 277 | { |
268 | p_unix un = (p_unix) aux_checkclass(L, "unix{master}", 1); | 278 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{master}", 1); |
269 | int backlog = (int) luaL_optnumber(L, 2, 32); | 279 | int backlog = (int) luaL_optnumber(L, 2, 32); |
270 | int err = sock_listen(&un->sock, backlog); | 280 | int err = socket_listen(&un->sock, backlog); |
271 | if (err != IO_DONE) { | 281 | if (err != IO_DONE) { |
272 | lua_pushnil(L); | 282 | lua_pushnil(L); |
273 | lua_pushstring(L, sock_strerror(err)); | 283 | lua_pushstring(L, socket_strerror(err)); |
274 | return 2; | 284 | return 2; |
275 | } | 285 | } |
276 | /* turn master object into a server object */ | 286 | /* turn master object into a server object */ |
277 | aux_setclass(L, "unix{server}", 1); | 287 | auxiliar_setclass(L, "unix{server}", 1); |
278 | lua_pushnumber(L, 1); | 288 | lua_pushnumber(L, 1); |
279 | return 1; | 289 | return 1; |
280 | } | 290 | } |
@@ -284,20 +294,20 @@ static int meth_listen(lua_State *L) | |||
284 | \*-------------------------------------------------------------------------*/ | 294 | \*-------------------------------------------------------------------------*/ |
285 | static int meth_shutdown(lua_State *L) | 295 | static int meth_shutdown(lua_State *L) |
286 | { | 296 | { |
287 | p_unix un = (p_unix) aux_checkclass(L, "unix{client}", 1); | 297 | p_unix un = (p_unix) auxiliar_checkclass(L, "unix{client}", 1); |
288 | const char *how = luaL_optstring(L, 2, "both"); | 298 | const char *how = luaL_optstring(L, 2, "both"); |
289 | switch (how[0]) { | 299 | switch (how[0]) { |
290 | case 'b': | 300 | case 'b': |
291 | if (strcmp(how, "both")) goto error; | 301 | if (strcmp(how, "both")) goto error; |
292 | sock_shutdown(&un->sock, 2); | 302 | socket_shutdown(&un->sock, 2); |
293 | break; | 303 | break; |
294 | case 's': | 304 | case 's': |
295 | if (strcmp(how, "send")) goto error; | 305 | if (strcmp(how, "send")) goto error; |
296 | sock_shutdown(&un->sock, 1); | 306 | socket_shutdown(&un->sock, 1); |
297 | break; | 307 | break; |
298 | case 'r': | 308 | case 'r': |
299 | if (strcmp(how, "receive")) goto error; | 309 | if (strcmp(how, "receive")) goto error; |
300 | sock_shutdown(&un->sock, 0); | 310 | socket_shutdown(&un->sock, 0); |
301 | break; | 311 | break; |
302 | } | 312 | } |
303 | lua_pushnumber(L, 1); | 313 | lua_pushnumber(L, 1); |
@@ -311,8 +321,8 @@ error: | |||
311 | * Just call tm methods | 321 | * Just call tm methods |
312 | \*-------------------------------------------------------------------------*/ | 322 | \*-------------------------------------------------------------------------*/ |
313 | static int meth_settimeout(lua_State *L) { | 323 | static int meth_settimeout(lua_State *L) { |
314 | p_unix un = (p_unix) aux_checkgroup(L, "unix{any}", 1); | 324 | p_unix un = (p_unix) auxiliar_checkgroup(L, "unix{any}", 1); |
315 | return tm_meth_settimeout(L, &un->tm); | 325 | return timeout_meth_settimeout(L, &un->tm); |
316 | } | 326 | } |
317 | 327 | ||
318 | /*=========================================================================*\ | 328 | /*=========================================================================*\ |
@@ -322,25 +332,25 @@ static int meth_settimeout(lua_State *L) { | |||
322 | * Creates a master unix object | 332 | * Creates a master unix object |
323 | \*-------------------------------------------------------------------------*/ | 333 | \*-------------------------------------------------------------------------*/ |
324 | static int global_create(lua_State *L) { | 334 | static int global_create(lua_State *L) { |
325 | t_sock sock; | 335 | t_socket sock; |
326 | int err = sock_create(&sock, AF_UNIX, SOCK_STREAM, 0); | 336 | int err = socket_create(&sock, AF_UNIX, SOCK_STREAM, 0); |
327 | /* try to allocate a system socket */ | 337 | /* try to allocate a system socket */ |
328 | if (err == IO_DONE) { | 338 | if (err == IO_DONE) { |
329 | /* allocate unix object */ | 339 | /* allocate unix object */ |
330 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); | 340 | p_unix un = (p_unix) lua_newuserdata(L, sizeof(t_unix)); |
331 | /* set its type as master object */ | 341 | /* set its type as master object */ |
332 | aux_setclass(L, "unix{master}", -1); | 342 | auxiliar_setclass(L, "unix{master}", -1); |
333 | /* initialize remaining structure fields */ | 343 | /* initialize remaining structure fields */ |
334 | sock_setnonblocking(&sock); | 344 | socket_setnonblocking(&sock); |
335 | un->sock = sock; | 345 | un->sock = sock; |
336 | io_init(&un->io, (p_send) sock_send, (p_recv) sock_recv, | 346 | io_init(&un->io, (p_send) socket_send, (p_recv) socket_recv, |
337 | (p_error) sock_ioerror, &un->sock); | 347 | (p_error) socket_ioerror, &un->sock); |
338 | tm_init(&un->tm, -1, -1); | 348 | timeout_init(&un->tm, -1, -1); |
339 | buf_init(&un->buf, &un->io, &un->tm); | 349 | buffer_init(&un->buf, &un->io, &un->tm); |
340 | return 1; | 350 | return 1; |
341 | } else { | 351 | } else { |
342 | lua_pushnil(L); | 352 | lua_pushnil(L); |
343 | lua_pushstring(L, sock_strerror(err)); | 353 | lua_pushstring(L, socket_strerror(err)); |
344 | return 2; | 354 | return 2; |
345 | } | 355 | } |
346 | } | 356 | } |