libDwm-0.6.0
DwmSocket.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmSocket.hh 8618 $
3 // @(#) $Id: DwmSocket.hh 8618 2016-05-30 04:12:07Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 1998-2007
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMSOCKET_HH_
43 #define _DWMSOCKET_HH_
44 
45 extern "C" {
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
49 #include <netinet/in.h>
50 }
51 
52 #include <string>
53 
54 #include "DwmDescriptor.hh"
55 #include "DwmIpv4Address.hh"
56 
57 namespace Dwm {
58 
59  //--------------------------------------------------------------------------
61  //--------------------------------------------------------------------------
62  class Socket
63  : public Descriptor
64  {
65  public:
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
69  Socket();
70 
71  //------------------------------------------------------------------------
75  //------------------------------------------------------------------------
76  Socket(const Socket & sock);
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  Socket & operator = (const Socket & sock);
82 
83  operator bool () const;
84 
85  //------------------------------------------------------------------------
89  //------------------------------------------------------------------------
90  virtual bool Open(int domain, int type, int protocol);
91 
92  int Type() const;
93 
94  //------------------------------------------------------------------------
96  //------------------------------------------------------------------------
97  virtual Socket Accept(struct sockaddr_in & addr);
98 
99  //------------------------------------------------------------------------
103  //------------------------------------------------------------------------
104  virtual Socket Accept(Ipv4Address & addr, uint16_t & port);
105 
106  //------------------------------------------------------------------------
111  //------------------------------------------------------------------------
112  virtual bool Accept(Socket & s, struct sockaddr_in & addr);
113 
114  //------------------------------------------------------------------------
120  //------------------------------------------------------------------------
121  virtual bool Accept(Socket & s, Ipv4Address & addr, uint16_t & port);
122 
123  //------------------------------------------------------------------------
125  //------------------------------------------------------------------------
126  virtual Socket Accept(struct sockaddr_un & addr);
127 
128  virtual bool Accept(Socket & s, struct sockaddr_un & addr);
129 
130  //------------------------------------------------------------------------
133  //------------------------------------------------------------------------
134  virtual bool Listen(int backlog);
135 
136  //------------------------------------------------------------------------
139  //------------------------------------------------------------------------
140  virtual bool Bind(const struct sockaddr_in & addr);
141 
142  //------------------------------------------------------------------------
145  //------------------------------------------------------------------------
146  virtual bool Bind(const Ipv4Address & addr, uint16_t port);
147 
148  //------------------------------------------------------------------------
151  //------------------------------------------------------------------------
152  virtual bool Bind(const struct sockaddr_un & addr);
153 
154  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  virtual bool Connect(const struct sockaddr_in & name);
159 
160  //------------------------------------------------------------------------
163  //------------------------------------------------------------------------
164  virtual bool Connect(const Ipv4Address & dstAddr, uint16_t port);
165 
166  //------------------------------------------------------------------------
169  //------------------------------------------------------------------------
170  virtual bool Connect(const struct sockaddr_un & name);
171 
172  //------------------------------------------------------------------------
174  // false on failure.
175  //------------------------------------------------------------------------
176  virtual bool Getpeername(struct sockaddr * name, socklen_t & namelen);
177 
178  //------------------------------------------------------------------------
182  //------------------------------------------------------------------------
183  virtual bool Getpeername(Ipv4Address & addr, uint16_t & port);
184 
185  //------------------------------------------------------------------------
187  // false on failure.
188  //------------------------------------------------------------------------
189  virtual bool Getsockname(struct sockaddr * name, socklen_t & namelen);
190 
191  //------------------------------------------------------------------------
196  //------------------------------------------------------------------------
197  virtual bool Getsockname(Ipv4Address & addr, uint16_t & port);
198 
199  //------------------------------------------------------------------------
201  // false on failure.
202  //------------------------------------------------------------------------
203  virtual bool Setsockopt(int level, int optname, const void * optval,
204  socklen_t optlen);
205 
206  //------------------------------------------------------------------------
208  // false on failure.
209  //------------------------------------------------------------------------
210  virtual bool Getsockopt(int level, int optname, void * optval,
211  socklen_t & optlen);
212 
213  //------------------------------------------------------------------------
215  //------------------------------------------------------------------------
216  virtual ssize_t RecvFrom(void *buf, size_t len, int flags,
217  struct sockaddr_in & from);
218 
219  //------------------------------------------------------------------------
225  //------------------------------------------------------------------------
226  virtual ssize_t RecvFrom(void *buf, size_t len, int flags,
227  Ipv4Address & srcAddr, uint16_t & srcPort);
228 
229  //------------------------------------------------------------------------
236  //------------------------------------------------------------------------
237  virtual ssize_t RecvFrom(std::string & s, int flags,
238  Ipv4Address & srcAddr, uint16_t & srcPort);
239 
240  //------------------------------------------------------------------------
242  //------------------------------------------------------------------------
243  virtual ssize_t SendTo(const void *buf, size_t len, int flags,
244  const struct sockaddr_in & to);
245 
246  //------------------------------------------------------------------------
250  //------------------------------------------------------------------------
251  virtual ssize_t SendTo(const void *buf, size_t len, int flags,
252  const Ipv4Address & dstAddr, uint16_t dstPort);
253 
254  //------------------------------------------------------------------------
262  //------------------------------------------------------------------------
263  virtual ssize_t SendTo(const std::string & s, int flags,
264  const Ipv4Address & dstAddr, uint16_t dstPort);
265 
266  //------------------------------------------------------------------------
269  //------------------------------------------------------------------------
270  virtual bool JoinMulticastGroup(const Ipv4Address & groupAddr,
271  const Ipv4Address & intfAddr);
272 
273  bool HeaderIncluded() const;
274 
275  protected:
276  int _type;
277  bool _headerIncluded;
278  };
279 
280 } // namespace Dwm
281 
282 
283 #endif // _DWMSOCKET_HH_
284 
285 //---------------------------- emacs settings -----------------------------
286 // Local Variables:
287 // mode: C++/la
288 // tab-width: 2
289 // indent-tabs-mode: nil
290 // c-basic-offset: 2
291 // End:
292 //-------------------------------------------------------------------------
This class abstracts a UNIX socket descriptor.
Definition: DwmSocket.hh:62
virtual bool Bind(const struct sockaddr_in &addr)
Calls the UNIX bind(2) function.
virtual bool Getsockname(struct sockaddr *name, socklen_t &namelen)
Calls the UNIX getsockname(2) function. Returns true on success,.
virtual bool Open(int domain, int type, int protocol)
Opens the socket.
virtual bool Setsockopt(int level, int optname, const void *optval, socklen_t optlen)
Calls the UNIX setsockopt(2) function. Returns true on success,.
virtual ssize_t SendTo(const void *buf, size_t len, int flags, const struct sockaddr_in &to)
Calls the UNIX sendto(2) function.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
virtual bool Listen(int backlog)
Calls the UNIX listen(2) function.
Socket()
Constructor.
virtual Socket Accept(struct sockaddr_in &addr)
Calls the UNIX accept(2) function. Returns the resulting socket.
virtual bool JoinMulticastGroup(const Ipv4Address &groupAddr, const Ipv4Address &intfAddr)
Join the given multicast group groupAddr on the given interface address intfAddr. ...
Definition: DwmBZ2IO.hh:67
virtual bool Getsockopt(int level, int optname, void *optval, socklen_t &optlen)
Calls the UNIX setsockopt(2) function. Returns true on success,.
Dwm::Descriptor class definition.
Socket & operator=(const Socket &sock)
operator = (assignment operator).
Dwm::Ipv4Address class definition.
virtual ssize_t RecvFrom(void *buf, size_t len, int flags, struct sockaddr_in &from)
Calls the UNIX recvfrom(2) function.
This class encapsulates UNIX file and socket descriptors.
Definition: DwmDescriptor.hh:62
virtual bool Getpeername(struct sockaddr *name, socklen_t &namelen)
Calls the UNIX getpeername(2) function. Returns true on success,.
virtual bool Connect(const struct sockaddr_in &name)
Calls the UNIX connect(2) function.