diff options
author | Natanael Copa <ncopa@alpinelinux.org> | 2017-01-25 12:43:29 +0100 |
---|---|---|
committer | Natanael Copa <ncopa@alpinelinux.org> | 2017-01-25 12:52:56 +0100 |
commit | 3041a808c3797e3c87272d71666e7b2f7c7a9f46 (patch) | |
tree | f5b71bce5509de900c2da7dae5193aef85737a7a | |
parent | a0baab5f3c041afdb66bbda951a07943b022d4ca (diff) | |
download | luasocket-3041a808c3797e3c87272d71666e7b2f7c7a9f46.tar.gz luasocket-3041a808c3797e3c87272d71666e7b2f7c7a9f46.tar.bz2 luasocket-3041a808c3797e3c87272d71666e7b2f7c7a9f46.zip |
Create socket on first sendto if family agnostic udp() was used
Create socket and set family on first sendto() if udp() was created
without address family.
Signed-off-by: Natanael Copa <ncopa@alpinelinux.org>
-rw-r--r-- | src/udp.c | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -189,6 +189,27 @@ static int meth_sendto(lua_State *L) { | |||
189 | lua_pushstring(L, gai_strerror(err)); | 189 | lua_pushstring(L, gai_strerror(err)); |
190 | return 2; | 190 | return 2; |
191 | } | 191 | } |
192 | |||
193 | /* create socket if on first sendto if AF_UNSPEC was set */ | ||
194 | if (udp->family == AF_UNSPEC && udp->sock == SOCKET_INVALID) { | ||
195 | struct addrinfo *ap; | ||
196 | const char *errstr = NULL; | ||
197 | for (ap = ai; ap != NULL; ap = ap->ai_next) { | ||
198 | errstr = inet_trycreate(&udp->sock, ap->ai_family, SOCK_DGRAM, 0); | ||
199 | if (errstr == NULL) { | ||
200 | socket_setnonblocking(&udp->sock); | ||
201 | udp->family = ap->ai_family; | ||
202 | break; | ||
203 | } | ||
204 | } | ||
205 | if (errstr != NULL) { | ||
206 | lua_pushnil(L); | ||
207 | lua_pushstring(L, errstr); | ||
208 | freeaddrinfo(ai); | ||
209 | return 2; | ||
210 | } | ||
211 | } | ||
212 | |||
192 | timeout_markstart(tm); | 213 | timeout_markstart(tm); |
193 | err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr, | 214 | err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr, |
194 | (socklen_t) ai->ai_addrlen, tm); | 215 | (socklen_t) ai->ai_addrlen, tm); |