=========================================================================*\ * Library functions \*=========================================================================*/ -------------------------------------------------------------------------*\ * Creates a master tcp object \*-------------------------------------------------------------------------*/
| 357 | * Creates a master tcp object |
| 358 | \*-------------------------------------------------------------------------*/ |
| 359 | static int tcp_create(lua_State *L, int family) { |
| 360 | t_socket sock; |
| 361 | const char *err = inet_trycreate(&sock, family, SOCK_STREAM); |
| 362 | /* try to allocate a system socket */ |
| 363 | if (!err) { |
| 364 | /* allocate tcp object */ |
| 365 | p_tcp tcp = (p_tcp) lua_newuserdata(L, sizeof(t_tcp)); |
| 366 | memset(tcp, 0, sizeof(t_tcp)); |
| 367 | /* set its type as master object */ |
| 368 | auxiliar_setclass(L, "tcp{master}", -1); |
| 369 | /* initialize remaining structure fields */ |
| 370 | socket_setnonblocking(&sock); |
| 371 | |
| 372 | #if !defined(DM_IPV6_UNSUPPORTED) && !defined(__EMSCRIPTEN__) |
| 373 | if (family == PF_INET6) { |
| 374 | int yes = 1; |
| 375 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 376 | (void *)&yes, sizeof(yes)); |
| 377 | } |
| 378 | #endif |
| 379 | tcp->sock = sock; |
| 380 | io_init(&tcp->io, (p_send) socket_send, (p_recv) socket_recv, |
| 381 | (p_error) socket_ioerror, &tcp->sock); |
| 382 | timeout_init(&tcp->tm, -1, -1); |
| 383 | buffer_init(&tcp->buf, &tcp->io, &tcp->tm); |
| 384 | tcp->family = family; |
| 385 | return 1; |
| 386 | } else { |
| 387 | lua_pushnil(L); |
| 388 | lua_pushstring(L, err); |
| 389 | return 2; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | static int global_create(lua_State *L) { |
| 394 | return tcp_create(L, AF_INET); |
no test coverage detected