1 /*
2  * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3  * All Rights Reserved
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  * 3. Neither the name of the Author nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR
23  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /** @file
34  * This file contains the definition of the Netxx::SockOpt class.
35 **/
36 
37 #ifndef _netxx_sockopt_h_
38 #define _netxx_sockopt_h_
39 
40 // Netxx includes
41 #include "types.h"
42 
43 namespace Netxx {
44 
45 /**
46  * The Netxx::SockOpt class is used to set general socket options.
47 **/
48 class SockOpt {
49 public:
50     //####################################################################
51     /**
52      * Construct a new Netxx::SockOpt class and link it with the given
53      * socket file descriptor. If you set the revert flag to true, any
54      * options that you set for the socket will be reversed when this
55      * SockOpt class is destroyed.
56      *
57      * @param socketfd The socket file descriptor to set options for.
58      * @param revert Whether or not to revert in the destructor.
59      * @author Peter Jones
60     **/
61     //####################################################################
62     explicit SockOpt (socket_type socketfd, bool revert=false);
63 
64     //####################################################################
65     /**
66      * Netxx::SockOpt class destructor. The destructor will possibly revert
67      * the socket back to its original state if the revert flag was true in
68      * the constructor.
69      *
70      * @author Peter Jones
71     **/
72     //####################################################################
73     ~SockOpt (void);
74 
75     //####################################################################
76     /**
77      * Set the socket to non-blocking. This will cause the read and write
78      * operations to return with a timeout (-1) if the operation could not
79      * be fullfilled imediatly.
80      *
81      * @return True if the socket was set to non-blocking.
82      * @return False if there was some error setting the socket to non-blocking
83      * @author Peter Jones
84     **/
85     //####################################################################
86     bool set_non_blocking (void);
87 
88     //####################################################################
89     /**
90      * Set the socket option that allows you to reuse an address/port pair
91      * even if the state for that pair is TIMED_WAIT.
92      *
93      * @return True if the address could be reused.
94      * @return False if the address could not be reused.
95      * @author Peter Jones
96     **/
97     //####################################################################
98     bool set_reuse_address (void);
99 
100     //####################################################################
101     /**
102      * Check the socket for errors, and record an error message in the given
103      * string if necessary.
104      *
105      * @param message A place to record an error message.
106      * @return True if no errors are flagged for this socket.
107      * @return False if there are errors flagged for this socket.
108      * @author Peter Jones
109     **/
110     //####################################################################
111     bool check_for_error (std::string &message) const;
112 
113     //####################################################################
114     /**
115      * Set the socket option that does not allow IPv4 mapped addresses on
116      * a listening IPv6 socket.
117      *
118      * @return True if the socket was set to IPv6 only.
119      * @return False if the socket was not set to IPv6 only.
120      * @author Matthew Gregan
121     **/
122     //####################################################################
123     bool set_ipv6_listen_for_v6_only (void) const;
124 private:
125     socket_type socket_;
126     bool revert_;
127 
128     struct pimpl; pimpl *pimpl_;
129     SockOpt (const SockOpt&);
130     SockOpt& operator= (const SockOpt&);
131 
132 }; // end Netxx::SockOpt class
133 } // end Netxx namespace
134 #endif
135