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
164 unsigned getMask(
const char *cp)
const;
183 cidr(
const char *
string);
230 inline const char *getName(
void)
const
237 inline int getFamily(
void)
const
264 unsigned getMask(
void)
const;
270 void set(
const char *
string);
277 bool isMember(
const struct sockaddr *address)
const;
284 inline bool operator==(
const struct sockaddr *address)
const
292 inline bool operator!=(
const struct sockaddr *address)
const
319 static struct addrinfo *getaddress(
const char *host,
const char *service,
int type = SOCK_STREAM,
int protocol = 0);
335 static const size_t masksize;
361 address(
int family,
const char *
address,
int type = SOCK_STREAM,
int protocol = 0);
375 address(
int family,
const char *hostname,
const char *service = NULL);
383 address(
const char *host,
const char *service,
int type = SOCK_STREAM);
392 address(
const char *hostname,
unsigned service = 0);
403 address(
const address& reference);
414 struct sockaddr *getAddr(
void)
const;
421 struct sockaddr *
get(
int family)
const;
427 int getfamily(
void)
const;
433 struct sockaddr *find(
struct sockaddr *
addr)
const;
460 inline operator bool()
const
461 {
return list != NULL;};
467 inline bool operator!()
const
468 {
return list == NULL;};
474 inline operator struct sockaddr *()
const
488 void set(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
496 void add(
const char *hostname,
const char *service = NULL,
int type = SOCK_STREAM);
505 void set(
int family,
const char *address,
int type = SOCK_STREAM,
int protocol = 0);
511 void add(sockaddr *address);
518 unsigned insert(
struct addrinfo *address);
525 unsigned remove(
struct addrinfo *address);
532 bool remove(
struct sockaddr *address);
540 bool insert(
struct sockaddr *address);
553 void set(
struct sockaddr *address);
560 void set(
const char *hostname,
unsigned service = 0);
567 static struct sockaddr *
dup(
struct sockaddr *address);
574 static struct sockaddr_in *ipv4(
struct sockaddr *address);
582 static struct sockaddr_in6 *ipv6(
struct sockaddr *address);
586 friend class address;
618 Socket(
int family,
int type,
int protocol = 0);
629 Socket(
const char *address,
const char *port,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
645 static void cancel(socket_t
socket);
655 inline int err(
void)
const
663 bool isPending(
unsigned value)
const;
669 bool isConnected(
void)
const;
677 bool waitPending(
timeout_t timeout = 0)
const;
683 inline int nodelay(
void)
const
701 bool waitSending(
timeout_t timeout = 0)
const;
707 inline unsigned getPending(
void)
const
708 {
return pending(so);};
715 inline int broadcast(
bool enable)
723 inline int keepalive(
bool enable)
731 inline int blocking(
bool enable)
739 inline int multicast(
unsigned ttl = 1)
747 inline int loopback(
bool enable)
754 inline int getError(
void)
762 inline int ttl(
unsigned char time)
763 {
return ttl(so, time);};
770 inline int sendsize(
unsigned size)
778 inline int sendwait(
unsigned size)
787 inline int recvsize(
unsigned size)
795 static int gettype(socket_t
socket);
803 static unsigned segsize(socket_t
socket,
unsigned size = 0);
811 static bool setccid(socket_t
socket, uint8_t ccid);
817 inline int gettype(
void)
825 inline unsigned segsize(
unsigned size)
833 inline bool setccid(uint8_t ccid)
844 inline int tos(
int type)
845 {
return tos(so, type);};
853 inline int priority(
int scheduling)
859 inline void shutdown(
void)
860 {::shutdown(so, SHUT_RDWR);};
869 int connectto(
struct addrinfo *list);
877 int disconnect(
void);
906 size_t peek(
void *data,
size_t number)
const;
915 size_t readfrom(
void *data,
size_t number,
struct sockaddr_storage *address = NULL);
924 size_t writeto(
const void *data,
size_t number,
struct sockaddr *address = NULL);
938 size_t readline(
char *data,
size_t size);
945 size_t printf(
const char *format, ...) __PRINTF(2,3);
958 size_t readline(
string& buffer);
971 static ssize_t readline(socket_t
socket,
char *data,
size_t size,
timeout_t timeout =
Timer::inf);
979 static ssize_t printf(socket_t socket, const
char *format, ...) __PRINTF(2,3);
988 size_t writes(const
char *
string);
1000 bool operator!() const;
1007 Socket& operator=(socket_t socket);
1013 inline operator socket_t()
const
1020 inline socket_t operator*()
const
1029 static unsigned pending(socket_t
socket);
1037 static int sendsize(socket_t
socket,
unsigned size);
1045 static int sendwait(socket_t
socket,
unsigned size);
1053 static int recvsize(socket_t
socket,
unsigned size);
1070 static int disconnect(socket_t
socket);
1093 static int error(socket_t
socket);
1101 static int multicast(socket_t
socket,
unsigned ttl = 1);
1109 static int loopback(socket_t
socket,
bool enable);
1117 static int blocking(socket_t
socket,
bool enable);
1125 static int keepalive(socket_t
socket,
bool enable);
1133 static int broadcast(socket_t
socket,
bool enable);
1140 static int nodelay(socket_t
socket);
1148 static int priority(socket_t
socket,
int scheduling);
1156 static int tos(socket_t
socket,
int type);
1164 static int ttl(socket_t
socket,
unsigned char time);
1170 static int getfamily(socket_t
socket);
1177 inline static int getfamily(
struct sockaddr_storage&
address)
1178 {
return ((
struct sockaddr *)&address)->sa_family;};
1186 {
return address.address.sa_family;};
1197 static ssize_t recvfrom(socket_t
socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_storage *address = NULL);
1208 static ssize_t sendto(socket_t
socket,
const void *buffer,
size_t size,
int flags = 0,
struct sockaddr *address = NULL);
1219 inline static ssize_t replyto(socket_t
socket,
const void *buffer,
size_t size,
int flags,
struct sockaddr_storage *
address)
1220 {
return sendto(socket, buffer, size, flags, (
struct sockaddr *)address);};
1232 {
return sendto(socket, buffer, size, flags, (
struct sockaddr *)address);};
1243 static ssize_t recvinet(socket_t
socket,
void *buffer,
size_t size,
int flags = 0,
struct sockaddr_internet *address = NULL);
1253 static int bindto(socket_t
socket,
const char *address,
const char *service,
int protocol = 0);
1262 static int listento(socket_t
socket,
struct sockaddr *address,
int backlog = 5);
1270 static int bindto(socket_t
socket,
struct sockaddr *address);
1278 static socket_t acceptfrom(socket_t
socket,
struct sockaddr_storage *address = NULL);
1287 static socket_t create(
int family,
int type,
int protocol);
1296 static socket_t create(
struct addrinfo *address,
int type,
int protocol);
1307 static socket_t create(
const char *iface,
const char *service,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1331 {
return socket.
writeto(buffer, size, address);};
1342 {
return socket.
readfrom(buffer, size, address);};
1375 static char *gethostname(
struct sockaddr *address,
char *buffer,
size_t size);
1396 static socklen_t getaddr(socket_t
socket,
struct sockaddr_storage *address,
const char *hostname,
const char *service);
1403 static socklen_t getlen(
struct sockaddr *address);
1412 static bool equal(
struct sockaddr *address1,
struct sockaddr *address2);
1420 static unsigned copy(
struct sockaddr *target,
struct sockaddr *origin);
1428 inline static unsigned store(
struct sockaddr_storage *storage,
struct sockaddr *
address)
1429 {
return copy((
struct sockaddr*)storage, address);};
1437 static unsigned store(
struct sockaddr_internet *storage,
struct sockaddr *address);
1446 static bool equalhost(
struct sockaddr *address1,
struct sockaddr *address2);
1455 inline static bool equalfrom(
struct sockaddr_storage *address1,
struct sockaddr_storage *address2)
1456 {
return equal((
struct sockaddr *)address1, (
struct sockaddr *)address2);};
1466 {
return equal((
struct sockaddr *)address1, (
struct sockaddr *)address2);};
1475 static bool subnet(
struct sockaddr *address1,
struct sockaddr *address2);
1484 static int getinterface(
struct sockaddr *address,
struct sockaddr *destination);
1493 static char *getaddress(
struct sockaddr *address,
char *buffer, socklen_t size);
1500 static short getservice(
struct sockaddr *address);
1508 {
return getservice((
struct sockaddr *)address);};
1516 static unsigned keyindex(
struct sockaddr *address,
unsigned size);
1524 static unsigned keyhost(
struct sockaddr *address,
unsigned size);
1529 static void init(
void);
1535 static void init(
const char *program);
1542 static void family(
int query);
1550 static void v4mapping(
bool enable);
1556 static int error(
void);
1566 static bool isNull(
const char *
string);
1575 static bool isNumeric(
const char *
string);
1585 static int getlocal(socket_t
socket,
struct sockaddr_storage *address);
1595 static int getremote(socket_t
socket,
struct sockaddr_storage *address);
1604 static int select(
int max, set_t read, set_t write, set_t error);
1614 static int select(
int max, set_t read, set_t write, set_t error,
timeout_t timeout);
1620 static set_t getmask(
void);
1626 static void clear(set_t mask);
1632 static void release(set_t mask);
1639 static void set(socket_t
socket, set_t mask);
1646 static void clear(socket_t
socket, set_t mask);
1654 static bool test(socket_t
socket, set_t mask);
1674 ListenSocket(
const char *
address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1686 static socket_t
create(
const char *address,
const char *service,
unsigned backlog = 5,
int family = AF_UNSPEC,
int type = 0,
int protocol = 0);
1693 socket_t accept(
struct sockaddr_storage *address = NULL)
const;
1700 inline bool waitConnection(
timeout_t timeout = Timer::inf)
const
1701 {
return Socket::waitPending(timeout);};
1707 inline operator socket_t()
const
1721 inline socket_t getsocket(
void)
const
1784 inline operator struct sockaddr *()
const
1791 inline struct sockaddr *operator*()
const
1794 inline operator struct sockaddr_in *()
const
1797 inline struct sockaddr_in *in(
void)
const
1801 inline operator struct sockaddr_in6 *()
const
1804 inline struct sockaddr_in6 *in6(
void)
const
1811 inline socket_t operator()(
void)
const
1818 inline operator bool()
const
1819 {
return ptr != NULL;};
1854 inline struct sockaddr* operator->()
const
1861 inline bool operator!()
const
1862 {
return ptr == NULL;};
1864 inline void next(
void)
1897 inline bool eq(
struct sockaddr *s1,
struct sockaddr *s2)
1898 {
return Socket::equal(s1, s2);}
1907 inline bool eq(
struct sockaddr_storage *s1,
struct sockaddr_storage *s2)
1908 {
return Socket::equal((
struct sockaddr *)s1, (
struct sockaddr *)s2);}
1917 inline bool host_eq(
struct sockaddr *s1,
struct sockaddr *s2)
1918 {
return Socket::equalhost(s1, s2);}
1921 inline bool ieq(
struct sockaddr *s1,
struct sockaddr *s2)
1922 {
return Socket::equalhost(s1, s2);}
1926 typedef TCPServer tcpserv_t;