ext/socket/udpsocket.c

Go to the documentation of this file.
00001 /************************************************
00002 
00003   udpsocket.c -
00004 
00005   created at: Thu Mar 31 12:21:29 JST 1994
00006 
00007   Copyright (C) 1993-2007 Yukihiro Matsumoto
00008 
00009 ************************************************/
00010 
00011 #include "rubysocket.h"
00012 
00013 /*
00014  * call-seq:
00015  *   UDPSocket.new([address_family]) => socket
00016  *
00017  * Creates a new UDPSocket object.
00018  *
00019  * _address_family_ should be an integer, a string or a symbol:
00020  * Socket::AF_INET, "AF_INET", :INET, etc.
00021  *
00022  *   UDPSocket.new                   #=> #<UDPSocket:fd 3>
00023  *   UDPSocket.new(Socket::AF_INET6) #=> #<UDPSocket:fd 4>
00024  *
00025  */
00026 static VALUE
00027 udp_init(int argc, VALUE *argv, VALUE sock)
00028 {
00029     VALUE arg;
00030     int family = AF_INET;
00031     int fd;
00032 
00033     rb_secure(3);
00034     if (rb_scan_args(argc, argv, "01", &arg) == 1) {
00035         family = rsock_family_arg(arg);
00036     }
00037     fd = rsock_socket(family, SOCK_DGRAM, 0);
00038     if (fd < 0) {
00039         rb_sys_fail("socket(2) - udp");
00040     }
00041 
00042     return rsock_init_sock(sock, fd);
00043 }
00044 
00045 struct udp_arg
00046 {
00047     struct rb_addrinfo *res;
00048     int fd;
00049 };
00050 
00051 static VALUE
00052 udp_connect_internal(struct udp_arg *arg)
00053 {
00054     int fd = arg->fd;
00055     struct addrinfo *res;
00056 
00057     for (res = arg->res->ai; res; res = res->ai_next) {
00058         if (rsock_connect(fd, res->ai_addr, res->ai_addrlen, 0) >= 0) {
00059             return Qtrue;
00060         }
00061     }
00062     return Qfalse;
00063 }
00064 
00065 /*
00066  * call-seq:
00067  *   udpsocket.connect(host, port) => 0
00068  *
00069  * Connects _udpsocket_ to _host_:_port_.
00070  *
00071  * This makes possible to send without destination address.
00072  *
00073  *   u1 = UDPSocket.new
00074  *   u1.bind("127.0.0.1", 4913)
00075  *   u2 = UDPSocket.new
00076  *   u2.connect("127.0.0.1", 4913)
00077  *   u2.send "uuuu", 0
00078  *   p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
00079  *
00080  */
00081 static VALUE
00082 udp_connect(VALUE sock, VALUE host, VALUE port)
00083 {
00084     rb_io_t *fptr;
00085     struct udp_arg arg;
00086     VALUE ret;
00087 
00088     rb_secure(3);
00089     arg.res = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
00090     GetOpenFile(sock, fptr);
00091     arg.fd = fptr->fd;
00092     ret = rb_ensure(udp_connect_internal, (VALUE)&arg,
00093                     rsock_freeaddrinfo, (VALUE)arg.res);
00094     if (!ret) rsock_sys_fail_host_port("connect(2)", host, port);
00095     return INT2FIX(0);
00096 }
00097 
00098 /*
00099  * call-seq:
00100  *   udpsocket.bind(host, port) #=> 0
00101  *
00102  * Binds _udpsocket_ to _host_:_port_.
00103  *
00104  *   u1 = UDPSocket.new
00105  *   u1.bind("127.0.0.1", 4913)
00106  *   u1.send "message-to-self", 0, "127.0.0.1", 4913
00107  *   p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]
00108  *
00109  */
00110 static VALUE
00111 udp_bind(VALUE sock, VALUE host, VALUE port)
00112 {
00113     rb_io_t *fptr;
00114     struct rb_addrinfo *res0;
00115     struct addrinfo *res;
00116 
00117     rb_secure(3);
00118     res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
00119     GetOpenFile(sock, fptr);
00120     for (res = res0->ai; res; res = res->ai_next) {
00121         if (bind(fptr->fd, res->ai_addr, res->ai_addrlen) < 0) {
00122             continue;
00123         }
00124         rb_freeaddrinfo(res0);
00125         return INT2FIX(0);
00126     }
00127     rb_freeaddrinfo(res0);
00128 
00129     rsock_sys_fail_host_port("bind(2)", host, port);
00130 
00131     return INT2FIX(0);
00132 }
00133 
00134 /*
00135  * call-seq:
00136  *   udpsocket.send(mesg, flags, host, port)  => numbytes_sent
00137  *   udpsocket.send(mesg, flags, sockaddr_to) => numbytes_sent
00138  *   udpsocket.send(mesg, flags)              => numbytes_sent
00139  *
00140  * Sends _mesg_ via _udpsocket_.
00141  *
00142  * _flags_ should be a bitwise OR of Socket::MSG_* constants.
00143  *
00144  *   u1 = UDPSocket.new
00145  *   u1.bind("127.0.0.1", 4913)
00146  *
00147  *   u2 = UDPSocket.new
00148  *   u2.send "hi", 0, "127.0.0.1", 4913
00149  *
00150  *   mesg, addr = u1.recvfrom(10)
00151  *   u1.send mesg, 0, addr[3], addr[1]
00152  *
00153  *   p u2.recv(100) #=> "hi"
00154  *
00155  */
00156 static VALUE
00157 udp_send(int argc, VALUE *argv, VALUE sock)
00158 {
00159     VALUE flags, host, port;
00160     rb_io_t *fptr;
00161     int n;
00162     struct rb_addrinfo *res0;
00163     struct addrinfo *res;
00164     struct rsock_send_arg arg;
00165 
00166     if (argc == 2 || argc == 3) {
00167         return rsock_bsock_send(argc, argv, sock);
00168     }
00169     rb_scan_args(argc, argv, "4", &arg.mesg, &flags, &host, &port);
00170 
00171     StringValue(arg.mesg);
00172     res0 = rsock_addrinfo(host, port, SOCK_DGRAM, 0);
00173     GetOpenFile(sock, fptr);
00174     arg.fd = fptr->fd;
00175     arg.flags = NUM2INT(flags);
00176     for (res = res0->ai; res; res = res->ai_next) {
00177       retry:
00178         arg.to = res->ai_addr;
00179         arg.tolen = res->ai_addrlen;
00180         rb_thread_fd_writable(arg.fd);
00181         n = (int)BLOCKING_REGION_FD(rsock_sendto_blocking, &arg);
00182         if (n >= 0) {
00183             rb_freeaddrinfo(res0);
00184             return INT2FIX(n);
00185         }
00186         if (rb_io_wait_writable(fptr->fd)) {
00187             goto retry;
00188         }
00189     }
00190     rb_freeaddrinfo(res0);
00191     rsock_sys_fail_host_port("sendto(2)", host, port);
00192     return INT2FIX(n);
00193 }
00194 
00195 /*
00196  * call-seq:
00197  *   udpsocket.recvfrom_nonblock(maxlen) => [mesg, sender_inet_addr]
00198  *   udpsocket.recvfrom_nonblock(maxlen, flags) => [mesg, sender_inet_addr]
00199  *
00200  * Receives up to _maxlen_ bytes from +udpsocket+ using recvfrom(2) after
00201  * O_NONBLOCK is set for the underlying file descriptor.
00202  * If _maxlen_ is omitted, its default value is 65536.
00203  * _flags_ is zero or more of the +MSG_+ options.
00204  * The first element of the results, _mesg_, is the data received.
00205  * The second element, _sender_inet_addr_, is an array to represent the sender address.
00206  *
00207  * When recvfrom(2) returns 0,
00208  * Socket#recvfrom_nonblock returns an empty string as data.
00209  * It means an empty packet.
00210  *
00211  * === Parameters
00212  * * +maxlen+ - the number of bytes to receive from the socket
00213  * * +flags+ - zero or more of the +MSG_+ options
00214  *
00215  * === Example
00216  *      require 'socket'
00217  *      s1 = UDPSocket.new
00218  *      s1.bind("127.0.0.1", 0)
00219  *      s2 = UDPSocket.new
00220  *      s2.bind("127.0.0.1", 0)
00221  *      s2.connect(*s1.addr.values_at(3,1))
00222  *      s1.connect(*s2.addr.values_at(3,1))
00223  *      s1.send "aaa", 0
00224  *      begin # emulate blocking recvfrom
00225  *        p s2.recvfrom_nonblock(10)  #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
00226  *      rescue IO::WaitReadable
00227  *        IO.select([s2])
00228  *        retry
00229  *      end
00230  *
00231  * Refer to Socket#recvfrom for the exceptions that may be thrown if the call
00232  * to _recvfrom_nonblock_ fails.
00233  *
00234  * UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
00235  * including Errno::EWOULDBLOCK.
00236  *
00237  * If the exception is Errno::EWOULDBLOCK or Errno::AGAIN,
00238  * it is extended by IO::WaitReadable.
00239  * So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.
00240  *
00241  * === See
00242  * * Socket#recvfrom
00243  */
00244 static VALUE
00245 udp_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
00246 {
00247     return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_IP);
00248 }
00249 
00250 void
00251 rsock_init_udpsocket(void)
00252 {
00253     /*
00254      * Document-class: UDPSocket < IPSocket
00255      *
00256      * UDPSocket represents a UDP/IP socket.
00257      *
00258      */
00259     rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket);
00260     rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1);
00261     rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2);
00262     rb_define_method(rb_cUDPSocket, "bind", udp_bind, 2);
00263     rb_define_method(rb_cUDPSocket, "send", udp_send, -1);
00264     rb_define_method(rb_cUDPSocket, "recvfrom_nonblock", udp_recvfrom_nonblock, -1);
00265 }
00266 
00267 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7