├── .travis.yml ├── README.md ├── mrbgem.rake ├── mrblib └── socket.rb ├── run_test.rb ├── src ├── const.cstub ├── const.def ├── gen.rb └── socket.c └── test ├── addrinfo.rb ├── basicsocket.rb ├── ipsocket.rb ├── socket.rb ├── sockettest.c ├── tcpsocket.rb ├── udpsocket.rb └── unix.rb /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | sudo: false 3 | script: 4 | - "ruby run_test.rb all test" 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mruby-socket 2 | ============ 3 | [![Build Status](https://travis-ci.org/iij/mruby-socket.svg?branch=master)](https://travis-ci.org/iij/mruby-socket) 4 | 5 | 6 | "mruby-socket" mrbgem provides BSD socket interface for mruby. 7 | API is compatible with CRuby's "socket" library. 8 | 9 | 10 | ## Example 11 | ```sh 12 | % vi kame.rb 13 | s = TCPSocket.open("www.kame.net", 80) 14 | s.write("GET / HTTP/1.0\r\n\r\n") 15 | puts s.read 16 | s.close 17 | 18 | % mruby kame.rb 19 | HTTP/1.1 200 OK 20 | Date: Tue, 21 May 2013 04:31:30 GMT 21 | ... 22 | ``` 23 | 24 | ## Requirement 25 | - [iij/mruby-io](https://github.com/iij/mruby-io) mrbgem 26 | - [iij/mruby-mtest](https://github.com/iij/mruby-mtest) mrgbem to run tests 27 | - system must have RFC3493 basic socket interface 28 | - and some POSIX API... 29 | 30 | ## TODO 31 | - add missing methods 32 | - write more tests 33 | - fix possible descriptor leakage (see XXX comments) 34 | - `UNIXSocket#recv_io` `UNIXSocket#send_io` 35 | 36 | 37 | ## License 38 | 39 | Copyright (c) 2013 Internet Initiative Japan Inc. 40 | 41 | Permission is hereby granted, free of charge, to any person obtaining a 42 | copy of this software and associated documentation files (the "Software"), 43 | to deal in the Software without restriction, including without limitation 44 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 45 | and/or sell copies of the Software, and to permit persons to whom the 46 | Software is furnished to do so, subject to the following conditions: 47 | 48 | The above copyright notice and this permission notice shall be included in 49 | all copies or substantial portions of the Software. 50 | 51 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 52 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 53 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 54 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 55 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 56 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 57 | DEALINGS IN THE SOFTWARE. 58 | -------------------------------------------------------------------------------- /mrbgem.rake: -------------------------------------------------------------------------------- 1 | MRuby::Gem::Specification.new('mruby-socket') do |spec| 2 | spec.license = 'MIT' 3 | spec.authors = 'Internet Initiative Japan Inc.' 4 | 5 | spec.cc.include_paths << "#{build.root}/src" 6 | #spec.cc.defines << "HAVE_SA_LEN=0" 7 | 8 | # If Windows, use winsock 9 | if ( /mswin|mingw|win32/ =~ RUBY_PLATFORM ) then 10 | spec.linker.libraries << "wsock32" 11 | spec.linker.libraries << "ws2_32" 12 | end 13 | 14 | spec.add_dependency('mruby-io') 15 | spec.add_dependency('mruby-pack') 16 | # spec.add_dependency('mruby-mtest') 17 | end 18 | -------------------------------------------------------------------------------- /mrblib/socket.rb: -------------------------------------------------------------------------------- 1 | class Addrinfo 2 | def initialize(sockaddr, family=Socket::PF_UNSPEC, socktype=0, protocol=0) 3 | @hostname = nil 4 | if sockaddr.is_a? Array 5 | sary = sockaddr 6 | if sary[0] == 'AF_INET' || sary[0] == 'AF_INET6' 7 | @sockaddr = Socket.sockaddr_in(sary[1], sary[3]) 8 | @hostname = sary[2] 9 | elsif sary[0] == 'AF_UNIX' 10 | @sockaddr = Socket.sockaddr_un(sary[1]) 11 | end 12 | else 13 | @sockaddr = sockaddr.dup 14 | end 15 | if family == Socket::PF_UNSPEC or family == nil 16 | @family = Socket._sockaddr_family(@sockaddr) 17 | else 18 | @family = family 19 | end 20 | @socktype = socktype 21 | @protocol = protocol 22 | @canonname = nil 23 | end 24 | 25 | def self.foreach(nodename, service, family=nil, socktype=nil, protocol=nil, flags=0, &block) 26 | a = self.getaddrinfo(nodename, service, family, socktype, protocol, flags) 27 | a.each { |ai| block.call(ai) } 28 | a 29 | end 30 | 31 | def self.ip(host) 32 | Addrinfo.new(Socket.sockaddr_in(0, host)) 33 | end 34 | 35 | def self.tcp(host, port) 36 | Addrinfo.getaddrinfo(host, port, nil, Socket::SOCK_STREAM, Socket::IPPROTO_TCP)[0] 37 | end 38 | 39 | def self.udp(host, port) 40 | Addrinfo.getaddrinfo(host, port, nil, Socket::SOCK_DGRAM, Socket::IPPROTO_UDP)[0] 41 | end 42 | 43 | def self.unix(path, socktype=Socket::SOCK_STREAM) 44 | Addrinfo.new(Socket.sockaddr_un(path), Socket::AF_UNIX, socktype) 45 | end 46 | 47 | def afamily 48 | @family 49 | end 50 | 51 | #def bind 52 | 53 | attr_reader :canonname 54 | 55 | #def connect 56 | #def connect_from 57 | #def connect_to 58 | 59 | #def family_addrinfo(host, port=nil) 60 | #def getnameinfo(flags=0) 61 | # Socket.getnameinfo 62 | #end 63 | 64 | def inspect 65 | if ipv4? or ipv6? 66 | if @protocol == Socket::IPPROTO_TCP or (@socktype == Socket::SOCK_STREAM and @protocol == 0) 67 | proto = 'TCP' 68 | elsif @protocol == Socket::IPPROTO_UDP or (@socktype == Socket::SOCK_DGRAM and @protocol == 0) 69 | proto = 'UDP' 70 | else 71 | proto = '???' 72 | end 73 | "#" 74 | else 75 | "#" 76 | end 77 | end 78 | 79 | def inspect_sockaddr 80 | if ipv4? 81 | a, p = ip_unpack 82 | "#{a}:#{p}" 83 | elsif ipv6? 84 | a, p = ip_unpack 85 | "[#{a}]:#{p}" 86 | elsif unix? 87 | unix_path 88 | else 89 | '???' 90 | end 91 | end 92 | 93 | def ip? 94 | ipv4? or ipv6? 95 | end 96 | 97 | def ip_address 98 | ip_unpack[0] 99 | end 100 | 101 | def ip_port 102 | ip_unpack[1] 103 | end 104 | 105 | def ip_unpack 106 | h, p = getnameinfo(Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV) 107 | [ h, p.to_i ] 108 | end 109 | 110 | def ipv4? 111 | @family == Socket::AF_INET 112 | end 113 | 114 | #def ipv4_loopback? 115 | #def ipv4_multicast? 116 | #def ipv4_private? 117 | 118 | def ipv6? 119 | @family == Socket::AF_INET6 120 | end 121 | 122 | #def ipv6_loopback? 123 | #def ipv6_mc_global? 124 | #def ipv6_mc_linklocal? 125 | #def ipv6_mc_nodelocal? 126 | #def ipv6_mc_orilocal? 127 | #def ipv6_mc_sitelocal? 128 | #def ipv6_multicast? 129 | #def ipv6_to_ipv4 130 | #def ipv6_unspecified 131 | #def ipv6_v4compat? 132 | #def ipv6_v4mapped? 133 | #def listen(backlog=5) 134 | 135 | def pfamily 136 | @family 137 | end 138 | 139 | attr_reader :protocol 140 | attr_reader :socktype 141 | 142 | def _to_array 143 | case @family 144 | when Socket::AF_INET 145 | s = "AF_INET" 146 | when Socket::AF_INET6 147 | s = "AF_INET6" 148 | when Socket::AF_UNIX 149 | s = "AF_UNIX" 150 | else 151 | s = "(unknown AF)" 152 | end 153 | addr, port = self.getnameinfo(Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV) 154 | [ s, port.to_i, addr, addr ] 155 | end 156 | 157 | def to_sockaddr 158 | @sockaddr 159 | end 160 | 161 | alias to_s to_sockaddr 162 | 163 | def unix? 164 | @family == Socket::AF_UNIX 165 | end 166 | end 167 | 168 | class BasicSocket < IO 169 | @@do_not_reverse_lookup = true 170 | 171 | def self.do_not_reverse_lookup 172 | @@do_not_reverse_lookup 173 | end 174 | 175 | def self.do_not_reverse_lookup=(val) 176 | @@do_not_reverse_lookup = val ? true : false 177 | end 178 | 179 | def initialize(*args) 180 | super(*args) 181 | @do_not_reverse_lookup = @@do_not_reverse_lookup 182 | end 183 | 184 | def self.for_fd(fd) 185 | super(fd, "r+") 186 | end 187 | 188 | #def connect_address 189 | 190 | def local_address 191 | Addrinfo.new self.getsockname 192 | end 193 | 194 | def recv_nonblock(maxlen, flags=0) 195 | begin 196 | _setnonblock(true) 197 | recv(maxlen, flags) 198 | ensure 199 | _setnonblock(false) 200 | end 201 | end 202 | 203 | def remote_address 204 | Addrinfo.new self.getpeername 205 | end 206 | 207 | attr_accessor :do_not_reverse_lookup 208 | end 209 | 210 | class IPSocket < BasicSocket 211 | def self.getaddress(host) 212 | Addrinfo.ip(host).ip_address 213 | end 214 | 215 | def addr 216 | Addrinfo.new(self.getsockname)._to_array 217 | end 218 | 219 | def peeraddr 220 | Addrinfo.new(self.getpeername)._to_array 221 | end 222 | 223 | def recvfrom(maxlen, flags=0) 224 | msg, sa = _recvfrom(maxlen, flags) 225 | [ msg, Addrinfo.new(sa)._to_array ] 226 | end 227 | end 228 | 229 | class TCPSocket < IPSocket 230 | def initialize(host, service, local_host=nil, local_service=nil) 231 | if @init_with_fd 232 | super(host, service) 233 | else 234 | s = nil 235 | e = nil 236 | Addrinfo.foreach(host, service) { |ai| 237 | begin 238 | s = Socket._socket(ai.afamily, Socket::SOCK_STREAM, 0) 239 | if local_host or local_service 240 | local_host ||= (ai.afamily == Socket::AF_INET) ? "0.0.0.0" : "::" 241 | local_service ||= "0" 242 | bi = Addrinfo.getaddrinfo(local_host, local_service, ai.afamily, ai.socktype)[0] 243 | Socket._bind(s, bi.to_sockaddr) 244 | end 245 | Socket._connect(s, ai.to_sockaddr) 246 | super(s, "r+") 247 | # Here is a workaround for a bug in mruby 1.3.0. We want to 248 | # return directly here but the next statement of the iterator 249 | # block (`raise e if e`) will be evaluated on mruby 1.3.0, 250 | # due to a known bug. 251 | e = nil 252 | break 253 | rescue => e0 254 | e = e0 255 | end 256 | } 257 | raise e if e 258 | end 259 | end 260 | 261 | def self.new_with_prelude pre, *args 262 | o = self._allocate 263 | o.instance_eval(&pre) 264 | o.initialize(*args) 265 | o 266 | end 267 | 268 | #def self.gethostbyname(host) 269 | end 270 | 271 | class TCPServer < TCPSocket 272 | def initialize(host=nil, service) 273 | ai = Addrinfo.getaddrinfo(host, service, nil, nil, nil, Socket::AI_PASSIVE)[0] 274 | @init_with_fd = true 275 | super(Socket._socket(ai.afamily, Socket::SOCK_STREAM, 0), "r+") 276 | if Socket.const_defined?(:SO_REUSEADDR) 277 | self.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) 278 | end 279 | Socket._bind(self.fileno, ai.to_sockaddr) 280 | listen(5) 281 | self 282 | end 283 | 284 | def accept 285 | fd = self.sysaccept 286 | begin 287 | TCPSocket.new_with_prelude(proc { @init_with_fd = true }, fd, "r+") 288 | rescue 289 | IO._sysclose(fd) rescue nil 290 | raise 291 | end 292 | end 293 | 294 | def accept_nonblock 295 | begin 296 | self._setnonblock(true) 297 | self.accept 298 | ensure 299 | self._setnonblock(false) 300 | end 301 | end 302 | 303 | def listen(backlog) 304 | Socket._listen(self.fileno, backlog) 305 | 0 306 | end 307 | 308 | def sysaccept 309 | Socket._accept(self.fileno)[0] 310 | end 311 | end 312 | 313 | class UDPSocket < IPSocket 314 | def initialize(af=Socket::AF_INET) 315 | super(Socket._socket(af, Socket::SOCK_DGRAM, 0), "r+") 316 | @af = af 317 | self 318 | end 319 | 320 | def bind(host, port) 321 | Socket._bind(self.fileno, _sockaddr_in(port, host)) 322 | 0 323 | end 324 | 325 | def connect(host, port) 326 | Socket._connect(self.fileno, _sockaddr_in(port, host)) 327 | 0 328 | end 329 | 330 | def recvfrom_nonblock(*args) 331 | s = self 332 | begin 333 | self._setnonblock(true) 334 | self.recvfrom(*args) 335 | ensure 336 | # XXX: self is a SystemcallException here! (should be bug) 337 | s._setnonblock(false) 338 | end 339 | end 340 | 341 | def send(mesg, flags, host=nil, port=nil) 342 | if port 343 | super(mesg, flags, _sockaddr_in(port, host)) 344 | elsif host 345 | super(mesg, flags, host) 346 | else 347 | super(mesg, flags) 348 | end 349 | end 350 | 351 | def _sockaddr_in(port, host) 352 | ai = Addrinfo.getaddrinfo(host, port, @af, Socket::SOCK_DGRAM)[0] 353 | ai.to_sockaddr 354 | end 355 | end 356 | 357 | class Socket < BasicSocket 358 | def initialize(domain, type, protocol=0) 359 | super(Socket._socket(domain, type, protocol), "r+") 360 | end 361 | 362 | #def self.accept_loop 363 | 364 | def self.getaddrinfo(nodename, servname, family=nil, socktype=nil, protocol=nil, flags=0) 365 | Addrinfo.getaddrinfo(nodename, servname, family, socktype, protocol, flags).map { |ai| 366 | ary = ai._to_array 367 | ary[2] = nodename 368 | ary[4] = ai.afamily 369 | ary[5] = ai.socktype 370 | ary[6] = ai.protocol 371 | ary 372 | } 373 | end 374 | 375 | #def self.getnameinfo 376 | #def self.ip_address_list 377 | 378 | def self.open(*args) 379 | new(args) 380 | end 381 | 382 | def self.sockaddr_in(port, host) 383 | ai = Addrinfo.getaddrinfo(host, port, nil, Socket::SOCK_DGRAM)[0] 384 | ai.to_sockaddr 385 | end 386 | 387 | #def self.tcp 388 | #def self.tcp_server_loop 389 | #def self.tcp_server_sockets 390 | #def self.udp_server_loop 391 | #def self.udp_server_loop_on 392 | #def self.udp_server_recv 393 | #def self.udp_server_sockets 394 | #def self.unix(path) 395 | #def self.unix_server_loop 396 | #def self.unix_server_socket 397 | 398 | def self.unpack_sockaddr_in(sa) 399 | Addrinfo.new(sa).ip_unpack.reverse 400 | end 401 | 402 | def self.unpack_sockaddr_un(sa) 403 | Addrinfo.new(sa).unix_path 404 | end 405 | 406 | class << self 407 | alias pack_sockaddr_in sockaddr_in 408 | alias pack_sockaddr_un sockaddr_un 409 | alias pair socketpair 410 | end 411 | 412 | def accept 413 | fd, addr = self.sysaccept 414 | [ Socket.for_fd(fd), addr ] 415 | end 416 | 417 | def accept_nonblock 418 | begin 419 | self._setnonblock(true) 420 | self.accept 421 | ensure 422 | self._setnonblock(false) 423 | end 424 | end 425 | 426 | def bind(sockaddr) 427 | sockaddr = sockaddr.to_sockaddr if sockaddr.is_a? Addrinfo 428 | Socket._bind(self.fileno, sockaddr) 429 | 0 430 | end 431 | 432 | def connect(sockaddr) 433 | sockaddr = sockaddr.to_sockaddr if sockaddr.is_a? Addrinfo 434 | Socket._connect(self.fileno, sockaddr) 435 | 0 436 | end 437 | 438 | def connect_nonblock(sockaddr) 439 | begin 440 | self._setnonblock(true) 441 | self.connect(sockaddr) 442 | ensure 443 | self._setnonblock(false) 444 | end 445 | end 446 | 447 | #def ipv6only! 448 | 449 | def listen(backlog) 450 | Socket._listen(self.fileno, backlog) 451 | 0 452 | end 453 | 454 | def recvfrom(maxlen, flags=0) 455 | msg, sa = _recvfrom(maxlen, flags) 456 | socktype = self.getsockopt(Socket::SOL_SOCKET, Socket::SO_TYPE).int 457 | [ msg, Addrinfo.new(sa, Socket::PF_UNSPEC, socktype) ] 458 | end 459 | 460 | def recvfrom_nonblock(*args) 461 | begin 462 | self._setnonblock(true) 463 | self._recvfrom(*args) 464 | ensure 465 | self._setnonblock(false) 466 | end 467 | end 468 | 469 | def sysaccept 470 | Socket._accept(self.fileno) 471 | end 472 | end 473 | 474 | class UNIXSocket < BasicSocket 475 | def initialize(path, &block) 476 | if self.is_a? UNIXServer 477 | super(path, "r") 478 | else 479 | super(Socket._socket(Socket::AF_UNIX, Socket::SOCK_STREAM, 0), "r+") 480 | Socket._connect(self.fileno, Socket.sockaddr_un(path)) 481 | 482 | if block_given? 483 | begin 484 | yield self 485 | ensure 486 | begin 487 | self.close unless self.closed? 488 | rescue StandardError 489 | end 490 | end 491 | end 492 | end 493 | end 494 | 495 | def self.socketpair(type=Socket::SOCK_STREAM, protocol=0) 496 | a = Socket.socketpair(Socket::AF_UNIX, type, protocol) 497 | [ UNIXSocket.for_fd(a[0]), UNIXSocket.for_fd(a[1]) ] 498 | end 499 | 500 | class << self 501 | alias pair socketpair 502 | end 503 | 504 | def addr 505 | [ "AF_UNIX", path ] 506 | end 507 | 508 | def path 509 | Addrinfo.new(self.getsockname).unix_path 510 | end 511 | 512 | def peeraddr 513 | [ "AF_UNIX", Addrinfo.new(self.getpeername).unix_path ] 514 | end 515 | 516 | #def recv_io 517 | 518 | def recvfrom(maxlen, flags=0) 519 | msg, sa = _recvfrom(maxlen, flags) 520 | path = (sa.size > 0) ? Addrinfo.new(sa).unix_path : "" 521 | [ msg, [ "AF_UNIX", path ] ] 522 | end 523 | 524 | #def send_io 525 | end 526 | 527 | class UNIXServer < UNIXSocket 528 | def initialize(path) 529 | fd = Socket._socket(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) 530 | begin 531 | super(fd) 532 | Socket._bind(fd, Socket.pack_sockaddr_un(path)) 533 | self.listen(5) 534 | rescue => e 535 | IO._sysclose(fd) rescue nil 536 | raise e 537 | end 538 | 539 | if block_given? 540 | begin 541 | yield self 542 | ensure 543 | self.close rescue nil unless self.closed? 544 | end 545 | end 546 | end 547 | 548 | def accept 549 | fd = self.sysaccept 550 | begin 551 | sock = UNIXSocket.for_fd(fd) 552 | rescue 553 | IO._sysclose(fd) rescue nil 554 | end 555 | sock 556 | end 557 | 558 | def accept_nonblock 559 | begin 560 | self._setnonblock(true) 561 | self.accept 562 | ensure 563 | self._setnonblock(false) 564 | end 565 | end 566 | 567 | def listen(backlog) 568 | Socket._listen(self.fileno, backlog) 569 | 0 570 | end 571 | 572 | def sysaccept 573 | Socket._accept(self.fileno)[0] 574 | end 575 | end 576 | 577 | class Socket 578 | include Constants 579 | end 580 | 581 | class Socket 582 | class Option 583 | def initialize(family, level, optname, data) 584 | @family = family 585 | @level = level 586 | @optname = optname 587 | @data = data 588 | end 589 | 590 | def self.bool(family, level, optname, bool) 591 | self.new(family, level, optname, [(bool ? 1 : 0)].pack('i')) 592 | end 593 | 594 | def self.int(family, level, optname, integer) 595 | self.new(family, level, optname, [integer].pack('i')) 596 | end 597 | 598 | #def self.linger(family, level, optname, integer) 599 | #end 600 | 601 | attr_reader :data, :family, :level, :optname 602 | 603 | def bool 604 | @data.unpack('i')[0] != 0 605 | end 606 | 607 | def inspect 608 | "#" 609 | end 610 | 611 | def int 612 | @data.unpack('i')[0] 613 | end 614 | 615 | def linger 616 | raise NotImplementedError.new 617 | end 618 | 619 | def unpack(template) 620 | raise NotImplementedError.new 621 | end 622 | end 623 | end 624 | 625 | class SocketError < StandardError; end 626 | -------------------------------------------------------------------------------- /run_test.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # 3 | # mrbgems test runner 4 | # 5 | 6 | if __FILE__ == $0 7 | repository, dir = 'https://github.com/mruby/mruby.git', 'tmp/mruby' 8 | build_args = ARGV 9 | 10 | Dir.mkdir 'tmp' unless File.exist?('tmp') 11 | unless File.exist?(dir) 12 | system "git clone #{repository} #{dir}" 13 | end 14 | 15 | exit system(%Q[cd #{dir}; MRUBY_CONFIG=#{File.expand_path __FILE__} ruby minirake #{build_args.join(' ')}]) 16 | end 17 | 18 | MRuby::Build.new do |conf| 19 | toolchain :gcc 20 | conf.gembox 'default' 21 | 22 | conf.gem :git => 'https://github.com/iij/mruby-mtest.git' 23 | conf.gem :git => 'https://github.com/iij/mruby-io.git' 24 | conf.gem :git => 'https://github.com/iij/mruby-pack.git' 25 | 26 | conf.gem File.expand_path(File.dirname(__FILE__)) 27 | conf.enable_test 28 | end 29 | -------------------------------------------------------------------------------- /src/const.cstub: -------------------------------------------------------------------------------- 1 | #ifdef AF_INET 2 | define_const(AF_INET); 3 | #endif 4 | #ifdef PF_INET 5 | define_const(PF_INET); 6 | #endif 7 | #ifdef AF_INET6 8 | define_const(AF_INET6); 9 | #endif 10 | #ifdef PF_INET6 11 | define_const(PF_INET6); 12 | #endif 13 | #ifdef AF_LINK 14 | define_const(AF_LINK); 15 | #endif 16 | #ifdef PF_LINK 17 | define_const(PF_LINK); 18 | #endif 19 | #ifdef AF_LOCAL 20 | define_const(AF_LOCAL); 21 | #endif 22 | #ifdef PF_LOCAL 23 | define_const(PF_LOCAL); 24 | #endif 25 | #ifdef AF_UNIX 26 | define_const(AF_UNIX); 27 | #endif 28 | #ifdef PF_UNIX 29 | define_const(PF_UNIX); 30 | #endif 31 | #ifdef AF_MAX 32 | define_const(AF_MAX); 33 | #endif 34 | #ifdef AF_UNSPEC 35 | define_const(AF_UNSPEC); 36 | #endif 37 | #ifdef PF_UNSPEC 38 | define_const(PF_UNSPEC); 39 | #endif 40 | #ifdef AF_ROUTE 41 | define_const(AF_ROUTE); 42 | #endif 43 | #ifdef PF_ROUTE 44 | define_const(PF_ROUTE); 45 | #endif 46 | #ifdef AI_CANONNAME 47 | define_const(AI_CANONNAME); 48 | #endif 49 | #ifdef AI_FQDN 50 | define_const(AI_FQDN); 51 | #endif 52 | #ifdef AI_NUMERICHOST 53 | define_const(AI_NUMERICHOST); 54 | #endif 55 | #ifdef AI_NUMERICSERV 56 | define_const(AI_NUMERICSERV); 57 | #endif 58 | #ifdef AI_PASSIVE 59 | define_const(AI_PASSIVE); 60 | #endif 61 | #ifdef IP_ADD_MEMBERSHIP 62 | define_const(IP_ADD_MEMBERSHIP); 63 | #endif 64 | #ifdef IP_ADD_SOURCE_MEMBERSHIP 65 | define_const(IP_ADD_SOURCE_MEMBERSHIP); 66 | #endif 67 | #ifdef IP_BLOCK_SOURCE 68 | define_const(IP_BLOCK_SOURCE); 69 | #endif 70 | #ifdef IP_DROP_MEMBERSHIP 71 | define_const(IP_DROP_MEMBERSHIP); 72 | #endif 73 | #ifdef IP_DROP_SOURCE_MEMBERSHIP 74 | define_const(IP_DROP_SOURCE_MEMBERSHIP); 75 | #endif 76 | #ifdef IP_FREEBIND 77 | define_const(IP_FREEBIND); 78 | #endif 79 | #ifdef IP_HDRINCL 80 | define_const(IP_HDRINCL); 81 | #endif 82 | #ifdef IP_IPSEC_POLICY 83 | define_const(IP_IPSEC_POLICY); 84 | #endif 85 | #ifdef IP_MINTTL 86 | define_const(IP_MINTTL); 87 | #endif 88 | #ifdef IP_MSFILTER 89 | define_const(IP_MSFILTER); 90 | #endif 91 | #ifdef IP_MTU 92 | define_const(IP_MTU); 93 | #endif 94 | #ifdef IP_MTU_DISCOVER 95 | define_const(IP_MTU_DISCOVER); 96 | #endif 97 | #ifdef IP_MULTICAST_ALL 98 | define_const(IP_MULTICAST_ALL); 99 | #endif 100 | #ifdef IP_MULTICAST_IF 101 | define_const(IP_MULTICAST_IF); 102 | #endif 103 | #ifdef IP_MULTICAST_LOOP 104 | define_const(IP_MULTICAST_LOOP); 105 | #endif 106 | #ifdef IP_MULTICAST_TTL 107 | define_const(IP_MULTICAST_TTL); 108 | #endif 109 | #ifdef IP_OPTIONS 110 | define_const(IP_OPTIONS); 111 | #endif 112 | #ifdef IP_ORIGDSTADDR 113 | define_const(IP_ORIGDSTADDR); 114 | #endif 115 | #ifdef IP_PASSSEC 116 | define_const(IP_PASSSEC); 117 | #endif 118 | #ifdef IP_PKTINFO 119 | define_const(IP_PKTINFO); 120 | #endif 121 | #ifdef IP_PKTOPTIONS 122 | define_const(IP_PKTOPTIONS); 123 | #endif 124 | #ifdef IP_PMTUDISC_DO 125 | define_const(IP_PMTUDISC_DO); 126 | #endif 127 | #ifdef IP_PMTUDISC_DONT 128 | define_const(IP_PMTUDISC_DONT); 129 | #endif 130 | #ifdef IP_PMTUDISC_PROBE 131 | define_const(IP_PMTUDISC_PROBE); 132 | #endif 133 | #ifdef IP_PMTUDISC_WANT 134 | define_const(IP_PMTUDISC_WANT); 135 | #endif 136 | #ifdef IP_RECVDSTADDR 137 | define_const(IP_RECVDSTADDR); 138 | #endif 139 | #ifdef IP_RECVERR 140 | define_const(IP_RECVERR); 141 | #endif 142 | #ifdef IP_RECVOPTS 143 | define_const(IP_RECVOPTS); 144 | #endif 145 | #ifdef IP_RECVORIGDSTADDR 146 | define_const(IP_RECVORIGDSTADDR); 147 | #endif 148 | #ifdef IP_RECVRETOPTS 149 | define_const(IP_RECVRETOPTS); 150 | #endif 151 | #ifdef IP_RECVTOS 152 | define_const(IP_RECVTOS); 153 | #endif 154 | #ifdef IP_RECVTTL 155 | define_const(IP_RECVTTL); 156 | #endif 157 | #ifdef IP_RETOPTS 158 | define_const(IP_RETOPTS); 159 | #endif 160 | #ifdef IP_ROUTER_ALERT 161 | define_const(IP_ROUTER_ALERT); 162 | #endif 163 | #ifdef IP_TOS 164 | define_const(IP_TOS); 165 | #endif 166 | #ifdef IP_TRANSPARENT 167 | define_const(IP_TRANSPARENT); 168 | #endif 169 | #ifdef IP_TTL 170 | define_const(IP_TTL); 171 | #endif 172 | #ifdef IP_UNBLOCK_SOURCE 173 | define_const(IP_UNBLOCK_SOURCE); 174 | #endif 175 | #ifdef IP_XFRM_POLICY 176 | define_const(IP_XFRM_POLICY); 177 | #endif 178 | #ifdef IPV6_JOIN_GROUP 179 | define_const(IPV6_JOIN_GROUP); 180 | #endif 181 | #ifdef IPV6_LEAVE_GROUP 182 | define_const(IPV6_LEAVE_GROUP); 183 | #endif 184 | #ifdef IPV6_MULTICAST_HOPS 185 | define_const(IPV6_MULTICAST_HOPS); 186 | #endif 187 | #ifdef IPV6_MULTICAST_IF 188 | define_const(IPV6_MULTICAST_IF); 189 | #endif 190 | #ifdef IPV6_MULTICAST_LOOP 191 | define_const(IPV6_MULTICAST_LOOP); 192 | #endif 193 | #ifdef IPV6_UNICAST_HOPS 194 | define_const(IPV6_UNICAST_HOPS); 195 | #endif 196 | #ifdef IPV6_V6ONLY 197 | define_const(IPV6_V6ONLY); 198 | #endif 199 | #ifdef IPPROTO_AH 200 | define_const(IPPROTO_AH); 201 | #endif 202 | #ifdef IPPROTO_DSTOPTS 203 | define_const(IPPROTO_DSTOPTS); 204 | #endif 205 | #ifdef IPPROTO_ESP 206 | define_const(IPPROTO_ESP); 207 | #endif 208 | #ifdef IPPROTO_FRAGMENT 209 | define_const(IPPROTO_FRAGMENT); 210 | #endif 211 | #ifdef IPPROTO_ICMP 212 | define_const(IPPROTO_ICMP); 213 | #endif 214 | #ifdef IPPROTO_ICMPV6 215 | define_const(IPPROTO_ICMPV6); 216 | #endif 217 | #ifdef IPPROTO_IP 218 | define_const(IPPROTO_IP); 219 | #endif 220 | #ifdef IPPROTO_IPV6 221 | define_const(IPPROTO_IPV6); 222 | #endif 223 | #ifdef IPPROTO_NONE 224 | define_const(IPPROTO_NONE); 225 | #endif 226 | #ifdef IPPROTO_RAW 227 | define_const(IPPROTO_RAW); 228 | #endif 229 | #ifdef IPPROTO_ROUTING 230 | define_const(IPPROTO_ROUTING); 231 | #endif 232 | #ifdef IPPROTO_TCP 233 | define_const(IPPROTO_TCP); 234 | #endif 235 | #ifdef IPPROTO_UDP 236 | define_const(IPPROTO_UDP); 237 | #endif 238 | #ifdef MCAST_BLOCK_SOURCE 239 | define_const(MCAST_BLOCK_SOURCE); 240 | #endif 241 | #ifdef MCAST_JOIN_GROUP 242 | define_const(MCAST_JOIN_GROUP); 243 | #endif 244 | #ifdef MCAST_JOIN_SOURCE_GROUP 245 | define_const(MCAST_JOIN_SOURCE_GROUP); 246 | #endif 247 | #ifdef MCAST_LEAVE_GROUP 248 | define_const(MCAST_LEAVE_GROUP); 249 | #endif 250 | #ifdef MCAST_LEAVE_SOURCE_GROUP 251 | define_const(MCAST_LEAVE_SOURCE_GROUP); 252 | #endif 253 | #ifdef MCAST_MSFILTER 254 | define_const(MCAST_MSFILTER); 255 | #endif 256 | #ifdef MCAST_UNBLOCK_SOURCE 257 | define_const(MCAST_UNBLOCK_SOURCE); 258 | #endif 259 | #ifdef MSG_BCAST 260 | define_const(MSG_BCAST); 261 | #endif 262 | #ifdef MSG_CTRUNC 263 | define_const(MSG_CTRUNC); 264 | #endif 265 | #ifdef MSG_DONTROUTE 266 | define_const(MSG_DONTROUTE); 267 | #endif 268 | #ifdef MSG_DONTWAIT 269 | define_const(MSG_DONTWAIT); 270 | #endif 271 | #ifdef MSG_EOR 272 | define_const(MSG_EOR); 273 | #endif 274 | #ifdef MSG_MCAST 275 | define_const(MSG_MCAST); 276 | #endif 277 | #ifdef MSG_NOSIGNAL 278 | define_const(MSG_NOSIGNAL); 279 | #endif 280 | #ifdef MSG_OOB 281 | define_const(MSG_OOB); 282 | #endif 283 | #ifdef MSG_PEEK 284 | define_const(MSG_PEEK); 285 | #endif 286 | #ifdef MSG_TRUNC 287 | define_const(MSG_TRUNC); 288 | #endif 289 | #ifdef MSG_WAITALL 290 | define_const(MSG_WAITALL); 291 | #endif 292 | #ifdef NI_DGRAM 293 | define_const(NI_DGRAM); 294 | #endif 295 | #ifdef NI_MAXHOST 296 | define_const(NI_MAXHOST); 297 | #endif 298 | #ifdef NI_MAXSERV 299 | define_const(NI_MAXSERV); 300 | #endif 301 | #ifdef NI_NAMEREQD 302 | define_const(NI_NAMEREQD); 303 | #endif 304 | #ifdef NI_NOFQDN 305 | define_const(NI_NOFQDN); 306 | #endif 307 | #ifdef NI_NUMERICHOST 308 | define_const(NI_NUMERICHOST); 309 | #endif 310 | #ifdef NI_NUMERICSERV 311 | define_const(NI_NUMERICSERV); 312 | #endif 313 | #ifdef SHUT_RD 314 | define_const(SHUT_RD); 315 | #endif 316 | #ifdef SHUT_WR 317 | define_const(SHUT_WR); 318 | #endif 319 | #ifdef SHUT_RDWR 320 | define_const(SHUT_RDWR); 321 | #endif 322 | #ifdef SO_BINDANY 323 | define_const(SO_BINDANY); 324 | #endif 325 | #ifdef SO_BROADCAST 326 | define_const(SO_BROADCAST); 327 | #endif 328 | #ifdef SO_DEBUG 329 | define_const(SO_DEBUG); 330 | #endif 331 | #ifdef SO_DONTROUTE 332 | define_const(SO_DONTROUTE); 333 | #endif 334 | #ifdef SO_ERROR 335 | define_const(SO_ERROR); 336 | #endif 337 | #ifdef SO_KEEPALIVE 338 | define_const(SO_KEEPALIVE); 339 | #endif 340 | #ifdef SO_LINGER 341 | define_const(SO_LINGER); 342 | #endif 343 | #ifdef SO_NOSIGPIPE 344 | define_const(SO_NOSIGPIPE); 345 | #endif 346 | #ifdef SO_OOBINLINE 347 | define_const(SO_OOBINLINE); 348 | #endif 349 | #ifdef SO_PEERCRED 350 | define_const(SO_PEERCRED); 351 | #endif 352 | #ifdef SO_RCVBUF 353 | define_const(SO_RCVBUF); 354 | #endif 355 | #ifdef SO_RCVLOWAT 356 | define_const(SO_RCVLOWAT); 357 | #endif 358 | #ifdef SO_RCVTIMEO 359 | define_const(SO_RCVTIMEO); 360 | #endif 361 | #ifdef SO_REUSEADDR 362 | define_const(SO_REUSEADDR); 363 | #endif 364 | #ifdef SO_REUSEPORT 365 | define_const(SO_REUSEPORT); 366 | #endif 367 | #ifdef SO_RTABLE 368 | define_const(SO_RTABLE); 369 | #endif 370 | #ifdef SO_SNDBUF 371 | define_const(SO_SNDBUF); 372 | #endif 373 | #ifdef SO_SNDLOWAT 374 | define_const(SO_SNDLOWAT); 375 | #endif 376 | #ifdef SO_SNDTIMEO 377 | define_const(SO_SNDTIMEO); 378 | #endif 379 | #ifdef SO_SPLICE 380 | define_const(SO_SPLICE); 381 | #endif 382 | #ifdef SO_TIMESTAMP 383 | define_const(SO_TIMESTAMP); 384 | #endif 385 | #ifdef SO_TYPE 386 | define_const(SO_TYPE); 387 | #endif 388 | #ifdef SOCK_DGRAM 389 | define_const(SOCK_DGRAM); 390 | #endif 391 | #ifdef SOCK_RAW 392 | define_const(SOCK_RAW); 393 | #endif 394 | #ifdef SOCK_SEQPACKET 395 | define_const(SOCK_SEQPACKET); 396 | #endif 397 | #ifdef SOCK_STREAM 398 | define_const(SOCK_STREAM); 399 | #endif 400 | #ifdef SOL_SOCKET 401 | define_const(SOL_SOCKET); 402 | #endif 403 | #ifdef SOL_IP 404 | define_const(SOL_IP); 405 | #endif 406 | #ifdef SOL_TCP 407 | define_const(SOL_TCP); 408 | #endif 409 | #ifdef TCP_CONGCTL 410 | define_const(TCP_CONGCTL); 411 | #endif 412 | #ifdef TCP_CONGESTION 413 | define_const(TCP_CONGESTION); 414 | #endif 415 | #ifdef TCP_CORK 416 | define_const(TCP_CORK); 417 | #endif 418 | #ifdef TCP_DEFER_ACCEPT 419 | define_const(TCP_DEFER_ACCEPT); 420 | #endif 421 | #ifdef TCP_INFO 422 | define_const(TCP_INFO); 423 | #endif 424 | #ifdef TCP_KEEPCNT 425 | define_const(TCP_KEEPCNT); 426 | #endif 427 | #ifdef TCP_KEEPIDLE 428 | define_const(TCP_KEEPIDLE); 429 | #endif 430 | #ifdef TCP_KEEPINIT 431 | define_const(TCP_KEEPINIT); 432 | #endif 433 | #ifdef TCP_KEEPINTVL 434 | define_const(TCP_KEEPINTVL); 435 | #endif 436 | #ifdef TCP_LINGER2 437 | define_const(TCP_LINGER2); 438 | #endif 439 | #ifdef TCP_MAXSEG 440 | define_const(TCP_MAXSEG); 441 | #endif 442 | #ifdef TCP_MD5SIG 443 | define_const(TCP_MD5SIG); 444 | #endif 445 | #ifdef TCP_NODELAY 446 | define_const(TCP_NODELAY); 447 | #endif 448 | #ifdef TCP_QUICKACK 449 | define_const(TCP_QUICKACK); 450 | #endif 451 | #ifdef TCP_SACK_ENABLE 452 | define_const(TCP_SACK_ENABLE); 453 | #endif 454 | #ifdef TCP_SYNCNT 455 | define_const(TCP_SYNCNT); 456 | #endif 457 | #ifdef TCP_WINDOW_CLAMP 458 | define_const(TCP_WINDOW_CLAMP); 459 | #endif 460 | -------------------------------------------------------------------------------- /src/const.def: -------------------------------------------------------------------------------- 1 | AF_INET 2 | PF_INET 3 | AF_INET6 4 | PF_INET6 5 | AF_LINK 6 | PF_LINK 7 | AF_LOCAL 8 | PF_LOCAL 9 | AF_UNIX 10 | PF_UNIX 11 | AF_MAX 12 | AF_UNSPEC 13 | PF_UNSPEC 14 | AF_ROUTE 15 | PF_ROUTE 16 | 17 | AI_CANONNAME 18 | AI_FQDN 19 | AI_NUMERICHOST 20 | AI_NUMERICSERV 21 | AI_PASSIVE 22 | 23 | IP_ADD_MEMBERSHIP 24 | IP_ADD_SOURCE_MEMBERSHIP 25 | IP_BLOCK_SOURCE 26 | IP_DROP_MEMBERSHIP 27 | IP_DROP_SOURCE_MEMBERSHIP 28 | IP_FREEBIND 29 | IP_HDRINCL 30 | IP_IPSEC_POLICY 31 | IP_MINTTL 32 | IP_MSFILTER 33 | IP_MTU 34 | IP_MTU_DISCOVER 35 | IP_MULTICAST_ALL 36 | IP_MULTICAST_IF 37 | IP_MULTICAST_LOOP 38 | IP_MULTICAST_TTL 39 | IP_OPTIONS 40 | IP_ORIGDSTADDR 41 | IP_PASSSEC 42 | IP_PKTINFO 43 | IP_PKTOPTIONS 44 | IP_PMTUDISC_DO 45 | IP_PMTUDISC_DONT 46 | IP_PMTUDISC_PROBE 47 | IP_PMTUDISC_WANT 48 | IP_RECVDSTADDR 49 | IP_RECVERR 50 | IP_RECVOPTS 51 | IP_RECVORIGDSTADDR 52 | IP_RECVRETOPTS 53 | IP_RECVTOS 54 | IP_RECVTTL 55 | IP_RETOPTS 56 | IP_ROUTER_ALERT 57 | IP_TOS 58 | IP_TRANSPARENT 59 | IP_TTL 60 | IP_UNBLOCK_SOURCE 61 | IP_XFRM_POLICY 62 | 63 | IPV6_JOIN_GROUP 64 | IPV6_LEAVE_GROUP 65 | IPV6_MULTICAST_HOPS 66 | IPV6_MULTICAST_IF 67 | IPV6_MULTICAST_LOOP 68 | IPV6_UNICAST_HOPS 69 | IPV6_V6ONLY 70 | 71 | IPPROTO_AH 72 | IPPROTO_DSTOPTS 73 | IPPROTO_ESP 74 | IPPROTO_FRAGMENT 75 | IPPROTO_ICMP 76 | IPPROTO_ICMPV6 77 | IPPROTO_IP 78 | IPPROTO_IPV6 79 | IPPROTO_NONE 80 | IPPROTO_RAW 81 | IPPROTO_ROUTING 82 | IPPROTO_TCP 83 | IPPROTO_UDP 84 | 85 | MCAST_BLOCK_SOURCE 86 | MCAST_JOIN_GROUP 87 | MCAST_JOIN_SOURCE_GROUP 88 | MCAST_LEAVE_GROUP 89 | MCAST_LEAVE_SOURCE_GROUP 90 | MCAST_MSFILTER 91 | MCAST_UNBLOCK_SOURCE 92 | 93 | MSG_BCAST 94 | MSG_CTRUNC 95 | MSG_DONTROUTE 96 | MSG_DONTWAIT 97 | MSG_EOR 98 | MSG_MCAST 99 | MSG_NOSIGNAL 100 | MSG_OOB 101 | MSG_PEEK 102 | MSG_TRUNC 103 | MSG_WAITALL 104 | 105 | NI_DGRAM 106 | NI_MAXHOST 107 | NI_MAXSERV 108 | NI_NAMEREQD 109 | NI_NOFQDN 110 | NI_NUMERICHOST 111 | NI_NUMERICSERV 112 | 113 | SHUT_RD 114 | SHUT_WR 115 | SHUT_RDWR 116 | 117 | SO_BINDANY 118 | SO_BROADCAST 119 | SO_DEBUG 120 | SO_DONTROUTE 121 | SO_ERROR 122 | SO_KEEPALIVE 123 | SO_LINGER 124 | SO_NOSIGPIPE 125 | SO_OOBINLINE 126 | SO_PEERCRED 127 | SO_RCVBUF 128 | SO_RCVLOWAT 129 | SO_RCVTIMEO 130 | SO_REUSEADDR 131 | SO_REUSEPORT 132 | SO_RTABLE 133 | SO_SNDBUF 134 | SO_SNDLOWAT 135 | SO_SNDTIMEO 136 | SO_SPLICE 137 | SO_TIMESTAMP 138 | SO_TYPE 139 | 140 | SOCK_DGRAM 141 | SOCK_RAW 142 | SOCK_SEQPACKET 143 | SOCK_STREAM 144 | 145 | SOL_SOCKET 146 | SOL_IP 147 | SOL_TCP 148 | 149 | TCP_CONGCTL 150 | TCP_CONGESTION 151 | TCP_CORK 152 | TCP_DEFER_ACCEPT 153 | TCP_INFO 154 | TCP_KEEPCNT 155 | TCP_KEEPIDLE 156 | TCP_KEEPINIT 157 | TCP_KEEPINTVL 158 | TCP_LINGER2 159 | TCP_MAXSEG 160 | TCP_MD5SIG 161 | TCP_NODELAY 162 | TCP_QUICKACK 163 | TCP_SACK_ENABLE 164 | TCP_SYNCNT 165 | TCP_WINDOW_CLAMP 166 | -------------------------------------------------------------------------------- /src/gen.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | Dir.chdir(File.dirname($0)) 4 | 5 | f = File.open("const.cstub", "w") 6 | 7 | IO.readlines("const.def").each { |name| 8 | name.sub(/^#.*/, "") 9 | name.strip! 10 | next if name.empty? 11 | 12 | f.write < 11 | #include 12 | #include 13 | 14 | #define SHUT_RDWR SD_BOTH 15 | #else 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #endif 27 | 28 | #include 29 | #include 30 | 31 | #include "mruby.h" 32 | #include "mruby/array.h" 33 | #include "mruby/class.h" 34 | #include "mruby/data.h" 35 | #include "mruby/string.h" 36 | #include "mruby/variable.h" 37 | #include "error.h" 38 | 39 | #if !defined(HAVE_SA_LEN) 40 | #if (defined(BSD) && (BSD >= 199006)) 41 | #define HAVE_SA_LEN 1 42 | #else 43 | #define HAVE_SA_LEN 0 44 | #endif 45 | #endif 46 | 47 | #define E_SOCKET_ERROR (mrb_class_get(mrb, "SocketError")) 48 | 49 | #if !defined(mrb_cptr) 50 | #define mrb_cptr_value(m,p) mrb_voidp_value((m),(p)) 51 | #define mrb_cptr(o) mrb_voidp(o) 52 | #define mrb_cptr_p(o) mrb_voidp_p(o) 53 | #endif 54 | 55 | #ifdef _WIN32 56 | static const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) 57 | { 58 | if (af == AF_INET) 59 | { 60 | struct sockaddr_in in; 61 | memset(&in, 0, sizeof(in)); 62 | in.sin_family = AF_INET; 63 | memcpy(&in.sin_addr, src, sizeof(struct in_addr)); 64 | getnameinfo((struct sockaddr *)&in, sizeof(struct 65 | sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); 66 | return dst; 67 | } 68 | else if (af == AF_INET6) 69 | { 70 | struct sockaddr_in6 in; 71 | memset(&in, 0, sizeof(in)); 72 | in.sin6_family = AF_INET6; 73 | memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); 74 | getnameinfo((struct sockaddr *)&in, sizeof(struct 75 | sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); 76 | return dst; 77 | } 78 | return NULL; 79 | } 80 | 81 | static int inet_pton(int af, const char *src, void *dst) 82 | { 83 | struct addrinfo hints, *res, *ressave; 84 | 85 | memset(&hints, 0, sizeof(struct addrinfo)); 86 | hints.ai_family = af; 87 | 88 | if (getaddrinfo(src, NULL, &hints, &res) != 0) 89 | { 90 | printf("Couldn't resolve host %s\n", src); 91 | return -1; 92 | } 93 | 94 | ressave = res; 95 | 96 | while (res) 97 | { 98 | memcpy(dst, res->ai_addr, res->ai_addrlen); 99 | res = res->ai_next; 100 | } 101 | 102 | freeaddrinfo(ressave); 103 | return 0; 104 | } 105 | 106 | #endif 107 | 108 | static mrb_value 109 | mrb_addrinfo_getaddrinfo(mrb_state *mrb, mrb_value klass) 110 | { 111 | struct addrinfo hints, *res0, *res; 112 | mrb_value ai, ary, family, lastai, nodename, protocol, sa, service, socktype; 113 | mrb_int flags; 114 | int arena_idx, error; 115 | const char *hostname = NULL, *servname = NULL; 116 | 117 | ary = mrb_ary_new(mrb); 118 | arena_idx = mrb_gc_arena_save(mrb); /* ary must be on arena! */ 119 | 120 | family = socktype = protocol = mrb_nil_value(); 121 | flags = 0; 122 | mrb_get_args(mrb, "oo|oooi", &nodename, &service, &family, &socktype, &protocol, &flags); 123 | 124 | if (mrb_string_p(nodename)) { 125 | hostname = mrb_str_to_cstr(mrb, nodename); 126 | } else if (mrb_nil_p(nodename)) { 127 | hostname = NULL; 128 | } else { 129 | mrb_raise(mrb, E_TYPE_ERROR, "nodename must be String or nil"); 130 | } 131 | 132 | if (mrb_string_p(service)) { 133 | servname = mrb_str_to_cstr(mrb, service); 134 | } else if (mrb_fixnum_p(service)) { 135 | servname = mrb_str_to_cstr(mrb, mrb_funcall(mrb, service, "to_s", 0)); 136 | } else if (mrb_nil_p(service)) { 137 | servname = NULL; 138 | } else { 139 | mrb_raise(mrb, E_TYPE_ERROR, "service must be String, Fixnum, or nil"); 140 | } 141 | 142 | memset(&hints, 0, sizeof(hints)); 143 | hints.ai_flags = flags; 144 | 145 | if (mrb_fixnum_p(family)) { 146 | hints.ai_family = mrb_fixnum(family); 147 | } 148 | 149 | if (mrb_fixnum_p(socktype)) { 150 | hints.ai_socktype = mrb_fixnum(socktype); 151 | } 152 | 153 | if (mrb_fixnum_p(protocol)) { 154 | hints.ai_protocol = mrb_fixnum(protocol); 155 | } 156 | 157 | lastai = mrb_cv_get(mrb, klass, mrb_intern_lit(mrb, "_lastai")); 158 | if (mrb_cptr_p(lastai)) { 159 | freeaddrinfo(mrb_cptr(lastai)); 160 | mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); 161 | } 162 | 163 | error = getaddrinfo(hostname, servname, &hints, &res0); 164 | if (error) { 165 | mrb_raisef(mrb, E_SOCKET_ERROR, "getaddrinfo: %S", mrb_str_new_cstr(mrb, gai_strerror(error))); 166 | } 167 | mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_cptr_value(mrb, res0)); 168 | 169 | for (res = res0; res != NULL; res = res->ai_next) { 170 | sa = mrb_str_new(mrb, (void *)res->ai_addr, res->ai_addrlen); 171 | ai = mrb_funcall(mrb, klass, "new", 4, sa, mrb_fixnum_value(res->ai_family), mrb_fixnum_value(res->ai_socktype), mrb_fixnum_value(res->ai_protocol)); 172 | mrb_ary_push(mrb, ary, ai); 173 | mrb_gc_arena_restore(mrb, arena_idx); 174 | } 175 | 176 | freeaddrinfo(res0); 177 | mrb_cv_set(mrb, klass, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); 178 | 179 | return ary; 180 | } 181 | 182 | static mrb_value 183 | mrb_addrinfo_getnameinfo(mrb_state *mrb, mrb_value self) 184 | { 185 | mrb_int flags; 186 | mrb_value ary, host, sastr, serv; 187 | int error; 188 | 189 | flags = 0; 190 | mrb_get_args(mrb, "|i", &flags); 191 | host = mrb_str_buf_new(mrb, NI_MAXHOST); 192 | serv = mrb_str_buf_new(mrb, NI_MAXSERV); 193 | 194 | sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr")); 195 | if (!mrb_string_p(sastr)) { 196 | mrb_raise(mrb, E_SOCKET_ERROR, "invalid sockaddr"); 197 | } 198 | error = getnameinfo((struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr), RSTRING_PTR(host), NI_MAXHOST, RSTRING_PTR(serv), NI_MAXSERV, flags); 199 | if (error != 0) { 200 | mrb_raisef(mrb, E_SOCKET_ERROR, "getnameinfo: %s", gai_strerror(error)); 201 | } 202 | ary = mrb_ary_new_capa(mrb, 2); 203 | mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host))); 204 | mrb_ary_push(mrb, ary, host); 205 | mrb_str_resize(mrb, serv, strlen(RSTRING_PTR(serv))); 206 | mrb_ary_push(mrb, ary, serv); 207 | return ary; 208 | } 209 | 210 | #ifndef _WIN32 211 | static mrb_value 212 | mrb_addrinfo_unix_path(mrb_state *mrb, mrb_value self) 213 | { 214 | mrb_value sastr; 215 | 216 | sastr = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@sockaddr")); 217 | if (((struct sockaddr *)RSTRING_PTR(sastr))->sa_family != AF_UNIX) 218 | mrb_raise(mrb, E_SOCKET_ERROR, "need AF_UNIX address"); 219 | if (RSTRING_LEN(sastr) < offsetof(struct sockaddr_un, sun_path) + 1) { 220 | return mrb_str_new(mrb, "", 0); 221 | } else { 222 | return mrb_str_new_cstr(mrb, ((struct sockaddr_un *)RSTRING_PTR(sastr))->sun_path); 223 | } 224 | } 225 | #endif 226 | 227 | static mrb_value 228 | sa2addrlist(mrb_state *mrb, const struct sockaddr *sa, socklen_t salen) 229 | { 230 | mrb_value ary, host; 231 | unsigned short port; 232 | const char *afstr; 233 | 234 | switch (sa->sa_family) { 235 | case AF_INET: 236 | afstr = "AF_INET"; 237 | port = ((struct sockaddr_in *)sa)->sin_port; 238 | break; 239 | case AF_INET6: 240 | afstr = "AF_INET6"; 241 | port = ((struct sockaddr_in6 *)sa)->sin6_port; 242 | break; 243 | default: 244 | mrb_raise(mrb, E_ARGUMENT_ERROR, "bad af"); 245 | return mrb_nil_value(); 246 | } 247 | port = ntohs(port); 248 | host = mrb_str_buf_new(mrb, NI_MAXHOST); 249 | if (getnameinfo(sa, salen, RSTRING_PTR(host), NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == -1) 250 | mrb_sys_fail(mrb, "getnameinfo"); 251 | mrb_str_resize(mrb, host, strlen(RSTRING_PTR(host))); 252 | ary = mrb_ary_new_capa(mrb, 4); 253 | mrb_ary_push(mrb, ary, mrb_str_new_cstr(mrb, afstr)); 254 | mrb_ary_push(mrb, ary, mrb_fixnum_value(port)); 255 | mrb_ary_push(mrb, ary, host); 256 | mrb_ary_push(mrb, ary, host); 257 | return ary; 258 | } 259 | 260 | static int 261 | socket_fd(mrb_state *mrb, mrb_value sock) 262 | { 263 | return mrb_fixnum(mrb_funcall(mrb, sock, "fileno", 0)); 264 | } 265 | 266 | static int 267 | socket_family(int s) 268 | { 269 | struct sockaddr_storage ss; 270 | socklen_t salen; 271 | 272 | salen = sizeof(ss); 273 | if (getsockname(s, (struct sockaddr *)&ss, &salen) == -1) 274 | return AF_UNSPEC; 275 | return ss.ss_family; 276 | } 277 | 278 | static mrb_value 279 | mrb_basicsocket_getpeereid(mrb_state *mrb, mrb_value self) 280 | { 281 | #ifdef HAVE_GETPEEREID 282 | mrb_value ary; 283 | gid_t egid; 284 | uid_t euid; 285 | int s; 286 | 287 | s = socket_fd(mrb, self); 288 | if (getpeereid(s, &euid, &egid) != 0) 289 | mrb_sys_fail(mrb, "getpeereid"); 290 | 291 | ary = mrb_ary_new_capa(mrb, 2); 292 | mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)euid)); 293 | mrb_ary_push(mrb, ary, mrb_fixnum_value((mrb_int)egid)); 294 | return ary; 295 | #else 296 | mrb_raise(mrb, E_RUNTIME_ERROR, "getpeereid is not avaialble on this system"); 297 | return mrb_nil_value(); 298 | #endif 299 | } 300 | 301 | static mrb_value 302 | mrb_basicsocket_getpeername(mrb_state *mrb, mrb_value self) 303 | { 304 | struct sockaddr_storage ss; 305 | socklen_t salen; 306 | 307 | salen = sizeof(ss); 308 | if (getpeername(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0) 309 | mrb_sys_fail(mrb, "getpeername"); 310 | 311 | return mrb_str_new(mrb, (void *)&ss, salen); 312 | } 313 | 314 | static mrb_value 315 | mrb_basicsocket_getsockname(mrb_state *mrb, mrb_value self) 316 | { 317 | struct sockaddr_storage ss; 318 | socklen_t salen; 319 | 320 | salen = sizeof(ss); 321 | if (getsockname(socket_fd(mrb, self), (struct sockaddr *)&ss, &salen) != 0) 322 | mrb_sys_fail(mrb, "getsockname"); 323 | 324 | return mrb_str_new(mrb, (void *)&ss, salen); 325 | } 326 | 327 | static mrb_value 328 | mrb_basicsocket_getsockopt(mrb_state *mrb, mrb_value self) 329 | { 330 | char opt[8]; 331 | int s; 332 | mrb_int family, level, optname; 333 | mrb_value c, data; 334 | socklen_t optlen; 335 | 336 | mrb_get_args(mrb, "ii", &level, &optname); 337 | s = socket_fd(mrb, self); 338 | optlen = sizeof(opt); 339 | if (getsockopt(s, level, optname, opt, &optlen) == -1) 340 | mrb_sys_fail(mrb, "getsockopt"); 341 | c = mrb_const_get(mrb, mrb_obj_value(mrb_class_get(mrb, "Socket")), mrb_intern_lit(mrb, "Option")); 342 | family = socket_family(s); 343 | data = mrb_str_new(mrb, opt, optlen); 344 | return mrb_funcall(mrb, c, "new", 4, mrb_fixnum_value(family), mrb_fixnum_value(level), mrb_fixnum_value(optname), data); 345 | } 346 | 347 | static mrb_value 348 | mrb_basicsocket_recv(mrb_state *mrb, mrb_value self) 349 | { 350 | int n; 351 | mrb_int maxlen, flags = 0; 352 | mrb_value buf; 353 | 354 | mrb_get_args(mrb, "i|i", &maxlen, &flags); 355 | buf = mrb_str_buf_new(mrb, maxlen); 356 | n = recv(socket_fd(mrb, self), RSTRING_PTR(buf), maxlen, flags); 357 | if (n == -1) 358 | mrb_sys_fail(mrb, "recv"); 359 | mrb_str_resize(mrb, buf, n); 360 | return buf; 361 | } 362 | 363 | static mrb_value 364 | mrb_basicsocket_recvfrom(mrb_state *mrb, mrb_value self) 365 | { 366 | int n; 367 | mrb_int maxlen, flags = 0; 368 | mrb_value ary, buf, sa; 369 | socklen_t socklen; 370 | 371 | mrb_get_args(mrb, "i|i", &maxlen, &flags); 372 | buf = mrb_str_buf_new(mrb, maxlen); 373 | socklen = sizeof(struct sockaddr_storage); 374 | sa = mrb_str_buf_new(mrb, socklen); 375 | n = recvfrom(socket_fd(mrb, self), RSTRING_PTR(buf), maxlen, flags, (struct sockaddr *)RSTRING_PTR(sa), &socklen); 376 | if (n == -1) 377 | mrb_sys_fail(mrb, "recvfrom"); 378 | mrb_str_resize(mrb, buf, n); 379 | mrb_str_resize(mrb, sa, socklen); 380 | ary = mrb_ary_new_capa(mrb, 2); 381 | mrb_ary_push(mrb, ary, buf); 382 | mrb_ary_push(mrb, ary, sa); 383 | return ary; 384 | } 385 | 386 | static mrb_value 387 | mrb_basicsocket_send(mrb_state *mrb, mrb_value self) 388 | { 389 | int n; 390 | mrb_int flags; 391 | mrb_value dest, mesg; 392 | 393 | dest = mrb_nil_value(); 394 | mrb_get_args(mrb, "Si|S", &mesg, &flags, &dest); 395 | if (mrb_nil_p(dest)) { 396 | n = send(socket_fd(mrb, self), RSTRING_PTR(mesg), RSTRING_LEN(mesg), flags); 397 | } else { 398 | n = sendto(socket_fd(mrb, self), RSTRING_PTR(mesg), RSTRING_LEN(mesg), flags, (const void *)RSTRING_PTR(dest), RSTRING_LEN(dest)); 399 | } 400 | if (n == -1) 401 | mrb_sys_fail(mrb, "send"); 402 | return mrb_fixnum_value(n); 403 | } 404 | 405 | static mrb_value 406 | mrb_basicsocket_setnonblock(mrb_state *mrb, mrb_value self) 407 | { 408 | int fd, flags; 409 | mrb_value bool; 410 | #ifdef _WIN32 411 | u_long mode = 1; 412 | #endif 413 | 414 | mrb_get_args(mrb, "o", &bool); 415 | fd = socket_fd(mrb, self); 416 | #ifdef _WIN32 417 | flags = ioctlsocket(fd, FIONBIO, &mode); 418 | if (flags != NO_ERROR) 419 | mrb_sys_fail(mrb, "ioctlsocket"); 420 | #else 421 | flags = fcntl(fd, F_GETFL, 0); 422 | if (flags == 1) 423 | mrb_sys_fail(mrb, "fcntl"); 424 | if (mrb_test(bool)) 425 | flags |= O_NONBLOCK; 426 | else 427 | flags &= ~O_NONBLOCK; 428 | if (fcntl(fd, F_SETFL, flags) == -1) 429 | mrb_sys_fail(mrb, "fcntl"); 430 | #endif 431 | return mrb_nil_value(); 432 | } 433 | 434 | static mrb_value 435 | mrb_basicsocket_setsockopt(mrb_state *mrb, mrb_value self) 436 | { 437 | int argc, s; 438 | mrb_int level = 0, optname; 439 | mrb_value optval, so; 440 | 441 | argc = mrb_get_args(mrb, "o|io", &so, &optname, &optval); 442 | if (argc == 3) { 443 | if (!mrb_fixnum_p(so)) { 444 | mrb_raise(mrb, E_ARGUMENT_ERROR, "level is not an integer"); 445 | } 446 | level = mrb_fixnum(so); 447 | if (mrb_string_p(optval)) { 448 | /* that's good */ 449 | } else if (mrb_type(optval) == MRB_TT_TRUE || mrb_type(optval) == MRB_TT_FALSE) { 450 | mrb_int i = mrb_test(optval) ? 1 : 0; 451 | optval = mrb_str_new(mrb, (char *)&i, sizeof(i)); 452 | } else if (mrb_fixnum_p(optval)) { 453 | if (optname == IP_MULTICAST_TTL || optname == IP_MULTICAST_LOOP) { 454 | char uc = mrb_fixnum(optval); 455 | optval = mrb_str_new(mrb, &uc, sizeof(uc)); 456 | } else { 457 | mrb_int i = mrb_fixnum(optval); 458 | optval = mrb_str_new(mrb, (char *)&i, sizeof(i)); 459 | } 460 | } else { 461 | mrb_raise(mrb, E_ARGUMENT_ERROR, "optval should be true, false, an integer, or a string"); 462 | } 463 | } else if (argc == 1) { 464 | if (strcmp(mrb_obj_classname(mrb, so), "Socket::Option") != 0) 465 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "not an instance of Socket::Option"); 466 | level = mrb_fixnum(mrb_funcall(mrb, so, "level", 0)); 467 | optname = mrb_fixnum(mrb_funcall(mrb, so, "optname", 0)); 468 | optval = mrb_funcall(mrb, so, "data", 0); 469 | } else { 470 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%d for 3)", argc); 471 | } 472 | 473 | s = socket_fd(mrb, self); 474 | if (setsockopt(s, level, optname, RSTRING_PTR(optval), RSTRING_LEN(optval)) == -1) 475 | mrb_sys_fail(mrb, "setsockopt"); 476 | return mrb_fixnum_value(0); 477 | } 478 | 479 | static mrb_value 480 | mrb_basicsocket_shutdown(mrb_state *mrb, mrb_value self) 481 | { 482 | mrb_int how = SHUT_RDWR; 483 | 484 | mrb_get_args(mrb, "|i", &how); 485 | if (shutdown(socket_fd(mrb, self), how) != 0) 486 | mrb_sys_fail(mrb, "shutdown"); 487 | return mrb_fixnum_value(0); 488 | } 489 | 490 | static mrb_value 491 | mrb_ipsocket_ntop(mrb_state *mrb, mrb_value klass) 492 | { 493 | mrb_int af, n; 494 | char *addr, buf[50]; 495 | 496 | mrb_get_args(mrb, "is", &af, &addr, &n); 497 | if ((af == AF_INET && n != 4) || (af == AF_INET6 && n != 16)) 498 | mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address"); 499 | if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL) 500 | mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address"); 501 | return mrb_str_new_cstr(mrb, buf); 502 | } 503 | 504 | static mrb_value 505 | mrb_ipsocket_pton(mrb_state *mrb, mrb_value klass) 506 | { 507 | mrb_int af, n; 508 | char *bp, buf[50]; 509 | 510 | mrb_get_args(mrb, "is", &af, &bp, &n); 511 | if (n > sizeof(buf) - 1) 512 | mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address"); 513 | memcpy(buf, bp, n); 514 | buf[n] = '\0'; 515 | 516 | if (af == AF_INET) { 517 | struct in_addr in; 518 | if (inet_pton(AF_INET, buf, (void *)&in.s_addr) != 1) 519 | goto invalid; 520 | return mrb_str_new(mrb, (char *)&in.s_addr, 4); 521 | } else if (af == AF_INET6) { 522 | struct in6_addr in6; 523 | if (inet_pton(AF_INET6, buf, (void *)&in6.s6_addr) != 1) 524 | goto invalid; 525 | return mrb_str_new(mrb, (char *)&in6.s6_addr, 16); 526 | } else 527 | mrb_raise(mrb, E_ARGUMENT_ERROR, "unsupported address family"); 528 | 529 | invalid: 530 | mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid address"); 531 | return mrb_nil_value(); /* dummy */ 532 | } 533 | 534 | static mrb_value 535 | mrb_ipsocket_recvfrom(mrb_state *mrb, mrb_value self) 536 | { 537 | struct sockaddr_storage ss; 538 | socklen_t socklen; 539 | mrb_value a, buf, pair; 540 | mrb_int flags, maxlen, n; 541 | int fd; 542 | 543 | fd = socket_fd(mrb, self); 544 | flags = 0; 545 | mrb_get_args(mrb, "i|i", &maxlen, &flags); 546 | buf = mrb_str_buf_new(mrb, maxlen); 547 | socklen = sizeof(ss); 548 | n = recvfrom(fd, RSTRING_PTR(buf), maxlen, flags, 549 | (struct sockaddr *)&ss, &socklen); 550 | if (n == -1) { 551 | mrb_sys_fail(mrb, "recvfrom"); 552 | } 553 | mrb_str_resize(mrb, buf, n); 554 | a = sa2addrlist(mrb, (struct sockaddr *)&ss, socklen); 555 | pair = mrb_ary_new_capa(mrb, 2); 556 | mrb_ary_push(mrb, pair, buf); 557 | mrb_ary_push(mrb, pair, a); 558 | return pair; 559 | } 560 | 561 | static mrb_value 562 | mrb_socket_gethostname(mrb_state *mrb, mrb_value cls) 563 | { 564 | mrb_value buf; 565 | size_t bufsize; 566 | 567 | #ifdef HOST_NAME_MAX 568 | bufsize = HOST_NAME_MAX + 1; 569 | #else 570 | bufsize = 256; 571 | #endif 572 | buf = mrb_str_buf_new(mrb, bufsize); 573 | if (gethostname(RSTRING_PTR(buf), bufsize) != 0) 574 | mrb_sys_fail(mrb, "gethostname"); 575 | mrb_str_resize(mrb, buf, strlen(RSTRING_PTR(buf))); 576 | return buf; 577 | } 578 | 579 | static mrb_value 580 | mrb_socket_accept(mrb_state *mrb, mrb_value klass) 581 | { 582 | mrb_value ary, sastr; 583 | int s1; 584 | mrb_int s0; 585 | socklen_t socklen; 586 | 587 | mrb_get_args(mrb, "i", &s0); 588 | socklen = sizeof(struct sockaddr_storage); 589 | sastr = mrb_str_buf_new(mrb, socklen); 590 | s1 = accept(s0, (struct sockaddr *)RSTRING_PTR(sastr), &socklen); 591 | if (s1 == -1) { 592 | mrb_sys_fail(mrb, "accept"); 593 | } 594 | // XXX: possible descriptor leakage here! 595 | mrb_str_resize(mrb, sastr, socklen); 596 | ary = mrb_ary_new_capa(mrb, 2); 597 | mrb_ary_push(mrb, ary, mrb_fixnum_value(s1)); 598 | mrb_ary_push(mrb, ary, sastr); 599 | return ary; 600 | } 601 | 602 | static mrb_value 603 | mrb_socket_bind(mrb_state *mrb, mrb_value klass) 604 | { 605 | mrb_value sastr; 606 | mrb_int s; 607 | 608 | mrb_get_args(mrb, "iS", &s, &sastr); 609 | if (bind((int)s, (struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr)) == -1) { 610 | mrb_sys_fail(mrb, "bind"); 611 | } 612 | return mrb_nil_value(); 613 | } 614 | 615 | static mrb_value 616 | mrb_socket_connect(mrb_state *mrb, mrb_value klass) 617 | { 618 | mrb_value sastr; 619 | mrb_int s; 620 | 621 | mrb_get_args(mrb, "iS", &s, &sastr); 622 | if (connect((int)s, (struct sockaddr *)RSTRING_PTR(sastr), (socklen_t)RSTRING_LEN(sastr)) == -1) { 623 | mrb_sys_fail(mrb, "connect"); 624 | } 625 | return mrb_nil_value(); 626 | } 627 | 628 | static mrb_value 629 | mrb_socket_listen(mrb_state *mrb, mrb_value klass) 630 | { 631 | mrb_int backlog, s; 632 | 633 | mrb_get_args(mrb, "ii", &s, &backlog); 634 | if (listen((int)s, (int)backlog) == -1) { 635 | mrb_sys_fail(mrb, "listen"); 636 | } 637 | return mrb_nil_value(); 638 | } 639 | 640 | static mrb_value 641 | mrb_socket_sockaddr_family(mrb_state *mrb, mrb_value klass) 642 | { 643 | const struct sockaddr *sa; 644 | mrb_value str; 645 | 646 | mrb_get_args(mrb, "S", &str); 647 | if (RSTRING_LEN(str) < offsetof(struct sockaddr, sa_family) + sizeof(sa->sa_family)) { 648 | mrb_raisef(mrb, E_SOCKET_ERROR, "invalid sockaddr (too short)"); 649 | } 650 | sa = (const struct sockaddr *)RSTRING_PTR(str); 651 | return mrb_fixnum_value(sa->sa_family); 652 | } 653 | 654 | static mrb_value 655 | mrb_socket_sockaddr_un(mrb_state *mrb, mrb_value klass) 656 | { 657 | #ifdef _WIN32 658 | mrb_raise(mrb, E_NOTIMP_ERROR, "sockaddr_un unsupported on Windows"); 659 | return mrb_nil_value(); 660 | #else 661 | struct sockaddr_un *sunp; 662 | mrb_value path, s; 663 | 664 | mrb_get_args(mrb, "S", &path); 665 | if (RSTRING_LEN(path) > sizeof(sunp->sun_path) - 1) { 666 | mrb_raisef(mrb, E_ARGUMENT_ERROR, "too long unix socket path (max: %ubytes)", (unsigned int)sizeof(sunp->sun_path) - 1); 667 | } 668 | s = mrb_str_buf_new(mrb, sizeof(struct sockaddr_un)); 669 | sunp = (struct sockaddr_un *)RSTRING_PTR(s); 670 | #if HAVE_SA_LEN 671 | sunp->sun_len = sizeof(struct sockaddr_un); 672 | #endif 673 | sunp->sun_family = AF_UNIX; 674 | memcpy(sunp->sun_path, RSTRING_PTR(path), RSTRING_LEN(path)); 675 | sunp->sun_path[RSTRING_LEN(path)] = '\0'; 676 | mrb_str_resize(mrb, s, sizeof(struct sockaddr_un)); 677 | return s; 678 | #endif 679 | } 680 | 681 | static mrb_value 682 | mrb_socket_socketpair(mrb_state *mrb, mrb_value klass) 683 | { 684 | #ifdef _WIN32 685 | mrb_raise(mrb, E_NOTIMP_ERROR, "socketpair unsupported on Windows"); 686 | return mrb_nil_value(); 687 | #else 688 | mrb_value ary; 689 | mrb_int domain, type, protocol; 690 | int sv[2]; 691 | 692 | mrb_get_args(mrb, "iii", &domain, &type, &protocol); 693 | if (socketpair(domain, type, protocol, sv) == -1) { 694 | mrb_sys_fail(mrb, "socketpair"); 695 | } 696 | // XXX: possible descriptor leakage here! 697 | ary = mrb_ary_new_capa(mrb, 2); 698 | mrb_ary_push(mrb, ary, mrb_fixnum_value(sv[0])); 699 | mrb_ary_push(mrb, ary, mrb_fixnum_value(sv[1])); 700 | return ary; 701 | #endif 702 | } 703 | 704 | static mrb_value 705 | mrb_socket_socket(mrb_state *mrb, mrb_value klass) 706 | { 707 | mrb_int domain, type, protocol; 708 | int s; 709 | 710 | mrb_get_args(mrb, "iii", &domain, &type, &protocol); 711 | s = socket(domain, type, protocol); 712 | if (s == -1) 713 | mrb_sys_fail(mrb, "socket"); 714 | return mrb_fixnum_value(s); 715 | } 716 | 717 | static mrb_value 718 | mrb_tcpsocket_allocate(mrb_state *mrb, mrb_value klass) 719 | { 720 | struct RClass *c = mrb_class_ptr(klass); 721 | enum mrb_vtype ttype = MRB_INSTANCE_TT(c); 722 | 723 | /* copied from mrb_instance_alloc() */ 724 | if (ttype == 0) ttype = MRB_TT_OBJECT; 725 | return mrb_obj_value((struct RObject*)mrb_obj_alloc(mrb, ttype, c)); 726 | } 727 | 728 | /* Windows overrides for IO methods on BasicSocket objects. 729 | * This is because sockets on Windows are not the same as file 730 | * descriptors, and thus functions which operate on file descriptors 731 | * will break on socket descriptors. 732 | */ 733 | #ifdef _WIN32 734 | static mrb_value 735 | mrb_win32_basicsocket_close(mrb_state *mrb, mrb_value self) 736 | { 737 | if (closesocket(socket_fd(mrb, self)) != NO_ERROR) 738 | mrb_raise(mrb, E_SOCKET_ERROR, "closesocket unsuccessful"); 739 | return mrb_nil_value(); 740 | } 741 | 742 | #define E_EOF_ERROR (mrb_class_get(mrb, "EOFError")) 743 | static mrb_value 744 | mrb_win32_basicsocket_sysread(mrb_state *mrb, mrb_value self) 745 | { 746 | int sd, ret; 747 | mrb_value buf = mrb_nil_value(); 748 | mrb_int maxlen; 749 | 750 | mrb_get_args(mrb, "i|S", &maxlen, &buf); 751 | if (maxlen < 0) { 752 | return mrb_nil_value(); 753 | } 754 | 755 | if (mrb_nil_p(buf)) { 756 | buf = mrb_str_new(mrb, NULL, maxlen); 757 | } 758 | if (RSTRING_LEN(buf) != maxlen) { 759 | buf = mrb_str_resize(mrb, buf, maxlen); 760 | } 761 | 762 | sd = socket_fd(mrb, self); 763 | ret = recv(sd, RSTRING_PTR(buf), maxlen, 0); 764 | 765 | switch (ret) { 766 | case 0: /* EOF */ 767 | if (maxlen == 0) { 768 | buf = mrb_str_new_cstr(mrb, ""); 769 | } else { 770 | mrb_raise(mrb, E_EOF_ERROR, "sysread failed: End of File"); 771 | } 772 | break; 773 | case SOCKET_ERROR: /* Error */ 774 | mrb_sys_fail(mrb, "recv"); 775 | break; 776 | default: 777 | if (RSTRING_LEN(buf) != ret) { 778 | buf = mrb_str_resize(mrb, buf, ret); 779 | } 780 | break; 781 | } 782 | 783 | return buf; 784 | } 785 | 786 | static mrb_value 787 | mrb_win32_basicsocket_sysseek(mrb_state *mrb, mrb_value self) 788 | { 789 | mrb_raise(mrb, E_NOTIMP_ERROR, "sysseek not implemented for windows sockets"); 790 | return mrb_nil_value(); 791 | } 792 | 793 | static mrb_value 794 | mrb_win32_basicsocket_syswrite(mrb_state *mrb, mrb_value self) 795 | { 796 | int n; 797 | SOCKET sd; 798 | mrb_value str; 799 | 800 | sd = socket_fd(mrb, self); 801 | mrb_get_args(mrb, "S", &str); 802 | n = send(sd, RSTRING_PTR(str), RSTRING_LEN(str), 0); 803 | if (n == SOCKET_ERROR) 804 | mrb_sys_fail(mrb, "send"); 805 | return mrb_fixnum_value(n); 806 | } 807 | 808 | #endif 809 | 810 | void 811 | mrb_mruby_socket_gem_init(mrb_state* mrb) 812 | { 813 | struct RClass *io, *ai, *sock, *bsock, *ipsock, *tcpsock; 814 | struct RClass *constants; 815 | 816 | #ifdef _WIN32 817 | WSADATA wsaData; 818 | int result; 819 | result = WSAStartup(MAKEWORD(2,2), &wsaData); 820 | if (result != NO_ERROR) 821 | mrb_raise(mrb, E_RUNTIME_ERROR, "WSAStartup failed"); 822 | #else 823 | struct RClass *usock; 824 | #endif 825 | 826 | ai = mrb_define_class(mrb, "Addrinfo", mrb->object_class); 827 | mrb_mod_cv_set(mrb, ai, mrb_intern_lit(mrb, "_lastai"), mrb_nil_value()); 828 | mrb_define_class_method(mrb, ai, "getaddrinfo", mrb_addrinfo_getaddrinfo, MRB_ARGS_REQ(2)|MRB_ARGS_OPT(4)); 829 | mrb_define_method(mrb, ai, "getnameinfo", mrb_addrinfo_getnameinfo, MRB_ARGS_OPT(1)); 830 | #ifndef _WIN32 831 | mrb_define_method(mrb, ai, "unix_path", mrb_addrinfo_unix_path, MRB_ARGS_NONE()); 832 | #endif 833 | 834 | io = mrb_class_get(mrb, "IO"); 835 | 836 | bsock = mrb_define_class(mrb, "BasicSocket", io); 837 | mrb_define_method(mrb, bsock, "_recvfrom", mrb_basicsocket_recvfrom, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 838 | mrb_define_method(mrb, bsock, "_setnonblock", mrb_basicsocket_setnonblock, MRB_ARGS_REQ(1)); 839 | mrb_define_method(mrb, bsock, "getpeereid", mrb_basicsocket_getpeereid, MRB_ARGS_NONE()); 840 | mrb_define_method(mrb, bsock, "getpeername", mrb_basicsocket_getpeername, MRB_ARGS_NONE()); 841 | mrb_define_method(mrb, bsock, "getsockname", mrb_basicsocket_getsockname, MRB_ARGS_NONE()); 842 | mrb_define_method(mrb, bsock, "getsockopt", mrb_basicsocket_getsockopt, MRB_ARGS_REQ(2)); 843 | mrb_define_method(mrb, bsock, "recv", mrb_basicsocket_recv, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 844 | // #recvmsg(maxlen, flags=0) 845 | mrb_define_method(mrb, bsock, "send", mrb_basicsocket_send, MRB_ARGS_REQ(2)|MRB_ARGS_OPT(1)); 846 | // #sendmsg 847 | // #sendmsg_nonblock 848 | mrb_define_method(mrb, bsock, "setsockopt", mrb_basicsocket_setsockopt, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(2)); 849 | mrb_define_method(mrb, bsock, "shutdown", mrb_basicsocket_shutdown, MRB_ARGS_OPT(1)); 850 | 851 | ipsock = mrb_define_class(mrb, "IPSocket", bsock); 852 | mrb_define_class_method(mrb, ipsock, "ntop", mrb_ipsocket_ntop, MRB_ARGS_REQ(1)); 853 | mrb_define_class_method(mrb, ipsock, "pton", mrb_ipsocket_pton, MRB_ARGS_REQ(2)); 854 | mrb_define_method(mrb, ipsock, "recvfrom", mrb_ipsocket_recvfrom, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 855 | 856 | tcpsock = mrb_define_class(mrb, "TCPSocket", ipsock); 857 | mrb_define_class_method(mrb, tcpsock, "_allocate", mrb_tcpsocket_allocate, MRB_ARGS_NONE()); 858 | mrb_define_class(mrb, "TCPServer", tcpsock); 859 | 860 | mrb_define_class(mrb, "UDPSocket", ipsock); 861 | //#recvfrom_nonblock 862 | 863 | sock = mrb_define_class(mrb, "Socket", bsock); 864 | mrb_define_class_method(mrb, sock, "_accept", mrb_socket_accept, MRB_ARGS_REQ(1)); 865 | mrb_define_class_method(mrb, sock, "_bind", mrb_socket_bind, MRB_ARGS_REQ(3)); 866 | mrb_define_class_method(mrb, sock, "_connect", mrb_socket_connect, MRB_ARGS_REQ(3)); 867 | mrb_define_class_method(mrb, sock, "_listen", mrb_socket_listen, MRB_ARGS_REQ(2)); 868 | mrb_define_class_method(mrb, sock, "_sockaddr_family", mrb_socket_sockaddr_family, MRB_ARGS_REQ(1)); 869 | mrb_define_class_method(mrb, sock, "_socket", mrb_socket_socket, MRB_ARGS_REQ(3)); 870 | //mrb_define_class_method(mrb, sock, "gethostbyaddr", mrb_socket_gethostbyaddr, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 871 | //mrb_define_class_method(mrb, sock, "gethostbyname", mrb_socket_gethostbyname, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 872 | mrb_define_class_method(mrb, sock, "gethostname", mrb_socket_gethostname, MRB_ARGS_NONE()); 873 | //mrb_define_class_method(mrb, sock, "getservbyname", mrb_socket_getservbyname, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 874 | //mrb_define_class_method(mrb, sock, "getservbyport", mrb_socket_getservbyport, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 875 | mrb_define_class_method(mrb, sock, "sockaddr_un", mrb_socket_sockaddr_un, MRB_ARGS_REQ(1)); 876 | mrb_define_class_method(mrb, sock, "socketpair", mrb_socket_socketpair, MRB_ARGS_REQ(3)); 877 | //mrb_define_method(mrb, sock, "sysaccept", mrb_socket_accept, MRB_ARGS_NONE()); 878 | 879 | #ifndef _WIN32 880 | usock = mrb_define_class(mrb, "UNIXSocket", bsock); 881 | #endif 882 | //mrb_define_class_method(mrb, usock, "pair", mrb_unixsocket_open, MRB_ARGS_OPT(2)); 883 | //mrb_define_class_method(mrb, usock, "socketpair", mrb_unixsocket_open, MRB_ARGS_OPT(2)); 884 | 885 | //mrb_define_method(mrb, usock, "recv_io", mrb_unixsocket_peeraddr, MRB_ARGS_NONE()); 886 | //mrb_define_method(mrb, usock, "recvfrom", mrb_unixsocket_peeraddr, MRB_ARGS_NONE()); 887 | //mrb_define_method(mrb, usock, "send_io", mrb_unixsocket_peeraddr, MRB_ARGS_NONE()); 888 | 889 | /* Windows IO Method Overrides on BasicSocket */ 890 | #ifdef _WIN32 891 | mrb_define_method(mrb, bsock, "close", mrb_win32_basicsocket_close, MRB_ARGS_NONE()); 892 | mrb_define_method(mrb, bsock, "sysread", mrb_win32_basicsocket_sysread, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); 893 | mrb_define_method(mrb, bsock, "sysseek", mrb_win32_basicsocket_sysseek, MRB_ARGS_REQ(1)); 894 | mrb_define_method(mrb, bsock, "syswrite", mrb_win32_basicsocket_syswrite, MRB_ARGS_REQ(1)); 895 | #endif 896 | 897 | constants = mrb_define_module_under(mrb, sock, "Constants"); 898 | 899 | #define define_const(SYM) \ 900 | do { \ 901 | mrb_define_const(mrb, constants, #SYM, mrb_fixnum_value(SYM)); \ 902 | } while (0) 903 | 904 | #include "const.cstub" 905 | } 906 | 907 | void 908 | mrb_mruby_socket_gem_final(mrb_state* mrb) 909 | { 910 | mrb_value ai; 911 | ai = mrb_mod_cv_get(mrb, mrb_class_get(mrb, "Addrinfo"), mrb_intern_lit(mrb, "_lastai")); 912 | if (mrb_cptr_p(ai)) { 913 | freeaddrinfo(mrb_cptr(ai)); 914 | } 915 | #ifdef _WIN32 916 | WSACleanup(); 917 | #endif 918 | } 919 | -------------------------------------------------------------------------------- /test/addrinfo.rb: -------------------------------------------------------------------------------- 1 | assert('Addrinfo') do 2 | assert_equal(Class, Addrinfo.class) 3 | end 4 | 5 | assert('super class of Addrinfo') do 6 | assert_equal(Object, Addrinfo.superclass) 7 | end 8 | 9 | assert('Addrinfo.getaddrinfo') do 10 | ary = Addrinfo.getaddrinfo("localhost", "domain", Socket::AF_INET, Socket::SOCK_STREAM) 11 | assert_true(ary.size >= 1) 12 | ai = ary[0] 13 | assert_equal(ai.afamily, Socket::AF_INET) 14 | assert_equal(ai.pfamily, Socket::PF_INET) 15 | assert_equal(ai.socktype, Socket::SOCK_STREAM) 16 | assert_equal(ai.ip_address, '127.0.0.1') 17 | assert_equal(ai.ip_port, 53) 18 | end 19 | 20 | assert('Addrinfo.foreach') do 21 | # assume Addrinfo.getaddrinfo works well 22 | a = Addrinfo.getaddrinfo("localhost", "domain") 23 | b = [] 24 | Addrinfo.foreach("localhost", "domain") { |ai| b << ai } 25 | assert_equal(a.size, b.size) 26 | end 27 | 28 | assert('Addrinfo.ip') do 29 | ai = Addrinfo.ip('127.0.0.1') 30 | assert_equal('127.0.0.1', ai.ip_address) 31 | assert_equal(Socket::AF_INET, ai.afamily) 32 | assert_equal(0, ai.ip_port) 33 | assert_equal(0, ai.socktype) 34 | assert_equal(0, ai.protocol) 35 | end 36 | 37 | assert('Addrinfo.tcp') do 38 | ai = Addrinfo.tcp('127.0.0.1', 'smtp') 39 | assert_equal('127.0.0.1', ai.ip_address) 40 | assert_equal(Socket::AF_INET, ai.afamily) 41 | assert_equal(25, ai.ip_port) 42 | assert_equal(Socket::SOCK_STREAM, ai.socktype) 43 | assert_equal(Socket::IPPROTO_TCP, ai.protocol) 44 | end 45 | 46 | assert('Addrinfo.udp') do 47 | ai = Addrinfo.udp('127.0.0.1', 'domain') 48 | assert_equal('127.0.0.1', ai.ip_address) 49 | assert_equal(Socket::AF_INET, ai.afamily) 50 | assert_equal(53, ai.ip_port) 51 | assert_equal(Socket::SOCK_DGRAM, ai.socktype) 52 | assert_equal(Socket::IPPROTO_UDP, ai.protocol) 53 | end 54 | 55 | assert('Addrinfo.unix') do 56 | a1 = Addrinfo.unix('/tmp/sock') 57 | assert_true(a1.unix?) 58 | assert_equal('/tmp/sock', a1.unix_path) 59 | assert_equal(Socket::SOCK_STREAM, a1.socktype) 60 | a2 = Addrinfo.unix('/tmp/sock', Socket::SOCK_DGRAM) 61 | assert_equal(Socket::SOCK_DGRAM, a2.socktype) 62 | end 63 | 64 | assert('Addrinfo#afamily') do 65 | ai4 = Addrinfo.new(Socket.sockaddr_in(1, '127.0.0.1')) 66 | ai6 = Addrinfo.new(Socket.sockaddr_in(1, '::1')) 67 | aiu = Addrinfo.new(Socket.sockaddr_un('/tmp/sock')) 68 | assert_equal(Socket::AF_INET, ai4.afamily) 69 | assert_equal(Socket::AF_INET6, ai6.afamily) 70 | assert_equal(Socket::AF_UNIX, aiu.afamily) 71 | end 72 | 73 | # assert('Addrinfo#canonname') do 74 | 75 | # #getnameinfo 76 | # assert('Addrinfo#inspect') do 77 | # assert('Addrinfo#inspect_socket') do 78 | # assert('Addrinfo#ip?') do 79 | # assert('Addrinfo#ip_address') do 80 | # assert('Addrinfo#ip_port') do 81 | # assert('Addrinfo#ip_unpack') do 82 | # assert('Addrinfo#ipv4?') do 83 | # assert('Addrinfo#ipv6?') do 84 | # assert('Addrinfo#pfamily') do 85 | # assert('Addrinfo#protocol') do 86 | # assert('Addrinfo#socktype') do 87 | # assert('Addrinfo#to_sockaddr') do 88 | # assert('Addrinfo#unix?') do 89 | # #unix_path 90 | -------------------------------------------------------------------------------- /test/basicsocket.rb: -------------------------------------------------------------------------------- 1 | assert('BasicSocket') do 2 | assert_equal(Class, BasicSocket.class) 3 | end 4 | 5 | assert('super class of BasicSocket') do 6 | assert_equal(IO, BasicSocket.superclass) 7 | end 8 | 9 | assert('BasicSocket.do_not_reverse_lookup') do 10 | assert_equal(BasicSocket.do_not_reverse_lookup, true) 11 | end 12 | 13 | assert('BasicSocket.do_not_reverse_lookup=') do 14 | BasicSocket.do_not_reverse_lookup = false 15 | assert_equal(BasicSocket.do_not_reverse_lookup, false) 16 | BasicSocket.do_not_reverse_lookup = true 17 | end 18 | -------------------------------------------------------------------------------- /test/ipsocket.rb: -------------------------------------------------------------------------------- 1 | # Note: most of tests below will fail if UDPSocket is broken. 2 | 3 | assert('IPSocket.getaddress') do 4 | l = IPSocket.getaddress("localhost") 5 | assert_true (l == "127.0.0.1" or l == "::1") 6 | end 7 | 8 | assert('IPSocket.addr') do 9 | localhost = "127.0.0.1" 10 | s = UDPSocket.new 11 | s.bind(localhost, 0) 12 | port = Addrinfo.new(s.getsockname).ip_port 13 | 14 | a = s.addr 15 | assert_equal "AF_INET", a[0] 16 | assert_equal port, a[1] 17 | assert_equal localhost, a[2] 18 | assert_equal localhost, a[3] 19 | s.close 20 | true 21 | end 22 | 23 | assert('IPSocket.peeraddr') do 24 | localhost = "127.0.0.1" 25 | server = UDPSocket.new 26 | server.bind(localhost, 0) 27 | port = server.local_address.ip_port 28 | 29 | client = UDPSocket.new 30 | client.connect(localhost, port) 31 | 32 | a = client.peeraddr 33 | assert_equal "AF_INET", a[0] 34 | assert_equal port, a[1] 35 | assert_equal localhost, a[2] 36 | assert_equal localhost, a[3] 37 | client.close 38 | server.close 39 | true 40 | end 41 | -------------------------------------------------------------------------------- /test/socket.rb: -------------------------------------------------------------------------------- 1 | assert('Socket.gethostname') do 2 | assert_true(Socket.gethostname.is_a? String) 3 | end 4 | 5 | assert('Socket::getaddrinfo') do 6 | ret = Socket.getaddrinfo("localhost", "domain", Socket::AF_INET, Socket::SOCK_DGRAM) 7 | assert_true ret.size >= 1 8 | a = ret[0] 9 | assert_equal "AF_INET", a[0] 10 | assert_equal 53, a[1] 11 | # documents says it's a hostname but CRuby returns an address 12 | #assert_equal "127.0.0.1", a[2] 13 | assert_equal "127.0.0.1", a[3] 14 | assert_equal Socket::AF_INET, a[4] 15 | assert_equal Socket::SOCK_DGRAM, a[5] 16 | assert_equal Socket::IPPROTO_UDP, a[6] 17 | end 18 | 19 | assert('Socket#recvfrom') do 20 | begin 21 | sstr = "abcdefg" 22 | s = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) 23 | c = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0) 24 | s.bind(Socket.sockaddr_in(0, "127.0.0.1")) 25 | c.send sstr, 0, s.getsockname 26 | rstr, ai = s.recvfrom sstr.size 27 | 28 | assert_equal sstr, rstr 29 | assert_true "127.0.0.1", ai.ip_address 30 | ensure 31 | s.close rescue nil 32 | c.close rescue nil 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /test/sockettest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mruby.h" 5 | 6 | mrb_value 7 | mrb_sockettest_tmppath(mrb_state *mrb, mrb_value klass) 8 | { 9 | char *tmp = tempnam(NULL, "mruby-socket"); 10 | mrb_value str = mrb_str_new_cstr(mrb, tmp); 11 | free(tmp); 12 | return str; 13 | } 14 | 15 | void 16 | mrb_mruby_socket_gem_test(mrb_state* mrb) 17 | { 18 | struct RClass *c = mrb_define_module(mrb, "SocketTest"); 19 | mrb_define_class_method(mrb, c, "tmppath", mrb_sockettest_tmppath, MRB_ARGS_NONE()); 20 | } 21 | -------------------------------------------------------------------------------- /test/tcpsocket.rb: -------------------------------------------------------------------------------- 1 | #assert('TCPSocket.gethostbyname') do 2 | #assert('TCPSocket.new') do 3 | #assert('TCPSocket#close') do 4 | #assert('TCPSocket#write') do 5 | -------------------------------------------------------------------------------- /test/udpsocket.rb: -------------------------------------------------------------------------------- 1 | assert('UDPSocket.new') do 2 | s = UDPSocket.new 3 | assert_true(s.is_a? UDPSocket) 4 | s.close 5 | s = UDPSocket.new(Socket::AF_INET6) 6 | assert_true(s.is_a? UDPSocket) 7 | s.close 8 | true 9 | end 10 | 11 | #assert('UDPSocket#connect') do 12 | #assert('UDPSocket#send') do 13 | #assert('UDPSocket#recv') do 14 | 15 | #assert('UDPSocket#bind') do 16 | #assert('UDPSocket#recvfrom_nonblock') do 17 | -------------------------------------------------------------------------------- /test/unix.rb: -------------------------------------------------------------------------------- 1 | def unixserver_test_block 2 | path = SocketTest.tmppath 3 | File.unlink path rescue nil 4 | begin 5 | result = yield path 6 | ensure 7 | File.unlink path rescue nil 8 | end 9 | result 10 | end 11 | 12 | def with_unix_server 13 | unixserver_test_block do |path| 14 | UNIXServer.open(path) { |server| 15 | yield path, server 16 | } 17 | end 18 | end 19 | 20 | def with_unix_client 21 | with_unix_server do |path, server| 22 | UNIXSocket.open(path) do |csock| 23 | ssock = server.accept 24 | begin 25 | yield path, server, ssock, csock 26 | ensure 27 | ssock.close unless ssock.closed? rescue nil 28 | end 29 | end 30 | end 31 | end 32 | 33 | assert('UNIXServer.new') do 34 | unixserver_test_block do |path| 35 | server = UNIXServer.new(path) 36 | assert_true server.is_a? UNIXServer 37 | server.close 38 | File.unlink path 39 | 40 | s2 = nil 41 | result = UNIXServer.open(path) { |s1| 42 | assert_true s1.is_a? UNIXServer 43 | s2 = s1 44 | 1234 45 | } 46 | assert_equal 1234, result 47 | assert_true s2.is_a? UNIXServer 48 | assert_true s2.closed? 49 | end 50 | end 51 | 52 | # assert('UNIXServer#accept_nonblock') - would block if fails 53 | 54 | assert('UNIXServer#addr') do 55 | with_unix_server do |path, server| 56 | assert_equal [ "AF_UNIX", path], server.addr 57 | end 58 | end 59 | 60 | assert('UNIXServer#path') do 61 | with_unix_server do |path, server| 62 | assert_equal path, server.path 63 | end 64 | end 65 | 66 | # assert('UNIXServer#peeraddr') - will raise a runtime exception 67 | 68 | assert('UNIXServer#listen') do 69 | with_unix_server do |path, server| 70 | assert_equal 0, server.listen(1) 71 | end 72 | end 73 | 74 | assert('UNIXServer#sysaccept') do 75 | with_unix_server do |path, server| 76 | UNIXSocket.open(path) do |csock| 77 | begin 78 | fd = server.sysaccept 79 | assert_true fd.kind_of? Integer 80 | ensure 81 | IO._sysclose(fd) rescue nil 82 | end 83 | end 84 | end 85 | end 86 | 87 | assert('UNIXSocket.new') do 88 | with_unix_server do |path, server| 89 | c = UNIXSocket.new(path) 90 | assert_true c.is_a? UNIXSocket 91 | c.close 92 | true 93 | end 94 | end 95 | 96 | assert('UNIXSocket#addr') do 97 | with_unix_client do |path, server, ssock, csock| 98 | assert_equal [ "AF_UNIX", path ], ssock.addr 99 | assert_equal [ "AF_UNIX", "" ], csock.addr 100 | end 101 | end 102 | 103 | assert('UNIXSocket#path') do 104 | with_unix_client do |path, server, ssock, csock| 105 | assert_equal path, ssock.path 106 | assert_equal "", csock.path 107 | end 108 | end 109 | 110 | assert('UNIXSocket#peeraddr') do 111 | with_unix_client do |path, server, ssock, csock| 112 | assert_equal [ "AF_UNIX", "" ], ssock.peeraddr 113 | assert_equal [ "AF_UNIX", path ], csock.peeraddr 114 | end 115 | end 116 | 117 | assert('UNIXSocket#recvfrom') do 118 | with_unix_client do |path, server, ssock, csock| 119 | str = "0123456789" 120 | ssock.send str, 0 121 | a = csock.recvfrom(8) 122 | assert_equal str[0, 8], a[0] 123 | assert_equal "AF_UNIX", a[1][0] 124 | # a[1][1] would be "" or something 125 | end 126 | end 127 | --------------------------------------------------------------------------------