Comparison

net/server_epoll.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 9445:af7eafe82447
child 9474:619ba78709a5
comparison
equal deleted inserted replaced
9472:ea40fe484c38 9473:5fdda751333a
19 local log = require "util.logger".init("server_epoll"); 19 local log = require "util.logger".init("server_epoll");
20 local socket = require "socket"; 20 local socket = require "socket";
21 local luasec = require "ssl"; 21 local luasec = require "ssl";
22 local gettime = require "util.time".now; 22 local gettime = require "util.time".now;
23 local createtable = require "util.table".create; 23 local createtable = require "util.table".create;
24 local inet = require "util.net";
25 local inet_pton = inet.pton;
24 local _SOCKETINVALID = socket._SOCKETINVALID or -1; 26 local _SOCKETINVALID = socket._SOCKETINVALID or -1;
25 27
26 local poll = require "util.poll".new(); 28 local poll = require "util.poll".new();
27 29
28 local _ENV = nil; 30 local _ENV = nil;
612 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx) 614 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx)
613 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx); 615 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx);
614 if not client.peername then 616 if not client.peername then
615 client.peername, client.peerport = addr, port; 617 client.peername, client.peerport = addr, port;
616 end 618 end
617 client:init(); 619 local ok, err = client:init();
620 if not ok then return ok, err; end
618 if tls_ctx then 621 if tls_ctx then
619 client:starttls(tls_ctx); 622 client:starttls(tls_ctx);
620 end 623 end
621 return client; 624 return client;
622 end 625 end
623 626
624 -- New outgoing TCP connection 627 -- New outgoing TCP connection
625 local function addclient(addr, port, listeners, read_size, tls_ctx) 628 local function addclient(addr, port, listeners, read_size, tls_ctx)
626 local conn, err = socket.tcp(); 629 local n = inet_pton(addr);
627 if not conn then return conn, err; end 630 if not n then return nil, "invalid-ip"; end
631 local create
632 if #n == 16 then
633 create = socket.tcp6 or socket.tcp;
634 else
635 create = socket.tcp4 or socket.tcp;
636 end
637 local conn, err = create();
628 conn:settimeout(0); 638 conn:settimeout(0);
629 conn:connect(addr, port); 639 conn:connect(addr, port);
630 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx) 640 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx)
631 client:init(); 641 local ok, err = client:init();
642 if not ok then return ok, err; end
632 if tls_ctx then 643 if tls_ctx then
633 client:starttls(tls_ctx); 644 client:starttls(tls_ctx);
634 end 645 end
635 return client, conn; 646 return client, conn;
636 end 647 end