26 #ifndef _UCOMMON_SOCKET_H_
27 #define _UCOMMON_SOCKET_H_
29 #ifndef _UCOMMON_TIMERS_H_
33 #ifndef _UCOMMON_LINKED_H_
37 #ifndef _UCOMMON_STRING_H_
46 #define SHUT_RDWR SD_BOTH
47 #define SHUT_WR SD_SEND
48 #define SHUT_RD SD_RECV
51 #include <sys/socket.h>
53 #include <netinet/in.h>
60 #ifndef IPTOS_LOWDELAY
61 #define IPTOS_LOWDELAY 0x10
62 #define IPTOS_THROUGHPUT 0x08
63 #define IPTOS_RELIABILITY 0x04
64 #define IPTOS_MINCOST 0x02
68 #define DEFAULT_FAMILY AF_UNSPEC
70 #define DEFAULT_FAMILY AF_INET
75 typedef struct sockaddr *sockaddr_t;
77 typedef struct sockaddr sockaddr_struct;
93 #if defined(AF_INET6) || defined(__CYGWIN__)
105 struct sockaddr_in6 ipv6;
107 struct sockaddr_in ipv4;
108 struct sockaddr address;
115 struct sockaddr_in ipv4;
116 struct sockaddr address;
120 struct sockaddr_storage
125 char sa_data[
sizeof(
struct sockaddr_in)];
135 #define IPPROTO_DCCP 23
142 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
143 #define DCCP_SOCKOPT_CCID 13
144 #define DCCP_SOCKOPT_TX_CCID 14
145 #define DCCP_SOCKOPT_RX_CCID 15
165 unsigned mask(
const char *cp)
const;
169 unsigned mask(
void)
const;
188 cidr(
const char *
string);
217 static const cidr *find(
const policy *
policy,
const struct sockaddr *address);
226 static const cidr *container(
const policy *
policy,
const struct sockaddr *address);
264 {
return broadcast();}
277 void set(
const char *
string);
284 bool is_member(
const struct sockaddr *address)
const;
326 static struct addrinfo *query(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
359 address(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
373 address(
int family,
const char *hostname,
const char *service = NULL);
381 address(
const char *host,
const char *service,
int type = SOCK_STREAM);
390 address(
const char *hostname,
unsigned service = 0);
401 address(
const address& reference);
412 struct sockaddr *
get(void)
const;
414 inline struct sockaddr *getAddr(
void)
const
417 inline struct sockaddr *operator()(
void)
const
424 inline operator struct sockaddr *()
const
432 struct sockaddr *
get(
int family)
const;
434 inline struct sockaddr *operator()(
int family)
const
435 {
return get(family);}
437 inline operator struct sockaddr_in *()
const
438 {
return (
struct sockaddr_in *)
get(AF_INET);}
441 inline operator struct sockaddr_in6 *()
const
442 {
return (
struct sockaddr_in6 *)
get(AF_INET6);}
449 int family(
void)
const;
455 struct sockaddr *find(
const struct sockaddr *
addr)
const;
482 inline operator bool()
const
483 {
return list != NULL;};
490 {
return list == NULL;};
504 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
512 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
521 void set(
int family,
const char *address,
int type = SOCK_STREAM,
int protocol = 0);
527 void add(sockaddr *address);
534 unsigned insert(
struct addrinfo *address);
541 unsigned remove(
struct addrinfo *address);
548 bool remove(
struct sockaddr *address);
556 bool insert(
struct sockaddr *address);
569 void set(
struct sockaddr *address);
576 void set(
const char *hostname,
unsigned service = 0);
583 static struct sockaddr *
dup(
struct sockaddr *address);
590 static struct sockaddr_in *ipv4(
struct sockaddr *address);
598 static struct sockaddr_in6 *ipv6(
struct sockaddr *address);
602 friend class address;
634 Socket(
int family,
int type,
int protocol = 0);
645 Socket(
const char *address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
661 static void cancel(socket_t socket);
671 inline int err(
void)
const
679 bool is_pending(
unsigned value)
const;
685 bool connected(
void)
const;
700 {
return nodelay(so);};
717 bool waitSending(
timeout_t timeout = 0)
const;
724 {
return pending(so);};
732 {
return broadcast(so, enable);};
740 {
return keepalive(so, enable);};
748 {
return blocking(so, enable);};
756 {
return multicast(so, ttl);};
764 {
return loopback(so, enable);};
778 inline int ttl(
unsigned char time)
779 {
return ttl(so, time);};
787 {
return sendsize(so, size);};
795 {
return sendwait(so, size);};
804 {
return recvsize(so, size);};
811 static int type(socket_t socket);
819 static unsigned segsize(socket_t socket,
unsigned size = 0);
827 static bool ccid(socket_t socket, uint8_t
id);
842 {
return segsize(so, size);};
850 {
return ccid(so,
id);};
861 {
return tos(so, type);};
870 {
return priority(so, scheduling);};
876 {::shutdown(so, SHUT_RDWR);};
885 int connectto(
struct addrinfo *list);
893 int disconnect(
void);
900 int join(
const struct addrinfo *list);
907 int drop(
const struct addrinfo *list);
922 size_t peek(
void *data,
size_t number)
const;
931 size_t readfrom(
void *data,
size_t number,
struct sockaddr_storage *address = NULL);
940 size_t writeto(
const void *data,
size_t number,
const struct sockaddr *address = NULL);
954 size_t readline(
char *data,
size_t size);
961 size_t printf(
const char *format, ...) __PRINTF(2,3);
974 size_t readline(
String& buffer);
987 static ssize_t readline(socket_t socket,
char *data,
size_t size,
timeout_t timeout =
Timer::inf);
995 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
1004 size_t writes(const
char *
string);
1016 bool operator!() const;
1023 Socket& operator=(socket_t socket);
1029 inline operator socket_t()
const
1045 static unsigned pending(socket_t socket);
1053 static int sendsize(socket_t socket,
unsigned size);
1061 static int sendwait(socket_t socket,
unsigned size);
1069 static int recvsize(socket_t socket,
unsigned size);
1079 static int connectto(socket_t socket,
struct addrinfo *list);
1086 static int disconnect(socket_t socket);
1094 static int drop(socket_t socket,
const struct addrinfo *list);
1102 static int join(socket_t socket,
const struct addrinfo *list);
1109 static int error(socket_t socket);
1117 static int multicast(socket_t socket,
unsigned ttl = 1);
1125 static int loopback(socket_t socket,
bool enable);
1133 static int blocking(socket_t socket,
bool enable);
1141 static int keepalive(socket_t socket,
bool enable);
1149 static int broadcast(socket_t socket,
bool enable);
1156 static int nodelay(socket_t socket);
1164 static int priority(socket_t socket,
int scheduling);
1172 static int tos(socket_t socket,
int type);
1180 static int ttl(socket_t socket,
unsigned char time);
1186 static int family(socket_t socket);
1193 inline static int family(
const struct sockaddr_storage& address)
1194 {
return ((
const struct sockaddr *)&address)->sa_family;};
1202 {
return address.address.sa_family;};
1213 static ssize_t recvfrom(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *address = NULL);
1224 static ssize_t sendto(socket_t socket,
const void *buffer,
size_t size,
int flags = 0,
const struct sockaddr *address = NULL);
1235 inline static ssize_t
replyto(socket_t socket,
const void *buffer,
size_t size,
int flags,
const struct sockaddr_storage *address)
1236 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);};
1248 {
return sendto(socket, buffer, size, flags, (
const struct sockaddr *)address);};
1259 static ssize_t recvinet(socket_t socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_internet *address = NULL);
1269 static int bindto(socket_t socket,
const char *address,
const char *service,
int protocol = 0);
1278 static int listento(socket_t socket,
const struct sockaddr *address,
int backlog = 5);
1286 static int bindto(socket_t socket,
const struct sockaddr *address);
1294 static socket_t acceptfrom(socket_t socket,
struct sockaddr_storage *address = NULL);
1303 static socket_t create(
int family,
int type,
int protocol);
1312 static socket_t create(
const struct addrinfo *address,
int type,
int protocol);
1323 static socket_t create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1336 static void release(socket_t socket);
1345 static char *hostname(
const struct sockaddr *address,
char *buffer,
size_t size);
1366 static socklen_t query(socket_t socket,
struct sockaddr_storage *address,
const char *hostname,
const char *service);
1373 static socklen_t len(
const struct sockaddr *address);
1382 static bool equal(
const struct sockaddr *address1,
const struct sockaddr *address2);
1390 static unsigned copy(
struct sockaddr *target,
const struct sockaddr *origin);
1398 inline static unsigned store(
struct sockaddr_storage *storage,
const struct sockaddr *address)
1399 {
return copy((
struct sockaddr*)storage, address);};
1407 static unsigned store(
struct sockaddr_internet *storage,
const struct sockaddr *address);
1416 static bool eq_host(
const struct sockaddr *address1,
const struct sockaddr *address2);
1425 inline static bool eq_from(
const struct sockaddr_storage *address1,
const struct sockaddr_storage *address2)
1426 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);};
1436 {
return equal((
const struct sockaddr *)address1, (
const struct sockaddr *)address2);};
1445 static bool eq_subnet(
const struct sockaddr *address1,
const struct sockaddr *address2);
1454 static int via(
struct sockaddr *address,
const struct sockaddr *destination);
1463 static char *query(
const struct sockaddr *address,
char *buffer, socklen_t size);
1470 static short service(
const struct sockaddr *address);
1478 {
return service((
const struct sockaddr *)address);};
1486 static unsigned keyindex(
const struct sockaddr *address,
unsigned size);
1494 static unsigned keyhost(
const struct sockaddr *address,
unsigned size);
1499 static void init(
void);
1505 static void init(
const char *program);
1512 static void query(
int family);
1520 static void v4mapping(
bool enable);
1526 static int error(
void);
1536 static bool is_null(
const char *
string);
1545 static bool is_numeric(
const char *
string);
1555 static int local(socket_t socket,
struct sockaddr_storage *address);
1565 static int remote(socket_t socket,
struct sockaddr_storage *address);
1585 ListenSocket(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1597 static socket_t
create(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1604 socket_t accept(
struct sockaddr_storage *address = NULL)
const;
1618 inline operator socket_t()
const
1635 inline socket_t handle(
void)
const
1655 TCPServer(
const char *address,
const char *service,
unsigned backlog = 5);
1698 inline operator struct sockaddr *()
const
1708 inline operator struct sockaddr_in *()
const
1711 inline struct sockaddr_in *in(
void)
const
1715 inline operator struct sockaddr_in6 *()
const
1718 inline struct sockaddr_in6 *in6(
void)
const
1732 inline operator bool()
const
1733 {
return ptr != NULL;};
1776 {
return ptr == NULL;};
1778 inline void next(
void)
1797 {
return address.
get();}
1806 inline bool eq(
const struct sockaddr *s1,
const struct sockaddr *s2)
1807 {
return Socket::equal(s1, s2);}
1816 inline bool eq(
const struct sockaddr_storage *s1,
const struct sockaddr_storage *s2)
1817 {
return Socket::equal((
const struct sockaddr *)s1, (
const struct sockaddr *)s2);}
1826 inline bool eq_host(
const struct sockaddr *s1,
const struct sockaddr *s2)
1829 inline bool eq_subnet(
const struct sockaddr *s1,
const struct sockaddr *s2)
1830 {
return Socket::eq_subnet(s1, s2);}
1834 typedef TCPServer tcpserv_t;
bool operator==(const struct sockaddr *address) const
Test if a given socket address falls within this cidr.
An object that can hold a ipv4 or ipv6 socket address.
struct sockaddr * addr(Socket::address &address)
A convenience function to convert a socket address list into a socket address.
static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
Send reply on socket.
struct sockaddr * operator*() const
Return the full socket address list by pointer reference.
int multicast(unsigned ttl=1)
Set multicast mode and multicast broadcast range.
static socket_t create(int family, int type, int protocol)
Create a socket object unbound.
bool eq(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
Compare two stored socket addresses to see if equal.
A common string class and character string support functions.
int keepalive(bool enable)
Set socket for keepalive packets.
int err(void) const
Get error code.
T * dup(const T &object)
Convenience function to duplicate object pointer to heap.
socket_t operator()(void) const
Get socket as expression operator.
void operator=(struct addrinfo *list)
Assign our pointer from an address list.
void set(Socket::address &list)
Assign our pointer from an address list.
An object that holds ipv4 or ipv6 binary encoded host addresses.
bool wait(timeout_t timeout=0) const
Test for pending input data.
int getError(void)
Get socket error code.
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
struct sockaddr * _getaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
A generic socket base class.
int ttl(unsigned char time)
Set the time to live before packets expire.
struct addrinfo * _nextaddrinfo(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
int sendsize(unsigned size)
Set the size of the socket send buffer.
struct addrinfo * operator*() const
Return the full socket address list by pointer reference.
A bound socket used to listen for inbound socket connections.
int priority(int scheduling)
Set packet priority, 0 to 6 unless privileged.
unsigned long timeout_t
Typedef for millisecond timer values.
int sendwait(unsigned size)
Set the size to wait before sending.
int recvsize(unsigned size)
Set the size of the socket receive buffer.
A generic tcp server class.
socket_t operator*() const
Get the socket descriptor by pointer reference.
bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
Compare two host addresses to see if equal.
inethostaddr_t getBroadcast(void) const
Get the broadcast host address represented by our cidr.
socket_t operator*() const
Get the socket descriptor of the listener by pointer reference.
struct sockaddr_internet inetsockaddr_t
An object that can hold a ipv4 or ipv6 socket address.
bool operator!() const
Test if we have no address list.
static int family(const struct sockaddr_storage &address)
Get the address family of a socket address object.
static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
Send to internet socket.
unsigned short strsize_t
A convenience class for size of strings.
A copy-on-write string class that operates by reference count.
A smart pointer template for iterating linked lists.
Realtime timers and timer queues.
static int family(const struct sockaddr_internet &address)
Get the address family of an internet socket address object.
void operator=(Socket::address &list)
Assign our pointer from an address list.
bool ccid(uint8_t id)
Set ccid of dccp socket.
int blocking(bool enable)
Set socket blocking I/O mode.
int broadcast(bool enable)
Set socket for unicast mode broadcasts.
static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
Store an address into an address object.
unsigned getMask(void) const
Get the number of bits in the cidr bitmask.
struct addrinfo * getList(void) const
Get the full socket address list from the object.
int tos(int type)
Set the type of service field of outgoing packets.
void release(SharedAccess &object)
Convenience function to unlock shared object through it's protocol.
static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
Compare socket addresses.
Common base class for all objects that can be formed into a linked list.
static short service(const struct sockaddr_internet *address)
Get the service port of an inet socket.
int getFamily(void) const
Get the address family of our cidr block object.
socket_t _getaddrsock(struct addrinfo *addrinfo)
Helper function for linked_pointer<struct sockaddr>.
A class to hold internet segment routing rules.
static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
Compare socket addresses.
int loopback(bool enable)
Set loopback to read multicast packets we broadcast.
bool operator!() const
Test if we have no address list.
struct sockaddr * get(void) const
Get the first socket address in our address list.
struct addrinfo * addrinfo(Socket::address &address)
A convenience function to convert a socket address list into an addrinfo.
unsigned segsize(unsigned size)
Set segment size and get mtu of a socket.
const char * getName(void) const
Get the saved name of our cidr.
inethostaddr_t getNetmask(void) const
Get the effective network mask for our cidr block.
bool is_member(LinkedObject *list) const
Search to see if we are a member of a specific list.
socket_t getsocket(void) const
Get the socket descriptor of the listener.
unsigned pending(void) const
Get the number of bytes of data in the socket receive buffer.
struct hostaddr_internet inethostaddr_t
An object that holds ipv4 or ipv6 binary encoded host addresses.
bool operator!=(const struct sockaddr *address) const
Test if a given socket address falls outside this cidr.
int nodelay(void) const
Set nodelay option for tcp socket.
A generic socket address class.
bool wait(timeout_t timeout=Timer::inf) const
Wait for a pending connection.
int type(void)
Get the type of a socket.
void wait(barrier_t &barrier)
Convenience function to wait on a barrier.
void shutdown(void)
Shutdown the socket communication channel.
Linked objects, lists, templates, and containers.
LinkedObject policy
A convenience type for using a pointer to a linked list as a policy chain.
ObjectProtocol * copy(ObjectProtocol *object)
Convenience function to access object copy.
Timer class to use when scheduling realtime events.
void set(struct addrinfo *list)
Assign our pointer from an address list.
struct sockaddr * operator->() const
Return member from typed object our pointer references.
inethostaddr_t getNetwork(void) const
Get the network host base address of our cidr block.