Project

General

Profile

Feature #17134 ยป resolv_timeout.patch

Glass_saga (Masaki Matsushita), 08/29/2020 05:26 AM

View differences:

ext/socket/ipsocket.c
19 19
    } remote, local;
20 20
    int type;
21 21
    int fd;
22
    VALUE resolv_timeout;
22 23
};
23 24

  
24 25
static VALUE
......
49 50
    int fd, status = 0, local = 0;
50 51
    int family = AF_UNSPEC;
51 52
    const char *syscall = 0;
52

  
53
    VALUE resolv_timeout = arg->resolv_timeout;
54

  
55
#ifdef HAVE_GETADDRINFO_A
56
    arg->remote.res = rsock_addrinfo_a(arg->remote.host, arg->remote.serv,
57
				       family, SOCK_STREAM,
58
				       (type == INET_SERVER) ? AI_PASSIVE : 0,
59
				       resolv_timeout);
60
#else
53 61
    arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv,
54 62
				     family, SOCK_STREAM,
55 63
				     (type == INET_SERVER) ? AI_PASSIVE : 0);
64
#endif
65

  
66

  
56 67
    /*
57 68
     * Maybe also accept a local address
58 69
     */
......
157 168

  
158 169
VALUE
159 170
rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv,
160
	            VALUE local_host, VALUE local_serv, int type)
171
	            VALUE local_host, VALUE local_serv, int type,
172
		    VALUE resolv_timeout)
161 173
{
162 174
    struct inetsock_arg arg;
163 175
    arg.sock = sock;
......
169 181
    arg.local.res = 0;
170 182
    arg.type = type;
171 183
    arg.fd = -1;
184
    arg.resolv_timeout = resolv_timeout;
172 185
    return rb_ensure(init_inetsock_internal, (VALUE)&arg,
173 186
		     inetsock_cleanup, (VALUE)&arg);
174 187
}
ext/socket/raddrinfo.c
662 662
    return rsock_getaddrinfo(host, port, &hints, 1);
663 663
}
664 664

  
665
#ifdef HAVE_GETADDRINFO_A
666
struct rb_addrinfo*
667
rsock_addrinfo_a(VALUE host, VALUE port, int family, int socktype, int flags, VALUE timeout)
668
{
669
    struct addrinfo hints;
670

  
671
    MEMZERO(&hints, struct addrinfo, 1);
672
    hints.ai_family = family;
673
    hints.ai_socktype = socktype;
674
    hints.ai_flags = flags;
675
    return rsock_getaddrinfo_a(host, port, &hints, 1, timeout);
676
}
677
#endif
678

  
665 679
VALUE
666 680
rsock_ipaddr(struct sockaddr *sockaddr, socklen_t sockaddrlen, int norevlookup)
667 681
{
ext/socket/rubysocket.h
321 321
struct rb_addrinfo *rsock_addrinfo(VALUE host, VALUE port, int family, int socktype, int flags);
322 322
struct rb_addrinfo *rsock_getaddrinfo(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack);
323 323
#ifdef HAVE_GETADDRINFO_A
324
struct rb_addrinfo *rsock_addrinfo_a(VALUE host, VALUE port, int family, int socktype, int flags, VALUE timeout);
324 325
struct rb_addrinfo *rsock_getaddrinfo_a(VALUE host, VALUE port, struct addrinfo *hints, int socktype_hack, VALUE timeout);
325 326
#endif
326 327

  
......
349 350
int rsock_detect_cloexec(int fd);
350 351
VALUE rsock_init_sock(VALUE sock, int fd);
351 352
VALUE rsock_sock_s_socketpair(int argc, VALUE *argv, VALUE klass);
352
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type);
353
VALUE rsock_init_inetsock(VALUE sock, VALUE remote_host, VALUE remote_serv, VALUE local_host, VALUE local_serv, int type, VALUE resolv_timeout);
353 354
VALUE rsock_init_unixsock(VALUE sock, VALUE path, int server);
354 355

  
355 356
struct rsock_send_arg {
ext/socket/sockssocket.c
34 34
	init = 1;
35 35
    }
36 36

  
37
    return rsock_init_inetsock(sock, host, port, Qnil, Qnil, INET_SOCKS);
37
    return rsock_init_inetsock(sock, host, port, Qnil, Qnil, INET_SOCKS, Qnil);
38 38
}
39 39

  
40 40
#ifdef SOCKS5
ext/socket/tcpserver.c
36 36
    VALUE hostname, port;
37 37

  
38 38
    rb_scan_args(argc, argv, "011", &hostname, &port);
39
    return rsock_init_inetsock(sock, hostname, port, Qnil, Qnil, INET_SERVER);
39
    return rsock_init_inetsock(sock, hostname, port, Qnil, Qnil, INET_SERVER, Qnil);
40 40
}
41 41

  
42 42
/*
ext/socket/tcpsocket.c
23 23
{
24 24
    VALUE remote_host, remote_serv;
25 25
    VALUE local_host, local_serv;
26
    VALUE opt;
27
    static ID keyword_ids[1];
28
    VALUE kwargs[1];
29
    VALUE resolv_timeout = Qnil;
26 30

  
27
    rb_scan_args(argc, argv, "22", &remote_host, &remote_serv,
28
			&local_host, &local_serv);
31
    if (!keyword_ids[0]) {
32
	CONST_ID(keyword_ids[0], "resolv_timeout");
33
    }
34

  
35
    rb_scan_args(argc, argv, "22:", &remote_host, &remote_serv,
36
			&local_host, &local_serv, &opt);
37

  
38
    if (!NIL_P(opt)) {
39
	rb_get_kwargs(opt, keyword_ids, 0, 1, kwargs);
40
	if (kwargs[0] != Qundef) {
41
	    resolv_timeout = kwargs[0];
42
	}
43
    }
29 44

  
30 45
    return rsock_init_inetsock(sock, remote_host, remote_serv,
31
			       local_host, local_serv, INET_CLIENT);
46
			       local_host, local_serv, INET_CLIENT,
47
			       resolv_timeout);
32 48
}
33 49

  
34 50
static VALUE
test/socket/test_tcp.rb
55 55
    t.close if t && !t.closed?
56 56
  end
57 57

  
58
  def test_initialize_resolv_timeout
59
    TCPServer.open("localhost", 0) do |svr|
60
      th = Thread.new {
61
        c = svr.accept
62
        c.close
63
      }
64
      addr = svr.addr
65
      s = TCPSocket.new(addr[3], addr[1], resolv_timeout: 10)
66
      th.join
67
    ensure
68
      s.close()
69
    end
70
  end
71

  
58 72
  def test_recvfrom
59 73
    TCPServer.open("localhost", 0) {|svr|
60 74
      th = Thread.new {