Claw  1.7.0
socket_traits_unix.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
31 #define __CLAW_SOCKET_TRAITS_UNIX_HPP__
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <netinet/in.h>
37 #include <netdb.h>
38 #include <unistd.h>
39 #include <cstring>
40 
41 #include <claw/assert.hpp>
42 
43 namespace claw
44 {
50  {
51  public:
53  typedef int descriptor;
54 
55  public:
57  static const descriptor invalid_socket = -1;
58 
59  public:
60  /*------------------------------------------------------------------------*/
65  static bool init()
66  {
67  return true;
68  } // socket_traits_unix::init()
69 
70  /*------------------------------------------------------------------------*/
75  static bool release()
76  {
77  return true;
78  } // socket_traits_unix::release()
79 
80  /*------------------------------------------------------------------------*/
85  static descriptor open()
86  {
88 
89  fd = socket(AF_INET, SOCK_STREAM, 0);
90 
91  return fd;
92  } // socket_traits_unix::open()
93 
94  /*------------------------------------------------------------------------*/
100  static bool close( descriptor d )
101  {
102  return ::close(d) == 0;
103  } // socket_traits_unix::close()
104 
105  /*------------------------------------------------------------------------*/
113  static bool connect( descriptor d, const std::string& address, int port )
114  {
116 
117  bool result = false;
118  struct hostent* hp = gethostbyname(address.c_str());
119 
120  if (hp)
121  {
122  struct sockaddr_in sa;
123 
124  memset (&sa, '\0', sizeof(sa));
125  sa.sin_family = hp->h_addrtype;
126  sa.sin_port = htons(port);
127  memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
128 
129  if (::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1)
130  result = true;
131  }
132 
133  return result;
134  } // socket_traits_unix::connect()
135 
136  /*------------------------------------------------------------------------*/
144  static bool listen( descriptor d, int port, unsigned int queue_size )
145  {
147 
148  struct sockaddr_in addr;
149 
150  memset (&addr, '\0', sizeof(addr));
151  addr.sin_family = AF_INET;
152  addr.sin_port = htons(port);
153  addr.sin_addr.s_addr = htonl(INADDR_ANY);
154 
155  if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1 )
156  return ::listen(d, queue_size) != -1;
157  else
158  return false;
159  } // socket_traits_unix::connect()
160 
161  /*------------------------------------------------------------------------*/
170  static bool select_read( descriptor d, int time_limit = -1 )
171  {
173 
174  struct timeval tv, *ptv;
175  fd_set fds;
176 
177  if ( time_limit < 0 )
178  ptv = NULL;
179  else
180  {
181  tv.tv_sec = time_limit;
182  tv.tv_usec = 0;
183 
184  ptv = &tv;
185  }
186 
187  FD_ZERO(&fds);
188  FD_SET(d, &fds);
189 
190  select( d+1, &fds, NULL, NULL, ptv );
191 
192  return FD_ISSET( d, &fds );
193  } // socket_traits_unix::select_read()
194 
195  /*------------------------------------------------------------------------*/
202  {
203  return ::accept( d, NULL, NULL );
204  } // socket_traits_unix::accept()
205 
206  /*------------------------------------------------------------------------*/
211  static bool valid_descriptor( descriptor d )
212  {
213  return d != invalid_socket;
214  } // socket_traits_unix::valid_descriptor()
215 
216  /*------------------------------------------------------------------------*/
221  static bool is_open( descriptor d )
222  {
223  struct stat buf;
224 
225  return fstat(d, &buf) == 0;
226  } // socket_traits_unix::is_open()
227 
228  }; // class socket_traits_unix
229 
230  typedef socket_traits_unix socket_traits;
231 } // namespace claw
232 
233 #endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__