• 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 extern "C" {
00038     struct addrinfo;
00039 }
00040 
00041 #ifdef  _MSWINDOWS_
00042 #define SHUT_RDWR   SD_BOTH
00043 #define SHUT_WR     SD_SEND
00044 #define SHUT_RD     SD_RECV
00045 #else
00046 #include <unistd.h>
00047 #include <sys/socket.h>
00048 #include <net/if.h>
00049 #include <netinet/in.h>
00050 #include <netdb.h>
00051 #endif
00052 
00053 #include <errno.h>
00054 #include <stdio.h>
00055 
00056 #ifndef IPTOS_LOWDELAY
00057 #define IPTOS_LOWDELAY      0x10
00058 #define IPTOS_THROUGHPUT    0x08
00059 #define IPTOS_RELIABILITY   0x04
00060 #define IPTOS_MINCOST       0x02
00061 #endif
00062 
00063 #ifdef  AF_UNSPEC
00064 #define DEFAULT_FAMILY  AF_UNSPEC
00065 #else
00066 #define DEFAULT_FAMILY  AF_INET
00067 #endif
00068 
00069 struct sockaddr_internet;
00070 
00074 typedef struct hostaddr_internet
00075 {
00076     union
00077     {
00078         struct in_addr ipv4;
00079 #ifdef  AF_INET6
00080         struct in6_addr ipv6;
00081 #endif
00082     };
00083 }   inethostaddr_t;
00084 
00085 #if defined(AF_INET6) || defined(__CYGWIN__)
00086 
00093 typedef struct sockaddr_internet
00094 {
00095     union {
00096 #ifdef  AF_INET6
00097         struct sockaddr_in6 ipv6;
00098 #endif
00099         struct sockaddr_in ipv4;
00100         struct sockaddr address;
00101     };
00102 } inetsockaddr_t;
00103 #else
00104 typedef struct sockaddr_internet
00105 {
00106     union {
00107         struct sockaddr_in ipv4;
00108         struct sockaddr address;
00109     };
00110 } inetsockaddr_t;
00111 
00112 struct sockaddr_storage
00113 {
00114 #ifdef  AF_UNIX
00115     char sa_data[128];
00116 #else
00117     char sa_data[sizeof(struct sockaddr_in)];
00118 #endif
00119 };
00120 #endif
00121 
00122 #ifndef SOCK_DCCP
00123 #define SOCK_DCCP       6
00124 #endif
00125 
00126 #ifndef IPPROTO_DCCP
00127 #define IPPROTO_DCCP    23
00128 #endif
00129 
00130 #ifndef SOL_DCCP
00131 #define SOL_DCCP        269
00132 #endif
00133 
00134 #define DCCP_SOCKOPT_AVAILABLE_CCIDS    12
00135 #define DCCP_SOCKOPT_CCID               13
00136 #define DCCP_SOCKOPT_TX_CCID            14
00137 #define DCCP_SOCKOPT_RX_CCID            15
00138 
00139 NAMESPACE_UCOMMON
00140 
00150 class __EXPORT cidr : public LinkedObject
00151 {
00152 protected:
00153     int family;
00154     inethostaddr_t netmask, network;
00155     char name[16];
00156     unsigned getMask(const char *cp) const;
00157 
00158 public:
00162     typedef LinkedObject policy;
00163 
00167     cidr();
00168 
00175     cidr(const char *string);
00176     
00182     cidr(policy **policy, const char *string);
00183 
00190     cidr(policy **policy, const char *string, const char *name);
00191 
00196     cidr(const cidr& existing);
00197 
00204     static cidr *find(policy *policy, const struct sockaddr *address);
00205 
00213     static cidr *container(policy *policy, const struct sockaddr *address);
00214 
00222     inline const char *getName(void) const
00223         {return name;};
00224 
00229     inline int getFamily(void) const
00230         {return family;};
00231 
00236     inline inethostaddr_t getNetwork(void) const
00237         {return network;};
00238 
00243     inline inethostaddr_t getNetmask(void) const
00244         {return netmask;};
00245 
00250     inethostaddr_t getBroadcast(void) const;
00251 
00256     unsigned getMask(void) const;
00257     
00262     void set(const char *string);
00263 
00269     bool isMember(const struct sockaddr *address) const;
00270 
00276     inline bool operator==(const struct sockaddr *address) const
00277         {return isMember(address);};
00278     
00284     inline bool operator!=(const struct sockaddr *address) const
00285         {return !isMember(address);}; 
00286 };
00287 
00295 class __EXPORT Socket 
00296 {
00297 protected:
00298     socket_t so;
00299 
00308     static struct addrinfo *getaddress(const char *host, const char *service, int type = SOCK_STREAM, int protocol = 0);
00309 
00315     static void release(struct addrinfo *list);
00316 
00317 public:
00323     typedef void *set_t;
00324 
00325     static const size_t masksize;
00326 
00335     class __EXPORT address
00336     {
00337     protected:
00338         struct addrinfo *list;
00339 
00340     public:
00348         address(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00349 
00356         address(Socket& socket, const char *hostname, const char *service = NULL);
00357 
00364         address(socket_t socket, const char *hostname, const char *service = NULL);
00365 
00372         address(const char *hostname, unsigned service = 0, int family = DEFAULT_FAMILY);
00373 
00377         address();
00378 
00383         address(const address& reference);
00384 
00388         ~address();
00389 
00394         struct sockaddr *getAddr(void) const;
00395 
00401         struct sockaddr *get(int family) const;
00402 
00407         int getfamily(void) const;
00408 
00413         struct sockaddr *find(struct sockaddr *addr) const;
00414 
00419         inline struct addrinfo *getList(void) const
00420             {return list;};
00421 
00426         inline operator struct addrinfo *() const
00427             {return list;};
00428 
00433         inline struct addrinfo *operator*() const
00434             {return list;};
00435 
00440         inline operator bool() const
00441             {return list != NULL;};
00442 
00447         inline bool operator!() const
00448             {return list == NULL;};
00449 
00454         inline operator struct sockaddr *() const
00455             {return getAddr();};
00456 
00460         void clear(void);
00461 
00469         void set(const char *hostname, const char *service = NULL, int family = 0, int type = SOCK_STREAM);
00470 
00478         void add(const char *hostname, const char *service = NULL, int family = 0, int type = SOCK_STREAM);
00479 
00487         void set(int family, const char *address, int type = SOCK_STREAM, int protocol = 0);
00488 
00493         void add(sockaddr *address);
00494 
00500         bool remove(struct sockaddr *address);
00501 
00508         bool insert(struct sockaddr *address);
00509 
00516         unsigned insert(struct addrinfo *address, int family = 0);
00517 
00524         unsigned remove(struct addrinfo *address, int family = 0);
00525 
00531         void copy(const struct addrinfo *address);
00532 
00537         void set(struct sockaddr *address);
00538 
00545         void set(const char *hostname, unsigned service = 0, int family = DEFAULT_FAMILY);
00546 
00552         static struct sockaddr *dup(struct sockaddr *address);
00553 
00559         static struct sockaddr_in *ipv4(struct sockaddr *address);
00560 
00561 #ifdef  AF_INET6
00562 
00567         static struct sockaddr_in6 *ipv6(struct sockaddr *address);
00568 #endif
00569     };
00570 
00571     friend class address;
00572 
00576     Socket();
00577 
00582     Socket(const Socket& existing);
00583 
00588     Socket(socket_t socket);
00589 
00595     Socket(struct addrinfo *address);
00596 
00603     Socket(int family, int type, int protocol = 0);
00604 
00614     Socket(const char *address, const char *port, int family = AF_UNSPEC, int type = 0, int protocol = 0, int backlog = 0);
00615 
00619     virtual ~Socket();
00620 
00624     void cancel(void);
00625 
00630     static void cancel(socket_t socket);
00631 
00635     void release(void);
00636 
00642     bool isPending(unsigned value) const;
00643 
00648     bool isConnected(void) const;
00649 
00656     bool waitPending(timeout_t timeout = 0) const;
00657 
00662     inline int nodelay(void) const
00663         {return nodelay(so);};
00664 
00672     static bool wait(socket_t socket, timeout_t timeout = 0);
00673 
00680     bool waitSending(timeout_t timeout = 0) const;
00681     
00686     inline unsigned getPending(void) const
00687         {return pending(so);};
00688 
00694     inline int broadcast(bool enable)
00695         {return broadcast(so, enable);};
00696 
00702     inline int keepalive(bool enable)
00703         {return keepalive(so, enable);};
00704 
00710     inline int blocking(bool enable)
00711         {return blocking(so, enable);};
00712 
00718     inline int multicast(unsigned ttl = 1)
00719         {return multicast(so, ttl);};
00720 
00726     inline int loopback(bool enable)
00727         {return loopback(so, enable);};
00728 
00733     inline int getError(void)
00734         {return error(so);};
00735 
00741     inline int ttl(unsigned char time)
00742         {return ttl(so, time);};
00743 
00749     inline int sendsize(unsigned size)
00750         {return sendsize(so, size);};
00751 
00757     inline int sendwait(unsigned size)
00758         {return sendwait(so, size);};
00759 
00760 
00766     inline int recvsize(unsigned size)
00767         {return recvsize(so, size);};
00768 
00774     static int gettype(socket_t socket);
00775 
00782     static unsigned segsize(socket_t socket, unsigned size = 0);
00783 
00790     static bool setccid(socket_t socket, uint8_t ccid);
00791 
00796     inline int gettype(void)
00797         {return gettype(so);};
00798 
00804     inline unsigned segsize(unsigned size)
00805         {return segsize(so, size);};
00806 
00812     inline bool setccid(uint8_t ccid)
00813         {return setccid(so, ccid);};
00814 
00823     inline int tos(int type)
00824         {return tos(so, type);};
00825 
00832     inline int priority(int scheduling)
00833         {return priority(so, scheduling);};
00834 
00838     inline void shutdown(void)
00839         {::shutdown(so, SHUT_RDWR);};
00840 
00848     inline int connectto(struct addrinfo *list)
00849         {return connectto(so, list);};
00850     
00857     inline int disconnect(void)
00858         {return disconnect(so);};
00859 
00865     inline int join(struct addrinfo *list)
00866         {return join(so, list);};
00867 
00873     inline int drop(struct addrinfo *list)
00874         {return drop(so, list);};
00875 
00882     size_t peek(void *data, size_t number) const;
00883 
00892     virtual ssize_t get(void *data, size_t number, struct sockaddr_storage *address = NULL);
00893 
00902     virtual ssize_t put(const void *data, size_t number, struct sockaddr *address = NULL);
00903 
00911     inline ssize_t readfrom(void *data, size_t number, struct sockaddr_storage *address = NULL)
00912         {return get(data, number, address);};
00913 
00921     inline ssize_t writeto(const void *data, size_t number, struct sockaddr *address = NULL)
00922         {return put(data, number, address);};
00923 
00934     virtual ssize_t gets(char *data, size_t size, timeout_t timeout = Timer::inf);
00935 
00947     static ssize_t readline(socket_t socket, char *data, size_t size, timeout_t timeout = Timer::inf);
00948 
00955     static ssize_t printf(socket_t socket, const char *format, ...) __PRINTF(2,3);
00956 
00962     ssize_t puts(const char *string);
00963 
00972     ssize_t puts(const char *string, struct sockaddr *address);
00973 
00978     operator bool();
00979 
00984     bool operator!() const;
00985 
00991     Socket& operator=(socket_t socket);
00992 
00997     inline operator socket_t() const
00998         {return so;};
00999 
01004     inline socket_t operator*() const
01005         {return so;};
01006 
01013     static unsigned pending(socket_t socket);
01014 
01021     static int sendsize(socket_t socket, unsigned size);
01022 
01029     static int sendwait(socket_t socket, unsigned size);
01030 
01037     static int recvsize(socket_t socket, unsigned size);
01038 
01047     static int connectto(socket_t socket, struct addrinfo *list);
01048 
01054     static int disconnect(socket_t socket);
01055 
01062     static int drop(socket_t socket, struct addrinfo *list);
01063 
01070     static int join(socket_t socket, struct addrinfo *list);
01071 
01077     static int error(socket_t socket);
01078 
01085     static int multicast(socket_t socket, unsigned ttl = 1);
01086 
01093     static int loopback(socket_t socket, bool enable);
01094 
01101     static int blocking(socket_t socket, bool enable);
01102 
01109     static int keepalive(socket_t socket, bool enable);
01110 
01117     static int broadcast(socket_t socket, bool enable);
01118 
01124     static int nodelay(socket_t socket);
01125 
01132     static int priority(socket_t socket, int scheduling);
01133 
01140     static int tos(socket_t socket, int type);
01141 
01148     static int ttl(socket_t socket, unsigned char time);
01149     
01154     static int getfamily(socket_t socket);
01155 
01161     inline static int getfamily(struct sockaddr_storage& address)
01162         {return ((struct sockaddr *)&address)->sa_family;};
01163 
01169     inline static int getfamily(struct sockaddr_internet& address)
01170         {return address.address.sa_family;};
01171 
01181     static ssize_t recvfrom(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_storage *address = NULL);
01182 
01192     static ssize_t sendto(socket_t socket, const void *buffer, size_t size, int flags = 0, struct sockaddr *address = NULL);
01193 
01203     inline static ssize_t replyto(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_storage *address)
01204         {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01205 
01215     inline static ssize_t sendinet(socket_t socket, const void *buffer, size_t size, int flags, struct sockaddr_internet *address)
01216         {return sendto(socket, buffer, size, flags, (struct sockaddr *)address);};
01217 
01227     static ssize_t recvinet(socket_t socket, void *buffer, size_t size, int flags = 0, struct sockaddr_internet *address = NULL);
01228 
01237     static int bindto(socket_t socket, const char *address, const char *service, int protocol = 0);
01238 
01246     static int listento(socket_t socket, struct sockaddr *address, int backlog = 5);
01247 
01254     static int bindto(socket_t socket, struct sockaddr *address);
01255 
01262     static socket_t acceptfrom(socket_t socket, struct sockaddr_storage *address = NULL);
01263 
01271     static socket_t create(int family, int type, int protocol);
01272 
01280     static socket_t create(struct addrinfo *address, int type, int protocol);
01281 
01292     static socket_t create(const char *iface, const char *service, int family = AF_UNSPEC, int type = 0, int protocol = 0, int backlog = 0);
01293 
01299     static socket_t create(Socket::address &address);
01300 
01305     static void release(socket_t socket);
01306 
01315     inline static ssize_t writeto(Socket& socket, const char *buffer, size_t size, struct sockaddr *address)
01316         {return socket.put(buffer, size, address);};
01317     
01326     inline static ssize_t readfrom(Socket& socket, char *buffer, size_t size, struct sockaddr_storage *address)
01327         {return socket.get(buffer, size, address);};
01328 
01334     inline static void connectto(Socket& socket, Socket::address &address)
01335         {socket.connectto(address);};
01336 
01341     inline static void disconnect(Socket& socket)
01342         {socket.disconnect();};
01343 
01350     inline static Socket acceptfrom(Socket& socket, struct sockaddr_storage *address)
01351         {return Socket(acceptfrom(socket.so, address));};
01352 
01360     static char *gethostname(struct sockaddr *address, char *buffer, size_t size);
01361 
01369     static struct addrinfo *gethint(socket_t socket, struct addrinfo *hint);
01370 
01381     static socklen_t getaddr(socket_t socket, struct sockaddr_storage *address, const char *hostname, const char *service);
01382 
01388     static socklen_t getlen(struct sockaddr *address);
01389 
01397     static bool equal(struct sockaddr *address1, struct sockaddr *address2);
01398 
01405     static unsigned copy(struct sockaddr *target, struct sockaddr *origin);
01406 
01413     inline static unsigned store(struct sockaddr_storage *storage, struct sockaddr *address)
01414         {return copy((struct sockaddr*)storage, address);};
01415 
01422     static unsigned store(struct sockaddr_internet *storage, struct sockaddr *address);
01423 
01431     static bool equalhost(struct sockaddr *address1, struct sockaddr *address2);
01432 
01440     inline static bool equalfrom(struct sockaddr_storage *address1, struct sockaddr_storage *address2)
01441         {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01442 
01450     inline static bool equalinet(struct sockaddr_internet *address1, struct sockaddr_internet *address2)
01451         {return equal((struct sockaddr *)address1, (struct sockaddr *)address2);};
01452 
01460     static bool subnet(struct sockaddr *address1, struct sockaddr *address2);
01461 
01469     static int getinterface(struct sockaddr *address, struct sockaddr *destination);
01470 
01478     static char *getaddress(struct sockaddr *address, char *buffer, socklen_t size);
01479 
01485     static short getservice(struct sockaddr *address);
01486 
01492     inline static short inetservice(struct sockaddr_internet *address)
01493         {return getservice((struct sockaddr *)address);};
01494 
01501     static unsigned keyindex(struct sockaddr *address, unsigned size);
01502 
01509     static unsigned keyhost(struct sockaddr *address, unsigned size);
01510 
01514     static void init(void);
01515 
01520     static void init(const char *program);
01521 
01527     static void family(int query);
01528 
01535     static void v4mapping(bool enable);
01536 
01543     static FILE *open(socket_t socket, bool mode = false);
01544 
01550     inline FILE *open(bool mode = false)
01551         {return open(so, mode);};
01552 
01557     static void close(FILE *file);
01558 
01563     static int error(void);
01564 
01573     static bool isNull(const char *string);
01574 
01582     static bool isNumeric(const char *string);
01583 
01592     static int getlocal(socket_t socket, struct sockaddr_storage *address);
01593 
01602     static int getremote(socket_t socket, struct sockaddr_storage *address);
01603 
01611     static int select(int max, set_t read, set_t write, set_t error);
01612 
01621     static int select(int max, set_t read, set_t write, set_t error, timeout_t timeout);
01622 
01627     static set_t getmask(void);
01628     
01633     static void clear(set_t mask);
01634 
01639     static void release(set_t mask);
01640 
01646     static void set(socket_t socket, set_t mask);
01647 
01653     static void clear(socket_t socket, set_t mask);
01654 
01661     static bool test(socket_t socket, set_t mask);
01662 };
01663 
01669 class __EXPORT ListenSocket : protected Socket
01670 {
01671 public:
01679     ListenSocket(const char *address, const char *service, unsigned backlog = 5, int protocol = 0);
01680 
01686     socket_t accept(struct sockaddr_storage *address = NULL);
01687 
01693     inline bool waitConnection(timeout_t timeout = Timer::inf) const
01694         {return Socket::waitPending(timeout);};
01695 
01700     inline operator socket_t() const
01701         {return so;};
01702 
01707     inline socket_t operator*() const
01708         {return so;};   
01709 
01714     inline socket_t getsocket(void) const
01715         {return so;};
01716 
01717 };
01718 
01722 typedef Socket socket;
01723 
01729 inline struct addrinfo *addrinfo(socket::address& address)
01730     {return address.getList();}
01731 
01738 inline struct sockaddr *addr(socket::address& address)
01739     {return address.getAddr();}
01740 
01748 inline bool eq(struct sockaddr *s1, struct sockaddr *s2)
01749     {return Socket::equal(s1, s2);}
01750 
01758 inline bool eq(struct sockaddr_storage *s1, struct sockaddr_storage *s2)
01759     {return Socket::equal((struct sockaddr *)s1, (struct sockaddr *)s2);}
01760 
01768 inline bool ieq(struct sockaddr *s1, struct sockaddr *s2)
01769     {return Socket::equalhost(s1, s2);}
01770 
01771 END_NAMESPACE
01772 
01773 #endif

Generated on Sun Sep 4 2011 for UCommon by  doxygen 1.7.1