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
}
#endif
/*
* Document-class: ::Socket::AncillaryData
*
* Socket::AncillaryData represents the ancillary data (control information)
* used by sendmsg and recvmsg system call.
* It contains socket family, cmsg level, cmsg type and cmsg data.
*/
void
rsock_init_ancdata(void)
{
#if defined(HAVE_ST_MSG_CONTROL)
/*
* Document-class: Socket::AncillaryData
*
* Socket::AncillaryData represents the ancillary data (control information)
* used by sendmsg and recvmsg system call.
* It contains socket family, cmsg level, cmsg type and cmsg data.
*/
rb_cAncillaryData = rb_define_class_under(rb_cSocket, "AncillaryData", rb_cObject);
rb_define_method(rb_cAncillaryData, "initialize", ancillary_initialize, 4);
rb_define_method(rb_cAncillaryData, "inspect", ancillary_inspect, 0);
ext/socket/basicsocket.c
return val;
}
/*
* BasicSocket is the super class for the all socket classes.
*/
void
rsock_init_basicsocket(void)
{
/*
* Document-class: BasicSocket < IO
*
* BasicSocket is the super class for all the socket classes.
*/
rb_cBasicSocket = rb_define_class("BasicSocket", rb_cIO);
rb_undef_method(rb_cBasicSocket, "initialize");
ext/socket/init.c
return ss.ss_family;
}
/*
* SocketError is the error class for socket.
*/
void
rsock_init_socket_init()
{
/*
* SocketError is the error class for socket.
*/
rb_eSocket = rb_define_class("SocketError", rb_eStandardError);
rsock_init_ipsocket();
rsock_init_tcpsocket();
ext/socket/ipsocket.c
return rsock_make_ipaddr((struct sockaddr*)&addr);
}
/*
* Document-class: ::IPSocket < BasicSocket
*
* IPSocket is the super class of TCPSocket and UDPSocket.
*/
void
rsock_init_ipsocket(void)
{
/*
* Document-class: IPSocket < BasicSocket
*
* IPSocket is the super class of TCPSocket and UDPSocket.
*/
rb_cIPSocket = rb_define_class("IPSocket", rb_cBasicSocket);
rb_define_method(rb_cIPSocket, "addr", ip_addr, -1);
rb_define_method(rb_cIPSocket, "peeraddr", ip_peeraddr, -1);
ext/socket/lib/socket.rb
# UDP/IP address information used by Socket.udp_server_loop.
class UDPSource
# _remote_adress_ is a Addrinfo object.
#
# _local_adress_ is a Addrinfo object.
#
# _reply_proc_ is a Proc used to send reply back to source.
def initialize(remote_address, local_address, &reply_proc)
@remote_address = remote_address
@local_address = local_address
@reply_proc = reply_proc
end
attr_reader :remote_address, :local_address
# :attr_reader: remote_address
# Address of the source
attr_reader :remote_address
# :attr_reader: local_address
# Local address
attr_reader :local_address
# inspection of a UDPSource object.
def inspect
"\#<#{self.class}: #{@remote_address.inspect_sockaddr} to #{@local_address.inspect_sockaddr}>"
end
# :call-seq:
# udp_source.reply(msg)
#
# Respond to UDP source.
#
# _msg_ is a string.
def reply(msg)
@reply_proc.call msg
end
ext/socket/mkconstants.rb
<%= INTERN_DEFS.map {|vardef, gen_hash, decl, func| vardef }.join("\n") %>
/*
* Document-module: ::Socket::Constants
*
* Socket::Constants provides socket related constants.
* Following lists possible constants.
* If underlying platform doesn't define a constant,
* the corresponding Ruby constant is not defined.
*
*/
static void
init_constants(void)
{
/*
* Document-module: ::Socket::Constants
*
* Socket::Constants provides socket related constants.
* Following lists possible constants.
* If underlying platform doesn't define a constant,
* the corresponding Ruby constant is not defined.
*
*/
rb_mSockConst = rb_define_module_under(rb_cSocket, "Constants");
<%= gen_const_defs %>
ext/socket/option.c
return rb_funcall(sockopt_data(self), rb_intern("unpack"), 1, template);
}
/*
* Document-class: ::Socket::Option
*
* Socket::Option represents a socket option used by getsockopt and setsockopt
* system call.
* It contains socket family, protocol level, option name and option value.
*/
void
rsock_init_sockopt(void)
{
/*
* Document-class: Socket::Option
*
* Socket::Option represents a socket option used by getsockopt and setsockopt
* system call.
* It contains socket family, protocol level, option name and option value.
*/
rb_cSockOpt = rb_define_class_under(rb_cSocket, "Option", rb_cObject);
rb_define_method(rb_cSockOpt, "initialize", sockopt_initialize, 4);
rb_define_method(rb_cSockOpt, "family", sockopt_family_m, 0);
ext/socket/raddrinfo.c
void
rsock_init_addrinfo(void)
{
/*
* Addrinfo class maps the +addrinfo+ struct. This structure identifies an Internet host and a service.
*/
rb_cAddrinfo = rb_define_class("Addrinfo", rb_cData);
rb_define_alloc_func(rb_cAddrinfo, addrinfo_s_allocate);
rb_define_method(rb_cAddrinfo, "initialize", addrinfo_initialize, -1);
ext/socket/socket.c
#define socket_s_ip_address_list rb_f_notimplement
#endif
/*
* Document-class: ::Socket < BasicSocket
*
* Class +Socket+ provides access to the underlying operating system
* socket implementations. It can be used to provide more operating system
* specific functionality than the protocol-specific socket classes.
*
* The constants defined under Socket::Constants are also defined under Socket.
* For example, Socket::AF_INET is usable as well as Socket::Constants::AF_INET.
* See Socket::Constants for the list of constants.
*
* === Exception Handling
* Ruby's implementation of +Socket+ causes an exception to be raised
* based on the error generated by the system dependent implementation.
* This is why the methods are documented in a way that isolate
* Unix-based system exceptions from Windows based exceptions. If more
* information on particular exception is needed please refer to the
* Unix manual pages or the Windows WinSock reference.
*
* === Convenient methods
*
* Although the general way to create socket is Socket.new,
* there are several methods for socket creation for most cases.
*
* * TCP client socket: Socket.tcp, TCPSocket.open
* * TCP server socket: Socket.tcp_server_loop, TCPServer.open
* * UNIX client socket: Socket.unix, UNIXSocket.open
* * UNIX server socket: Socket.unix_server_loop, UNIXServer.open
*
* === Documentation by
* * Zach Dennis
* * Sam Roberts
* * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
*
* Much material in this documentation is taken with permission from
* <em>Programming Ruby</em> from The Pragmatic Bookshelf.
*/
void
Init_socket()
{
rsock_init_basicsocket();
/*
* Document-class: Socket < BasicSocket
*
* Class +Socket+ provides access to the underlying operating system
* socket implementations. It can be used to provide more operating system
* specific functionality than the protocol-specific socket classes.
*
* The constants defined under Socket::Constants are also defined under Socket.
* For example, Socket::AF_INET is usable as well as Socket::Constants::AF_INET.
* See Socket::Constants for the list of constants.
*
* === What's a socket ?
* Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
* within a process, and between processes on the same machine or on different machines. There
* are many types of socket : TCPSocket, UDPSocket or UNIXSocket by example.
*
* Sockets have their own vocabulary :
* *domain*:: the family of protocols : Socket::PF_INET, Socket::PF_INET6,
* Socket::PF_UNIX, etc.
* *type*:: the type of communications between le endpoints, typically
* Socket::SOCK_STREAM or Socket::SOCK_DGRAM.
* *protocol*:: typically zero. This may be used to identify a variant of a
* protocol.
* *hostname*:: the identifier of a network interface :
* * a string (hostname, IPv4 or IPv6 adress, <tt><broadcast></tt> which specifies a
* broadcast address),
* * a zero-length string which specifies INADDR_ANY,
* * an integer (interpreted as binary address in host byte order).
*
* === Quick start
* Some classes, as TCPSocket, UDPSocket or UNIXSocket ease use of sockets of these types.
* # Socket way
* s = Socket.new(Socket::INET, Socket::SOCK_STREAM) # Create a TCP socket. s is a Socket object
* s.connect(Socket.pack_sockaddr_in(80, 'www.google.com'))
*
* # TCPSocket simpler way
* s = TCPSocket.new('www.google.com', 80) # Create a TCP socket. s is a TCPSocket object
*
* A simple server may look like :
* require 'socket'
*
* server = TCPServer.new(2000) # Server bind to port 2000
* loop do
* client = server.accept # Wait for a client to connect
* client.puts "Hello !"
* client.puts "Time is #{Time.now}"
* client.close
* end
*
* A simple client may look like :
* require 'socket'
*
* s = TCPSocket.new('localhost', 2000) # open a TCP socket on localhost on port 2000
*
* while line = s.gets # Read lines from socket
* puts line # and print them
* end
*
* s.close # close socket when done
*
* === Exception Handling
* Ruby's implementation of +Socket+ causes an exception to be raised
* based on the error generated by the system dependent implementation.
* This is why the methods are documented in a way that isolate
* Unix-based system exceptions from Windows based exceptions. If more
* information on particular exception is needed please refer to the
* Unix manual pages or the Windows WinSock reference.
*
* === Convenient methods
*
* Although the general way to create socket is Socket.new,
* there are several methods for socket creation for most cases.
*
* * TCP client socket: Socket.tcp, TCPSocket.open
* * TCP server socket: Socket.tcp_server_loop, TCPServer.open
* * UNIX client socket: Socket.unix, UNIXSocket.open
* * UNIX server socket: Socket.unix_server_loop, UNIXServer.open
*
* === Documentation by
* * Zach Dennis
* * Sam Roberts
* * <em>Programming Ruby</em> from The Pragmatic Bookshelf.
*
* Much material in this documentation is taken with permission from
* <em>Programming Ruby</em> from The Pragmatic Bookshelf.
*/
rb_cSocket = rb_define_class("Socket", rb_cBasicSocket);
rsock_init_socket_init();
ext/socket/sockssocket.c
#include "rubysocket.h"
#ifdef SOCKS
/*
* call-seq:
* SOCKSSocket.new(host, serv) => socket
*
* Opens a SOCKS connection to +host+ via +serv+ SOCKS server.
*
*/
static VALUE
socks_init(VALUE sock, VALUE host, VALUE serv)
{
......
}
#ifdef SOCKS5
/*
* Close a connection.
*
*/
static VALUE
socks_s_close(VALUE sock)
{
......
#endif
#endif
/*
* Document-class: ::SOCKSSocket < TCPSocket
*
* SOCKSSocket class
*/
void
rsock_init_sockssocket(void)
{
#ifdef SOCKS
/*
* Document-class: SOCKSSocket < TCPSocket
*
* SOCKS is an Internet protocol that routes packets between a client and a server
* through a proxy server. SOCKS5, if supported, additionnaly provides authentification
* so only authorized users may access a server.
*/
rb_cSOCKSSocket = rb_define_class("SOCKSSocket", rb_cTCPSocket);
rb_define_method(rb_cSOCKSSocket, "initialize", socks_init, 2);
#ifdef SOCKS5
ext/socket/tcpserver.c
return rsock_s_accept(0, fptr->fd, (struct sockaddr*)&from, &fromlen);
}
/*
* Document-class: ::TCPServer < TCPSocket
*
* TCPServer represents a TCP/IP server socket.
*/
void
rsock_init_tcpserver(void)
{
/*
* Document-class: TCPServer < TCPSocket
*
* TCPServer represents a TCP/IP server socket.
*
* A simple TCP server may look like :
* require 'socket'
*
* server = TCPServer.new(2000) # Server bind to port 2000
* loop do
* client = server.accept # Wait for a client to connect
* client.puts "Hello !"
* client.puts "Time is #{Time.now}"
* client.close
* end
*
* A more usable server (serving multiple clients) :
* require 'socket'
*
* server = TCPServer.new(2000)
* loop do
* Thread.start(server.accept) do |client|
* client.puts "Hello !"
* client.puts "Time is #{Time.now}"
* client.close
* end
* end
*
*/
rb_cTCPServer = rb_define_class("TCPServer", rb_cTCPSocket);
rb_define_method(rb_cTCPServer, "accept", tcp_accept, 0);
rb_define_method(rb_cTCPServer, "accept_nonblock", tcp_accept_nonblock, 0);
ext/socket/tcpsocket.c
tcp_sockaddr);
}
/*
* Document-class: ::TCPSocket < IPSocket
*
* TCPSocket represents a TCP/IP client socket.
*/
void
rsock_init_tcpsocket(void)
{
/*
* Document-class: TCPSocket < IPSocket
*
* TCPSocket represents a TCP/IP client socket.
*
* Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
* within a process, and between processes on the same machine or on different machines. TCP socket
* is one of sockets (as UDPSocket or UNIXSocket).
*
*
*/
rb_cTCPSocket = rb_define_class("TCPSocket", rb_cIPSocket);
rb_define_singleton_method(rb_cTCPSocket, "gethostbyname", tcp_s_gethostbyname, 1);
rb_define_method(rb_cTCPSocket, "initialize", tcp_init, -1);
ext/socket/udpsocket.c
return rsock_s_recvfrom_nonblock(sock, argc, argv, RECV_IP);
}
/*
* Document-class: ::UDPSocket < IPSocket
*
* UDPSocket represents a UDP/IP socket.
*/
void
rsock_init_udpsocket(void)
{
/*
* Document-class: UDPSocket < IPSocket
*
* UDPSocket represents a UDP/IP socket.
*
* Sockets are endpoints of a bidirectionnal communication channel. Sockets can communicate
* within a process, and between processes on the same machine or on different machines. UDP socket
* is one of sockets (as TCPSocket or UNIXSocket).
*
*/
rb_cUDPSocket = rb_define_class("UDPSocket", rb_cIPSocket);
rb_define_method(rb_cUDPSocket, "initialize", udp_init, -1);
rb_define_method(rb_cUDPSocket, "connect", udp_connect, 2);
ext/socket/unixserver.c
#endif
/*
* Document-class: ::UNIXServer < UNIXSocket
*
* UNIXServer represents a UNIX domain stream server socket.
*/
void
rsock_init_unixserver(void)
{
#ifdef HAVE_SYS_UN_H
/*
* Document-class: UNIXServer < UNIXSocket
*
* UNIXServer represents a UNIX domain stream server socket.
*/
rb_cUNIXServer = rb_define_class("UNIXServer", rb_cUNIXSocket);
rb_define_method(rb_cUNIXServer, "initialize", unix_svr_init, 1);
rb_define_method(rb_cUNIXServer, "accept", unix_accept, 0);
ext/socket/unixsocket.c
}
#endif
/*
* Document-class: ::UNIXSocket < BasicSocket
*
* UNIXSocket represents a UNIX domain stream client socket.
*/
void
rsock_init_unixsocket(void)
{
#ifdef HAVE_SYS_UN_H
/*
* Document-class: UNIXSocket < BasicSocket
*
* UNIXSocket represents a UNIX domain stream client socket.
*/
rb_cUNIXSocket = rb_define_class("UNIXSocket", rb_cBasicSocket);
rb_define_method(rb_cUNIXSocket, "initialize", unix_init, 1);
rb_define_method(rb_cUNIXSocket, "path", unix_path, 0);
    (1-1/1)