Claw 1.7.0
|
00001 /* 00002 CLAW - a C++ Library Absolutely Wonderful 00003 00004 CLAW is a free library without any particular aim but being useful to 00005 anyone. 00006 00007 Copyright (C) 2005-2011 Julien Jorge 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 00023 contact: julien.jorge@gamned.org 00024 */ 00030 #ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__ 00031 #define __CLAW_SOCKET_TRAITS_UNIX_HPP__ 00032 00033 #include <sys/types.h> 00034 #include <sys/socket.h> 00035 #include <sys/stat.h> 00036 #include <netinet/in.h> 00037 #include <netdb.h> 00038 #include <unistd.h> 00039 #include <cstring> 00040 00041 #include <claw/assert.hpp> 00042 00043 namespace claw 00044 { 00049 class socket_traits_unix 00050 { 00051 public: 00053 typedef int descriptor; 00054 00055 public: 00057 static const descriptor invalid_socket = -1; 00058 00059 public: 00060 /*------------------------------------------------------------------------*/ 00065 static bool init() 00066 { 00067 return true; 00068 } // socket_traits_unix::init() 00069 00070 /*------------------------------------------------------------------------*/ 00075 static bool release() 00076 { 00077 return true; 00078 } // socket_traits_unix::release() 00079 00080 /*------------------------------------------------------------------------*/ 00085 static descriptor open() 00086 { 00087 descriptor fd = invalid_socket; 00088 00089 fd = socket(AF_INET, SOCK_STREAM, 0); 00090 00091 return fd; 00092 } // socket_traits_unix::open() 00093 00094 /*------------------------------------------------------------------------*/ 00100 static bool close( descriptor d ) 00101 { 00102 return ::close(d) == 0; 00103 } // socket_traits_unix::close() 00104 00105 /*------------------------------------------------------------------------*/ 00113 static bool connect( descriptor d, const std::string& address, int port ) 00114 { 00115 CLAW_PRECOND( d != invalid_socket ); 00116 00117 bool result = false; 00118 struct hostent* hp = gethostbyname(address.c_str()); 00119 00120 if (hp) 00121 { 00122 struct sockaddr_in sa; 00123 00124 memset (&sa, '\0', sizeof(sa)); 00125 sa.sin_family = hp->h_addrtype; 00126 sa.sin_port = htons(port); 00127 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length ); 00128 00129 if (::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1) 00130 result = true; 00131 } 00132 00133 return result; 00134 } // socket_traits_unix::connect() 00135 00136 /*------------------------------------------------------------------------*/ 00144 static bool listen( descriptor d, int port, unsigned int queue_size ) 00145 { 00146 CLAW_PRECOND( d != invalid_socket ); 00147 00148 struct sockaddr_in addr; 00149 00150 memset (&addr, '\0', sizeof(addr)); 00151 addr.sin_family = AF_INET; 00152 addr.sin_port = htons(port); 00153 addr.sin_addr.s_addr = htonl(INADDR_ANY); 00154 00155 if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1 ) 00156 return ::listen(d, queue_size) != -1; 00157 else 00158 return false; 00159 } // socket_traits_unix::connect() 00160 00161 /*------------------------------------------------------------------------*/ 00170 static bool select_read( descriptor d, int time_limit = -1 ) 00171 { 00172 CLAW_PRECOND( d != invalid_socket ); 00173 00174 struct timeval tv, *ptv; 00175 fd_set fds; 00176 00177 if ( time_limit < 0 ) 00178 ptv = NULL; 00179 else 00180 { 00181 tv.tv_sec = time_limit; 00182 tv.tv_usec = 0; 00183 00184 ptv = &tv; 00185 } 00186 00187 FD_ZERO(&fds); 00188 FD_SET(d, &fds); 00189 00190 select( d+1, &fds, NULL, NULL, ptv ); 00191 00192 return FD_ISSET( d, &fds ); 00193 } // socket_traits_unix::select_read() 00194 00195 /*------------------------------------------------------------------------*/ 00201 static descriptor accept( descriptor d ) 00202 { 00203 return ::accept( d, NULL, NULL ); 00204 } // socket_traits_unix::accept() 00205 00206 /*------------------------------------------------------------------------*/ 00211 static bool valid_descriptor( descriptor d ) 00212 { 00213 return d != invalid_socket; 00214 } // socket_traits_unix::valid_descriptor() 00215 00216 /*------------------------------------------------------------------------*/ 00221 static bool is_open( descriptor d ) 00222 { 00223 struct stat buf; 00224 00225 return fstat(d, &buf) == 0; 00226 } // socket_traits_unix::is_open() 00227 00228 }; // class socket_traits_unix 00229 00230 typedef socket_traits_unix socket_traits; 00231 } // namespace claw 00232 00233 #endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__