raw_socket.h

Go to the documentation of this file.
00001 #ifndef RAW_SOCKET_CLASS
00002 #define RAW_SOCKET_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : raw_socket                                                        *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00019 
00029 #include <basis/array.h>
00030 #include <basis/astring.h>
00031 #include <basis/contracts.h>
00032 
00033 namespace sockets {
00034 
00035 // forward declarations.
00036 class fd_set_wrapper;
00037 class tcpip_stack;
00038 
00040 
00041 // sockets can be intrigued by the occurrence of the following conditions:
00042 enum socket_interests {
00043   SI_READABLE      = 0x1,   // the socket is readable; there's data there.
00044   SI_WRITABLE      = 0x2,   // the socket will accept data if it's sent.
00045   SI_CONNECTED     = 0x4,   // the socket is connected.
00046   SI_DISCONNECTED  = 0x8,   // the socket is disconnected.
00047   SI_ERRONEOUS     = 0x10,  // the socket is in an erroneous state.
00048   SI_BASELINE      = 0x20,  // the socket seems okay but not interesting.
00049 
00050   SI_ALL_SOCK_INT  = 0xFF   // all socket interests are active.
00051 };
00052 
00054 
00055 class raw_socket : public virtual basis::root_object
00056 {
00057 public:
00058   raw_socket();
00059 
00060   ~raw_socket();
00061 
00062   DEFINE_CLASS_NAME("raw_socket");
00063 
00064 //  int open(
00065 //hmmm...
00066 
00067   int close(basis::un_int &socket);
00068     // disconnects, destroys and resets the "socket" to zero.
00069 
00070   int ioctl(basis::un_int socket, int request, void *argp) const;
00071     // manipulates the device parameters for the "socket".
00072 
00073   bool set_non_blocking(basis::un_int socket, bool non_blocking = true);
00074     // makes the "socket" into a non-blocking socket if "non_blocking" is true.
00075     // if "non_blocking" is false, then the socket is reset to blocking mode.
00076 
00077   bool set_nagle_algorithm(basis::un_int socket, bool use_nagle = true);
00078     // sets the nagle algorithm on "socket" if "use_nagle" is true.  note that
00079     // true is the default when a socket is created; to change that for the
00080     // socket, "use_nagle" should be false.
00081 
00082   bool set_broadcast(basis::un_int socket, bool broadcasting = true);
00083     // sets broadcast mode on the socket so that it can receive packets that
00084     // are broadcast to the network.
00085 
00086   bool set_reuse_address(basis::un_int socket, bool reuse = true);
00087     // sets the socket to allow an address to be re-used when it's already
00088     // in use, rather than getting a bind error.
00089 
00090   bool set_keep_alive(basis::un_int socket, bool keep_alive = true);
00091     // marks a connected-mode socket so that keep alive packets will be sent
00092     // occasionally to ensure that a disconnection is noticed.
00093 
00094   static basis::astring interest_name(int to_name);
00095     // returns the textual form for the interests set in "to_name".
00096 
00097   // outlines any special constraints on the select invocation.
00098   enum select_types {
00099     SELECTING_JUST_WRITE = 0x1,
00100     SELECTING_JUST_READ = 0x2
00101   };
00102 
00103   int select(basis::un_int socket, int selection_mode, int timeout = 0) const;
00104     // this is similar to the low-level select on a bsd socket.  usually this
00105     // will return events of any type--read, write and exception.  exceptions
00106     // will always be cause for a return, but if you just want to look at a
00107     // read condition, include the JUST_READ flag in the "selection_mode".  to
00108     // just check for write, add the JUST_WRITE flag.  this function returns a
00109     // bitwise ORed value from the different types of 'socket_interests' (see
00110     // tcpip definitions, which is where the enum is currently stored).  if
00111     // there are no special conditions noted on the socket, then zero is
00112     // returned.  if the "timeout" is zero, the function will return right
00113     // away.  if "timeout" (measured in milliseconds) is non-zero, then the
00114     // function will wait until the condition becomes true or the "timeout"
00115     // elapses.  note: no infinite timeouts are provided here.
00116 
00117   int select(basis::int_array &read_sox, basis::int_array &write_sox, int timeout = 0) const;
00118     // similar to select above, but operates on a list of sockets in the
00119     // "read_sox" and "write_sox".  the "read_sox" are checked to see whether
00120     // those sockets have data pending that could be read.  the "write_sox" are
00121     // checked to see if the sockets are currently writable without blocking.
00122     // if any sockets have events applicable to the "selection_mode", then they
00123     // will still be present in the lists when the call returns.  zero is
00124     // returned if absolutely nothing happened during the "timeout" period;
00125     // otherwise, a non-zero number is returned but the individual sockets
00126     // must still be inspected to determine what happened.
00127 
00128   int analyze_select_result(basis::un_int socket, int mode, fd_set_wrapper &read_list,
00129           fd_set_wrapper &write_list, fd_set_wrapper &exceptions) const;
00130     // examines the "socket" in the fd_sets passed in and returns a result
00131     // based on those sets and the "mode".
00132 
00133 private:
00134   tcpip_stack *_stack;
00135 
00136   int test_readability(basis::un_int socket) const;
00137     // checks on the readability state for the "socket", assuming that select()
00138     // reported the socket as readable, and returns either SI_ERRONEOUS,
00139     // SI_READABLE or SI_DISCONNECTED based on what ioctl() says about it.
00140 
00141   int inner_select(basis::un_int socket, int selection_mode, int timeout,
00142           fd_set_wrapper &read_list, fd_set_wrapper &write_list,
00143           fd_set_wrapper &exceptions) const;
00144     // intermediate function that doesn't attempt to fully analyze the select
00145     // result.  the returned value will be non-zero if something interesting
00146     // is happening on the socket.  if that value is SI_ERRONEOUS, then
00147     // something bad happened to the socket.  the other non-zero value is
00148     // SI_BASELINE, which means the socket has something to report in the
00149     // fd_set parameters.
00150 };
00151 
00153 
00154 #ifdef __UNIX__
00155   // provide some unifying definitions.
00156   #define INVALID_SOCKET -1
00157   #define SOCKET_ERROR -1
00158   typedef void sock_hop;
00159 
00160   // provide synonyms for errors so we don't conflict with the windows
00161   // brain-deadness.  they define error values like EACCESS but they're not
00162   // the real values you need to use with tcp/ip.  french fried gates time.
00163   #define SOCK_EACCES EACCES
00164   #define SOCK_EADDRINUSE EADDRINUSE
00165   #define SOCK_EADDRNOTAVAIL EADDRNOTAVAIL
00166   #define SOCK_EAFNOSUPPORT EAFNOSUPPORT
00167   #define SOCK_EALREADY EALREADY
00168   #define SOCK_EBADF EBADF
00169   #define SOCK_ECONNABORTED ECONNABORTED
00170   #define SOCK_ECONNREFUSED ECONNREFUSED
00171   #define SOCK_ECONNRESET ECONNRESET
00172   #define SOCK_EDESTADDRREQ EDESTADDRREQ
00173   #define SOCK_EDQUOT EDQUOT
00174   #define SOCK_EFAULT EFAULT
00175   #define SOCK_EHOSTDOWN EHOSTDOWN
00176   #define SOCK_EHOSTUNREACH EHOSTUNREACH
00177   #define SOCK_EINPROGRESS EINPROGRESS
00178   #define SOCK_EINTR EINTR
00179   #define SOCK_EINVAL EINVAL
00180   #define SOCK_EISCONN EISCONN
00181   #define SOCK_ELOOP ELOOP
00182   #define SOCK_EMFILE EMFILE
00183   #define SOCK_EMSGSIZE EMSGSIZE
00184   #define SOCK_ENAMETOOLONG ENAMETOOLONG
00185   #define SOCK_ENETDOWN ENETDOWN
00186   #define SOCK_ENETUNREACH ENETUNREACH
00187   #define SOCK_ENETRESET ENETRESET
00188   #define SOCK_ENOBUFS ENOBUFS
00189   #define SOCK_ENOPROTOOPT ENOPROTOOPT
00190   #define SOCK_ENOTCONN ENOTCONN
00191   #define SOCK_ENOTEMPTY ENOTEMPTY
00192   #define SOCK_ENOTSOCK ENOTSOCK
00193   #define SOCK_EOPNOTSUPP EOPNOTSUPP
00194   #define SOCK_EPFNOSUPPORT EPFNOSUPPORT
00195   #define SOCK_EPROCLIM EPROCLIM
00196   #define SOCK_EPROTOTYPE EPROTOTYPE
00197   #define SOCK_EPROTONOSUPPORT EPROTONOSUPPORT
00198   #define SOCK_EREMOTE EREMOTE
00199   #define SOCK_ESHUTDOWN ESHUTDOWN
00200   #define SOCK_ESOCKTNOSUPPORT ESOCKTNOSUPPORT
00201   #define SOCK_ESTALE ESTALE
00202   #define SOCK_ETIMEDOUT ETIMEDOUT
00203   #define SOCK_ETOOMANYREFS ETOOMANYREFS
00204   #define SOCK_EWOULDBLOCK EWOULDBLOCK
00205   #define SOCK_EUSERS EUSERS
00206 #endif //unix.
00207 
00209 
00210 #ifdef __WIN32__
00211   typedef char sock_hop;
00212   typedef int socklen_t;
00213 
00214   // provide close to the real BSD error names using windows values.
00215   #define SOCK_EACCES WSAEACCES
00216   #define SOCK_EADDRINUSE WSAEADDRINUSE
00217   #define SOCK_EADDRNOTAVAIL WSAEADDRNOTAVAIL
00218   #define SOCK_EAFNOSUPPORT WSAEAFNOSUPPORT
00219   #define SOCK_EALREADY WSAEALREADY
00220   #define SOCK_EBADF WSAEBADF
00221   #define SOCK_ECONNABORTED WSAECONNABORTED
00222   #define SOCK_ECONNREFUSED WSAECONNREFUSED
00223   #define SOCK_ECONNRESET WSAECONNRESET
00224   #define SOCK_EDESTADDRREQ WSAEDESTADDRREQ
00225   #define SOCK_EDQUOT WSAEDQUOT
00226   #define SOCK_EFAULT WSAEFAULT
00227   #define SOCK_EHOSTDOWN WSAEHOSTDOWN
00228   #define SOCK_EHOSTUNREACH WSAEHOSTUNREACH
00229   #define SOCK_EINPROGRESS WSAEINPROGRESS
00230   #define SOCK_EINTR WSAEINTR
00231   #define SOCK_EINVAL WSAEINVAL
00232   #define SOCK_EISCONN WSAEISCONN
00233   #define SOCK_ELOOP WSAELOOP
00234   #define SOCK_EMFILE WSAEMFILE
00235   #define SOCK_EMSGSIZE WSAEMSGSIZE
00236   #define SOCK_ENAMETOOLONG WSAENAMETOOLONG
00237   #define SOCK_ENETDOWN WSAENETDOWN
00238   #define SOCK_ENETUNREACH WSAENETUNREACH
00239   #define SOCK_ENETRESET WSAENETRESET
00240   #define SOCK_ENOBUFS WSAENOBUFS
00241   #define SOCK_ENOPROTOOPT WSAENOPROTOOPT
00242   #define SOCK_ENOTCONN WSAENOTCONN
00243   #define SOCK_ENOTEMPTY WSAENOTEMPTY
00244   #define SOCK_ENOTSOCK WSAENOTSOCK
00245   #define SOCK_EOPNOTSUPP WSAEOPNOTSUPP
00246   #define SOCK_EPFNOSUPPORT WSAEPFNOSUPPORT
00247   #define SOCK_EPROCLIM WSAEPROCLIM
00248   #define SOCK_EPROTOTYPE WSAEPROTOTYPE
00249   #define SOCK_EPROTONOSUPPORT WSAEPROTONOSUPPORT
00250   #define SOCK_EREMOTE WSAEREMOTE
00251   #define SOCK_ESHUTDOWN WSAESHUTDOWN
00252   #define SOCK_ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
00253   #define SOCK_ESTALE WSAESTALE
00254   #define SOCK_ETIMEDOUT WSAETIMEDOUT
00255   #define SOCK_ETOOMANYREFS WSAETOOMANYREFS
00256   #define SOCK_EUSERS WSAEUSERS
00257 
00258   // windows specific names.
00259   #define SOCK_EWOULDBLOCK WSAEWOULDBLOCK
00260   #define SOCK_HOST_NOT_FOUND WSAHOST_NOT_FOUND
00261   #define SOCK_NO_DATA WSANO_DATA
00262   #define SOCK_NO_RECOVERY WSANO_RECOVERY
00263   #define SOCK_NOTINITIALISED WSANOTINITIALISED
00264   #define SOCK_SYSNOTREADY WSASYSNOTREADY
00265   #define SOCK_TRY_AGAIN WSATRY_AGAIN
00266   #define SOCK_VERNOTSUPPORTED WSAVERNOTSUPPORTED
00267 #endif //win32.
00268 
00270 
00271 } //namespace.
00272 
00273 #endif // outer guard.
00274 
Generated on Sat Jan 28 04:22:43 2012 for hoople2 project by  doxygen 1.6.3