=========================================================================*\ * Library functions \*=========================================================================*/ -------------------------------------------------------------------------*\ * Creates a master udp object \*-------------------------------------------------------------------------*/
| 415 | * Creates a master udp object |
| 416 | \*-------------------------------------------------------------------------*/ |
| 417 | static int udp_create(lua_State *L, int family) { |
| 418 | t_socket sock; |
| 419 | const char *err = inet_trycreate(&sock, family, SOCK_DGRAM); |
| 420 | /* try to allocate a system socket */ |
| 421 | if (!err) { |
| 422 | /* allocate udp object */ |
| 423 | p_udp udp = (p_udp) lua_newuserdata(L, sizeof(t_udp)); |
| 424 | auxiliar_setclass(L, "udp{unconnected}", -1); |
| 425 | /* initialize remaining structure fields */ |
| 426 | socket_setnonblocking(&sock); |
| 427 | #if !defined(DM_IPV6_UNSUPPORTED) && !defined(__EMSCRIPTEN__) |
| 428 | if (family == PF_INET6) { |
| 429 | int yes = 1; |
| 430 | setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 431 | (void *)&yes, sizeof(yes)); |
| 432 | } |
| 433 | #endif // DM_IPV6_UNSUPPORTED |
| 434 | udp->sock = sock; |
| 435 | timeout_init(&udp->tm, -1, -1); |
| 436 | udp->family = family; |
| 437 | return 1; |
| 438 | } else { |
| 439 | lua_pushnil(L); |
| 440 | lua_pushstring(L, err); |
| 441 | return 2; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | static int global_create(lua_State *L) { |
| 446 | return udp_create(L, AF_INET); |
no test coverage detected