diff options
author | Diego Nehab <diego.nehab@gmail.com> | 2017-01-25 13:15:51 -0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-25 13:15:51 -0200 |
commit | 843fe9b65fd93b015e5fe3e457c98fb806cd8c79 (patch) | |
tree | f5b71bce5509de900c2da7dae5193aef85737a7a | |
parent | a0baab5f3c041afdb66bbda951a07943b022d4ca (diff) | |
parent | 3041a808c3797e3c87272d71666e7b2f7c7a9f46 (diff) | |
download | luasocket-843fe9b65fd93b015e5fe3e457c98fb806cd8c79.tar.gz luasocket-843fe9b65fd93b015e5fe3e457c98fb806cd8c79.tar.bz2 luasocket-843fe9b65fd93b015e5fe3e457c98fb806cd8c79.zip |
Merge pull request #206 from ncopa/create-socket-on-first-sendto
Create socket on first sendto if family agnostic udp() was used
-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); |