• Main Page
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

ucommon/socket.h

Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published 
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
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 
00078 typedef struct hostaddr_internet
00079 {
00080     union
00081     {
00082         struct in_addr ipv4;
00083 #ifdef  AF_INET6
00084         struct in6_addr ipv6;
00085 #endif
00086     };
00087 }   inethostaddr_t;
00088 
00089 #if defined(AF_INET6) || defined(__CYGWIN__)
00090 
00097 typedef struct sockaddr_internet
00098 {
00099     union {
00100 #ifdef  AF_INET6
00101         struct sockaddr_in6 ipv6;
00102 #endif
00103         struct sockaddr_in ipv4;
00104         struct sockaddr address;
00105     };
00106 } inetsockaddr_t;
00107 #else
00108 typedef struct sockaddr_internet
00109 {
00110     union {
00111         struct sockaddr_in ipv4;
00112         struct sockaddr address;
00113     };
00114 } inetsockaddr_t;
00115 
00116 struct sockaddr_storage
00117 {
00118 #ifdef  AF_UNIX
00119     char sa_data[128];
00120 #else
00121     char sa_data[sizeof(struct sockaddr_in)];
00122 #endif
00123 };
00124 #endif
00125 
00126 #ifndef SOCK_DCCP
00127 #define SOCK_DCCP       6
00128 #endif
00129 
00130 #ifndef IPPROTO_DCCP
00131 #define IPPROTO_DCCP    23
00132 #endif
00133 
00134 #ifndef SOL_DCCP
00135 #define SOL_DCCP        269
00136 #endif
00137 
00138 #define DCCP_SOCKOPT_AVAILABLE_CCIDS    12
00139 #define DCCP_SOCKOPT_CCID               13
00140 #define DCCP_SOCKOPT_TX_CCID            14
00141 #define DCCP_SOCKOPT_RX_CCID            15
00142 
00143 NAMESPACE_UCOMMON
00144 
00154 class __EXPORT cidr : public LinkedObject
00155 {
00156 protected:
00157     int family;
00158     inethostaddr_t netmask, network;
00159     char name[16];
00160     unsigned getMask(const char *cp) const;
00161 
00162 public:
00166     typedef LinkedObject policy;
00167 
00171     cidr();
00172 
00179     cidr(const char *string);
00180     
00186     cidr(policy **policy, const char *string);
00187 
00194     cidr(policy **policy, const char *string, const char *name);
00195 
00200     cidr(const cidr& existing);
00201 
00208     static cidr *find(policy *policy, const struct sockaddr *address);
00209 
00217     static cidr *container(policy *policy, const struct sockaddr *address);
00218 
00226     inline const char *getName(void) const
00227         {return name;};
00228 
00233     inline int getFamily(void) const
00234         {return family;};
00235 
00240     inline inethostaddr_t getNetwork(void) const
00241         {return network;};
00242 
00247     inline inethostaddr_t getNetmask(void) const
00248         {return netmask;};
00249 
00254     inethostaddr_t getBroadcast(void) const;
00255 
00260     unsigned getMask(void) const;
00261     
00266     void set(const char *string);
00267 
00273     bool isMember(const struct sockaddr *address) const;
00274 
00280     inline bool operator==(const struct sockaddr *address) const
00281         {return isMember(address);};
00282     
00288     inline bool operator!=(const struct sockaddr *address) const
00289         {return !isMember(address);}; 
00290 };
00291 
00299 class __EXPORT Socket
00300 {
00301 protected:
00302     socket_t so;
00303     int ioerr;
00304     timeout_t iowait;
00305 
00306 public:
00315     static struct addrinfo *getaddress(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
00316 
00322     static void release(struct addrinfo *list);
00323 
00329     typedef void *set_t;
00330 
00331     static const size_t masksize;
00332 
00341     class __EXPORT address
00342     {
00343     protected:
00344         struct addrinfo *list;
00345 
00346     public:
00357         address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00358 
00371         address(int family, const char *hostname, const char *service = NULL);
00372 
00379         address(const char *host, const char *service, int type = SOCK_STREAM);
00380 
00388         address(const char *hostname, unsigned service = 0);
00389 
00393         address();
00394 
00399         address(const address& reference);
00400 
00404         ~address();
00405 
00410         struct sockaddr *getAddr(void) const;
00411 
00417         struct sockaddr *get(int family) const;
00418 
00423         int getfamily(void) const;
00424 
00429         struct sockaddr *find(struct sockaddr *addr) const;
00430 
00435         inline struct addrinfo *getList(void) const
00436             {return list;};
00437 
00442         inline operator struct addrinfo *() const
00443             {return list;};
00444 
00449         inline struct addrinfo *operator*() const
00450             {return list;};
00451 
00456         inline operator bool() const
00457             {return list != NULL;};
00458 
00463         inline bool operator!() const
00464             {return list == NULL;};
00465 
00470         inline operator struct sockaddr *() const
00471             {return getAddr();};
00472 
00476         void clear(void);
00477 
00484         void set(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
00485 
00492         void add(const char *hostname, const char *service = NULL, int type = SOCK_STREAM);
00493 
00501         void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00502 
00507         void add(sockaddr *address);
00508 
00514         unsigned insert(struct addrinfo *address);
00515 
00521         unsigned remove(struct addrinfo *address);
00522 
00528         bool remove(struct sockaddr *address);
00529 
00536         bool insert(struct sockaddr *address);
00537 
00543         void copy(const struct addrinfo *address);
00544 
00549         void set(struct sockaddr *address);
00550 
00556         void set(const char *hostname, unsigned service = 0);
00557 
00563         static struct sockaddr *dup(struct sockaddr *address);
00564 
00570         static struct sockaddr_in *ipv4(struct sockaddr *address);
00571 
00572 #ifdef  AF_INET6
00573 
00578         static struct sockaddr_in6 *ipv6(struct sockaddr *address);
00579 #endif
00580     };
00581 
00582     friend class address;
00583 
00587     Socket();
00588 
00593     Socket(const Socket& existing);
00594 
00599     Socket(socket_t socket);
00600 
00606     Socket(struct addrinfo *address);
00607 
00614     Socket(int family, int type, int protocol = 0);
00615 
00625     Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0);
00626 
00630     virtual ~Socket();
00631 
00635     void cancel(void);
00636 
00641     static void cancel(socket_t socket);
00642 
00646     void release(void);
00647 
00651     inline int err(void) const
00652         {return ioerr;}
00653 
00659     bool isPending(unsigned value) const;
00660 
00665     bool isConnected(void) const;
00666 
00673     bool waitPending(timeout_t timeout = 0) const;
00674 
00679     inline int nodelay(void) const
00680         {return nodelay(so);};
00681 
00689     static bool wait(socket_t socket, timeout_t timeout = 0);
00690 
00697     bool waitSending(timeout_t timeout = 0) const;
00698     
00703     inline unsigned getPending(void) const
00704         {return pending(so);};
00705 
00711     inline int broadcast(bool enable)
00712         {return broadcast(so, enable);};
00713 
00719     inline int keepalive(bool enable)
00720         {return keepalive(so, enable);};
00721 
00727     inline int blocking(bool enable)
00728         {return blocking(so, enable);};
00729 
00735     inline int multicast(unsigned ttl = 1)
00736         {return multicast(so, ttl);};
00737 
00743     inline int loopback(bool enable)
00744         {return loopback(so, enable);};
00745 
00750     inline int getError(void)
00751         {return error(so);};
00752 
00758     inline int ttl(unsigned char time)
00759         {return ttl(so, time);};
00760 
00766     inline int sendsize(unsigned size)
00767         {return sendsize(so, size);};
00768 
00774     inline int sendwait(unsigned size)
00775         {return sendwait(so, size);};
00776 
00777 
00783     inline int recvsize(unsigned size)
00784         {return recvsize(so, size);};
00785 
00791     static int gettype(socket_t socket);
00792 
00799     static unsigned segsize(socket_t socket, unsigned size = 0);
00800 
00807     static bool setccid(socket_t socket, uint8_t ccid);
00808 
00813     inline int gettype(void)
00814         {return gettype(so);};
00815 
00821     inline unsigned segsize(unsigned size)
00822         {return segsize(so, size);};
00823 
00829     inline bool setccid(uint8_t ccid)
00830         {return setccid(so, ccid);};
00831 
00840     inline int tos(int type)
00841         {return tos(so, type);};
00842 
00849     inline int priority(int scheduling)
00850         {return priority(so, scheduling);};
00851 
00855     inline void shutdown(void)
00856         {::shutdown(so, SHUT_RDWR);};
00857 
00865     int connectto(struct addrinfo *list);
00866     
00873     int disconnect(void);
00874 
00880     int join(struct addrinfo *list);
00881 
00887     int drop(struct addrinfo *list);
00888 
00894     int wait(timeout_t timeout = Timer::inf);
00895 
00902     size_t peek(void *data, size_t number) const;
00903 
00911     size_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL);
00912 
00920     size_t writeto(const void *data, size_t number, struct sockaddr *address = NULL);
00921 
00934     size_t readline(char *data, size_t size);
00935 
00941     size_t printf(const char *format, ...) __PRINTF(2,3);
00942 
00954     size_t readline(string& buffer);
00955 
00967     static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
00968 
00975     static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
00976 
00984     size_t writes(const char *string);
00985 
00990     operator bool();
00991 
00996     bool operator!() const;
00997 
01003     Socket& operator=(socket_t socket);
01004 
01009     inline operator socket_t() const
01010         {return so;};
01011 
01016     inline socket_t operator*() const
01017         {return so;};
01018 
01025     static unsigned pending(socket_t socket);
01026 
01033     static int sendsize(socket_t socket, unsigned size);
01034 
01041     static int sendwait(socket_t socket, unsigned size);
01042 
01049     static int recvsize(socket_t socket, unsigned size);
01050 
01059     static int connectto(socket_t socket, struct addrinfo *list);
01060 
01066     static int disconnect(socket_t socket);
01067 
01074     static int drop(socket_t socket, struct addrinfo *list);
01075 
01082     static int join(socket_t socket, struct addrinfo *list);
01083 
01089     static int error(socket_t socket);
01090 
01097     static int multicast(socket_t socket, unsigned ttl = 1);
01098 
01105     static int loopback(socket_t socket, bool enable);
01106 
01113     static int blocking(socket_t socket, bool enable);
01114 
01121     static int keepalive(socket_t socket, bool enable);
01122 
01129     static int broadcast(socket_t socket, bool enable);
01130 
01136     static int nodelay(socket_t socket);
01137 
01144     static int priority(socket_t socket, int scheduling);
01145 
01152     static int tos(socket_t socket, int type);
01153 
01160     static int ttl(socket_t socket, unsigned char time);
01161     
01166     static int getfamily(socket_t socket);
01167 
01173     inline static int getfamily(struct sockaddr_storage& address)
01174         {return ((struct sockaddr *)&address)->sa_family;};
01175 
01181     inline static int getfamily(struct sockaddr_internet& address)
01182         {return address.address.sa_family;};
01183 
01193     static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
01194 
01204     static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, struct sockaddr *address = NULL);
01205 
01215     inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_storage *address)
01216         {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01217 
01227     inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_internet *address)
01228         {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01229 
01239     static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
01240 
01249     static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
01250 
01258     static int listento(socket_t socket, struct sockaddr *address, int backlog = 5);
01259 
01266     static int bindto(socket_t socket, struct sockaddr *address);
01267 
01274     static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
01275 
01283     static socket_t create(int family, int type, int protocol);
01284 
01292     static socket_t create(struct addrinfo *address, int type, int protocol);
01293 
01303     static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01304 
01310     static socket_t create(Socket::address &address);
01311 
01316     static void release(socket_t socket);
01317 
01326     inline static size_t writeto(Socket& socket, const char *buffer, size_t size, struct sockaddr *address)
01327         {return socket.writeto(buffer, size, address);};
01328     
01337     inline static size_t readfrom(Socket& socket, char *buffer, size_t size, struct sockaddr_storage *address)
01338         {return socket.readfrom(buffer, size, address);};
01339 
01345     inline static void connectto(Socket& socket, Socket::address &address)
01346         {socket.connectto(address);};
01347 
01352     inline static void disconnect(Socket& socket)
01353         {socket.disconnect();};
01354 
01361     inline static Socket acceptfrom(Socket& socket, struct sockaddr_storage *address)
01362         {return Socket(acceptfrom(socket.so, address));};
01363 
01371     static char *gethostname(struct sockaddr *address, char *buffer, size_t size);
01372 
01380     static struct addrinfo *gethint(socket_t socket, struct addrinfo *hint);
01381 
01392     static socklen_t getaddr(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
01393 
01399     static socklen_t getlen(struct sockaddr *address);
01400 
01408     static bool equal(struct sockaddr *address1, struct sockaddr *address2);
01409 
01416     static unsigned copy(struct sockaddr *target, struct sockaddr *origin);
01417 
01424     inline static unsigned store(struct sockaddr_storage *storage, struct sockaddr *address)
01425         {return copy((struct sockaddr*)storage, address);};
01426 
01433     static unsigned store(struct sockaddr_internet *storage, struct sockaddr *address);
01434 
01442     static bool equalhost(struct sockaddr *address1, struct sockaddr *address2);
01443 
01451     inline static bool equalfrom(struct sockaddr_storage *address1, struct sockaddr_storage *address2)
01452         {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01453 
01461     inline static bool equalinet(struct sockaddr_internet *address1, struct sockaddr_internet *address2)
01462         {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01463 
01471     static bool subnet(struct sockaddr *address1, struct sockaddr *address2);
01472 
01480     static int getinterface(struct sockaddr *address, struct sockaddr *destination);
01481 
01489     static char *getaddress(struct sockaddr *address, char *buffer, socklen_t size);
01490 
01496     static short getservice(struct sockaddr *address);
01497 
01503     inline static short inetservice(struct sockaddr_internet *address)
01504         {return getservice((struct sockaddr *)address);};
01505 
01512     static unsigned keyindex(struct sockaddr *address, unsigned size);
01513 
01520     static unsigned keyhost(struct sockaddr *address, unsigned size);
01521 
01525     static void init(void);
01526 
01531     static void init(const char *program);
01532 
01538     static void family(int query);
01539 
01546     static void v4mapping(bool enable);
01547 
01552     static int error(void);
01553 
01562     static bool isNull(const char *string);
01563 
01571     static bool isNumeric(const char *string);
01572 
01581     static int getlocal(socket_t socket, struct sockaddr_storage *address);
01582 
01591     static int getremote(socket_t socket, struct sockaddr_storage *address);
01592 
01600     static int select(int max, set_t read, set_t write, set_t error);
01601 
01610     static int select(int max, set_t read, set_t write, set_t error, timeout_t timeout);
01611 
01616     static set_t getmask(void);
01617     
01622     static void clear(set_t mask);
01623 
01628     static void release(set_t mask);
01629 
01635     static void set(socket_t socket, set_t mask);
01636 
01642     static void clear(socket_t socket, set_t mask);
01643 
01650     static bool test(socket_t socket, set_t mask);
01651 };
01652 
01658 class __EXPORT ListenSocket : protected Socket
01659 {
01660 public:
01670     ListenSocket(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01671 
01682     static socket_t create(const char *address, const char *service, unsigned backlog = 5, int family = AF_UNSPEC, int type = 0, int protocol = 0);
01683 
01689     socket_t accept(struct sockaddr_storage *address = NULL) const;
01690 
01696     inline bool waitConnection(timeout_t timeout = Timer::inf) const
01697         {return Socket::waitPending(timeout);};
01698 
01703     inline operator socket_t() const
01704         {return so;};
01705 
01710     inline socket_t operator*() const
01711         {return so;};   
01712 
01717     inline socket_t getsocket(void) const
01718         {return so;};
01719 
01720 };
01721 
01727 class __EXPORT TCPServer : public ListenSocket
01728 {
01729 public:
01737     TCPServer(const char *address, const char *service, unsigned backlog = 5);
01738 };
01739 
01743 typedef Socket socket;
01744 
01750 inline struct addrinfo *addrinfo(socket::address& address)
01751     {return address.getList();}
01752 
01759 inline struct sockaddr *addr(socket::address& address)
01760     {return address.getAddr();}
01761 
01769 inline bool eq(struct sockaddr *s1, struct sockaddr *s2)
01770     {return Socket::equal(s1, s2);}
01771 
01779 inline bool eq(struct sockaddr_storage *s1, struct sockaddr_storage *s2)
01780     {return Socket::equal((struct sockaddr *)s1, (struct sockaddr *)s2);}
01781 
01789 inline bool ieq(struct sockaddr *s1, struct sockaddr *s2)
01790     {return Socket::equalhost(s1, s2);}
01791 
01792 String str(Socket& so, strsize_t size);
01793 
01794 typedef TCPServer   tcpserv_t;
01795 
01796 END_NAMESPACE
01797 
01798 #endif

Generated on Fri Oct 1 2010 for UCommon by  doxygen 1.7.1