VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsocket_unix.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gsocket_unix.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "gsocket.h"
24 #include "gprocess.h"
25 #include "gdebug.h"
26 #include "gstr.h"
27 #include "gassert.h"
28 #include "glog.h"
29 #include <cstring>
30 #include <sstream>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 
36 bool GNet::Socket::setNonBlock()
37 {
38  int mode = ::fcntl( m_socket.fd() , F_GETFL ) ;
39  if( mode < 0 )
40  return false ;
41 
42  int rc = ::fcntl( m_socket.fd() , F_SETFL , mode | O_NONBLOCK ) ;
43  return rc >= 0 ;
44 }
45 
46 int GNet::Socket::reason()
47 {
48  int e = errno ;
49  return e ;
50 }
51 
52 void GNet::Socket::doClose()
53 {
54  ::close( m_socket.fd() ) ;
55  m_socket = Descriptor::invalid() ;
56 }
57 
58 bool GNet::Socket::error( int rc )
59 {
60  return rc < 0 ;
61 }
62 
63 bool GNet::Socket::sizeError( ssize_t size )
64 {
65  return size < 0 ;
66 }
67 
69 {
70  return m_reason == EWOULDBLOCK || m_reason == EAGAIN || m_reason == EINTR ;
71 }
72 
74 {
75  return m_reason == EINPROGRESS ;
76 }
77 
79 {
80  return m_reason == EMSGSIZE ;
81 }
82 
83 void GNet::Socket::setFault()
84 {
85  m_reason = EFAULT ;
86 }
87 
88 bool GNet::Socket::canBindHint( const Address & address )
89 {
90  return bind( address , NoThrow() ) ;
91 }
92 
93 void GNet::Socket::setOptionReuse()
94 {
95  setOption( SOL_SOCKET , "so_reuseaddr" , SO_REUSEADDR , 1 ) ; // allow bind on TIME_WAIT address -- see also SO_REUSEPORT
96 }
97 
98 void GNet::Socket::setOptionExclusive()
99 {
100  // no-op
101 }
102 
103 void GNet::Socket::setOptionPureV6( bool active )
104 {
105  #if GCONFIG_HAVE_IPV6
106  if( active )
107  setOption( IPPROTO_IPV6 , "ipv6_v6only" , IPV6_V6ONLY , 1 ) ;
108  #else
109  if( active )
110  throw SocketError( "cannot set socket option for pure ipv6" ) ;
111  #endif
112 }
113 
114 bool GNet::Socket::setOptionPureV6( bool active , NoThrow )
115 {
116  #if GCONFIG_HAVE_IPV6
117  return active ? setOption( IPPROTO_IPV6 , "ipv6_v6only" , IPV6_V6ONLY , 1 , NoThrow() ) : true ;
118  #else
119  return !active ;
120  #endif
121 }
122 
123 bool GNet::Socket::setOptionImp( int level , int op , const void * arg , socklen_t n )
124 {
125  int rc = ::setsockopt( m_socket.fd() , level , op , arg , n ) ;
126  return ! error(rc) ;
127 }
128 
129 std::string GNet::Socket::reasonStringImp( int e )
130 {
131  return G::Process::strerror( e ) ;
132 }
133 
134 /// \file gsocket_unix.cpp
bool canBindHint(const Address &address)
Returns true if the socket can probably be bound with the given address.
static Descriptor invalid()
Returns an invalid descriptor.
Definition: gdescriptor.cpp:30
virtual bool eWouldBlock()
Returns true if the previous socket operation failed because the socket would have blocked...
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
bool eMsgSize()
Returns true if the previous socket operation failed with the EMSGSIZE error status.
SOCKET fd() const
Returns the low-level descriptor.
Definition: gdescriptor.h:66
bool eInProgress()
Returns true if the previous socket operation failed with the EINPROGRESS error status.
Overload discriminator class for GNet::Socket.
Definition: gsocket.h:63