Comparison

net/server_select.lua @ 9473:5fdda751333a

net.server: Require IP address as argument to addclient (no DNS names) The net.connect API should be used to resolve DNS names first
author Kim Alvefur <zash@zash.se>
date Thu, 11 Oct 2018 15:48:30 +0200
parent 9387:33e52f727f0f
child 9495:89e05b118f6e
comparison
equal deleted inserted replaced
9472:ea40fe484c38 9473:5fdda751333a
48 --// extern libs //-- 48 --// extern libs //--
49 49
50 local has_luasec, luasec = pcall ( require , "ssl" ) 50 local has_luasec, luasec = pcall ( require , "ssl" )
51 local luasocket = use "socket" or require "socket" 51 local luasocket = use "socket" or require "socket"
52 local luasocket_gettime = luasocket.gettime 52 local luasocket_gettime = luasocket.gettime
53 local getaddrinfo = luasocket.dns.getaddrinfo 53 local inet = require "util.net";
54 local inet_pton = inet.pton;
54 55
55 --// extern lib methods //-- 56 --// extern lib methods //--
56 57
57 local ssl_wrap = ( has_luasec and luasec.wrap ) 58 local ssl_wrap = ( has_luasec and luasec.wrap )
58 local socket_bind = luasocket.bind 59 local socket_bind = luasocket.bind
1005 elseif type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then 1006 elseif type( port ) ~= "number" or not ( port >= 0 and port <= 65535 ) then
1006 err = "invalid port" 1007 err = "invalid port"
1007 elseif sslctx and not has_luasec then 1008 elseif sslctx and not has_luasec then
1008 err = "luasec not found" 1009 err = "luasec not found"
1009 end 1010 end
1010 if getaddrinfo and not typ then 1011 if not typ then
1011 local addrinfo, err = getaddrinfo(address) 1012 local n = inet_pton(addr);
1012 if not addrinfo then return nil, err end 1013 if not n then return nil, "invalid-ip"; end
1013 if addrinfo[1] and addrinfo[1].family == "inet6" then 1014 if #n == 16 then
1014 typ = "tcp6" 1015 typ = "tcp6";
1015 end 1016 elseif #n == 4 then
1016 end 1017 typ = "tcp4";
1017 local create = luasocket[typ or "tcp"] 1018 end
1019 end
1020 local create = luasocket[typ] or luasocket.tcp;
1018 if type( create ) ~= "function" then 1021 if type( create ) ~= "function" then
1019 err = "invalid socket type" 1022 err = "invalid socket type"
1020 end 1023 end
1021 1024
1022 if err then 1025 if err then