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;
91 #if defined(AF_INET6) || defined(__CYGWIN__)
103 struct sockaddr_in6 ipv6;
105 struct sockaddr_in ipv4;
106 struct sockaddr address;
113 struct sockaddr_in ipv4;
114 struct sockaddr address;
118 struct sockaddr_storage
123 char sa_data[
sizeof(
struct sockaddr_in)];
133 #define IPPROTO_DCCP 23
140 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
141 #define DCCP_SOCKOPT_CCID 13
142 #define DCCP_SOCKOPT_TX_CCID 14
143 #define DCCP_SOCKOPT_RX_CCID 15
162 unsigned getMask(
const char *cp)
const;
181 cidr(
const char *
string);
228 inline const char *getName(
void)
const
235 inline int getFamily(
void)
const
262 unsigned getMask(
void)
const;
268 void set(
const char *
string);
275 bool isMember(
const struct sockaddr *address)
const;
282 inline bool operator==(
const struct sockaddr *address)
const
290 inline bool operator!=(
const struct sockaddr *address)
const
317 static struct addrinfo *getaddress(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
333 static const size_t masksize;
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 *getAddr(
void)
const;
419 struct sockaddr *
get(
int family)
const;
425 int getfamily(
void)
const;
431 struct sockaddr *find(
struct sockaddr *
addr)
const;
458 inline operator bool()
const
459 {
return list != NULL;};
465 inline bool operator!()
const
466 {
return list == NULL;};
472 inline operator struct sockaddr *()
const
486 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
494 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
503 void set(
int family,
const char *address,
int type = SOCK_STREAM,
int protocol = 0);
509 void add(sockaddr *address);
516 unsigned insert(
struct addrinfo *address);
523 unsigned remove(
struct addrinfo *address);
530 bool remove(
struct sockaddr *address);
538 bool insert(
struct sockaddr *address);
551 void set(
struct sockaddr *address);
558 void set(
const char *hostname,
unsigned service = 0);
565 static struct sockaddr *
dup(
struct sockaddr *address);
572 static struct sockaddr_in *ipv4(
struct sockaddr *address);
580 static struct sockaddr_in6 *ipv6(
struct sockaddr *address);
584 friend class address;
616 Socket(
int family,
int type,
int protocol = 0);
627 Socket(
const char *address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
643 static void cancel(socket_t
socket);
653 inline int err(
void)
const
661 bool isPending(
unsigned value)
const;
667 bool isConnected(
void)
const;
675 bool waitPending(
timeout_t timeout = 0)
const;
681 inline int nodelay(
void)
const
699 bool waitSending(
timeout_t timeout = 0)
const;
705 inline unsigned getPending(
void)
const
706 {
return pending(so);};
713 inline int broadcast(
bool enable)
721 inline int keepalive(
bool enable)
729 inline int blocking(
bool enable)
737 inline int multicast(
unsigned ttl = 1)
745 inline int loopback(
bool enable)
752 inline int getError(
void)
760 inline int ttl(
unsigned char time)
761 {
return ttl(so, time);};
768 inline int sendsize(
unsigned size)
776 inline int sendwait(
unsigned size)
785 inline int recvsize(
unsigned size)
793 static int gettype(socket_t
socket);
801 static unsigned segsize(socket_t
socket,
unsigned size = 0);
809 static bool setccid(socket_t
socket, uint8_t ccid);
815 inline int gettype(
void)
823 inline unsigned segsize(
unsigned size)
831 inline bool setccid(uint8_t ccid)
842 inline int tos(
int type)
843 {
return tos(so, type);};
851 inline int priority(
int scheduling)
857 inline void shutdown(
void)
858 {::shutdown(so, SHUT_RDWR);};
867 int connectto(
struct addrinfo *list);
875 int disconnect(
void);
904 size_t peek(
void *data,
size_t number)
const;
913 size_t readfrom(
void *data,
size_t number,
struct sockaddr_storage *address = NULL);
922 size_t writeto(
const void *data,
size_t number,
struct sockaddr *address = NULL);
936 size_t readline(
char *data,
size_t size);
943 size_t printf(
const char *format, ...) __PRINTF(2,3);
956 size_t readline(
string& buffer);
969 static ssize_t readline(socket_t
socket,
char *data,
size_t size,
timeout_t timeout =
Timer::inf);
977 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
986 size_t writes(const
char *
string);
998 bool operator!() const;
1005 Socket& operator=(socket_t socket);
1011 inline operator socket_t()
const
1018 inline socket_t operator*()
const
1027 static unsigned pending(socket_t
socket);
1035 static int sendsize(socket_t
socket,
unsigned size);
1043 static int sendwait(socket_t
socket,
unsigned size);
1051 static int recvsize(socket_t
socket,
unsigned size);
1068 static int disconnect(socket_t
socket);
1091 static int error(socket_t
socket);
1099 static int multicast(socket_t
socket,
unsigned ttl = 1);
1107 static int loopback(socket_t
socket,
bool enable);
1115 static int blocking(socket_t
socket,
bool enable);
1123 static int keepalive(socket_t
socket,
bool enable);
1131 static int broadcast(socket_t
socket,
bool enable);
1138 static int nodelay(socket_t
socket);
1146 static int priority(socket_t
socket,
int scheduling);
1154 static int tos(socket_t
socket,
int type);
1162 static int ttl(socket_t
socket,
unsigned char time);
1168 static int getfamily(socket_t
socket);
1175 inline static int getfamily(
struct sockaddr_storage&
address)
1176 {
return ((
struct sockaddr *)&address)->sa_family;};
1184 {
return address.address.sa_family;};
1195 static ssize_t recvfrom(socket_t
socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *address = NULL);
1206 static ssize_t sendto(socket_t
socket,
const void *buffer,
size_t size,
int flags = 0,
struct sockaddr *address = NULL);
1217 inline static ssize_t replyto(socket_t
socket,
const void *buffer,
size_t size,
int flags,
struct sockaddr_storage *
address)
1218 {
return sendto(socket, buffer, size, flags, (
struct sockaddr *)address);};
1230 {
return sendto(socket, buffer, size, flags, (
struct sockaddr *)address);};
1241 static ssize_t recvinet(socket_t
socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_internet *address = NULL);
1251 static int bindto(socket_t
socket,
const char *address,
const char *service,
int protocol = 0);
1260 static int listento(socket_t
socket,
struct sockaddr *address,
int backlog = 5);
1268 static int bindto(socket_t
socket,
struct sockaddr *address);
1276 static socket_t acceptfrom(socket_t
socket,
struct sockaddr_storage *address = NULL);
1285 static socket_t create(
int family,
int type,
int protocol);
1294 static socket_t create(
struct addrinfo *address,
int type,
int protocol);
1305 static socket_t create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1329 {
return socket.
writeto(buffer, size, address);};
1340 {
return socket.
readfrom(buffer, size, address);};
1373 static char *gethostname(
struct sockaddr *address,
char *buffer,
size_t size);
1394 static socklen_t getaddr(socket_t
socket,
struct sockaddr_storage *address,
const char *hostname,
const char *service);
1401 static socklen_t getlen(
struct sockaddr *address);
1410 static bool equal(
struct sockaddr *address1,
struct sockaddr *address2);
1418 static unsigned copy(
struct sockaddr *target,
struct sockaddr *origin);
1426 inline static unsigned store(
struct sockaddr_storage *storage,
struct sockaddr *
address)
1427 {
return copy((
struct sockaddr*)storage, address);};
1435 static unsigned store(
struct sockaddr_internet *storage,
struct sockaddr *address);
1444 static bool equalhost(
struct sockaddr *address1,
struct sockaddr *address2);
1453 inline static bool equalfrom(
struct sockaddr_storage *address1,
struct sockaddr_storage *address2)
1454 {
return equal((
struct sockaddr *)address1, (
struct sockaddr *)address2);};
1464 {
return equal((
struct sockaddr *)address1, (
struct sockaddr *)address2);};
1473 static bool subnet(
struct sockaddr *address1,
struct sockaddr *address2);
1482 static int getinterface(
struct sockaddr *address,
struct sockaddr *destination);
1491 static char *getaddress(
struct sockaddr *address,
char *buffer, socklen_t size);
1498 static short getservice(
struct sockaddr *address);
1506 {
return getservice((
struct sockaddr *)address);};
1514 static unsigned keyindex(
struct sockaddr *address,
unsigned size);
1522 static unsigned keyhost(
struct sockaddr *address,
unsigned size);
1527 static void init(
void);
1533 static void init(
const char *program);
1540 static void family(
int query);
1548 static void v4mapping(
bool enable);
1554 static int error(
void);
1564 static bool isNull(
const char *
string);
1573 static bool isNumeric(
const char *
string);
1583 static int getlocal(socket_t
socket,
struct sockaddr_storage *address);
1593 static int getremote(socket_t
socket,
struct sockaddr_storage *address);
1602 static int select(
int max, set_t read, set_t write, set_t error);
1612 static int select(
int max, set_t read, set_t write, set_t error,
timeout_t timeout);
1618 static set_t getmask(
void);
1624 static void clear(set_t mask);
1630 static void release(set_t mask);
1637 static void set(socket_t
socket, set_t mask);
1644 static void clear(socket_t
socket, set_t mask);
1652 static bool test(socket_t
socket, set_t mask);
1672 ListenSocket(
const char *
address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1684 static socket_t
create(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1691 socket_t accept(
struct sockaddr_storage *address = NULL)
const;
1698 inline bool waitConnection(
timeout_t timeout = Timer::inf)
const
1699 {
return Socket::waitPending(timeout);};
1705 inline operator socket_t()
const
1719 inline socket_t getsocket(
void)
const
1782 inline operator struct sockaddr *()
const
1789 inline struct sockaddr *operator*()
const
1792 inline operator struct sockaddr_in *()
const
1795 inline struct sockaddr_in *in(
void)
const
1799 inline operator struct sockaddr_in6 *()
const
1802 inline struct sockaddr_in6 *in6(
void)
const
1809 inline socket_t operator()(
void)
const
1816 inline operator bool()
const
1817 {
return ptr != NULL;};
1852 inline struct sockaddr* operator->()
const
1859 inline bool operator!()
const
1860 {
return ptr == NULL;};
1862 inline void next(
void)
1895 inline bool eq(
struct sockaddr *s1,
struct sockaddr *s2)
1896 {
return Socket::equal(s1, s2);}
1905 inline bool eq(
struct sockaddr_storage *s1,
struct sockaddr_storage *s2)
1906 {
return Socket::equal((
struct sockaddr *)s1, (
struct sockaddr *)s2);}
1915 inline bool host_eq(
struct sockaddr *s1,
struct sockaddr *s2)
1916 {
return Socket::equalhost(s1, s2);}
1919 inline bool ieq(
struct sockaddr *s1,
struct sockaddr *s2)
1920 {
return Socket::equalhost(s1, s2);}
1924 typedef TCPServer tcpserv_t;