sockbase.h

Go to the documentation of this file.
00001 /*
00002     socklibpp library
00003     Copyright (C) 2005  Daniel K. O. <danielosmari at users.sf.net>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library 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 GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00020 #ifndef SOCK_BASE_H
00021 #define SOCK_BASE_H
00022 
00023 
00024 
00029 #include "sockaddr.h"
00030 
00036 namespace socklibpp {
00037 
00038 
00043 struct sock
00044 {
00045 
00047         enum format {
00048                 pf_unspec       = PF_UNSPEC,    
00049                 pf_unix         = PF_UNIX,      
00050                 pf_inet         = PF_INET,      
00051                 pf_inet6        = PF_INET6,     
00052                 pf_max          = PF_MAX        
00053         };
00054         
00056         enum type {
00057                 sock_stream     = SOCK_STREAM,  
00058                 sock_dgram      = SOCK_DGRAM,   
00059                 sock_raw        = SOCK_RAW,     
00060                 sock_rdm        = SOCK_RDM,
00061                 sock_seqpacket  = SOCK_SEQPACKET
00062         };
00063 
00065         enum protocol {
00066                 proto_ip        = IPPROTO_IP,   
00067                 proto_icmp      = IPPROTO_ICMP, 
00068                 proto_tcp       = IPPROTO_TCP,  
00069                 proto_pup       = IPPROTO_PUP,  
00070                 proto_udp       = IPPROTO_UDP,  
00071                 proto_idp       = IPPROTO_IDP,  
00072                 proto_raw       = IPPROTO_RAW,  
00073                 proto_max       = IPPROTO_MAX   
00074                 /* TODO (DanielKO#2#): Support IPv6 protocol options */
00075         };
00076 
00077         enum {
00078                 invalid_socket  = INVALID_SOCKET        
00079         };
00080 
00082         enum msgflag {
00083                 msg_none        = 0,            
00084                 msg_oob         = MSG_OOB,      
00085                 msg_peek        = MSG_PEEK,     
00086                 msg_dontroute   = MSG_DONTROUTE 
00087         };
00088 
00090         enum shuthow {
00091                 shut_recv       = 0,    
00092                 shut_send       = 1,    
00093                 shut_both       = 2     
00094         };
00095 
00097         enum errors {
00098                 /* TODO (DanielKO#2#): Add more error enums here */
00099                 socket_error    = SOCKET_ERROR, 
00100 
00101                 eintr           = WSAEINTR,
00102                 ebadf           = WSAEBADF,
00103                 eacces          = WSAEACCES,
00104                 efault          = WSAEFAULT,
00105                 einval          = WSAEINVAL,
00106                 emfile          = WSAEMFILE,
00107                 ewouldblock     = WSAEWOULDBLOCK,       
00108                 einprogress     = WSAEINPROGRESS,
00109                 ealready        = WSAEALREADY,
00110                 enotsock        = WSAENOTSOCK,
00111                 edestaddrreq    = WSAEDESTADDRREQ,
00112                 emsgsize        = WSAEMSGSIZE,
00113                 eprototype      = WSAEPROTOTYPE,
00114                 enoprotoopt     = WSAENOPROTOOPT,
00115                 eprotonosupport = WSAEPROTONOSUPPORT,
00116                 esocktnotsupport = WSAESOCKTNOSUPPORT,
00117                 eopnotsupp      = WSAEOPNOTSUPP,
00118                 epfnosupport    = WSAEPFNOSUPPORT,
00119                 afnosupport     = WSAEAFNOSUPPORT,
00120                 eaddrinuse      = WSAEADDRINUSE,
00121                 eaddrnotavail   = WSAEADDRNOTAVAIL,
00122                 enetdown        = WSAENETDOWN,
00123                 enetunreach     = WSAENETUNREACH,
00124                 enetreset       = WSAENETRESET,
00125                 econnaborted    = WSAECONNABORTED,
00126                 econnreset      = WSAECONNRESET,
00127                 enobufs         = WSAENOBUFS,
00128                 eisconn         = WSAEISCONN,
00129                 enotconn        = WSAENOTCONN,
00130                 eshutdown       = WSAESHUTDOWN,
00131                 etoomanyrefs    = WSAETOOMANYREFS,
00132                 etimedout       = WSAETIMEDOUT,
00133                 econnrefused    = WSAECONNREFUSED,
00134                 eloop           = WSAELOOP,
00135                 enametoolong    = WSAENAMETOOLONG,
00136                 ehostdown       = WSAEHOSTDOWN,
00137                 ehostunreach    = WSAEHOSTUNREACH,
00138                 host_not_found  = WSAHOST_NOT_FOUND,
00139                 try_again       = WSATRY_AGAIN,
00140                 no_recovery     = WSANO_RECOVERY,
00141                 no_data         = WSANO_DATA
00142         };
00143 
00145         enum ioctl_cmd {
00146                 fionbio         = FIONBIO,      
00147                 fionread        = FIONREAD,     
00148                 siocatmark      = SIOCATMARK    
00149         };
00150 
00152         enum mode {
00153                 passive,        
00154                 active          
00155         };
00156         
00158         enum optlevel {
00159                 sol_socket      = SOL_SOCKET,
00160                 ipproto_tcp     = IPPROTO_TCP
00161         };
00162         
00164         enum optname {
00165                 so_acceptconn   = SO_ACCEPTCONN,        
00166                 so_broadcast    = SO_BROADCAST,         
00167                 so_debug        = SO_DEBUG,             
00168 //                so_dontlinger   = SO_DONTLINGER,        ///< (bool) If true, the so_linger option is disabled..
00169                 so_dontroute    = SO_DONTROUTE,         
00170                 so_error        = SO_ERROR,             
00171                 so_keepalive    = SO_KEEPALIVE,         
00172                 so_linger       = SO_LINGER,            
00173                 so_oobinline    = SO_OOBINLINE,         
00174                 so_rcvbuf       = SO_RCVBUF,            
00175                 so_reuseaddr    = SO_REUSEADDR,         
00176                 so_sndbuf       = SO_SNDBUF,            
00177                 so_type         = SO_TYPE,              
00178                 tcp_nodelay     = TCP_NODELAY           
00179         };
00180 
00182         enum selectstate {
00183                 readable = 1,   
00184                 writable = 2,   
00185                 exceptable = 4  
00186         };
00187 
00188 
00189         // Here are the two data members of socklibpp::sock
00190 
00199         socket_type fd;
00200         
00201         
00209         mutable int error;
00210         
00211 
00218         sock() throw() : fd(invalid_socket), error(0)  {}
00219 
00223         sock(socket_type fd_) throw() : fd(fd_), error(0) {}
00224 
00225 
00227         sock(format, type, protocol = proto_ip) throw();
00228 
00229 
00231         sock accept() throw();
00232 
00235         sock accept(addr&) throw();
00236 
00237 
00239         bool bind(const addr&) throw();
00240 
00241 
00243         bool close() throw();
00244 
00245 
00247         bool connect(const addr&) throw();
00248 
00249 
00251         bool peername(addr&) const throw();
00252         
00253 
00255         bool sockname(addr&) throw();
00256 
00257 
00258         /* TODO (DanielKO#2#): Support the so_linger option */
00260         bool getsockopt(optlevel, optname, int&) const throw();
00262         bool getsockopt(optlevel, optname, bool&) const throw();
00263 
00264 
00266         bool ioctl(ioctl_cmd, unsigned long&) throw();
00267 
00268 
00270         bool listen(int=3) throw();
00271         
00272 
00274         int recv(char *, int, msgflag=msg_none) throw();
00275 
00276 
00278         int recvfrom(addr&, char*, int, msgflag=msg_none) throw();
00279 
00280 
00281         // for select()
00283         bool can_read(long=0, long=0) const throw();
00284 
00286         bool wait_read(long&, long&) const throw();
00287         
00289         bool wait_read() const throw();
00290         
00292         bool can_write(int=0, int=0) throw();
00294         bool can_except(int=0, int=0) throw();
00295 
00297         unsigned select(int=0, int=0) throw();
00298 
00300         int send(const char*, int, msgflag=msg_none) throw();
00301 
00303         int sendto(const addr&, const char*, int, msgflag=msg_none) throw();
00304 
00306         /* TODO (DanielKO#2#): Support the so_linger option */
00307         bool setsockopt(optlevel, optname, int) throw();
00308 
00309 
00311         bool shutdown(shuthow) throw();
00312         
00313         
00315         bool create(format, type, protocol = proto_ip) throw();
00316 
00318         inline bool socket(format fmt, type tp, protocol pr = proto_ip) throw()
00319         {       return create(fmt, tp, pr);     }
00320 
00321 
00322 
00323 
00324         // ****************** Convenience functions **********************
00325 
00326 
00328         bool sendall(const char*, int, msgflag=msg_none) throw();
00329 
00330 
00332         bool recvall(char *, int, msgflag=msg_none) throw();
00333 
00334 
00336         bool block(bool) throw();
00337 
00338 
00340         bool reuseaddr() const throw();
00341 
00343         bool reuseaddr(bool) throw();
00344 
00345 
00347         int sendbuf() const throw();
00348 
00350         void sendbuf(int) throw();
00351         
00352 
00354         int recvbuf() const throw();
00355 
00357         void recvbuf(int) throw();
00358 
00359         /* TODO (DanielKO#2#): Add more convenience methods for {set|get}sockopt */
00360 
00361 };
00362 
00363 
00364 
00365 }
00366 
00367 #endif

Generated on Thu Jan 18 19:26:35 2007 for socklib++ by  doxygen 1.5.1