- enum SocketOption;
- enum SocketOptionLevel;
- enum SocketType;
- STREAM
- sequential, reliable
- DGRAM
- connectionless unreliable, max length
- SEQPACKET
- sequential, reliable, max length
- enum ProtocolType;
- IP
- default internet protocol (probably 4 for compatibility)
- IPV4
- internet protocol version 4
- IPV6
- internet protocol version 6
- ICMP
- internet control message protocol
- IGMP
- internet group management protocol
- TCP
- transmission control protocol
- PUP
- PARC universal packet protocol
- UDP
- user datagram protocol
- IDP
- Xerox NS protocol
- enum AddressFamily;
- enum SocketShutdown;
- enum SocketFlags;
- OOB
- out of band
- PEEK
- only for receiving
- DONTROUTE
- only for sending
- NOSIGNAL
- inhibit signals
- enum socket_t;
- package socket_t socket(int af, int type, int protocol);
- package int fcntl(socket_t s, int f, ...);
- package uint inet_addr(const(char)* cp);
- package int bind(socket_t s, const(Address.sockaddr)* name, int namelen);
- package int connect(socket_t s, const(Address.sockaddr)* name, int namelen);
- package int listen(socket_t s, int backlog);
- package socket_t accept(socket_t s, Address.sockaddr* addr, int* addrlen);
- package int close(socket_t s);
- package int shutdown(socket_t s, int how);
- package int getpeername(socket_t s, Address.sockaddr* name, int* namelen);
- package int getsockname(socket_t s, Address.sockaddr* name, int* namelen);
- package int send(socket_t s, const(void)* buf, size_t len, int flags);
- package int sendto(socket_t s, const(void)* buf, size_t len, int flags, Address.sockaddr* to, int tolen);
- package int recv(socket_t s, void* buf, size_t len, int flags);
- package int recvfrom(socket_t s, void* buf, size_t len, int flags, Address.sockaddr* from, int* fromlen);
- package int select(int nfds, SocketSet.fd* readfds, SocketSet.fd* writefds, SocketSet.fd* errorfds, SocketSet.timeval* timeout);
- package int getsockopt(socket_t s, int level, int optname, void* optval, int* optlen);
- package int setsockopt(socket_t s, int level, int optname, const(void)* optval, int optlen);
- package int gethostname(void* namebuffer, int buflen);
- package char* inet_ntoa(uint ina);
- package NetHost.hostent* gethostbyname(const(char)* name);
- package NetHost.hostent* gethostbyaddr(const(void)* addr, int len, int type);
- package int getaddrinfo(const(char)* node, const(char)* service, Address.addrinfo* hints, Address.addrinfo** res);
- 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 or
connect. The getaddrinfo() function combines the functionality provided
by the getservbyname and getservbyport functions into a single interface,
but unlike the latter functions, getaddrinfo() is reentrant and allows
programs to eliminate IPv4-versus-IPv6 dependencies. (C) MAN
- package void freeaddrinfo(Address.addrinfo* res);
- The freeaddrinfo() function frees the memory that was allocated for the
dynamically allocated linked list res. (C) MAN
- package int getnameinfo(Address.sockaddr* sa, int salen, char* host, int hostlen, char* serv, int servlen, int flags);
- The getnameinfo() function is the inverse of getaddrinfo: it converts a socket
address to a corresponding host and service, in a protocol-independent manner.
It combines the functionality of gethostbyaddr and getservbyport, but unlike
those functions, getaddrinfo is reentrant and allows programs to eliminate
IPv4-versus-IPv6 dependencies. (C) MAN
- package const(char)* gai_strerror(int errcode);
- The gai_strerror function translates error codes of getaddrinfo, freeaddrinfo
and getnameinfo to a human readable string, suitable for error reporting. (C) MAN
- package const(char)* inet_ntop(int af, const(void)* src, char* dst, int len);
- struct Berkeley;
- void open(AddressFamily family, SocketType type, ProtocolType protocol, bool create = true);
- Configure this instance
- void reopen(socket_t sock = sock.init);
- Open/reopen a native socket for this instance
- void detach();
- calling shutdown() before this is recommended for connection-
oriented sockets
- const const @property socket_t handle();
- Return the underlying OS handle of this Conduit
- const const @property int error();
- Return socket error status
- static @property int lastError();
- Return the last error
- const const @property bool isAlive();
- Is this socket still alive? A closed socket is considered to
be dead, but a shutdown socket is still alive.
- @property AddressFamily addressFamily();
- Berkeley* bind(Address addr);
- Berkeley* connect(Address to);
- Berkeley* listen(int backlog);
- need to bind() first
- void accept(ref Berkeley target);
- need to bind() first
- Berkeley* shutdown(SocketShutdown how);
- The shutdown function shuts down the connection of the socket.
Depending on the argument value, it will:
- stop receiving data for this socket. If further data
arrives, it is rejected.
- stop trying to transmit data from this socket. Also
discards any data waiting to be sent. Stop looking for
acknowledgement of data already sent; don't retransmit
if any data is lost.
- @property Berkeley* linger(int period);
- set linger timeout
- @property Berkeley* addressReuse(bool enabled);
- enable/disable address reuse
- @property Berkeley* noDelay(bool enabled);
- enable/disable noDelay option (nagle)
- void joinGroup(IPv4Address address, bool onOff);
- Helper function to handle the adding and dropping of group
membership.
- const const Address newFamilyObject();
- static @property char[] hostName();
- return the hostname
- static @property uint hostAddress();
- return the default host address (IPv4)
- const const @property Address remoteAddress();
- return the remote address of the current connection (IPv4)
- const const @property Address localAddress();
- return the local address of the current connection (IPv4)
- const const int send(const(void)[] buf, SocketFlags flags = SocketFlags.NONE);
- Send data on the connection. Returns the number of bytes
actually sent, or ERROR on failure. If the socket is blocking
and there is no buffer space left, send waits.
Returns number of bytes actually sent, or -1 on error
- const const int sendTo(const(void)[] buf, SocketFlags flags, Address to);
const const int sendTo(const(void)[] buf, Address to);
- Send data to a specific destination Address. If the
destination address is not specified, a connection
must have been made and that address is used. If the
socket is blocking and there is no buffer space left,
sendTo waits.
- const const int sendTo(const(void)[] buf, SocketFlags flags = SocketFlags.NONE);
- ditto - assumes you connect()ed
- const const int receive(void[] buf, SocketFlags flags = SocketFlags.NONE);
- Receive data on the connection. Returns the number of
bytes actually received, 0 if the remote side has closed
the connection, or ERROR on failure. If the socket is blocking,
receive waits until there is data to be received.
Returns number of bytes actually received, 0 on connection
closure, or -1 on error
- const const int receiveFrom(void[] buf, SocketFlags flags, Address from);
const const int receiveFrom(void[] buf, Address from);
- Receive data and get the remote endpoint Address. Returns
the number of bytes actually received, 0 if the remote side
has closed the connection, or ERROR on failure. If the socket
is blocking, receiveFrom waits until there is data to be
received.
- const const int receiveFrom(void[] buf, SocketFlags flags = SocketFlags.NONE);
- ditto - assumes you connect()ed
- const const int getOption(SocketOptionLevel level, SocketOption option, void[] result);
- returns the length, in bytes, of the actual result - very
different from getsockopt()
- Berkeley* setOption(SocketOptionLevel level, SocketOption option, const(void)[] value);
- const const @property bool blocking();
- getter
- @property void blocking(bool yes);
- setter
- static void exception(immutable(char)[] msg);
- protected static void badArg(immutable(char)[] msg);
- abstract class Address;
- static void exception(immutable(char)[] msg);
- Tango:
added this common function
- static Address create(sockaddr* sa);
- Address factory
- static Address resolve(const(char)[] host, const(char)[] service = null, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- static Address resolve(const(char)[] host, ushort port, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- static Address[] resolveAll(const(char)[] host, const(char)[] service = null, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- static Address[] resolveAll(const(char)[] host, ushort port, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- static Address passive(const(char)[] service, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- static Address passive(ushort port, AddressFamily af = AddressFamily.UNSPEC, AIFlags flags = cast(AIFlags)0);
- @property char[] toAddrString();
- @property char[] toPortString();
- string toString();
- @property AddressFamily addressFamily();
- class UnknownAddress: tango.net.device.Berkeley.Address;
- @property sockaddr* name();
- const const @property int nameLen();
- @property AddressFamily addressFamily();
- string toString();
- class IPv4Address: tango.net.device.Berkeley.Address;
- Examples:
IPv4Address ia = new IPv4Address("63.105.9.61", 80);
assert(ia.toString() == "63.105.9.61:80");
- struct sockaddr_in;
- protected this();
- this(ushort port);
- this(uint addr, ushort port);
- this(const(char)[] addr, ushort port = PORT_ANY);
- -port- can be PORT_ANY
-addr- is an IP address or host name
- this(sockaddr* addr);
- @property sockaddr* name();
- const const @property int nameLen();
- @property AddressFamily addressFamily();
- const const @property ushort port();
- const const @property uint addr();
- const const @property char[] toAddrString();
- const const @property char[] toPortString();
- string toString();
- static uint parse(const(char)[] addr);
- -addr- is an IP address in the format "a.b.c.d"
returns ADDR_NONE on failure
- class IPv6Address: tango.net.device.Berkeley.Address;
- IPv6 is the next-generation Internet Protocol version
designated as the successor to IPv4, the first
implementation used in the Internet that is still in
dominant use currently.
More information: http://ipv6.com/
IPv6 supports 128-bit address space as opposed to 32-bit
address space of IPv4.
IPv6 is written as 8 blocks of 4 octal digits (16 bit)
separated by a colon (":"). Zero block can be replaced by "::".
For example:
0000:0000:0000:0000:0000:0000:0000:0001
is equal
::0001
is equal
::1
is analogue IPv4 127.0.0.1
0000:0000:0000:0000:0000:0000:0000:0000
is equal
::
is analogue IPv4 0.0.0.0
2001:cdba:0000:0000:0000:0000:3257:9652
is equal
2001:cdba::3257:9652
IPv4 address can be submitted through IPv6 as ::ffff:xx.xx.xx.xx,
where xx.xx.xx.xx 32-bit IPv4 addresses.
::ffff:51b0:ec6d
is equal
::ffff:81.176.236.109
is analogue IPv4 81.176.236.109
The URL for the IPv6 address will be of the form:
http://[2001:cdba:0000:0000:0000:0000:3257:9652]/
If needed to specify a port, it will be listed after the
closing square bracket followed by a colon.
http://[2001:cdba:0000:0000:0000:0000:3257:9652]:8080/
address: "2001:cdba:0000:0000:0000:0000:3257:9652"
port: 8080
IPv6Address can be used as well as IPv4Address.
scope addr = new IPv6Address(8080);
address: "::"
port: 8080
scope addr_2 = new IPv6Address("::1", 8081);
address: "::1"
port: 8081
scope addr_3 = new IPv6Address("::1");
address: "::1"
port: PORT_ANY
Also in the IPv6Address constructor can specify the service name
or port as string
scope addr_3 = new IPv6Address("::", "ssh");
address: "::"
port: 22 (ssh service port)
scope addr_4 = new IPv6Address("::", "8080");
address: "::"
port: 8080
Examples:
IPv6Address ia = new IPv6Address("7628:0d18:11a3:09d7:1f34:8a2e:07a0:765d", 8080);
assert(ia.toString() == "[7628:d18:11a3:9d7:1f34:8a2e:7a0:765d]:8080");
//assert(ia.toString() == "[7628:0d18:11a3:09d7:1f34:8a2e:07a0:765d]:8080");
- struct sockaddr_in6;
- protected this();
- protected this(sockaddr* sa);
- protected @property sockaddr* name();
- protected const const @property int nameLen();
- @property AddressFamily addressFamily();
- const const @property ushort port();
- this(int port);
- Create IPv6Address with zero address
- this(const(char)[] addr, int port = PORT_ANY);
- -port- can be PORT_ANY
-addr- is an IP address or host name
- this(const(char)[] addr, const(char)[] service);
- -service- can be a port number or service name
-addr- is an IP address or host name
- @property ubyte[] addr();
- const const @property char[] toAddrString();
- const const @property char[] toPortString();
- string toString();
- class NetHost;
- Examples:
NetHost ih = new NetHost;
ih.getHostByName(Berkeley.hostName());
assert(ih.addrList.length > 0);
IPv4Address ia = new IPv4Address(ih.addrList[0], IPv4Address.PORT_ANY);
Stdout.formatln("addrses: {} {}\n", ia.toAddrString, ih.name);
Stdout.formatln("IP address = {}\nname = {}\n", ia.toAddrString(), ih.name);
foreach(int i, const(char)[] s; ih.aliases)
{
Stdout.formatln("aliases[%d] = {}\n", i, s);
}
Stdout("---\n");
assert(ih.getHostByAddr(ih.addrList[0]));
Stdout.formatln("name = {}\n", ih.name);
foreach(int i, const(char)[] s; ih.aliases)
{
Stdout.formatln("aliases[{}] = {}\n", i, s);
}
- struct hostent;
- protected void validHostent(hostent* he);
- void populate(hostent* he);
- bool getHostByName(const(char)[] name);
- bool getHostByAddr(uint addr);
- bool getHostByAddr(const(char)[] addr);
- class SocketSet;
- a set of sockets for Berkeley.select()
- static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, timeval* tv);
- SocketSet's are updated to include only those sockets which an
event occured.
Returns the number of events, 0 on timeout, or -1 on error
for a connect()ing socket, writeability means connected
for a listen()ing socket, readability means listening
Winsock:
possibly internally limited to 64 sockets per set
- static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError, long microseconds);
- select with specified timeout
- static int select(SocketSet checkRead, SocketSet checkWrite, SocketSet checkError);
- select with maximum timeout