00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00026 #ifndef _UCOMMON_SOCKET_H_
00027 #define _UCOMMON_SOCKET_H_
00028
00029 #ifndef _UCOMMON_TIMERS_H_
00030 #include <ucommon/timers.h>
00031 #endif
00032
00033 #ifndef _UCOMMON_LINKED_H_
00034 #include <ucommon/linked.h>
00035 #endif
00036
00037 struct addrinfo;
00038
00039 #ifdef _MSWINDOWS_
00040 #define SHUT_RDWR SD_BOTH
00041 #define SHUT_WR SD_SEND
00042 #define SHUT_RD SD_RECV
00043 #else
00044 #include <unistd.h>
00045 #include <sys/socket.h>
00046 #include <net/if.h>
00047 #include <netinet/in.h>
00048 #include <netdb.h>
00049 #endif
00050
00051 #include <errno.h>
00052 #include <stdio.h>
00053
00054 #ifndef IPTOS_LOWDELAY
00055 #define IPTOS_LOWDELAY 0x10
00056 #define IPTOS_THROUGHPUT 0x08
00057 #define IPTOS_RELIABILITY 0x04
00058 #define IPTOS_MINCOST 0x02
00059 #endif
00060
00061 #ifdef AF_UNSPEC
00062 #define DEFAULT_FAMILY AF_UNSPEC
00063 #else
00064 #define DEFAULT_FAMILY AF_INET
00065 #endif
00066
00074 struct sockaddr_internet;
00075
00079 typedef struct hostaddr_internet
00080 {
00081 union
00082 {
00083 struct in_addr ipv4;
00084 #ifdef AF_INET6
00085 struct in6_addr ipv6;
00086 #endif
00087 };
00088 } inethostaddr_t;
00089
00090 #if defined(AF_INET6) || defined(__CYGWIN__)
00091 typedef struct sockaddr_internet
00092 {
00093 union {
00094 #ifdef AF_INET6
00095 struct sockaddr_in6 ipv6;
00096 #endif
00097 struct sockaddr_in ipv4;
00098 struct sockaddr address;
00099 };
00100 } inetsockaddr_t;
00101 #else
00102 typedef struct sockaddr_internet
00103 {
00104 union {
00105 struct sockaddr_in ipv4;
00106 struct sockaddr address;
00107 };
00108 } inetsockaddr_t;
00109
00110 struct sockaddr_storage
00111 {
00112 #ifdef AF_UNIX
00113 char sa_data[128];
00114 #else
00115 char sa_data[sizeof(struct sockaddr_in)];
00116 #endif
00117 };
00118 #endif
00119
00120 #ifndef SOCK_DCCP
00121 #define SOCK_DCCP 6
00122 #endif
00123
00124 #ifndef IPPROTO_DCCP
00125 #define IPPROTO_DCCP 23
00126 #endif
00127
00128 #ifndef SOL_DCCP
00129 #define SOL_DCCP 269
00130 #endif
00131
00132 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
00133 #define DCCP_SOCKOPT_CCID 13
00134 #define DCCP_SOCKOPT_TX_CCID 14
00135 #define DCCP_SOCKOPT_RX_CCID 15
00136
00137 NAMESPACE_UCOMMON
00138
00148 class __EXPORT cidr : public LinkedObject
00149 {
00150 protected:
00151 int family;
00152 inethostaddr_t netmask, network;
00153 char name[16];
00154 unsigned getMask(const char *cp) const;
00155
00156 public:
00160 typedef LinkedObject policy;
00161
00165 cidr();
00166
00173 cidr(const char *string);
00174
00180 cidr(policy **policy, const char *string);
00181
00188 cidr(policy **policy, const char *string, const char *name);
00189
00194 cidr(const cidr& existing);
00195
00202 static cidr *find(policy *policy, const struct sockaddr *address);
00203
00211 static cidr *container(policy *policy, const struct sockaddr *address);
00212
00220 inline const char *getName(void) const
00221 {return name;};
00222
00227 inline int getFamily(void) const
00228 {return family;};
00229
00234 inline inethostaddr_t getNetwork(void) const
00235 {return network;};
00236
00241 inline inethostaddr_t getNetmask(void) const
00242 {return netmask;};
00243
00248 inethostaddr_t getBroadcast(void) const;
00249
00254 unsigned getMask(void) const;
00255
00260 void set(const char *string);
00261
00267 bool isMember(const struct sockaddr *address) const;
00268
00274 inline bool operator==(const struct sockaddr *address) const
00275 {return isMember(address);};
00276
00282 inline bool operator!=(const struct sockaddr *address) const
00283 {return !isMember(address);};
00284 };
00285
00293 class __EXPORT Socket
00294 {
00295 protected:
00296 socket_t so;
00297
00298 public:
00307 class __EXPORT address
00308 {
00309 protected:
00310 struct addrinfo *list;
00311
00312 public:
00320 address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00321
00328 address(Socket& socket, const char *hostname, const char *service = NULL);
00329
00336 address(socket_t socket, const char *hostname, const char *service = NULL);
00337
00344 address(const char *hostname, unsigned service = 0, int family = DEFAULT_FAMILY);
00345
00349 address();
00350
00355 address(const address& reference);
00356
00360 ~address();
00361
00366 struct sockaddr *getAddr(void);
00367
00373 struct sockaddr *get(int family);
00374
00379 int getfamily(void);
00380
00385 struct sockaddr *find(struct sockaddr *addr);
00386
00391 inline struct addrinfo *getList(void)
00392 {return list;};
00393
00398 inline operator struct addrinfo *()
00399 {return list;};
00400
00405 inline struct addrinfo *operator*()
00406 {return list;};
00407
00412 inline operator bool()
00413 {return list != NULL;};
00414
00419 inline bool operator!()
00420 {return list == NULL;};
00421
00426 inline operator struct sockaddr *()
00427 {return getAddr();};
00428
00432 void clear(void);
00433
00441 void set(const char *hostname, const char *service = NULL, int family = 0, int type = SOCK_STREAM);
00442
00450 void add(const char *hostname, const char *service = NULL, int family = 0, int type = SOCK_STREAM);
00451
00459 void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00460
00465 void add(sockaddr *address);
00466
00472 bool remove(struct sockaddr *address);
00473
00480 bool insert(struct sockaddr *address);
00481
00488 unsigned insert(struct addrinfo *address, int family = 0);
00489
00496 unsigned remove(struct addrinfo *address, int family = 0);
00497
00503 void copy(const struct addrinfo *address);
00504
00509 void set(struct sockaddr *address);
00510
00517 void set(const char *hostname, unsigned service = 0, int family = DEFAULT_FAMILY);
00518
00524 static struct sockaddr *dup(struct sockaddr *address);
00525
00531 static struct sockaddr_in *ipv4(struct sockaddr *address);
00532
00533 #ifdef AF_INET6
00534
00539 static struct sockaddr_in6 *ipv6(struct sockaddr *address);
00540 #endif
00541 };
00542
00543 friend class address;
00544
00548 Socket();
00549
00554 Socket(const Socket& existing);
00555
00560 Socket(socket_t socket);
00561
00567 Socket(struct addrinfo *address);
00568
00575 Socket(int family, int type, int protocol = 0);
00576
00586 Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0, int backlog = 0);
00587
00591 virtual ~Socket();
00592
00596 void cancel(void);
00597
00602 static void cancel(socket_t socket);
00603
00607 void release(void);
00608
00614 bool isPending(unsigned value) const;
00615
00620 bool isConnected(void) const;
00621
00628 bool waitPending(timeout_t timeout = 0) const;
00629
00637 static bool wait(socket_t socket, timeout_t timeout = 0);
00638
00645 bool waitSending(timeout_t timeout = 0) const;
00646
00651 inline unsigned getPending(void) const
00652 {return pending(so);};
00653
00659 inline int broadcast(bool enable)
00660 {return broadcast(so, enable);};
00661
00667 inline int keepalive(bool enable)
00668 {return keepalive(so, enable);};
00669
00675 inline int blocking(bool enable)
00676 {return blocking(so, enable);};
00677
00683 inline int multicast(unsigned ttl = 1)
00684 {return multicast(so, ttl);};
00685
00691 inline int loopback(bool enable)
00692 {return loopback(so, enable);};
00693
00698 inline int getError(void)
00699 {return error(so);};
00700
00706 inline int ttl(unsigned char time)
00707 {return ttl(so, time);};
00708
00714 inline int sendsize(unsigned size)
00715 {return sendsize(so, size);};
00716
00722 inline int sendwait(unsigned size)
00723 {return sendwait(so, size);};
00724
00725
00731 inline int recvsize(unsigned size)
00732 {return recvsize(so, size);};
00733
00739 static int gettype(socket_t socket);
00740
00747 static unsigned segsize(socket_t socket, unsigned size = 0);
00748
00755 static bool setccid(socket_t socket, uint8_t ccid);
00756
00761 inline int gettype(void)
00762 {return gettype(so);};
00763
00769 inline unsigned segsize(unsigned size)
00770 {return segsize(so, size);};
00771
00777 inline bool setccid(uint8_t ccid)
00778 {return setccid(so, ccid);};
00779
00788 inline int tos(int type)
00789 {return tos(so, type);};
00790
00797 inline int priority(int scheduling)
00798 {return priority(so, scheduling);};
00799
00803 inline void shutdown(void)
00804 {::shutdown(so, SHUT_RDWR);};
00805
00813 inline int connectto(struct addrinfo *list)
00814 {return connectto(so, list);};
00815
00822 inline int disconnect(void)
00823 {return disconnect(so);};
00824
00830 inline int join(struct addrinfo *list)
00831 {return join(so, list);};
00832
00838 inline int drop(struct addrinfo *list)
00839 {return drop(so, list);};
00840
00847 size_t peek(void *data, size_t number) const;
00848
00857 virtual ssize_t get(void *data, size_t number, struct sockaddr_storage *address = NULL);
00858
00867 virtual ssize_t put(const void *data, size_t number, struct sockaddr *address = NULL);
00868
00879 virtual ssize_t gets(char *data, size_t size, timeout_t timeout = Timer::inf);
00880
00892 static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
00893
00900 static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
00901
00907 ssize_t puts(const char *string);
00908
00913 operator bool();
00914
00919 bool operator!() const;
00920
00926 Socket& operator=(socket_t socket);
00927
00932 inline operator socket_t() const
00933 {return so;};
00934
00939 inline socket_t operator*() const
00940 {return so;};
00941
00948 static unsigned pending(socket_t socket);
00949
00956 static int sendsize(socket_t socket, unsigned size);
00957
00964 static int sendwait(socket_t socket, unsigned size);
00965
00972 static int recvsize(socket_t socket, unsigned size);
00973
00982 static int connectto(socket_t socket, struct addrinfo *list);
00983
00989 static int disconnect(socket_t socket);
00990
00997 static int drop(socket_t socket, struct addrinfo *list);
00998
01005 static int join(socket_t socket, struct addrinfo *list);
01006
01012 static int error(socket_t socket);
01013
01020 static int multicast(socket_t socket, unsigned ttl = 1);
01021
01028 static int loopback(socket_t socket, bool enable);
01029
01036 static int blocking(socket_t socket, bool enable);
01037
01044 static int keepalive(socket_t socket, bool enable);
01045
01052 static int broadcast(socket_t socket, bool enable);
01053
01060 static int priority(socket_t socket, int scheduling);
01061
01068 static int tos(socket_t socket, int type);
01069
01076 static int ttl(socket_t socket, unsigned char time);
01077
01082 static int getfamily(socket_t socket);
01083
01089 inline static int getfamily(struct sockaddr_storage& address)
01090 {return ((struct sockaddr *)&address)->sa_family;};
01091
01097 inline static int getfamily(struct sockaddr_internet& address)
01098 {return address.address.sa_family;};
01099
01109 static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
01110
01120 static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, struct sockaddr *address = NULL);
01121
01131 inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_storage *address)
01132 {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01133
01143 inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_internet *address)
01144 {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01145
01155 static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
01156
01165 static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
01166
01174 static int listento(socket_t socket, struct sockaddr *address, int backlog = 5);
01175
01182 static int bindto(socket_t socket, struct sockaddr *address);
01183
01190 static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
01191
01199 static socket_t create(int family, int type, int protocol);
01200
01211 static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0, int backlog = 0);
01212
01218 static socket_t create(Socket::address &address);
01219
01224 static void release(socket_t socket);
01225
01234 inline static ssize_t sendto(Socket& socket, const char *buffer, size_t size, struct sockaddr *address)
01235 {return socket.put(buffer, size, address);};
01236
01245 inline static ssize_t recvfrom(Socket& socket, char *buffer, size_t size, struct sockaddr_storage *address)
01246 {return socket.get(buffer, size, address);};
01247
01253 inline static void connectto(Socket& socket, Socket::address &address)
01254 {socket.connectto(address);};
01255
01260 inline static void disconnect(Socket& socket)
01261 {socket.disconnect();};
01262
01269 inline static Socket acceptfrom(Socket& socket, struct sockaddr_storage *address)
01270 {return Socket(acceptfrom(socket.so, address));};
01271
01279 static char *gethostname(struct sockaddr *address, char *buffer, size_t size);
01280
01288 static struct addrinfo *gethint(socket_t socket, struct addrinfo *hint);
01289
01300 static socklen_t getaddr(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
01301
01307 static socklen_t getlen(struct sockaddr *address);
01308
01316 static bool equal(struct sockaddr *address1, struct sockaddr *address2);
01317
01324 static unsigned copy(struct sockaddr *target, struct sockaddr *origin);
01325
01332 inline static unsigned store(struct sockaddr_storage *storage, struct sockaddr *address)
01333 {return copy((struct sockaddr*)storage, address);};
01334
01341 static unsigned store(struct sockaddr_internet *storage, struct sockaddr *address);
01342
01350 static bool equalhost(struct sockaddr *address1, struct sockaddr *address2);
01351
01359 inline static bool equalfrom(struct sockaddr_storage *address1, struct sockaddr_storage *address2)
01360 {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01361
01369 inline static bool equalinet(struct sockaddr_internet *address1, struct sockaddr_internet *address2)
01370 {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01371
01379 static bool subnet(struct sockaddr *address1, struct sockaddr *address2);
01380
01388 static int getinterface(struct sockaddr *address, struct sockaddr *destination);
01389
01397 static char *getaddress(struct sockaddr *address, char *buffer, socklen_t size);
01398
01404 static short getservice(struct sockaddr *address);
01405
01411 inline static short inetservice(struct sockaddr_internet *address)
01412 {return getservice((struct sockaddr *)address);};
01413
01420 static unsigned keyindex(struct sockaddr *address, unsigned size);
01421
01428 static unsigned keyhost(struct sockaddr *address, unsigned size);
01429
01433 static void init(void);
01434
01440 static void family(int query);
01441
01448 static void v4mapping(bool enable);
01449
01456 static FILE *open(socket_t socket, bool mode = false);
01457
01463 inline FILE *open(bool mode = false)
01464 {return open(so, mode);};
01465
01470 static void close(FILE *file);
01471
01476 static int error(void);
01477
01486 static bool isNull(const char *string);
01487
01495 static bool isNumeric(const char *string);
01496 };
01497
01503 class __EXPORT ListenSocket : protected Socket
01504 {
01505 public:
01513 ListenSocket(const char *address, const char *service, unsigned backlog = 5, int protocol = 0);
01514
01520 socket_t accept(struct sockaddr_storage *address = NULL);
01521
01527 inline bool waitConnection(timeout_t timeout = Timer::inf) const
01528 {return Socket::waitPending(timeout);};
01529
01534 inline operator socket_t() const
01535 {return so;};
01536
01541 inline socket_t operator*() const
01542 {return so;};
01543 };
01544
01548 typedef Socket socket;
01549
01555 inline struct addrinfo *addrinfo(socket::address& address)
01556 {return address.getList();}
01557
01564 inline struct sockaddr *addr(socket::address& address)
01565 {return address.getAddr();}
01566
01567 END_NAMESPACE
01568
01569 #endif