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 #ifndef _UCOMMON_STRING_H_
00038 #include <ucommon/string.h>
00039 #endif
00040
00041 extern "C" {
00042 struct addrinfo;
00043 }
00044
00045 #ifdef _MSWINDOWS_
00046 #define SHUT_RDWR SD_BOTH
00047 #define SHUT_WR SD_SEND
00048 #define SHUT_RD SD_RECV
00049 #else
00050 #include <unistd.h>
00051 #include <sys/socket.h>
00052 #include <net/if.h>
00053 #include <netinet/in.h>
00054 #include <netdb.h>
00055 #endif
00056
00057 #include <errno.h>
00058 #include <stdio.h>
00059
00060 #ifndef IPTOS_LOWDELAY
00061 #define IPTOS_LOWDELAY 0x10
00062 #define IPTOS_THROUGHPUT 0x08
00063 #define IPTOS_RELIABILITY 0x04
00064 #define IPTOS_MINCOST 0x02
00065 #endif
00066
00067 #ifdef AF_UNSPEC
00068 #define DEFAULT_FAMILY AF_UNSPEC
00069 #else
00070 #define DEFAULT_FAMILY AF_INET
00071 #endif
00072
00073 struct sockaddr_internet;
00074
00075 typedef struct sockaddr *sockaddr_t;
00076
00077 typedef struct sockaddr sockaddr_struct;
00078
00082 typedef struct hostaddr_internet
00083 {
00084 union
00085 {
00086 struct in_addr ipv4;
00087 #ifdef AF_INET6
00088 struct in6_addr ipv6;
00089 #endif
00090 };
00091 } inethostaddr_t;
00092
00093 #if defined(AF_INET6) || defined(__CYGWIN__)
00094
00101 typedef struct sockaddr_internet
00102 {
00103 union {
00104 #ifdef AF_INET6
00105 struct sockaddr_in6 ipv6;
00106 #endif
00107 struct sockaddr_in ipv4;
00108 struct sockaddr address;
00109 };
00110 } inetsockaddr_t;
00111 #else
00112 typedef struct sockaddr_internet
00113 {
00114 union {
00115 struct sockaddr_in ipv4;
00116 struct sockaddr address;
00117 };
00118 } inetsockaddr_t;
00119
00120 struct sockaddr_storage
00121 {
00122 #ifdef AF_UNIX
00123 char sa_data[128];
00124 #else
00125 char sa_data[sizeof(struct sockaddr_in)];
00126 #endif
00127 };
00128 #endif
00129
00130 #ifndef SOCK_DCCP
00131 #define SOCK_DCCP 6
00132 #endif
00133
00134 #ifndef IPPROTO_DCCP
00135 #define IPPROTO_DCCP 23
00136 #endif
00137
00138 #ifndef SOL_DCCP
00139 #define SOL_DCCP 269
00140 #endif
00141
00142 #define DCCP_SOCKOPT_AVAILABLE_CCIDS 12
00143 #define DCCP_SOCKOPT_CCID 13
00144 #define DCCP_SOCKOPT_TX_CCID 14
00145 #define DCCP_SOCKOPT_RX_CCID 15
00146
00147 NAMESPACE_UCOMMON
00148
00158 class __EXPORT cidr : public LinkedObject
00159 {
00160 protected:
00161 int Family;
00162 inethostaddr_t Netmask, Network;
00163 char Name[16];
00164
00165 unsigned mask(const char *cp) const;
00166
00167 inethostaddr_t broadcast(void) const;
00168
00169 unsigned mask(void) const;
00170
00171 public:
00175 typedef LinkedObject policy;
00176
00180 cidr();
00181
00188 cidr(const char *string);
00189
00195 cidr(policy **policy, const char *string);
00196
00203 cidr(policy **policy, const char *string, const char *name);
00204
00209 cidr(const cidr& existing);
00210
00217 static const cidr *find(const policy *policy, const struct sockaddr *address);
00218
00226 static const cidr *container(const policy *policy, const struct sockaddr *address);
00227
00235 inline const char *getName(void) const
00236 {return Name;};
00237
00242 inline int getFamily(void) const
00243 {return Family;};
00244
00249 inline inethostaddr_t getNetwork(void) const
00250 {return Network;};
00251
00256 inline inethostaddr_t getNetmask(void) const
00257 {return Netmask;};
00258
00263 inline inethostaddr_t getBroadcast(void) const
00264 {return broadcast();}
00265
00270 inline unsigned getMask(void) const
00271 {return mask();}
00272
00277 void set(const char *string);
00278
00284 bool is_member(const struct sockaddr *address) const;
00285
00291 inline bool operator==(const struct sockaddr *address) const
00292 {return is_member(address);};
00293
00299 inline bool operator!=(const struct sockaddr *address) const
00300 {return !is_member(address);};
00301 };
00302
00310 class __EXPORT Socket
00311 {
00312 protected:
00313 socket_t so;
00314 int ioerr;
00315 timeout_t iowait;
00316
00317 public:
00326 static struct addrinfo *query(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
00327
00333 static void release(struct addrinfo *list);
00334
00343 class __EXPORT address
00344 {
00345 protected:
00346 struct addrinfo *list;
00347
00348 public:
00359 address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00360
00373 address(int family, const char *hostname, const char *service = NULL);
00374
00381 address(const char *host, const char *service, int type = SOCK_STREAM);
00382
00390 address(const char *hostname, unsigned service = 0);
00391
00395 address();
00396
00401 address(const address& reference);
00402
00406 ~address();
00407
00412 struct sockaddr *get(void) const;
00413
00414 inline struct sockaddr *getAddr(void) const
00415 {return get();}
00416
00417 inline struct sockaddr *operator()(void) const
00418 {return get();}
00419
00424 inline operator struct sockaddr *() const
00425 {return get();};
00426
00432 struct sockaddr *get(int family) const;
00433
00434 inline struct sockaddr *operator()(int family) const
00435 {return get(family);}
00436
00437 inline operator struct sockaddr_in *() const
00438 {return (struct sockaddr_in *)get(AF_INET);}
00439
00440 #ifdef AF_INET6
00441 inline operator struct sockaddr_in6 *() const
00442 {return (struct sockaddr_in6 *)get(AF_INET6);}
00443 #endif
00444
00449 int family(void) const;
00450
00455 struct sockaddr *find(const struct sockaddr *addr) const;
00456
00461 inline struct addrinfo *getList(void) const
00462 {return list;};
00463
00468 inline operator struct addrinfo *() const
00469 {return list;};
00470
00475 inline struct addrinfo *operator*() const
00476 {return list;};
00477
00482 inline operator bool() const
00483 {return list != NULL;};
00484
00489 inline bool operator!() const
00490 {return list == NULL;};
00491
00492
00496 void clear(void);
00497
00504 void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
00505
00512 void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
00513
00521 void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00522
00527 void add(sockaddr *address);
00528
00534 unsigned insert(struct addrinfo *address);
00535
00541 unsigned remove(struct addrinfo *address);
00542
00548 bool remove(struct sockaddr *address);
00549
00556 bool insert(struct sockaddr *address);
00557
00563 void copy(const struct addrinfo *address);
00564
00569 void set(struct sockaddr *address);
00570
00576 void set(const char *hostname, unsigned service = 0);
00577
00583 static struct sockaddr *dup(struct sockaddr *address);
00584
00590 static struct sockaddr_in *ipv4(struct sockaddr *address);
00591
00592 #ifdef AF_INET6
00593
00598 static struct sockaddr_in6 *ipv6(struct sockaddr *address);
00599 #endif
00600 };
00601
00602 friend class address;
00603
00607 Socket();
00608
00613 Socket(const Socket& existing);
00614
00619 Socket(socket_t socket);
00620
00626 Socket(struct addrinfo *address);
00627
00634 Socket(int family, int type, int protocol = 0);
00635
00645 Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
00646
00650 virtual ~Socket();
00651
00655 void cancel(void);
00656
00661 static void cancel(socket_t socket);
00662
00666 void release(void);
00667
00671 inline int err(void) const
00672 {return ioerr;}
00673
00679 bool is_pending(unsigned value) const;
00680
00685 bool connected(void) const;
00686
00693 bool wait(timeout_t timeout = 0) const;
00694
00699 inline int nodelay(void) const
00700 {return nodelay(so);};
00701
00709 static bool wait(socket_t socket, timeout_t timeout = 0);
00710
00717 bool waitSending(timeout_t timeout = 0) const;
00718
00723 inline unsigned pending(void) const
00724 {return pending(so);};
00725
00731 inline int broadcast(bool enable)
00732 {return broadcast(so, enable);};
00733
00739 inline int keepalive(bool enable)
00740 {return keepalive(so, enable);};
00741
00747 inline int blocking(bool enable)
00748 {return blocking(so, enable);};
00749
00755 inline int multicast(unsigned ttl = 1)
00756 {return multicast(so, ttl);};
00757
00763 inline int loopback(bool enable)
00764 {return loopback(so, enable);};
00765
00770 inline int getError(void)
00771 {return error(so);};
00772
00778 inline int ttl(unsigned char time)
00779 {return ttl(so, time);};
00780
00786 inline int sendsize(unsigned size)
00787 {return sendsize(so, size);};
00788
00794 inline int sendwait(unsigned size)
00795 {return sendwait(so, size);};
00796
00797
00803 inline int recvsize(unsigned size)
00804 {return recvsize(so, size);};
00805
00811 static int type(socket_t socket);
00812
00819 static unsigned segsize(socket_t socket, unsigned size = 0);
00820
00827 static bool ccid(socket_t socket, uint8_t id);
00828
00833 inline int type(void)
00834 {return type(so);};
00835
00841 inline unsigned segsize(unsigned size)
00842 {return segsize(so, size);};
00843
00849 inline bool ccid(uint8_t id)
00850 {return ccid(so, id);};
00851
00860 inline int tos(int type)
00861 {return tos(so, type);};
00862
00869 inline int priority(int scheduling)
00870 {return priority(so, scheduling);};
00871
00875 inline void shutdown(void)
00876 {::shutdown(so, SHUT_RDWR);};
00877
00885 int connectto(struct addrinfo *list);
00886
00893 int disconnect(void);
00894
00900 int join(const struct addrinfo *list);
00901
00907 int drop(const struct addrinfo *list);
00908
00914 int wait(timeout_t timeout = Timer::inf);
00915
00922 size_t peek(void *data, size_t number) const;
00923
00931 size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
00932
00940 size_t writeto(const void *data, size_t number, const struct sockaddr *address = NULL);
00941
00954 size_t readline(char *data, size_t size);
00955
00961 size_t printf(const char *format, ...) __PRINTF(2,3);
00962
00974 size_t readline(String& buffer);
00975
00987 static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
00988
00995 static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
00996
01004 size_t writes(const char *string);
01005
01010 operator bool();
01011
01016 bool operator!() const;
01017
01023 Socket& operator=(socket_t socket);
01024
01029 inline operator socket_t() const
01030 {return so;};
01031
01036 inline socket_t operator*() const
01037 {return so;};
01038
01045 static unsigned pending(socket_t socket);
01046
01053 static int sendsize(socket_t socket, unsigned size);
01054
01061 static int sendwait(socket_t socket, unsigned size);
01062
01069 static int recvsize(socket_t socket, unsigned size);
01070
01079 static int connectto(socket_t socket, struct addrinfo *list);
01080
01086 static int disconnect(socket_t socket);
01087
01094 static int drop(socket_t socket, const struct addrinfo *list);
01095
01102 static int join(socket_t socket, const struct addrinfo *list);
01103
01109 static int error(socket_t socket);
01110
01117 static int multicast(socket_t socket, unsigned ttl = 1);
01118
01125 static int loopback(socket_t socket, bool enable);
01126
01133 static int blocking(socket_t socket, bool enable);
01134
01141 static int keepalive(socket_t socket, bool enable);
01142
01149 static int broadcast(socket_t socket, bool enable);
01150
01156 static int nodelay(socket_t socket);
01157
01164 static int priority(socket_t socket, int scheduling);
01165
01172 static int tos(socket_t socket, int type);
01173
01180 static int ttl(socket_t socket, unsigned char time);
01181
01186 static int family(socket_t socket);
01187
01193 inline static int family(const struct sockaddr_storage& address)
01194 {return ((const struct sockaddr *)&address)->sa_family;};
01195
01201 inline static int family(const struct sockaddr_internet& address)
01202 {return address.address.sa_family;};
01203
01213 static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
01214
01224 static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, const struct sockaddr *address = NULL);
01225
01235 inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_storage *address)
01236 {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);};
01237
01247 inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, const struct sockaddr_internet *address)
01248 {return sendto(socket, buffer, size, flags, (const struct sockaddr *)address);};
01249
01259 static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
01260
01269 static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
01270
01278 static int listento(socket_t socket, const struct sockaddr *address, int backlog = 5);
01279
01286 static int bindto(socket_t socket, const struct sockaddr *address);
01287
01294 static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
01295
01303 static socket_t create(int family, int type, int protocol);
01304
01312 static socket_t create(const struct addrinfo *address, int type, int protocol);
01313
01323 static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01324
01330 static socket_t create(const Socket::address &address);
01331
01336 static void release(socket_t socket);
01337
01345 static char *hostname(const struct sockaddr *address, char *buffer, size_t size);
01346
01354 static struct addrinfo *hinting(socket_t socket, struct addrinfo *hint);
01355
01366 static socklen_t query(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
01367
01373 static socklen_t len(const struct sockaddr *address);
01374
01382 static bool equal(const struct sockaddr *address1, const struct sockaddr *address2);
01383
01390 static unsigned copy(struct sockaddr *target, const struct sockaddr *origin);
01391
01398 inline static unsigned store(struct sockaddr_storage *storage, const struct sockaddr *address)
01399 {return copy((struct sockaddr*)storage, address);};
01400
01407 static unsigned store(struct sockaddr_internet *storage, const struct sockaddr *address);
01408
01416 static bool eq_host(const struct sockaddr *address1, const struct sockaddr *address2);
01417
01425 inline static bool eq_from(const struct sockaddr_storage *address1, const struct sockaddr_storage *address2)
01426 {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);};
01427
01435 inline static bool eq_inet(const struct sockaddr_internet *address1, const struct sockaddr_internet *address2)
01436 {return equal((const struct sockaddr *)address1, (const struct sockaddr *)address2);};
01437
01445 static bool eq_subnet(const struct sockaddr *address1, const struct sockaddr *address2);
01446
01454 static int via(struct sockaddr *address, const struct sockaddr *destination);
01455
01463 static char *query(const struct sockaddr *address, char *buffer, socklen_t size);
01464
01470 static short service(const struct sockaddr *address);
01471
01477 inline static short service(const struct sockaddr_internet *address)
01478 {return service((const struct sockaddr *)address);};
01479
01486 static unsigned keyindex(const struct sockaddr *address, unsigned size);
01487
01494 static unsigned keyhost(const struct sockaddr *address, unsigned size);
01495
01499 static void init(void);
01500
01505 static void init(const char *program);
01506
01512 static void query(int family);
01513
01520 static void v4mapping(bool enable);
01521
01526 static int error(void);
01527
01536 static bool is_null(const char *string);
01537
01545 static bool is_numeric(const char *string);
01546
01555 static int local(socket_t socket, struct sockaddr_storage *address);
01556
01565 static int remote(socket_t socket, struct sockaddr_storage *address);
01566 };
01567
01573 class __EXPORT ListenSocket : protected Socket
01574 {
01575 public:
01585 ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01586
01597 static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01598
01604 socket_t accept(struct sockaddr_storage *address = NULL) const;
01605
01611 inline bool wait(timeout_t timeout = Timer::inf) const
01612 {return Socket::wait(timeout);};
01613
01618 inline operator socket_t() const
01619 {return so;}
01620
01625 inline socket_t operator*() const
01626 {return so;}
01627
01632 inline socket_t getsocket(void) const
01633 {return so;}
01634
01635 inline socket_t handle(void) const
01636 {return so;}
01637
01638 };
01639
01645 class __EXPORT TCPServer : public ListenSocket
01646 {
01647 public:
01655 TCPServer(const char *address, const char *service, unsigned backlog = 5);
01656 };
01657
01661 __EXPORT struct addrinfo *_nextaddrinfo(struct addrinfo *addrinfo);
01662
01666 __EXPORT struct sockaddr *_getaddrinfo(struct addrinfo *addrinfo);
01667
01671 __EXPORT socket_t _getaddrsock(struct addrinfo *addrinfo);
01672
01678 template <>
01679 class linked_pointer<sockaddr_struct>
01680 {
01681 private:
01682 struct addrinfo *ptr;
01683
01684 public:
01685 inline linked_pointer(struct addrinfo *list)
01686 {ptr = list;}
01687
01688 inline linked_pointer()
01689 {ptr = NULL;}
01690
01691 inline linked_pointer(Socket::address& list)
01692 {ptr = list.getList();};
01693
01698 inline operator struct sockaddr *() const
01699 {return _getaddrinfo(ptr);};
01700
01705 inline struct sockaddr *operator*() const
01706 {return _getaddrinfo(ptr);};
01707
01708 inline operator struct sockaddr_in *() const
01709 {return (struct sockaddr_in *)_getaddrinfo(ptr);};
01710
01711 inline struct sockaddr_in *in(void) const
01712 {return (struct sockaddr_in *)_getaddrinfo(ptr);};
01713
01714 #ifdef AF_INET6
01715 inline operator struct sockaddr_in6 *() const
01716 {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
01717
01718 inline struct sockaddr_in6 *in6(void) const
01719 {return (struct sockaddr_in6 *)_getaddrinfo(ptr);};
01720 #endif
01721
01725 inline socket_t operator()(void) const
01726 {return _getaddrsock(ptr);};
01727
01732 inline operator bool() const
01733 {return ptr != NULL;};
01734
01739 inline void operator=(struct addrinfo *list)
01740 {ptr = list;};
01741
01746 inline void operator=(Socket::address& list)
01747 {ptr = list.getList();};
01748
01753 inline void set(struct addrinfo *list)
01754 {ptr = list;};
01755
01760 inline void set(Socket::address& list)
01761 {ptr = list.getList();};
01762
01763
01768 inline struct sockaddr* operator->() const
01769 {return _getaddrinfo(ptr);};
01770
01775 inline bool operator!() const
01776 {return ptr == NULL;};
01777
01778 inline void next(void)
01779 {ptr = _nextaddrinfo(ptr);};
01780 };
01781
01787 inline struct addrinfo *addrinfo(Socket::address& address)
01788 {return address.getList();}
01789
01796 inline struct sockaddr *addr(Socket::address& address)
01797 {return address.get();}
01798
01806 inline bool eq(const struct sockaddr *s1, const struct sockaddr *s2)
01807 {return Socket::equal(s1, s2);}
01808
01816 inline bool eq(const struct sockaddr_storage *s1, const struct sockaddr_storage *s2)
01817 {return Socket::equal((const struct sockaddr *)s1, (const struct sockaddr *)s2);}
01818
01826 inline bool eq_host(const struct sockaddr *s1, const struct sockaddr *s2)
01827 {return Socket::eq_host(s1, s2);}
01828
01829 inline bool eq_subnet(const struct sockaddr *s1, const struct sockaddr *s2)
01830 {return Socket::eq_subnet(s1, s2);}
01831
01832 String str(Socket& so, strsize_t size);
01833
01834 typedef TCPServer tcpserv_t;
01835
01836 END_NAMESPACE
01837
01838 #endif