monotone

monotone Mtn Source Tree

Root/netxx/sockopt.cxx

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 implementation of the Netxx::SockOpt class.
35**/
36
37// common header
38#include "common.h"
39
40// Netxx includes
41#include "netxx/sockopt.h"
42#include "netxx/types.h"
43
44// standard includes
45#include <cstring>
46
47//####################################################################
48struct Netxx::SockOpt::pimpl
49{
50 pimpl (int socket)
51: fcntl_flags_(0), fcntl_changed_(false), win_blocking_(false)
52 {
53#ifndef WIN32
54 fcntl_flags_ = fcntl(socket, F_GETFL, 0);
55#endif
56 }
57
58 int fcntl_flags_;
59 bool fcntl_changed_;
60 bool win_blocking_;
61};
62//####################################################################
63Netxx::SockOpt::SockOpt (int socketfd, bool revert)
64 : socket_(socketfd), revert_(revert)
65{
66 pimpl_ = new pimpl(socket_);
67}
68//####################################################################
69Netxx::SockOpt::~SockOpt (void)
70{
71 if (revert_ && (pimpl_->fcntl_changed_ || pimpl_->win_blocking_)) {
72#if defined(WIN32)
73 unsigned long off = 0;
74 ioctlsocket(socket_, FIONBIO, &off);
75#else
76 fcntl(socket_, F_SETFL, pimpl_->fcntl_flags_);
77#endif
78 }
79
80 delete pimpl_;
81}
82//####################################################################
83bool Netxx::SockOpt::set_non_blocking (void)
84{
85# if defined(WIN32)
86/*
87 * FIXME FIXME FIXME FIXME
88 *
89 * Call ioctlsocket to get the NBIO flag instead of using
90 * win_blocking_
91 *
92 * FIXME FIXME FIXME FIXME
93 */
94if (pimpl_->win_blocking_) return true;
95
96unsigned long on = 1;
97if (ioctlsocket(socket_, FIONBIO, &on) != 0) return false;
98pimpl_->win_blocking_ = true;
99return true;
100# else
101int flags = fcntl(socket_, F_GETFL, 0);
102if (flags & O_NONBLOCK) return true;
103
104if (fcntl(socket_, F_SETFL, pimpl_->fcntl_flags_ | O_NONBLOCK) == -1) return false;
105pimpl_->fcntl_changed_ = true;
106return true;
107# endif
108}
109//####################################################################
110bool Netxx::SockOpt::set_reuse_address (void)
111{
112 int on = 1;
113 char *val = reinterpret_cast<char*>(&on);
114
115 if (setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, val, sizeof(on)) != 0) {
116std::string error("setsockopt(2) failure: ");
117error += str_error(get_last_error());
118throw Exception(error);
119 }
120
121 return true;
122}
123//####################################################################
124bool Netxx::SockOpt::check_for_error (std::string &message) const
125{
126 int so_error, so_return;
127 char *val = reinterpret_cast<char*>(&so_error);
128 os_socklen_type so_len(sizeof(so_error));
129
130 if ( (so_return = getsockopt(socket_, SOL_SOCKET, SO_ERROR, val, &so_len)) < 0) {
131message = str_error(get_last_error());
132return false;
133 }
134
135 if (so_error) {
136message = str_error(so_error);
137return false;
138 }
139
140 return true;
141}
142//####################################################################
143bool Netxx::SockOpt::set_ipv6_listen_for_v6_only (void) const
144{
145# ifndef NETXX_NO_INET6
146
147# ifndef IPV6_V6ONLY
148 return false;
149# else
150 int on = 1;
151 char *val = reinterpret_cast<char*>(&on);
152
153 if (setsockopt(socket_, IPPROTO_IPV6, IPV6_V6ONLY, val, sizeof(on)) != 0)
154 return false;
155# endif
156# endif
157 return true;
158}
159//####################################################################

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status