Project

General

Profile

Feature #5182 ยป 0001-ancdata.c-basicsocket.c-init.c-ipsocket.c-mkconstant.patch

sdaubert (Sylvain Daubert), 08/13/2011 05:28 PM

View differences:

ext/socket/ancdata.c
1774 1774
}
1775 1775
#endif
1776 1776

  
1777
/*
1778
 * Document-class: ::Socket::AncillaryData
1779
 *
1780
 * Socket::AncillaryData represents the ancillary data (control information)
1781
 * used by sendmsg and recvmsg system call.
1782
 * It contains socket family, cmsg level, cmsg type and cmsg data.
1783
 */
1784 1777
void
1785 1778
rsock_init_ancdata(void)
1786 1779
{
1787 1780
#if defined(HAVE_ST_MSG_CONTROL)
1781
    /*
1782
     * Document-class: Socket::AncillaryData
1783
     *
1784
     * Socket::AncillaryData represents the ancillary data (control information)
1785
     * used by sendmsg and recvmsg system call.
1786
     * It contains socket family, cmsg level, cmsg type and cmsg data.
1787
     */
1788 1788
    rb_cAncillaryData = rb_define_class_under(rb_cSocket, "AncillaryData", rb_cObject);
1789 1789
    rb_define_method(rb_cAncillaryData, "initialize", ancillary_initialize, 4);
1790 1790
    rb_define_method(rb_cAncillaryData, "inspect", ancillary_inspect, 0);
ext/socket/basicsocket.c
732 732
    return val;
733 733
}
734 734

  
735
/*
736
 * BasicSocket is the super class for the all socket classes.
737
 */
738 735
void
739 736
rsock_init_basicsocket(void)
740 737
{
738
    /*
739
     * Document-class: BasicSocket < IO
740
     *
741
     * BasicSocket is the super class for all the socket classes.
742
     */
741 743
    rb_cBasicSocket = rb_define_class("BasicSocket", rb_cIO);
742 744
    rb_undef_method(rb_cBasicSocket, "initialize");
743 745

  
ext/socket/init.c
531 531
    return ss.ss_family;
532 532
}
533 533

  
534
/*
535
 * SocketError is the error class for socket.
536
 */
537 534
void
538 535
rsock_init_socket_init()
539 536
{
537
    /*
538
     * SocketError is the error class for socket.
539
     */
540 540
    rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
541 541
    rsock_init_ipsocket();
542 542
    rsock_init_tcpsocket();
ext/socket/ipsocket.c
288 288
    return rsock_make_ipaddr((struct sockaddr*)&addr);
289 289
}
290 290

  
291
/*
292
 * Document-class: ::IPSocket < BasicSocket
293
 *
294
 * IPSocket is the super class of TCPSocket and UDPSocket.
295
 */
296 291
void
297 292
rsock_init_ipsocket(void)
298 293
{
294
    /*
295
     * Document-class: IPSocket < BasicSocket
296
     *
297
     * IPSocket is the super class of TCPSocket and UDPSocket.
298
     */
299 299
    rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
300 300
    rb_define_method(rb_cIPSocket, "addr", ip_addr, -1);
301 301
    rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
ext/socket/lib/socket.rb
643 643

  
644 644
  # UDP/IP address information used by Socket.udp_server_loop.
645 645
  class UDPSource
646
    # _remote_adress_ is a Addrinfo object.
647
    #
648
    # _local_adress_ is a Addrinfo object.
649
    #
650
    # _reply_proc_ is a Proc used to send reply back to source.
646 651
    def initialize(remote_address, local_address, &reply_proc)
647 652
      @remote_address = remote_address
648 653
      @local_address = local_address
649 654
      @reply_proc = reply_proc
650 655
    end
651
    attr_reader :remote_address, :local_address
652

  
656
    # :attr_reader: remote_address
657
    # Address of the source
658
    attr_reader :remote_address
659
    # :attr_reader: local_address
660
    # Local address
661
    attr_reader :local_address
662

  
663
    # inspection of a UDPSource object.
653 664
    def inspect
654 665
      "\#<#{self.class}: #{@remote_address.inspect_sockaddr} to #{@local_address.inspect_sockaddr}>"
655 666
    end
656 667

  
668
    # :call-seq:
669
    #   udp_source.reply(msg)
670
    #
671
    # Respond to UDP source.
672
    #
673
    # _msg_ is a string.
657 674
    def reply(msg)
658 675
      @reply_proc.call msg
659 676
    end
ext/socket/mkconstants.rb
284 284

  
285 285
<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| vardef }.join("\n") %>
286 286

  
287
/*
288
 * Document-module: ::Socket::Constants
289
 *
290
 * Socket::Constants provides socket related constants.
291
 * Following lists possible constants.
292
 * If underlying platform doesn't define a constant,
293
 * the corresponding Ruby constant is not defined.
294
 *
295
 */
296 287
static void
297 288
init_constants(void)
298 289
{
290
    /*
291
     * Document-module: ::Socket::Constants
292
     *
293
     * Socket::Constants provides socket related constants.
294
     * Following lists possible constants.
295
     * If underlying platform doesn't define a constant,
296
     * the corresponding Ruby constant is not defined.
297
     *
298
     */
299 299
    rb_mSockConst = rb_define_module_under(rb_cSocket, "Constants");
300 300

  
301 301
<%= gen_const_defs %>
ext/socket/option.c
888 888
    return rb_funcall(sockopt_data(self), rb_intern("unpack"), 1, template);
889 889
}
890 890

  
891
/*
892
 * Document-class: ::Socket::Option
893
 *
894
 * Socket::Option represents a socket option used by getsockopt and setsockopt
895
 * system call.
896
 * It contains socket family, protocol level, option name and option value.
897
 */
898 891
void
899 892
rsock_init_sockopt(void)
900 893
{
894
    /*
895
     * Document-class: Socket::Option
896
     *
897
     * Socket::Option represents a socket option used by getsockopt and setsockopt
898
     * system call.
899
     * It contains socket family, protocol level, option name and option value.
900
     */
901 901
    rb_cSockOpt = rb_define_class_under(rb_cSocket, "Option", rb_cObject);
902 902
    rb_define_method(rb_cSockOpt, "initialize", sockopt_initialize, 4);
903 903
    rb_define_method(rb_cSockOpt, "family", sockopt_family_m, 0);
ext/socket/raddrinfo.c
2151 2151
void
2152 2152
rsock_init_addrinfo(void)
2153 2153
{
2154
    /*
2155
     * Addrinfo class maps the +addrinfo+ struct. This structure identifies an Internet host and a service.
2156
     */
2154 2157
    rb_cAddrinfo = rb_define_class("Addrinfo", rb_cData);
2155 2158
    rb_define_alloc_func(rb_cAddrinfo, addrinfo_s_allocate);
2156 2159
    rb_define_method(rb_cAddrinfo, "initialize", addrinfo_initialize, -1);
ext/socket/socket.c
1792 1792
#define socket_s_ip_address_list rb_f_notimplement
1793 1793
#endif
1794 1794

  
1795
/*
1796
 * Document-class: ::Socket < BasicSocket
1797
 *
1798
 * Class +Socket+ provides access to the underlying operating system
1799
 * socket implementations. It can be used to provide more operating system
1800
 * specific functionality than the protocol-specific socket classes.
1801
 *
1802
 * The constants defined under Socket::Constants are also defined under Socket.
1803
 * For example, Socket::AF_INET is usable as well as Socket::Constants::AF_INET.
1804
 * See Socket::Constants for the list of constants.
1805
 *
1806
 * === Exception Handling
1807
 * Ruby's implementation of +Socket+ causes an exception to be raised
1808
 * based on the error generated by the system dependent implementation.
1809
 * This is why the methods are documented in a way that isolate
1810
 * Unix-based system exceptions from Windows based exceptions. If more
1811
 * information on particular exception is needed please refer to the
1812
 * Unix manual pages or the Windows WinSock reference.
1813
 *
1814
 * === Convenient methods
1815
 *
1816
 * Although the general way to create socket is Socket.new,
1817
 * there are several methods for socket creation for most cases.
1818
 *
1819
 * * TCP client socket: Socket.tcp, TCPSocket.open
1820
 * * TCP server socket: Socket.tcp_server_loop, TCPServer.open
1821
 * * UNIX client socket: Socket.unix, UNIXSocket.open
1822
 * * UNIX server socket: Socket.unix_server_loop, UNIXServer.open
1823
 *
1824
 * === Documentation by
1825
 * * Zach Dennis
1826
 * * Sam Roberts
1827
 * * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
1828
 *
1829
 * Much material in this documentation is taken with permission from
1830
 * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
1831
 */
1832 1795
void
1833 1796
Init_socket()
1834 1797
{
1835 1798
    rsock_init_basicsocket();
1836 1799

  
1800
    /*
1801
     * Document-class: Socket < BasicSocket
1802
     *
1803
     * Class +Socket+ provides access to the underlying operating system
1804
     * socket implementations. It can be used to provide more operating system
1805
     * specific functionality than the protocol-specific socket classes.
1806
     *
1807
     * The constants defined under Socket::Constants are also defined under Socket.
1808
     * For example, Socket::AF_INET is usable as well as Socket::Constants::AF_INET.
1809
     * See Socket::Constants for the list of constants.
1810
     *
1811
     * === What's a socket ?
1812
     * Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
1813
     * within a process, and between processes on the same machine or on different machines. There
1814
     * are many types of socket : TCPSocket, UDPSocket or UNIXSocket by example.
1815
     *
1816
     * Sockets have their own vocabulary :
1817
     * *domain*:: the family of protocols :  Socket::PF_INET, Socket::PF_INET6,
1818
     *            Socket::PF_UNIX, etc.
1819
     * *type*::   the type of communications between le endpoints, typically 
1820
     *            Socket::SOCK_STREAM or Socket::SOCK_DGRAM.
1821
     * *protocol*:: typically zero. This may be used to identify a variant of a
1822
     *              protocol.
1823
     * *hostname*:: the identifier of a network interface : 
1824
     *              * a string (hostname, IPv4 or IPv6 adress, <tt><broadcast></tt> which specifies a 
1825
     *                broadcast address), 
1826
     *              * a zero-length string which specifies INADDR_ANY,
1827
     *              * an integer (interpreted as binary address in host byte order).
1828
     *
1829
     * === Quick start
1830
     * Some classes, as TCPSocket, UDPSocket or UNIXSocket ease use of sockets of these types.
1831
     *  # Socket way
1832
     *  s = Socket.new(Socket::INET, Socket::SOCK_STREAM) # Create a TCP socket. s is a Socket object
1833
     *  s.connect(Socket.pack_sockaddr_in(80, 'www.google.com'))
1834
     *  
1835
     *  # TCPSocket simpler way
1836
     *  s = TCPSocket.new('www.google.com', 80)           # Create a TCP socket. s is a TCPSocket object
1837
     *
1838
     * A simple server may look like :
1839
     *  require 'socket'
1840
     *  
1841
     *  server = TCPServer.new(2000)   # Server bind to port 2000
1842
     *  loop do
1843
     *    client = server.accept       # Wait for a client to connect
1844
     *    client.puts "Hello !"
1845
     *    client.puts "Time is #{Time.now}"
1846
     *    client.close
1847
     *  end
1848
     *
1849
     * A simple client may look like :
1850
     *  require 'socket'
1851
     *  
1852
     *  s = TCPSocket.new('localhost', 2000) # open a TCP socket on localhost on port 2000
1853
     *  
1854
     *  while line = s.gets                  # Read lines from socket
1855
     *    puts line                          # and print them
1856
     *  end
1857
     *  
1858
     *  s.close                              # close socket when done
1859
     *
1860
     * === Exception Handling
1861
     * Ruby's implementation of +Socket+ causes an exception to be raised
1862
     * based on the error generated by the system dependent implementation.
1863
     * This is why the methods are documented in a way that isolate
1864
     * Unix-based system exceptions from Windows based exceptions. If more
1865
     * information on particular exception is needed please refer to the
1866
     * Unix manual pages or the Windows WinSock reference.
1867
     *
1868
     * === Convenient methods
1869
     *
1870
     * Although the general way to create socket is Socket.new,
1871
     * there are several methods for socket creation for most cases.
1872
     *
1873
     * * TCP client socket: Socket.tcp, TCPSocket.open
1874
     * * TCP server socket: Socket.tcp_server_loop, TCPServer.open
1875
     * * UNIX client socket: Socket.unix, UNIXSocket.open
1876
     * * UNIX server socket: Socket.unix_server_loop, UNIXServer.open
1877
     *
1878
     * === Documentation by
1879
     * * Zach Dennis
1880
     * * Sam Roberts
1881
     * * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
1882
     *
1883
     * Much material in this documentation is taken with permission from
1884
     * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
1885
     */
1837 1886
    rb_cSocket = rb_define_class("Socket", rb_cBasicSocket);
1838 1887

  
1839 1888
    rsock_init_socket_init();
ext/socket/sockssocket.c
11 11
#include "rubysocket.h"
12 12

  
13 13
#ifdef SOCKS
14
/*
15
 * call-seq:
16
 *   SOCKSSocket.new(host, serv) => socket
17
 *
18
 * Opens a SOCKS connection to +host+ via +serv+ SOCKS server.
19
 *
20
 */
14 21
static VALUE
15 22
socks_init(VALUE sock, VALUE host, VALUE serv)
16 23
{
......
25 32
}
26 33

  
27 34
#ifdef SOCKS5
35
/*
36
 * Close a connection.
37
 *
38
 */
28 39
static VALUE
29 40
socks_s_close(VALUE sock)
30 41
{
......
40 51
#endif
41 52
#endif
42 53

  
43
/*
44
 * Document-class: ::SOCKSSocket < TCPSocket
45
 *
46
 * SOCKSSocket class
47
 */
48 54
void
49 55
rsock_init_sockssocket(void)
50 56
{
51 57
#ifdef SOCKS
58
    /*
59
     * Document-class: SOCKSSocket < TCPSocket
60
     *
61
     * SOCKS is an Internet protocol that routes packets between a client and a server 
62
     * through a proxy server. SOCKS5, if supported, additionnaly provides authentification
63
     * so only authorized users may access a server.
64
     */
52 65
    rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket);
53 66
    rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2);
54 67
#ifdef SOCKS5
ext/socket/tcpserver.c
128 128
    return rsock_s_accept(0, fptr->fd, (struct sockaddr*)&from, &fromlen);
129 129
}
130 130

  
131
/*
132
 * Document-class: ::TCPServer < TCPSocket
133
 *
134
 * TCPServer represents a TCP/IP server socket.
135
 */
136 131
void
137 132
rsock_init_tcpserver(void)
138 133
{
134
    /*
135
     * Document-class: TCPServer < TCPSocket
136
     *
137
     * TCPServer represents a TCP/IP server socket.
138
     *
139
     * A simple TCP server may look like :
140
     *  require 'socket'
141
     *  
142
     *  server = TCPServer.new(2000)   # Server bind to port 2000
143
     *  loop do
144
     *    client = server.accept       # Wait for a client to connect
145
     *    client.puts "Hello !"
146
     *    client.puts "Time is #{Time.now}"
147
     *    client.close
148
     *  end
149
     *
150
     * A more usable server (serving multiple clients) :
151
     *  require 'socket'
152
     *  
153
     *  server = TCPServer.new(2000)
154
     *  loop do
155
     *    Thread.start(server.accept) do |client|
156
     *      client.puts "Hello !"
157
     *      client.puts "Time is #{Time.now}"
158
     *      client.close
159
     *    end
160
     *  end
161
     *
162
     */
139 163
    rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket);
140 164
    rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0);
141 165
    rb_define_method(rb_cTCPServer, "accept_nonblock", tcp_accept_nonblock, 0);
ext/socket/tcpsocket.c
55 55
			tcp_sockaddr);
56 56
}
57 57

  
58
/*
59
 * Document-class: ::TCPSocket < IPSocket
60
 *
61
 * TCPSocket represents a TCP/IP client socket.
62
 */
63 58
void
64 59
rsock_init_tcpsocket(void)
65 60
{
61
    /*
62
     * Document-class: TCPSocket < IPSocket
63
     *
64
     * TCPSocket represents a TCP/IP client socket.
65
     *
66
     * Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
67
     * within a process, and between processes on the same machine or on different machines. TCP socket
68
     * is one of sockets (as UDPSocket or UNIXSocket).
69
     *
70
     *
71
     */
66 72
    rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
67 73
    rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
68 74
    rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
ext/socket/udpsocket.c
246 246
    return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_IP);
247 247
}
248 248

  
249
/*
250
 * Document-class: ::UDPSocket < IPSocket
251
 *
252
 * UDPSocket represents a UDP/IP socket.
253
 */
254 249
void
255 250
rsock_init_udpsocket(void)
256 251
{
252
    /*
253
     * Document-class: UDPSocket < IPSocket
254
     *
255
     * UDPSocket represents a UDP/IP socket.
256
     *
257
     * Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
258
     * within a process, and between processes on the same machine or on different machines. UDP socket
259
     * is one of sockets (as TCPSocket or UNIXSocket).
260
     *
261
     */
257 262
    rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket);
258 263
    rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1);
259 264
    rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2);
ext/socket/unixserver.c
135 135

  
136 136
#endif
137 137

  
138
/*
139
 * Document-class: ::UNIXServer < UNIXSocket
140
 *
141
 * UNIXServer represents a UNIX domain stream server socket.
142
 */
143 138
void
144 139
rsock_init_unixserver(void)
145 140
{
146 141
#ifdef HAVE_SYS_UN_H
142
    /*
143
     * Document-class: UNIXServer < UNIXSocket
144
     *
145
     * UNIXServer represents a UNIX domain stream server socket.
146
     */
147 147
    rb_cUNIXServer = rb_define_class("UNIXServer", rb_cUNIXSocket);
148 148
    rb_define_method(rb_cUNIXServer, "initialize", unix_svr_init, 1);
149 149
    rb_define_method(rb_cUNIXServer, "accept", unix_accept, 0);
ext/socket/unixsocket.c
492 492
}
493 493
#endif
494 494

  
495
/*
496
 * Document-class: ::UNIXSocket < BasicSocket
497
 *
498
 * UNIXSocket represents a UNIX domain stream client socket.
499
 */
500 495
void
501 496
rsock_init_unixsocket(void)
502 497
{
503 498
#ifdef HAVE_SYS_UN_H
499
    /*
500
     * Document-class: UNIXSocket < BasicSocket
501
     *
502
     * UNIXSocket represents a UNIX domain stream client socket.
503
     */
504 504
    rb_cUNIXSocket = rb_define_class("UNIXSocket", rb_cBasicSocket);
505 505
    rb_define_method(rb_cUNIXSocket, "initialize", unix_init, 1);
506 506
    rb_define_method(rb_cUNIXSocket, "path", unix_path, 0);
507
-