Top Level Namespace

Defined Under Namespace

Classes: Addrinfo, BasicSocket, IPSocket, SOCKSSocket, Socket, SocketError, TCPServer, TCPSocket, UDPSocket, UNIXServer, UNIXSocket

Constant Summary collapse

AF_INET6_SOCKET_CREATION_TEST =
"#include <sys/types.h>\n#ifndef _WIN32\n#include <sys/socket.h>\n#endif\nint\nmain(void)\n{\n  socket(AF_INET6, SOCK_STREAM, 0);\n  return 0;\n}\n"
GETADDRINFO_GETNAMEINFO_TEST =
"#include <stdlib.h>\n\n#ifndef EXIT_SUCCESS\n#define EXIT_SUCCESS 0\n#endif\n#ifndef EXIT_FAILURE\n#define EXIT_FAILURE 1\n#endif\n\n#ifndef AF_LOCAL\n#define AF_LOCAL AF_UNIX\n#endif\n\nint\nmain(void)\n{\n  int passive, gaierr, inet4 = 0, inet6 = 0;\n  struct addrinfo hints, *ai, *aitop;\n  char straddr[INET6_ADDRSTRLEN], strport[16];\n#ifdef _WIN32\n  WSADATA retdata;\n\n  WSAStartup(MAKEWORD(2, 0), &retdata);\n#endif\n\n  for (passive = 0; passive <= 1; passive++) {\n    memset(&hints, 0, sizeof(hints));\n    hints.ai_family = AF_UNSPEC;\n    hints.ai_protocol = IPPROTO_TCP;\n    hints.ai_flags = passive ? AI_PASSIVE : 0;\n    hints.ai_socktype = SOCK_STREAM;\n    if ((gaierr = getaddrinfo(NULL, \"54321\", &hints, &aitop)) != 0) {\n      (void)gai_strerror(gaierr);\n      goto bad;\n    }\n    for (ai = aitop; ai; ai = ai->ai_next) {\n      if (ai->ai_family == AF_LOCAL) continue;\n      if (ai->ai_addr == NULL)\n        goto bad;\n#if defined(_AIX)\n      if (ai->ai_family == AF_INET6 && passive) {\n        inet6++;\n        continue;\n      }\n      ai->ai_addr->sa_len = ai->ai_addrlen;\n      ai->ai_addr->sa_family = ai->ai_family;\n#endif\n      if (ai->ai_addrlen == 0 ||\n          getnameinfo(ai->ai_addr, ai->ai_addrlen,\n                      straddr, sizeof(straddr), strport, sizeof(strport),\n                      NI_NUMERICHOST|NI_NUMERICSERV) != 0) {\n        goto bad;\n      }\n      if (strcmp(strport, \"54321\") != 0) {\n        goto bad;\n      }\n      switch (ai->ai_family) {\n      case AF_INET:\n        if (passive) {\n          if (strcmp(straddr, \"0.0.0.0\") != 0) {\n            goto bad;\n          }\n        } else {\n          if (strcmp(straddr, \"127.0.0.1\") != 0) {\n            goto bad;\n          }\n        }\n        inet4++;\n        break;\n      case AF_INET6:\n        if (passive) {\n          if (strcmp(straddr, \"::\") != 0) {\n            goto bad;\n          }\n        } else {\n          if (strcmp(straddr, \"::1\") != 0) {\n            goto bad;\n          }\n        }\n        inet6++;\n        break;\n      case AF_UNSPEC:\n        goto bad;\n        break;\n      default:\n        /* another family support? */\n        break;\n      }\n    }\n  }\n\n  if (!(inet4 == 0 || inet4 == 2))\n    goto bad;\n  if (!(inet6 == 0 || inet6 == 2))\n    goto bad;\n\n  if (aitop)\n    freeaddrinfo(aitop);\n  return EXIT_SUCCESS;\n\n bad:\n  if (aitop)\n    freeaddrinfo(aitop);\n  return EXIT_FAILURE;\n}\n"
RECVMSG_WITH_MSG_PEEK_ALLOCATE_FD_TEST =
"#include <stdlib.h>\n#include <stdio.h>\n#include <string.h>\n#include <sys/types.h>\n#include <sys/stat.h>\n#include <sys/socket.h>\n#include <sys/un.h>\n#include <unistd.h>\n\nint main(int argc, char *argv[])\n{\n    int ps[2], sv[2];\n    int ret;\n    ssize_t ss;\n    int s_fd, r_fd;\n    struct msghdr s_msg, r_msg;\n    union {\n        struct cmsghdr hdr;\n        char dummy[CMSG_SPACE(sizeof(int))];\n    } s_cmsg, r_cmsg;\n    struct iovec s_iov, r_iov;\n    char s_buf[1], r_buf[1];\n    struct stat s_statbuf, r_statbuf;\n\n    ret = pipe(ps);\n    if (ret == -1) { perror(\"pipe\"); exit(EXIT_FAILURE); }\n\n    s_fd = ps[0];\n\n    ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);\n    if (ret == -1) { perror(\"socketpair\"); exit(EXIT_FAILURE); }\n\n    s_msg.msg_name = NULL;\n    s_msg.msg_namelen = 0;\n    s_msg.msg_iov = &s_iov;\n    s_msg.msg_iovlen = 1;\n    s_msg.msg_control = &s_cmsg;\n    s_msg.msg_controllen = CMSG_SPACE(sizeof(int));;\n    s_msg.msg_flags = 0;\n\n    s_iov.iov_base = &s_buf;\n    s_iov.iov_len = sizeof(s_buf);\n\n    s_buf[0] = 'a';\n\n    s_cmsg.hdr.cmsg_len = CMSG_LEN(sizeof(int));\n    s_cmsg.hdr.cmsg_level = SOL_SOCKET;\n    s_cmsg.hdr.cmsg_type = SCM_RIGHTS;\n    memcpy(CMSG_DATA(&s_cmsg.hdr), (char *)&s_fd, sizeof(int));\n\n    ss = sendmsg(sv[0], &s_msg, 0);\n    if (ss == -1) { perror(\"sendmsg\"); exit(EXIT_FAILURE); }\n\n    r_msg.msg_name = NULL;\n    r_msg.msg_namelen = 0;\n    r_msg.msg_iov = &r_iov;\n    r_msg.msg_iovlen = 1;\n    r_msg.msg_control = &r_cmsg;\n    r_msg.msg_controllen = CMSG_SPACE(sizeof(int));\n    r_msg.msg_flags = 0;\n\n    r_iov.iov_base = &r_buf;\n    r_iov.iov_len = sizeof(r_buf);\n\n    r_buf[0] = '0';\n\n    memset(&r_cmsg, 0xff, CMSG_SPACE(sizeof(int)));\n\n    ss = recvmsg(sv[1], &r_msg, MSG_PEEK);\n    if (ss == -1) { perror(\"recvmsg\"); exit(EXIT_FAILURE); }\n\n    if (ss != 1) {\n        fprintf(stderr, \"unexpected return value from recvmsg: %ld\\n\", (long)ss);\n        exit(EXIT_FAILURE);\n    }\n    if (r_buf[0] != 'a') {\n        fprintf(stderr, \"unexpected return data from recvmsg: 0x%02x\\n\", r_buf[0]);\n        exit(EXIT_FAILURE);\n    }\n\n    if (r_msg.msg_controllen < CMSG_LEN(sizeof(int))) {\n        fprintf(stderr, \"unexpected: r_msg.msg_controllen < CMSG_LEN(sizeof(int)) not hold: %ld\\n\",\n                (long)r_msg.msg_controllen);\n        exit(EXIT_FAILURE);\n    }\n    if (r_cmsg.hdr.cmsg_len < CMSG_LEN(sizeof(int))) {\n        fprintf(stderr, \"unexpected: r_cmsg.hdr.cmsg_len < CMSG_LEN(sizeof(int)) not hold: %ld\\n\",\n                (long)r_cmsg.hdr.cmsg_len);\n        exit(EXIT_FAILURE);\n    }\n    memcpy((char *)&r_fd, CMSG_DATA(&r_cmsg.hdr), sizeof(int));\n\n    if (r_fd < 0) {\n        fprintf(stderr, \"negative r_fd: %d\\n\", r_fd);\n        exit(EXIT_FAILURE);\n    }\n\n    if (r_fd == s_fd) {\n        fprintf(stderr, \"r_fd and s_fd is same: %d\\n\", r_fd);\n        exit(EXIT_FAILURE);\n    }\n\n    ret = fstat(s_fd, &s_statbuf);\n    if (ret == -1) { perror(\"fstat(s_fd)\"); exit(EXIT_FAILURE); }\n\n    ret = fstat(r_fd, &r_statbuf);\n    if (ret == -1) { perror(\"fstat(r_fd)\"); exit(EXIT_FAILURE); }\n\n    if (s_statbuf.st_dev != r_statbuf.st_dev ||\n        s_statbuf.st_ino != r_statbuf.st_ino) {\n        fprintf(stderr, \"dev/ino doesn't match: s_fd:%ld/%ld r_fd:%ld/%ld\\n\",\n                (long)s_statbuf.st_dev, (long)s_statbuf.st_ino,\n                (long)r_statbuf.st_dev, (long)r_statbuf.st_ino);\n        exit(EXIT_FAILURE);\n    }\n\n    return EXIT_SUCCESS;\n}\n"
C_ESC =
{
  "\\" => "\\\\",
  '"' => '\"',
  "\n" => '\n',
}
C_ESC_PAT =
Regexp.union(*C_ESC.keys)
COMMENTS =
Hash.new { |h, name| h[name] = name }
DEFS =
h.to_a
NAME_TO_INT_DEFS =
[]
INTERN_DEFS =
[]

Instance Method Summary collapse

Instance Method Details

#c_str(str) ⇒ Object


31
32
33
# File 'mkconstants.rb', line 31

def c_str(str)
  '"' + str.gsub(C_ESC_PAT) {|s| C_ESC[s]} + '"'
end

#def_intern(func_name, pat, prefix_optional = nil) ⇒ Object


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'mkconstants.rb', line 233

def def_intern(func_name, pat, prefix_optional=nil)
  prefix_pat = nil
  if prefix_optional
    if Regexp === prefix_optional
      prefix_pat = prefix_optional
    else
      prefix_pat = /\A#{Regexp.escape prefix_optional}/
    end
  end
  hash_var = "#{func_name}_hash"
  vardef = "static st_table *#{hash_var};"
  gen_hash = gen_int_to_name_hash(hash_var, pat, prefix_pat)
  decl = gen_int_to_name_decl(func_name, hash_var)
  func = gen_int_to_name_func(func_name, hash_var)
  INTERN_DEFS << [vardef, gen_hash, decl, func]
end

#def_name_to_int(funcname, pat, prefix_optional, guard = nil) ⇒ Object


190
191
192
193
194
# File 'mkconstants.rb', line 190

def def_name_to_int(funcname, pat, prefix_optional, guard=nil)
  decl = gen_name_to_int_decl(funcname, pat, prefix_optional, guard)
  func = gen_name_to_int_func(funcname, pat, prefix_optional, guard)
  NAME_TO_INT_DEFS << [decl, func]
end

#each_constObject


57
58
59
60
61
62
63
64
65
66
# File 'mkconstants.rb', line 57

def each_const
  DEFS.each {|name, default_value|
    guard = nil
    if /\A(AF_INET6|PF_INET6|IPV6_.*)\z/ =~ name
      # IPv6 is not supported although AF_INET6 is defined on mingw
      guard = "defined(INET6)"
    end
    yield guard, name, default_value
  }
end

#each_name(pat) ⇒ Object


68
69
70
71
72
73
# File 'mkconstants.rb', line 68

def each_name(pat)
  DEFS.each {|name, default_value|
    next if pat !~ name
    yield name
  }
end

#each_names_with_len(pat, prefix_optional = nil) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'mkconstants.rb', line 117

def each_names_with_len(pat, prefix_optional=nil)
  h = {}
  DEFS.each {|name, default_value|
    next if pat !~ name
    (h[name.length] ||= []) << [name, name]
  }
  if prefix_optional
    if Regexp === prefix_optional
      prefix_pat = prefix_optional
    else
      prefix_pat = /\A#{Regexp.escape prefix_optional}/
    end
    DEFS.each {|const, default_value|
      next if pat !~ const
      next if prefix_pat !~ const
      name = $'
      (h[name.length] ||= []) << [name, const]
    }
  end
  hh = {}
  h.each {|len, pairs|
    pairs.each {|name, const|
      raise "name crash: #{name}" if hh[name]
      hh[name] = true
    }
  }
  h.keys.sort.each {|len|
    yield h[len], len
  }
end

#reverse_each_name(pat) ⇒ Object


110
111
112
113
114
115
# File 'mkconstants.rb', line 110

def reverse_each_name(pat)
  DEFS.reverse_each {|name, default_value|
    next if pat !~ name
    yield name
  }
end

#reverse_each_name_with_prefix_optional(pat, prefix_pat) ⇒ Object


196
197
198
199
200
201
202
203
204
205
206
# File 'mkconstants.rb', line 196

def reverse_each_name_with_prefix_optional(pat, prefix_pat)
  reverse_each_name(pat) {|n|
    yield n, n
  }
  if prefix_pat
    reverse_each_name(pat) {|n|
      next if prefix_pat !~ n
      yield n, $'
    }
  end
end

#test_recvmsg_with_msg_peek_creates_fds(headers) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'extconf.rb', line 245

def test_recvmsg_with_msg_peek_creates_fds(headers)
  case RUBY_PLATFORM
  when /linux/
    # Linux 2.6.38 allocate fds by recvmsg with MSG_PEEK.
    close_fds = true
  when /bsd|darwin/
    # FreeBSD 8.2.0, NetBSD 5 and MacOS X Snow Leopard doesn't
    # allocate fds by recvmsg with MSG_PEEK.
    # [ruby-dev:44189]
    # http://bugs.ruby-lang.org/issues/5075
    close_fds = false
  when /cygwin/
    # Cygwin doesn't support fd passing.
    # http://cygwin.com/ml/cygwin/2003-09/msg01808.html
    close_fds = false
  else
    close_fds = nil
  end
  if !CROSS_COMPILING
    if checking_for("recvmsg() with MSG_PEEK allocate file descriptors") {
        try_run(cpp_include(headers) + RECVMSG_WITH_MSG_PEEK_ALLOCATE_FD_TEST)
       }
      if close_fds == false
        warn "unexpected fd-passing recvmsg() with MSG_PEEK behavor on #{RUBY_PLATFORM}: fd allocation unexpected."
      elsif close_fds == nil
        puts "info: #{RUBY_PLATFORM} recvmsg() with MSG_PEEK allocates fds on fd-passing."
      end
      close_fds = true
    else
      if close_fds == true
        warn "unexpected fd-passing recvmsg() with MSG_PEEK behavor on #{RUBY_PLATFORM}: fd allocation expected."
      elsif close_fds == nil
        puts "info: #{RUBY_PLATFORM}: recvmsg() with MSG_PEEK doesn't allocates fds on fd-passing."
      end
      close_fds = false
    end
  end
  if close_fds == nil
    abort "Fatal: cannot test fd-passing recvmsg() with MSG_PEEK behavor\nbecause cross-compilation for \#{RUBY_PLATFORM}.\nIf recvmsg() with MSG_PEEK allocates fds on fd passing:\n--enable-close-fds-by-recvmsg-with-peek\nIf recvmsg() with MSG_PEEK doesn't allocate fds on fd passing:\n--disable-close-fds-by-recvmsg-with-peek\n"
  end
  close_fds
end