Changeset 7d70182829211f6a49777014122994e32dc0cd43
- Timestamp:
- 02/19/10 21:17:28 (3 years ago)
- git-parent:
- Files:
-
- src/modules/lua.c (modified) (1 diff)
- src/modules/lua_noit.c (modified) (12 diffs)
- src/modules/lua_noit.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/modules/lua.c
r8135eae r7d70182 174 174 noit_lua_module_set_description(lua_State *L) { 175 175 noit_module_t *module; 176 if(lua_gettop(L) != 1) luaL_error(L, "wrong number of arguments");177 176 module = lua_touserdata(L, lua_upvalueindex(1)); 178 module->hdr.description = strdup(lua_tostring(L, 1)); 179 return 0; 177 if(lua_gettop(L) == 1) 178 module->hdr.description = strdup(lua_tostring(L, 1)); 179 else if(lua_gettop(L) > 1) 180 luaL_error(L, "wrong number of arguments"); 181 lua_pushstring(L, module->hdr.description); 182 return 1; 180 183 } 181 184 static int 182 185 noit_lua_module_set_name(lua_State *L) { 183 186 noit_module_t *module; 184 if(lua_gettop(L) != 1) luaL_error(L, "wrong number of arguments");185 187 module = lua_touserdata(L, lua_upvalueindex(1)); 186 module->hdr.name = strdup(lua_tostring(L, 1)); 187 return 0; 188 if(lua_gettop(L) == 1) 189 module->hdr.name = strdup(lua_tostring(L, 1)); 190 else if(lua_gettop(L) > 1) 191 luaL_error(L, "wrong number of arguments"); 192 lua_pushstring(L, module->hdr.name); 193 return 1; 188 194 } 189 195 static int 190 196 noit_lua_module_set_xml_description(lua_State *L) { 191 197 noit_module_t *module; 192 if(lua_gettop(L) != 1) luaL_error(L, "wrong number of arguments");193 198 module = lua_touserdata(L, lua_upvalueindex(1)); 194 module->hdr.xml_description = strdup(lua_tostring(L, 1)); 195 return 0; 199 if(lua_gettop(L) == 1) 200 module->hdr.xml_description = strdup(lua_tostring(L, 1)); 201 else if(lua_gettop(L) > 1) 202 luaL_error(L, "wrong number of arguments"); 203 lua_pushstring(L, module->hdr.xml_description); 204 return 1; 196 205 } 197 206 static int src/modules/lua_noit.c
r0be2950 r7d70182 60 60 #define DEFLATE_CHUNK_SIZE 32768 61 61 62 #define LUA_DISPATCH(n, f) \ 63 if(!strcmp(k, #n)) { \ 64 lua_pushlightuserdata(L, udata); \ 65 lua_pushcclosure(L, f, 1); \ 66 return 1; \ 67 } 68 #define LUA_RETSTRING(n, g) \ 69 if(!strcmp(k, #n)) { \ 70 lua_pushstring(L, g); \ 71 return 1; \ 72 } 73 #define LUA_RETINTEGER(n, g) \ 74 if(!strcmp(k, #n)) { \ 75 lua_pushinteger(L, g); \ 76 return 1; \ 77 } 78 62 79 static void 63 80 nl_extended_free(void *vcl) { … … 65 82 if(cl->inbuff) free(cl->inbuff); 66 83 free(cl); 84 } 85 static int 86 lua_push_inet_ntop(lua_State *L, struct sockaddr *r) { 87 char remote_str[128]; 88 int len; 89 switch(r->sa_family) { 90 case AF_INET: 91 len = sizeof(struct sockaddr_in); 92 inet_ntop(AF_INET, &((struct sockaddr_in *)r)->sin_addr, 93 remote_str, len); 94 lua_pushstring(L, remote_str); 95 lua_pushinteger(L, ntohs(((struct sockaddr_in *)r)->sin_port)); 96 break; 97 case AF_INET6: 98 len = sizeof(struct sockaddr_in6); 99 inet_ntop(AF_INET6, &((struct sockaddr_in6 *)r)->sin6_addr, 100 remote_str, len); 101 lua_pushstring(L, remote_str); 102 lua_pushinteger(L, ntohs(((struct sockaddr_in6 *)r)->sin6_port)); 103 break; 104 default: 105 lua_pushnil(L); 106 lua_pushnil(L); 107 } 108 return 2; 67 109 } 68 110 static void … … 116 158 noit_lua_resume(ci, args); 117 159 return 0; 160 } 161 static int 162 noit_lua_socket_recv_complete(eventer_t e, int mask, void *vcl, 163 struct timeval *now) { 164 noit_lua_check_info_t *ci; 165 struct nl_slcl *cl = vcl; 166 int rv, args = 0; 167 void *inbuff = NULL; 168 socklen_t alen; 169 170 ci = get_ci(cl->L); 171 assert(ci); 172 173 if(mask & EVENTER_EXCEPTION) { 174 lua_pushinteger(cl->L, -1); 175 args = 1; 176 goto alldone; 177 } 178 179 inbuff = malloc(cl->read_goal); 180 if(!inbuff) { 181 lua_pushinteger(cl->L, -1); 182 args = 1; 183 goto alldone; 184 } 185 186 alen = sizeof(cl->address); 187 while((rv = recvfrom(e->fd, inbuff, cl->read_goal, 0, 188 (struct sockaddr *)&cl->address, &alen)) == -1 && 189 errno == EINTR); 190 if(rv < 0) { 191 if(errno == EAGAIN) { 192 free(inbuff); 193 return EVENTER_READ | EVENTER_EXCEPTION; 194 } 195 lua_pushinteger(cl->L, rv); 196 lua_pushstring(cl->L, strerror(errno)); 197 args = 2; 198 } 199 else { 200 lua_pushinteger(cl->L, rv); 201 lua_pushlstring(cl->L, inbuff, rv); 202 args = 2; 203 args += lua_push_inet_ntop(cl->L, (struct sockaddr *)&cl->address); 204 } 205 206 alldone: 207 if(inbuff) free(inbuff); 208 eventer_remove_fd(e->fd); 209 noit_lua_check_deregister_event(ci, e, 0); 210 *(cl->eptr) = eventer_alloc(); 211 memcpy(*cl->eptr, e, sizeof(*e)); 212 noit_lua_check_register_event(ci, *cl->eptr); 213 noit_lua_resume(ci, args); 214 return 0; 215 } 216 static int 217 noit_lua_socket_recv(lua_State *L) { 218 int args, rv; 219 struct nl_slcl *cl; 220 noit_lua_check_info_t *ci; 221 eventer_t e, *eptr; 222 void *inbuff; 223 socklen_t alen; 224 225 ci = get_ci(L); 226 assert(ci); 227 228 eptr = lua_touserdata(L, lua_upvalueindex(1)); 229 if(eptr != lua_touserdata(L, 1)) 230 luaL_error(L, "must be called as method"); 231 e = *eptr; 232 cl = e->closure; 233 cl->read_goal = lua_tointeger(L, 2); 234 inbuff = malloc(cl->read_goal); 235 236 alen = sizeof(cl->address); 237 while((rv = recvfrom(e->fd, inbuff, cl->read_goal, 0, 238 (struct sockaddr *)&cl->address, &alen)) == -1 && 239 errno == EINTR); 240 if(rv < 0) { 241 if(errno == EAGAIN) { 242 e->callback = noit_lua_socket_recv_complete; 243 e->mask = EVENTER_READ | EVENTER_EXCEPTION; 244 eventer_add(e); 245 free(inbuff); 246 return noit_lua_yield(ci, 0); 247 } 248 lua_pushinteger(cl->L, rv); 249 lua_pushstring(cl->L, strerror(errno)); 250 args = 2; 251 } 252 else { 253 lua_pushinteger(cl->L, rv); 254 lua_pushlstring(cl->L, inbuff, rv); 255 args = 2; 256 args += lua_push_inet_ntop(cl->L, (struct sockaddr *)&cl->address); 257 } 258 free(inbuff); 259 return args; 260 } 261 static int 262 noit_lua_socket_send_complete(eventer_t e, int mask, void *vcl, 263 struct timeval *now) { 264 noit_lua_check_info_t *ci; 265 struct nl_slcl *cl = vcl; 266 int sbytes; 267 int args = 0; 268 269 ci = get_ci(cl->L); 270 assert(ci); 271 272 if(mask & EVENTER_EXCEPTION) { 273 lua_pushinteger(cl->L, -1); 274 args = 1; 275 goto alldone; 276 } 277 if(cl->sendto) { 278 while((sbytes = sendto(e->fd, cl->outbuff, cl->write_goal, 0, 279 (struct sockaddr *)&cl->address, 280 cl->address.sin4.sin_family==AF_INET ? 281 sizeof(cl->address.sin4) : 282 sizeof(cl->address.sin6))) == -1 && 283 errno == EINTR); 284 } 285 else { 286 while((sbytes = send(e->fd, cl->outbuff, cl->write_goal, 0)) == -1 && 287 errno == EINTR); 288 } 289 if(sbytes > 0) { 290 lua_pushinteger(cl->L, sbytes); 291 args = 1; 292 } 293 else if(sbytes == -1 && errno == EAGAIN) { 294 return EVENTER_WRITE | EVENTER_EXCEPTION; 295 } 296 else { 297 lua_pushinteger(cl->L, sbytes); 298 args = 1; 299 if(sbytes == -1) { 300 lua_pushstring(cl->L, strerror(errno)); 301 args++; 302 } 303 } 304 305 alldone: 306 eventer_remove_fd(e->fd); 307 noit_lua_check_deregister_event(ci, e, 0); 308 *(cl->eptr) = eventer_alloc(); 309 memcpy(*cl->eptr, e, sizeof(*e)); 310 noit_lua_check_register_event(ci, *cl->eptr); 311 noit_lua_resume(ci, args); 312 return 0; 313 } 314 static int 315 noit_lua_socket_send(lua_State *L) { 316 noit_lua_check_info_t *ci; 317 eventer_t e, *eptr; 318 const void *bytes; 319 size_t nbytes; 320 ssize_t sbytes; 321 322 ci = get_ci(L); 323 assert(ci); 324 325 eptr = lua_touserdata(L, lua_upvalueindex(1)); 326 if(eptr != lua_touserdata(L, 1)) 327 luaL_error(L, "must be called as method"); 328 e = *eptr; 329 if(lua_gettop(L) != 2) 330 luaL_error(L, "noit.socket.send with bad arguments"); 331 bytes = lua_tolstring(L, 2, &nbytes); 332 333 while((sbytes = send(e->fd, bytes, nbytes, 0)) == -1 && errno == EINTR); 334 if(sbytes < 0 && errno == EAGAIN) { 335 struct nl_slcl *cl; 336 /* continuation */ 337 cl = e->closure; 338 cl->write_sofar = 0; 339 cl->outbuff = bytes; 340 cl->write_goal = nbytes; 341 cl->sendto = 0; 342 e->callback = noit_lua_socket_send_complete; 343 e->mask = EVENTER_WRITE | EVENTER_EXCEPTION; 344 eventer_add(e); 345 return noit_lua_yield(ci, 0); 346 } 347 lua_pushinteger(L, sbytes); 348 if(sbytes < 0) { 349 lua_pushstring(L, strerror(errno)); 350 return 2; 351 } 352 return 1; 353 } 354 355 static int 356 noit_lua_socket_sendto(lua_State *L) { 357 noit_lua_check_info_t *ci; 358 eventer_t e, *eptr; 359 const char *target; 360 unsigned short port; 361 int8_t family; 362 int rv; 363 const void *bytes; 364 size_t nbytes; 365 ssize_t sbytes; 366 union { 367 struct sockaddr_in sin4; 368 struct sockaddr_in6 sin6; 369 } a; 370 371 ci = get_ci(L); 372 assert(ci); 373 374 eptr = lua_touserdata(L, lua_upvalueindex(1)); 375 if(eptr != lua_touserdata(L, 1)) 376 luaL_error(L, "must be called as method"); 377 e = *eptr; 378 if(lua_gettop(L) != 4) 379 luaL_error(L, "noit.socket.sendto with bad arguments"); 380 bytes = lua_tolstring(L, 2, &nbytes); 381 target = lua_tostring(L, 3); 382 port = lua_tointeger(L, 4); 383 384 family = AF_INET; 385 rv = inet_pton(family, target, &a.sin4.sin_addr); 386 if(rv != 1) { 387 family = AF_INET6; 388 rv = inet_pton(family, target, &a.sin6.sin6_addr); 389 if(rv != 1) { 390 noitL(noit_stderr, "Cannot translate '%s' to IP\n", target); 391 memset(&a, 0, sizeof(a)); 392 lua_pushinteger(L, -1); 393 lua_pushfstring(L, "Cannot translate '%s' to IP\n", target); 394 return 2; 395 } 396 else { 397 /* We've IPv6 */ 398 a.sin6.sin6_family = AF_INET6; 399 a.sin6.sin6_port = htons(port); 400 } 401 } 402 else { 403 a.sin4.sin_family = family; 404 a.sin4.sin_port = htons(port); 405 } 406 407 while((sbytes = sendto(e->fd, bytes, nbytes, 0, (struct sockaddr *)&a, 408 family==AF_INET ? sizeof(a.sin4) 409 : sizeof(a.sin6))) == -1 && 410 errno == EINTR); 411 if(sbytes < 0 && errno == EAGAIN) { 412 struct nl_slcl *cl; 413 /* continuation */ 414 cl = e->closure; 415 cl->write_sofar = 0; 416 cl->outbuff = bytes; 417 cl->write_goal = nbytes; 418 cl->sendto = 1; 419 e->callback = noit_lua_socket_send_complete; 420 e->mask = EVENTER_WRITE | EVENTER_EXCEPTION; 421 eventer_add(e); 422 return noit_lua_yield(ci, 0); 423 } 424 lua_pushinteger(L, sbytes); 425 if(sbytes < 0) { 426 lua_pushstring(L, strerror(errno)); 427 return 2; 428 } 429 return 1; 118 430 } 119 431 static int … … 518 830 switch(*k) { 519 831 case 'c': 520 if(!strcmp(k, "connect")) { 521 lua_pushlightuserdata(L, udata); 522 lua_pushcclosure(L, noit_lua_socket_connect, 1); 523 return 1; 524 } 832 LUA_DISPATCH(connect, noit_lua_socket_connect); 525 833 break; 526 834 case 'r': 527 if(!strcmp(k, "read")) { 528 lua_pushlightuserdata(L, udata); 529 lua_pushcclosure(L, noit_lua_socket_read, 1); 530 return 1; 531 } 835 LUA_DISPATCH(read, noit_lua_socket_read); 836 LUA_DISPATCH(recv, noit_lua_socket_recv); 532 837 break; 533 838 case 's': 534 if(!strcmp(k, "ssl_upgrade_socket")) { 535 lua_pushlightuserdata(L, udata); 536 lua_pushcclosure(L, noit_lua_socket_connect_ssl, 1); 537 return 1; 538 } 539 if(!strcmp(k, "ssl_ctx")) { 540 lua_pushlightuserdata(L, udata); 541 lua_pushcclosure(L, noit_lua_socket_ssl_ctx, 1); 542 return 1; 543 } 839 LUA_DISPATCH(send, noit_lua_socket_send); 840 LUA_DISPATCH(sendto, noit_lua_socket_sendto); 841 LUA_DISPATCH(ssl_upgrade_socket, noit_lua_socket_connect_ssl); 842 LUA_DISPATCH(ssl_ctx, noit_lua_socket_ssl_ctx); 544 843 break; 545 844 case 'w': 546 if(!strcmp(k, "write")) { 547 lua_pushlightuserdata(L, udata); 548 lua_pushcclosure(L, noit_lua_socket_write, 1); 549 return 1; 550 } 845 LUA_DISPATCH(write, noit_lua_socket_write); 551 846 break; 552 847 default: … … 585 880 switch(*k) { 586 881 case 'e': 587 if(!strcmp(k,"error")) { 588 lua_pushstring(L,eventer_ssl_get_peer_error(ssl_ctx)); 589 return 1; 590 } 591 if(!strcmp(k,"end_time")) { 592 lua_pushinteger(L,eventer_ssl_get_peer_end_time(ssl_ctx)); 593 return 1; 594 } 882 LUA_RETSTRING(error, eventer_ssl_get_peer_error(ssl_ctx)); 883 LUA_RETINTEGER(end_time, eventer_ssl_get_peer_end_time(ssl_ctx)); 595 884 break; 596 885 case 'i': 597 if(!strcmp(k,"issuer")) { 598 lua_pushstring(L,eventer_ssl_get_peer_issuer(ssl_ctx)); 599 return 1; 600 } 886 LUA_RETSTRING(issuer, eventer_ssl_get_peer_issuer(ssl_ctx)); 601 887 break; 602 888 case 's': 603 if(!strcmp(k,"subject")) { 604 lua_pushstring(L,eventer_ssl_get_peer_subject(ssl_ctx)); 605 return 1; 606 } 607 if(!strcmp(k,"start_time")) { 608 lua_pushinteger(L,eventer_ssl_get_peer_start_time(ssl_ctx)); 609 return 1; 610 } 889 LUA_RETSTRING(subject, eventer_ssl_get_peer_subject(ssl_ctx)); 890 LUA_RETINTEGER(start_time, eventer_ssl_get_peer_start_time(ssl_ctx)); 611 891 break; 612 892 default: … … 759 1039 } 760 1040 static int 761 nl_socket_ tcp(lua_State *L, int family) {1041 nl_socket_internal(lua_State *L, int family, int proto) { 762 1042 struct nl_slcl *cl; 763 1043 noit_lua_check_info_t *ci; … … 766 1046 eventer_t e; 767 1047 768 fd = socket(family, SOCK_STREAM, 0);1048 fd = socket(family, proto, 0); 769 1049 if(fd < 0) { 770 1050 lua_pushnil(L); … … 800 1080 static int 801 1081 nl_socket(lua_State *L) { 802 return nl_socket_tcp(L, AF_INET); 803 } 804 static int 805 nl_socket_ipv6(lua_State *L) { 806 return nl_socket_tcp(L, AF_INET6); 1082 int n = lua_gettop(L); 1083 uint8_t family = AF_INET; 1084 1085 if(n > 0 && lua_isstring(L,1)) { 1086 const char *fam = lua_tostring(L,1); 1087 if(!strcmp(fam, "inet")) family = AF_INET; 1088 else if(!strcmp(fam, "inet6")) family = AF_INET6; 1089 else luaL_error(L, "noit.socket unknown family %s", fam); 1090 } 1091 1092 if(n <= 1) return nl_socket_internal(L, family, SOCK_STREAM); 1093 if(n == 2 && lua_isstring(L,2)) { 1094 const char *type = lua_tostring(L,2); 1095 if(!strcmp(type, "tcp")) 1096 return nl_socket_internal(L, family, SOCK_STREAM); 1097 else if(!strcmp(type, "udp")) 1098 return nl_socket_internal(L, family, SOCK_DGRAM); 1099 } 1100 luaL_error(L, "noit.socket called with invalid arguments"); 1101 return 0; 807 1102 } 808 1103 … … 1200 1495 switch(*k) { 1201 1496 case 'a': 1202 if(!strcmp(k,"attr") || 1203 !strcmp(k,"attribute")) { 1204 lua_pushlightuserdata(L, udata); 1205 lua_pushcclosure(L, noit_lua_xmlnode_attr, 1); 1206 return 1; 1207 } 1497 LUA_DISPATCH(attr, noit_lua_xmlnode_attr); 1498 LUA_DISPATCH(attribute, noit_lua_xmlnode_attr); 1208 1499 break; 1209 1500 case 'c': 1210 if(!strcmp(k,"children")) { 1211 lua_pushlightuserdata(L, udata); 1212 lua_pushcclosure(L, noit_lua_xmlnode_children, 1); 1213 return 1; 1214 } 1215 if(!strcmp(k,"contents")) { 1216 lua_pushlightuserdata(L, udata); 1217 lua_pushcclosure(L, noit_lua_xmlnode_contents, 1); 1218 return 1; 1219 } 1501 LUA_DISPATCH(children, noit_lua_xmlnode_children); 1502 LUA_DISPATCH(contents, noit_lua_xmlnode_contents); 1220 1503 break; 1221 1504 case 'n': 1222 if(!strcmp(k,"name")) { 1223 lua_pushlightuserdata(L, udata); 1224 lua_pushcclosure(L, noit_lua_xmlnode_name, 1); 1225 return 1; 1226 } 1505 LUA_DISPATCH(name, noit_lua_xmlnode_name); 1227 1506 break; 1228 1507 default: … … 1278 1557 switch(*k) { 1279 1558 case 'r': 1280 if(!strcmp(k, "root")) { 1281 lua_pushlightuserdata(L, udata); 1282 lua_pushcclosure(L, noit_lua_xml_docroot, 1); 1283 return 1; 1284 } 1559 LUA_DISPATCH(root, noit_lua_xml_docroot); 1285 1560 break; 1286 1561 case 'x': 1287 if(!strcmp(k, "xpath")) { 1288 lua_pushlightuserdata(L, udata); 1289 lua_pushcclosure(L, noit_lua_xpath, 1); 1290 return 1; 1291 } 1562 LUA_DISPATCH(xpath, noit_lua_xpath); 1292 1563 break; 1293 1564 default: … … 1306 1577 { "md5_hex", nl_md5_hex }, 1307 1578 { "pcre", nl_pcre }, 1308 { "socket_ipv6", nl_socket_ipv6 },1309 1579 { "gunzip", nl_gunzip }, 1310 1580 { "conf_get", nl_conf_get_string }, … … 1359 1629 eventer_name_callback("lua/socket_write", 1360 1630 noit_lua_socket_write_complete); 1631 eventer_name_callback("lua/socket_send", 1632 noit_lua_socket_send_complete); 1361 1633 eventer_name_callback("lua/socket_connect", 1362 1634 noit_lua_socket_connect_complete); src/modules/lua_noit.h
r202f851 r7d70182 90 90 size_t write_goal; 91 91 eventer_t *eptr; 92 93 int sendto; /* whether this send is a sendto call */ 94 union { 95 struct sockaddr_in sin4; 96 struct sockaddr_in6 sin6; 97 } address; 98 92 99 lua_State *L; 93 100 };
