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);
235 inline const char *getName(
void)
const
242 inline int getFamily(
void)
const
264 {
return broadcast();}
270 inline unsigned getMask(
void)
const
277 void set(
const char *string);
284 bool is_member(
const struct sockaddr *address)
const;
291 inline bool operator==(
const struct sockaddr *address)
const
299 inline bool operator!=(
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;};
489 inline bool operator!()
const
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;
699 inline int nodelay(
void)
const
717 bool waitSending(
timeout_t timeout = 0)
const;
723 inline unsigned pending(
void)
const
731 inline int broadcast(
bool enable)
739 inline int keepalive(
bool enable)
747 inline int blocking(
bool enable)
755 inline int multicast(
unsigned ttl = 1)
763 inline int loopback(
bool enable)
770 inline int getError(
void)
778 inline int ttl(
unsigned char time)
779 {
return ttl(so, time);};
786 inline int sendsize(
unsigned size)
794 inline int sendwait(
unsigned size)
803 inline int recvsize(
unsigned 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);
833 inline int type(
void)
841 inline unsigned segsize(
unsigned size)
849 inline bool ccid(uint8_t
id)
850 {
return ccid(so,
id);};
860 inline int tos(
int type)
861 {
return tos(so, type);};
869 inline int priority(
int scheduling)
875 inline void shutdown(
void)
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
1036 inline socket_t operator*()
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);};
1247 inline static ssize_t sendinet(socket_t socket,
const void *buffer,
size_t size,
int flags,
const struct sockaddr_internet *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
1632 inline socket_t getsocket(
void)
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
1705 inline struct sockaddr *operator*()
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
1725 inline socket_t operator()(
void)
const
1732 inline operator bool()
const
1733 {
return ptr != NULL;};
1768 inline struct sockaddr* operator->()
const
1775 inline bool operator!()
const
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;