00001 /* 00002 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00003 May be copied only for purposes and under conditions described 00004 on the Web page www.basepath.com/aup/copyright.htm. 00005 00006 The Example Files are provided "as is," without any warranty; 00007 without even the implied warranty of merchantability or fitness 00008 for a particular purpose. The author and his publisher are not 00009 responsible for any damages, direct or incidental, resulting 00010 from the use or non-use of these Example Files. 00011 00012 The Example Files may contain defects, and some contain deliberate 00013 coding mistakes that were included for educational reasons. 00014 You are responsible for determining if and how the Example Files 00015 are to be used. 00016 00017 */ 00018 #ifndef _UXSOCKET_HPP_ 00019 #define _UXSOCKET_HPP_ 00020 00021 #ifdef SOLARIS 00022 #define __EXTENSIONS__ 00023 #endif 00024 #include <sys/socket.h> 00025 #include <sys/un.h> 00026 #include <netinet/in.h> 00027 #include <netdb.h> 00028 #ifdef SOLARIS 00029 #undef _EXTENSIONS_ 00030 #endif 00031 00032 namespace Ux { 00033 00034 /** 00035 \ingroup Ux 00036 */ 00037 class SockIPv4 : public Base { 00038 protected: 00039 in_addr_t ipv4; 00040 00041 public: 00042 SockIPv4(in_addr_t ip = 0) 00043 { set(ip); } 00044 SockIPv4(const char *str) 00045 { set(str); } 00046 void set(const char *str); 00047 void set(in_addr_t ip = 0) 00048 { ipv4 = ip; } 00049 in_addr_t get_ipv4(void) const 00050 { return ipv4; } 00051 const char *get_string(char *buf, socklen_t bufsize) const; 00052 }; 00053 00054 /** 00055 \ingroup Ux 00056 */ 00057 class SockIPv6 : public Base { 00058 protected: 00059 uint8_t ipv6[16]; 00060 00061 public: 00062 SockIPv6(uint8_t *ip = NULL) 00063 { set(ip); } 00064 SockIPv6(const char *str) 00065 { set(str); } 00066 void set(const char *str); 00067 void set(uint8_t *ip = NULL); 00068 const uint8_t *get_ipv6(void) const 00069 { return ipv6; } 00070 const char *get_string(char *buf, socklen_t bufsize) const; 00071 }; 00072 00073 /** 00074 \ingroup Ux 00075 */ 00076 class SockAddr : public Base { 00077 protected: 00078 struct sockaddr_storage sa_storage; 00079 socklen_t sa_len; 00080 int sa_protocol; 00081 00082 public: 00083 SockAddr(void) 00084 : sa_len(0), sa_protocol(0) 00085 { } 00086 const struct sockaddr *get_addr(void) const 00087 { return (const struct sockaddr *)&sa_storage; } 00088 struct sockaddr *get_addr(void) 00089 { return (struct sockaddr *)&sa_storage; } 00090 socklen_t get_len(void) const 00091 { return sa_len; } 00092 socklen_t *get_len_ptr(void) 00093 { return &sa_len; } 00094 void set_len(socklen_t len) 00095 { sa_len = len; } 00096 void set_protocol(int protocol) 00097 { sa_protocol = protocol; } 00098 int get_protocol(void) 00099 { return sa_protocol; } 00100 static void getaddrinfo(const char *nodename, const char *servname, const struct addrinfo *hint, 00101 struct addrinfo **infop); 00102 static void getaddrinfo(const char *nodename, const char *servname, int family, int type, int flags, 00103 struct addrinfo **infop); 00104 static void freeaddrinfo(struct addrinfo *infop) 00105 { ::freeaddrinfo(infop); } 00106 void set_server(const char *nodename, const char *servname = "80"); 00107 static void getnameinfo(const struct sockaddr *sa, socklen_t sa_len, char *nodename, 00108 socklen_t nodelen, char *servname, socklen_t servlen, unsigned flags); 00109 }; 00110 00111 /** 00112 \ingroup Ux 00113 */ 00114 class SockAddrUn : public SockAddr { 00115 public: 00116 SockAddrUn(const char *path = "") 00117 { set(path); } 00118 void set(const char *path = ""); 00119 }; 00120 00121 /** 00122 \ingroup Ux 00123 */ 00124 class SockAddrIn : public SockAddr { 00125 public: 00126 SockAddrIn(in_port_t port, struct in_addr& sa) 00127 { set(port, sa); } 00128 SockAddrIn(in_port_t port, in_addr_t ipv4) 00129 { set(port, ipv4); } 00130 SockAddrIn(in_port_t port, const char *dotted) 00131 { set(port, dotted); } 00132 void set(in_port_t port, struct in_addr& sa); 00133 void set(in_port_t port, in_addr_t ipv4); 00134 void set(in_port_t port, const char *dotted); 00135 }; 00136 00137 /** 00138 \ingroup Ux 00139 */ 00140 class Socket : public File { 00141 00142 public: 00143 Socket(int f = -1, const char *p = NULL, ssize_t s = -1) 00144 : File(f, p, s) 00145 { } 00146 Socket(const char *p, ssize_t s = -1) 00147 : File(-1, p, s) 00148 { } 00149 Socket accept(SockAddr *sa = NULL); 00150 void connect(const SockAddr& sa); 00151 void bind(const SockAddr& sa); 00152 void getpeername(SockAddr& sa); 00153 void getsockname(SockAddr& sa); 00154 void getsockopt(int level, int option, void *value, socklen_t& value_len); 00155 void listen(int backlog = SOMAXCONN); 00156 ssize_t recv(void *buffer, size_t length, int flags); 00157 ssize_t recvfrom(void *buffer, size_t length, int flags, struct sockaddr *sa, 00158 socklen_t *sa_len); 00159 ssize_t recvmsg(struct msghdr *message, int flags); 00160 ssize_t send(const void *data, size_t length, int flags); 00161 ssize_t sendmsg(const struct msghdr *message, int flags); 00162 ssize_t sendto(const void *message, size_t length, int flags, const struct sockaddr *sa, 00163 socklen_t sa_len); 00164 void setsockopt(int level, int option, const void *value, socklen_t value_len); 00165 void shutdown(int how); 00166 bool sockatmark(void); 00167 void socket(int domain = AF_UNIX, int type = SOCK_STREAM, int protocol = 0); 00168 static void socketpair(Socket sv[2], int domain = AF_UNIX, int type = SOCK_STREAM, int protocol = 0); 00169 00170 }; 00171 00172 } // namespace 00173 00174 #endif // _UXSOCKET_HPP_