diff options
| author | Florian Zeitz <florob@babelmonkeys.de> | 2012-07-17 19:02:20 +0200 |
|---|---|---|
| committer | Florian Zeitz <florob@babelmonkeys.de> | 2012-07-17 19:02:20 +0200 |
| commit | a6cf48596d421c5dcc61b745dde73e3265876f69 (patch) | |
| tree | 2407007d651b7ee9d878828b78a20307392f1f6a | |
| parent | 05535a19f81d181b7e15d241892e794a00905bb9 (diff) | |
| download | luasocket-a6cf48596d421c5dcc61b745dde73e3265876f69.tar.gz luasocket-a6cf48596d421c5dcc61b745dde73e3265876f69.tar.bz2 luasocket-a6cf48596d421c5dcc61b745dde73e3265876f69.zip | |
Add IPv6 support to udp:sendto()
| -rw-r--r-- | src/udp.c | 39 |
1 files changed, 30 insertions, 9 deletions
| @@ -153,16 +153,37 @@ static int meth_sendto(lua_State *L) { | |||
| 153 | const char *ip = luaL_checkstring(L, 3); | 153 | const char *ip = luaL_checkstring(L, 3); |
| 154 | unsigned short port = (unsigned short) luaL_checknumber(L, 4); | 154 | unsigned short port = (unsigned short) luaL_checknumber(L, 4); |
| 155 | p_timeout tm = &udp->tm; | 155 | p_timeout tm = &udp->tm; |
| 156 | struct sockaddr_in addr; | ||
| 157 | int err; | 156 | int err; |
| 158 | memset(&addr, 0, sizeof(addr)); | 157 | switch (udp->family) { |
| 159 | if (!inet_aton(ip, &addr.sin_addr)) | 158 | case PF_INET: { |
| 160 | luaL_argerror(L, 3, "invalid ip address"); | 159 | struct sockaddr_in addr; |
| 161 | addr.sin_family = AF_INET; | 160 | memset(&addr, 0, sizeof(addr)); |
| 162 | addr.sin_port = htons(port); | 161 | if (!inet_pton(AF_INET, ip, &addr.sin_addr)) |
| 163 | timeout_markstart(tm); | 162 | luaL_argerror(L, 3, "invalid ip address"); |
| 164 | err = socket_sendto(&udp->sock, data, count, &sent, | 163 | addr.sin_family = AF_INET; |
| 165 | (SA *) &addr, sizeof(addr), tm); | 164 | addr.sin_port = htons(port); |
| 165 | timeout_markstart(tm); | ||
| 166 | err = socket_sendto(&udp->sock, data, count, &sent, | ||
| 167 | (SA *) &addr, sizeof(addr), tm); | ||
| 168 | break; | ||
| 169 | } | ||
| 170 | case PF_INET6: { | ||
| 171 | struct sockaddr_in6 addr; | ||
| 172 | memset(&addr, 0, sizeof(addr)); | ||
| 173 | if (!inet_pton(AF_INET6, ip, &addr.sin6_addr)) | ||
| 174 | luaL_argerror(L, 3, "invalid ip address"); | ||
| 175 | addr.sin6_family = AF_INET6; | ||
| 176 | addr.sin6_port = htons(port); | ||
| 177 | timeout_markstart(tm); | ||
| 178 | err = socket_sendto(&udp->sock, data, count, &sent, | ||
| 179 | (SA *) &addr, sizeof(addr), tm); | ||
| 180 | break; | ||
| 181 | } | ||
| 182 | default: | ||
| 183 | lua_pushnil(L); | ||
| 184 | lua_pushfstring(L, "unknown family %d", udp->family); | ||
| 185 | return 2; | ||
| 186 | } | ||
| 166 | if (err != IO_DONE) { | 187 | if (err != IO_DONE) { |
| 167 | lua_pushnil(L); | 188 | lua_pushnil(L); |
| 168 | lua_pushstring(L, udp_strerror(err)); | 189 | lua_pushstring(L, udp_strerror(err)); |
