mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 12:28:21 +01:00
Merge pull request #206 from ncopa/create-socket-on-first-sendto
Create socket on first sendto if family agnostic udp() was used
This commit is contained in:
commit
843fe9b65f
21
src/udp.c
21
src/udp.c
@ -189,6 +189,27 @@ static int meth_sendto(lua_State *L) {
|
|||||||
lua_pushstring(L, gai_strerror(err));
|
lua_pushstring(L, gai_strerror(err));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* create socket if on first sendto if AF_UNSPEC was set */
|
||||||
|
if (udp->family == AF_UNSPEC && udp->sock == SOCKET_INVALID) {
|
||||||
|
struct addrinfo *ap;
|
||||||
|
const char *errstr = NULL;
|
||||||
|
for (ap = ai; ap != NULL; ap = ap->ai_next) {
|
||||||
|
errstr = inet_trycreate(&udp->sock, ap->ai_family, SOCK_DGRAM, 0);
|
||||||
|
if (errstr == NULL) {
|
||||||
|
socket_setnonblocking(&udp->sock);
|
||||||
|
udp->family = ap->ai_family;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (errstr != NULL) {
|
||||||
|
lua_pushnil(L);
|
||||||
|
lua_pushstring(L, errstr);
|
||||||
|
freeaddrinfo(ai);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
timeout_markstart(tm);
|
timeout_markstart(tm);
|
||||||
err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
|
err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
|
||||||
(socklen_t) ai->ai_addrlen, tm);
|
(socklen_t) ai->ai_addrlen, tm);
|
||||||
|
Loading…
Reference in New Issue
Block a user