Name
SPVM::Sys::Socket - Socket System Call
Usage
use Sys::Socket;
use Sys::Socket::Constant as Sock;
my $socket = Sys::Socket->socket(Sock->AF_INET, Sock->SOCK_STREAM, 0);
Description
Sys::Socket
is the class for system calls of socket.
Class Methods
htonl
static method htonl : int ($hostlong : int);
The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
See the detail of the htonl function in the case of Linux.
htons
static method htons : short ($hostshort : short);
The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.
See the detail of the htons function in the case of Linux.
ntohl
static method ntohl : int ($netlong : int);
The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.
See the detail of the ntohl function in the case of Linux.
ntohs
static method ntohs : short ($netshort : short);
The ntohs() function converts the unsigned short integer netshort from network byte order to host byte order.
See the detail of the ntohs function in the case of Linux.
inet_aton
static method inet_aton : int ($cp : string, $inp : Sys::Socket::In_addr);
inet_aton() converts the Internet host address cp from the IPv4 numbers-and-dots notation into binary form (in network byte order) and stores it in the structure that inp points to. inet_aton() returns nonzero if the address is valid, zero if not. The address supplied in cp can have one of the following forms:
See the detail of the inet_aton function in the case of Linux.
The input address(inp) is a Sys::Socket::In_addr object.
The input address(cp) must be defined. Otherwise an exception is thrown.
The output address(inp) must be defined. Otherwise an exception is thrown.
inet_ntoa
static method inet_ntoa : string ($in : Sys::Socket::In_addr);
The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite.
See the detail of the inet_ntoa function in the case of Linux.
The input address(in) is a Sys::Socket::In_addr object.
The input address must be defined. Otherwise an exception is thrown.
inet_pton
static method inet_pton : int ($af : int, $src : string, $dst : object of Sys::Socket::In_addr|Sys::Socket::In6_addr);
This function converts the character string src into a network address structure in the af address family, then copies the network address structure to dst. The af argument must be either AF_INET or AF_INET6.
See the detail of the inet_pton function in the case of Linux.
The output address(dst) is assumed to be Sys::Socket::In_addr or Sys::Socket::In6_addr corresponding to the address family(af).
The input address(src) must be defined. Otherwise an exception is thrown.
The output address(dst) must be defined. Otherwise an exception is thrown.
inet_ntop
static method inet_ntop : mutable string ($af : int, $src : object of Sys::Socket::In_addr|Sys::Socket::In6_addr, $dst : mutable string, $size : int);
This function converts the network address structure src in the af address family into a character string. The resulting string is copied to the buffer pointed to by dst, which must be a non-NULL pointer. The caller specifies the number of bytes available in this buffer in the argument size.
See the detail of the inet_ntop function in the case of Linux.
The input address(src) is assumed to be Sys::Socket::In_addr or Sys::Socket::In6_addr corresponding to the address family(af).
The input address(src) must be defined. Otherwise an exception is thrown.
The output address(dst) must be defined. Otherwise an exception is thrown.
socket
static method socket : int ($domain : int, $type : int, $protocol : int);
socket() creates an endpoint for communication and returns a descriptor. The domain argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>. The currently understood formats include:
See the detail of the socket function in the case of Linux.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
connect
static method connect : int ($sockfd : int, $addr : Sys::Socket::Sockaddr, $addrlen : int);
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr. The addrlen argument specifies the size of addr. The format of the address in addr is determined by the address space of the socket sockfd; see socket(2) for further details.
See the detail of the connect function in the case of Linux.
The address($addr
) is a Sys::Socket::Sockaddr object.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
bind
static method bind : int ($sockfd : int, $addr : Sys::Socket::Sockaddr, $addrlen : int);
When a socket is created with socket(2), it exists in a name space (address family) but has no address assigned to it. bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called "assigning a name to a socket".
See the detail of the bind function in the case of Linux.
The address($addr
) is a Sys::Socket::Sockaddr object.
The address must be defined. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
accept
static method accept : int ($sockfd : int, $addr : Sys::Socket::Sockaddr, $addrlen_ref : int*);
The accept() system call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.
See the detail of the accept function in the case of Linux.
The address($addr
) is a Sys::Socket::Sockaddr object.
The address must be defined. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
listen
static method listen : int ($sockfd : int, $backlog : int);
listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept(2).
See the detail of the listen function in the case of Linux.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
shutdown
static method shutdown : int ($sockfd : int, $how : int);
The shutdown() call causes all or part of a full-duplex connection on the socket associated with sockfd to be shut down. If how is SHUT_RD, further receptions will be disallowed. If how is SHUT_WR, further transmissions will be disallowed. If how is SHUT_RDWR, further receptions and transmissions will be disallowed.
See the detail of the shutdown function in the case of Linux.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
recv
static method recv : int ($sockfd : int, $buf : mutable string, $len : int, $flags : int, $buf_offset : int = 0);
The recv() call is normally used only on a connected socket (see connect(2)) and is identical to recvfrom() with a NULL src_addr argument.
See the detail of the recv function in the case of Linux.
The buffer($buf
must be defined. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
send
static method send : int ($sockfd : int, $buf : string, $len : int, $flags : int, $buf_offset : int = 0);
The send() call may be used only when the socket is in a connected state (so that the intended recipient is known). The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2). Also, the following call
The buffer($buf
must be defined. Otherwise an exception is thrown.
See the detail of the send function in the case of Linux.
sendto
static method sendto : int ($sockfd : int, $buf : string, $len : int, $flags : int, $addr : Sys::Socket::Sockaddr, $addrlen : int, $buf_offset : int = 0);
getpeername
static method getpeername : int ($sockfd : int, $addr : Sys::Socket::Sockaddr, $addrlen_ref : int*);
getpeername() returns the address of the peer connected to the socket sockfd, in the buffer pointed to by addr. The addrlen argument should be initialized to indicate the amount of space pointed to by addr. On return it contains the actual size of the name returned (in bytes). The name is truncated if the buffer provided is too small.
See the detail of the getpeername function in the case of Linux.
The address($addr
) is a Sys::Socket::Sockaddr object.
The address must be defined. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
getsockname
static method getsockname : int ($sockfd : int, $addr : Sys::Socket::Sockaddr, $addrlen_ref : int*);
getsockname() returns the current address to which the socket sockfd is bound, in the buffer pointed to by addr. The addrlen argument should be initialized to indicate the amount of space (in bytes) pointed to by addr. On return it contains the actual size of the socket address.
See the detail of the getsockname function in the case of Linux.
The address($addr
) is a Sys::Socket::Sockaddr object.
The address must be defined. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
socketpair
static method socketpair : int ($domain : int, $type : int, $protocol : int, $sv : int[]);
The socketpair() call creates an unnamed pair of connected sockets in the specified domain, of the specified type, and using the optionally specified protocol. For further details of these arguments, see socket(2).
See the detail of the socketpair function in the case of Linux.
The output of the socket pair(sv) must be defined. Otherwise an exception is thrown.
The length of the output of the socket pair(sv) must be greater than or equal to 2. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
setsockopt
static method setsockopt : int ($sockfd : int, $level : int, $optname : int, $optval : string, $optlen : int);
getsockopt() and setsockopt() manipulate options for the socket referred to by the file descriptor sockfd. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.
See the detail of the setsockopt function in the case of Linux.
The option value must be defined. Otherwise an exception is thrown.
The option length must be greater than or equal to 0. Otherwise an exception is thrown.
The length of the option value must be less than or equal to the option length. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
getsockopt
static method getsockopt : int ($sockfd : int, $level : int, $optname : int, $optval : mutable string, $optlen_ref : int*);
getsockopt() and setsockopt() manipulate options for the socket referred to by the file descriptor sockfd. Options may exist at multiple protocol levels; they are always present at the uppermost socket level.
See the detail of the getsockopt function in the case of Linux.
The option value must be defined. Otherwise an exception is thrown.
The option length must be greater than or equal to 0. Otherwise an exception is thrown.
The length of the option value must be less than or equal to the option length. Otherwise an exception is thrown.
If the system call failed, an exception is thrown with the error code set to the class id of the Error::System class.
$optval is a string. The following is an example to get an int value.
my $optval_int_ref = [0];
Fn->memcpy($result_ref, 0, $optval, 0, 4);
my $optval_int = $optval_int_ref->[0];
getaddrinfo
static method getaddrinfo : int ($node : string, $service : string,
$hints : Sys::Socket::Addrinfo, $res : Sys::Socket::Addrinfo[]);
Given node and service, which identify an Internet host and a service, getaddrinfo() returns one or more addrinfo structures, each of which contains an Internet address that can be specified in a call to bind(2) or connect(2). The getaddrinfo() function combines the functionality provided by the gethostbyname(3) and getservbyname(3) functions into a single interface, but unlike the latter functions, getaddrinfo() is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
See the detail of the getaddrinfo function in the case of Linux.
The hints is a Sys::Socket::Addrinfo object.
The response(res) is an array of the Sys::Socket::Addrinfo.
The response must be defined. Otherwise an exception is thrown.
The length of the array of the response must be greater than or equal to 1. Otherwise an exception is thrown.
If a system error occur, an exception is thrown with the error code set to the class id of the Error::System class.
getnameinfo
static method getnameinfo : int ($sa : Sys::Socket::Sockaddr, $salen : int,
$host : mutable string, $hostlen : int, $serv : mutable string, $servlen : int, $flags : int);
The getnameinfo() function is the inverse of getaddrinfo(3): it converts a socket address to a corresponding host and service, in a protocol-independent manner. It combines the functionality of gethostbyaddr(3) and getservbyport(3), but unlike those functions, getnameinfo() is reentrant and allows programs to eliminate IPv4-versus-IPv6 dependencies.
See the detail of the getnameinfo function in the case of Linux.
The socket address(sa) is a Sys::Socket::Addrinfo object.
If a system error occur, an exception is thrown with the error code set to the class id of the Error::System class.
gai_strerror
static method gai_strerror : string($errcode : int);
The gai_strerror() function translates these error codes to a human readable string, suitable for error reporting.
See the detail of the getnameinfo function in the case of Linux.
close
static method close : int ($fd : int);
Calls the closesocket method in the Sys::Socket class in Windows.
Calls the close method in the the Sys::Socket class in other systems.
socket_errno
static method socket_errno : int ();
Portalbe errno
related to the errors of the socket.
socket_strerror
static method socket_strerror : string ($errno : int, $length : int);
Portalbe strerror
related to the errors of the socket.
sockatmark
static method sockatmark : int ($sockfd : int);
ioctlsocket
static method ioctlsocket : int ($fd : int, $request : int, $request_arg_ref : int[] = undef);
See the ioctlsocket function in Windows.
static method to_child_sockaddr : int ($addr : Sys::Socket::Sockaddr);
to_family_sockaddr
static method to_family_sockaddr : Sys::Socket::Sockaddr ($addr : Sys::Socket::Sockaddr);
Copies $addr and creates a new Sys::Socket::Sockaddr object corresponding to the address family, and returns it.
If address family is AF_INET, returns a Sys::Socket::Sockaddr::In object.
If address family is AF_INET6, returns a Sys::Socket::Sockaddr::In6 object.
If address family is AF_UNIX, returns a Sys::Socket::Sockaddr::Un object.
Exceptions:
$addr must be defined. Otherwise an exception is thrown.
If the address family is not available, an exception is thrown.
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License