socket_exception_handler_pos.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Cross platform
11 *****************************************************************************/
12 
13 #include <ecl/config/ecl.hpp>
14 #ifndef ECL_IS_APPLE
15 #ifdef ECL_IS_POSIX
16 
17 /*****************************************************************************
18 ** Includes
19 *****************************************************************************/
20 
21 #include <sstream>
22 #include <errno.h>
23 #include <netdb.h> // gethostbyname
25 #include "../../../include/ecl/devices/detail/socket_exception_handler_pos.hpp"
26 
27 /*****************************************************************************
28 ** Namespaces
29 *****************************************************************************/
30 
31 namespace ecl {
32 namespace devices {
33 
34 StandardException socket_exception(const char* loc) {
35  switch( errno ) {
36  case ( EACCES ) : return StandardException(LOC,OpenError,"Unable to open socket. Permission to create is denied.");
37  case ( EAFNOSUPPORT): return StandardException(LOC,NotSupportedError,"Unable to open socket. Your implementation does not support the specified address family (in this case AF_INET or otherwise known as ipv4).");
38  case ( EINVAL ) : return StandardException(LOC,InvalidArgError,"Unable to open socket. Unknown or invalid protocol, family.");
39  case ( EMFILE ) : return StandardException(LOC,OutOfRangeError,"Unable to open socket. Process file table overflow.");
40  case ( ENFILE ) : return StandardException(LOC,OutOfResourcesError,"Unable to open socket. The system limit on the number of open files has been reached.");
41  case ( ENOBUFS ) : return StandardException(LOC,MemoryError,"Unable to open socket. Insufficient memory available.");
42  case ( ENOMEM ) : return StandardException(LOC,MemoryError,"Unable to open socket. Insufficient memory available.");
43  case ( EPROTONOSUPPORT ) : return StandardException(LOC,NotSupportedError,"Unable to open socket. The protocol type (socket streams) is not supported within this address family (ipv4).");
44  default : {
45  std::ostringstream ostream;
46  ostream << "Unknown errno [" << errno << "]";
47  return StandardException(loc, UnknownError, ostream.str());
48  }
49  }
50 }
51 
52 StandardException bind_exception(const char* loc) {
53  switch( errno ) {
54  case ( EACCES ) : return StandardException(LOC,PermissionsError,"Unable to bind the socket. The address is protected (maybe need to be superuser?).");
55  case ( EADDRINUSE ) : return StandardException(LOC,BusyError,"Unable to bind the socket. Address already in use (might be timing out, try again in a moment).");
56  case ( EBADF ) : return StandardException(LOC,InvalidObjectError,"Unable to bind the socket. Not a valid socket descriptor.");
57  case ( EINVAL ) : return StandardException(LOC,BusyError,"Unable to bind the socket. The socket is already bound to an address.");
58  case ( ENOTSOCK ) : return StandardException(LOC,InvalidObjectError,"Unable to bind the socket. The descriptor is a file descriptor, not a socket descriptor.");
59  case ( EADDRNOTAVAIL ) : return StandardException(LOC,InvalidObjectError,"Unable to bind the socket. Interface does not exist or is not local.");
60  case ( EFAULT ) : return StandardException(LOC,OutOfRangeError,"Unable to bind the socket. Socket specification is outside the user address space.");
61  case ( ELOOP ) : return StandardException(LOC,SystemFailureError,"Unable to bind the socket. Too many symbolic links involved.");
62  case ( ENAMETOOLONG ) : return StandardException(LOC,InvalidArgError,"Unable to bind the socket. Address is too long.");
63  case ( ENOENT ) : return StandardException(LOC,InvalidObjectError,"Unable to bind the socket. The file does not exist.");
64  case ( ENOMEM ) : return StandardException(LOC,MemoryError,"Unable to bind the socket. Insufficient kernel memory.");
65  case ( ENOTDIR ) : return StandardException(LOC,InvalidArgError,"Unable to bind the socket. A component of the path prefix is not a directory.");
66  case ( EROFS ) : return StandardException(LOC,PermissionsError,"Unable to bind the socket. Socket inode resides on a read only file system.");
67  default : {
68  std::ostringstream ostream;
69  ostream << "Unknown error [" << errno << "]";
70  return StandardException(loc, UnknownError, ostream.str());
71  }
72  }
73 }
74 
75 StandardException accept_exception(const char* loc) {
76  switch( errno ) {
77  case ( EWOULDBLOCK ) : return StandardException(LOC,BlockingError,"Unable to accept client connection. The socket is non-blocking and no connections are available.");
78  case ( EBADF ) : return StandardException(LOC,InvalidObjectError,"Unable to accept client connection. Not a valid socket descriptor.");
79  case ( ECONNABORTED ): return StandardException(LOC,InterruptedError,"Unable to accept client connection. A connection has been aborted.");
80  case ( EINTR ) : return StandardException(LOC,InterruptedError,"Unable to accept client connection. A system signal has interrupted.");
81  case ( EINVAL ) : return StandardException(LOC,UsageError,"Unable to accept client connection. Socket is not listening for connections or address length is invalid.");
82  case ( EMFILE ) : return StandardException(LOC,OutOfResourcesError,"Unable to accept client connection. The system or per-process limit on files has been reached.");
83  case ( ENFILE ) : return StandardException(LOC,OutOfResourcesError,"Unable to accept client connection. The system or per-process limit on files has been reached.");
84  case ( ENOTSOCK ) : return StandardException(LOC,InvalidObjectError,"Unable to accept client connection. The descriptor is a file descriptor, not a socket descriptor..");
85  case ( EOPNOTSUPP ) : return StandardException(LOC,InvalidObjectError,"Unable to accept client connection. The client socket is not of type SOCK_STREAM.");
86  case ( EFAULT ) : return StandardException(LOC,PermissionsError,"Unable to accept client connection. The address argument is not writable by the user.");
87  case ( ENOBUFS ) : return StandardException(LOC,MemoryError,"Unable to accept client connection. Not enough free memory (buffer or system).");
88  case ( ENOMEM ) : return StandardException(LOC,MemoryError,"Unable to accept client connection. Not enough free memory (buffer or system).");
89  case ( EPROTO ) : return StandardException(LOC,InvalidArgError,"Unable to accept client connection. Protocol error.");
90  case ( EPERM ) : return StandardException(LOC,PermissionsError,"Unable to accept client connection. Permissions do not allow this connection.");
91  default : {
92  std::ostringstream ostream;
93  ostream << "Unknown error [" << errno << "]";
94  return StandardException(loc, UnknownError, ostream.str());
95  }
96  }
97 }
98 
99 StandardException receive_exception(const char* loc) {
100 
101  switch( errno ) {
102  case ( EAGAIN || EWOULDBLOCK ) : return StandardException(LOC,InterruptedError,"Unable to read the socket. Probably a timeout occured.");
103  case ( EBADF ) : return StandardException(LOC,InvalidObjectError,"Unable to read the socket. Bad file descriptor.");
104  case ( ECONNREFUSED) : return StandardException(LOC,ConnectionError,"Unable to read the socket. Remote host refused the connection (probably not running).");
105  case ( EFAULT ) : return StandardException(LOC,SystemFailureError,"Unable to read the socket. Receive buffer has an address problem.");
106  case ( EINTR ) : return StandardException(LOC,InterruptedError,"Unable to read the socket. Signal interruption.");
107  case ( EINVAL ) : return StandardException(LOC,InvalidArgError,"Unable to read the socket. Invalid argument was used.");
108  case ( ENOMEM ) : return StandardException(LOC,MemoryError,"Unable to read the socket. Could not allocate memory for the operation.");
109  case ( ENOTCONN ) : return StandardException(LOC,ConnectionError,"Unable to read the socket. Has not been connected.");
110  case ( ENOTSOCK ) : return StandardException(LOC,InvalidObjectError,"Unable to read the socket. The file descriptor does not refer to a socket.");
111  default : {
112  std::ostringstream ostream;
113  ostream << "Unknown error [" << errno << "]";
114  return StandardException(loc, UnknownError, ostream.str());
115  }
116  }
117 }
118 
119 StandardException send_exception(const char* loc) {
120 
121  switch( errno ) {
122  case ( EAGAIN || EWOULDBLOCK ) : return StandardException(LOC,BlockingError,"Unable to write to the socket. Socket is configured as non-blocking and this would block.");
123  case ( EWOULDBLOCK ) : return StandardException(LOC,BlockingError,"Unable to write to the socket. Socket is configured as non-blocking and this would block.");
124  case ( EACCES ) : return StandardException(LOC,PermissionsError,"Unable to write to the socket. Permission to write is denied.");
125  case ( EBADF ) : return StandardException(LOC,InvalidObjectError,"Unable to write to the socket. Bad file descriptor.");
126  case ( ECONNRESET ) : return StandardException(LOC,InterruptedError,"Unable to write to the socket. Connection reset by peer.");
127  case ( EFAULT ) : return StandardException(LOC,SystemFailureError,"Unable to write to the socket. Buffer has an address problem.");
128  case ( EINTR ) : return StandardException(LOC,InterruptedError,"Unable to write to the socket. Signal interruption.");
129  case ( EINVAL ) : return StandardException(LOC,InvalidArgError,"Unable to write to the socket. Invalid argument was used.");
130  case ( EISCONN ) : return StandardException(LOC,ConnectionError,"Unable to write to the socket. Connection mismatch???");
131  case ( EMSGSIZE ) : return StandardException(LOC,WriteError,"Unable to write to the socket. Socket type required to send atomically, but the size of this message is too large to handle in this way.");
132  case ( ENOBUFS ) : return StandardException(LOC,OutOfResourcesError,"Unable to write to the socket. Output queue is full (could be caused by transient congestion, but this doesn't usually happen in linux which typically just drops packets).");
133  case ( ENOMEM ) : return StandardException(LOC,MemoryError,"Unable to write to the socket. Could not allocate memory for the operation.");
134  case ( ENOTCONN ) : return StandardException(LOC,ConnectionError,"Unable to write to the socket. Has not been connected.");
135  case ( ENOTSOCK ) : return StandardException(LOC,InvalidObjectError,"Unable to write to the socket. The file descriptor does not refer to a socket.");
136  case ( EOPNOTSUPP ) : return StandardException(LOC,NotSupportedError,"Unable to write to the socket. Some api here not supported.");
137  case ( EPIPE ) : return StandardException(LOC,InterruptedError,"Unable to write to the socket. Local end has been shutdown. Probably bad and will receive a SIGPIPE signal too.");
138  default : {
139  std::ostringstream ostream;
140  ostream << "Unknown error [" << errno << "]";
141  return StandardException(loc, UnknownError, ostream.str());
142  }
143  }
144 }
145 
146 StandardException ioctl_exception(const char* loc) {
147 
148  switch( errno ) {
149  case ( EBADF ) : return StandardException(LOC,InvalidObjectError, "Socket control error. The file descriptor was not valid.");
150  case ( EFAULT ) : return StandardException(LOC,OutOfRangeError, "Socket control error. Tried to reference inaccessible memory.");
151  case ( EINVAL ) : return StandardException(LOC,InvalidArgError, "Socket control error. Ioctl input arguments were not valid.");
152  case ( ENOTTY ) : return StandardException(LOC,InvalidObjectError, "Socket control error. The file descriptor is not valid or this operation may not be performed on it.");
153  default : {
154  std::ostringstream ostream;
155  ostream << "Unknown error [" << errno << "]";
156  return StandardException(loc, UnknownError, ostream.str());
157  }
158  }
159 }
160 
161 StandardException gethostbyname_exception(const char* loc, const std::string& hostname) {
162  switch( h_errno ) {
163  case ( HOST_NOT_FOUND ) : {
164  std::string header;
165  header += "Unable to correctly determine the server hostname: ";
166  header += hostname;
167  return StandardException(LOC,OpenError,header);
168  }
169  case ( TRY_AGAIN ) : return StandardException(LOC, OpenError,"A temporary error occurred on an authoritative name server. Try again later.");
170  case ( NO_ADDRESS ) : return StandardException(LOC,InvalidArgError,"Requested server hostname is valid, but does not have an IP address.");
171  case ( NO_RECOVERY ) : return StandardException(LOC,UnknownError);
172  default : {
173  std::ostringstream ostream;
174  ostream << "Unknown error [" << h_errno << "]";
175  return StandardException(loc, UnknownError, ostream.str());
176  }
177  }
178 }
179 
180 StandardException connection_exception(const char* loc) {
181  switch( errno ) {
182  case ( ( EACCES ) || ( EPERM ) ): return StandardException(LOC,PermissionsError,"Write permission on the socket denied or firewalled.");
183  case ( EADDRINUSE ) : return StandardException(LOC,BusyError,"Address already in use.");
184  case ( EAFNOSUPPORT ): return StandardException(LOC,NotSupportedError,"Incorrect address family used (no support for AF maybe?");
185  case ( EAGAIN ) : return StandardException(LOC,OutOfResourcesError,"No free local ports remaining.");
186  case ( EALREADY ) : return StandardException(LOC,BlockingError,"Socket is non-blocking and a previous connection attempt has not yet completed (wtf?).");
187  case ( EBADF ) : return StandardException(LOC,InvalidObjectError,"Not a valid socket descriptor.");
188  case ( ECONNREFUSED ): return StandardException(LOC,ConnectionRefusedError,"Connection refused (no-one listening).");
189  case ( EFAULT ) : return StandardException(LOC,OutOfRangeError,"Socket specification is outside the user address space.");
190  case ( EINPROGRESS ) : return StandardException(LOC,BlockingError,"Socket is non-blocking and the connection cannot be completed immediately (try select or poll for writing).");
191  case ( EINTR ) : return StandardException(LOC,InterruptedError,"Connection interrupted by a system signal.");
192  case ( EISCONN ) : return StandardException(LOC,ConnectionError,"This socket is already connected.");
193  case ( ENETUNREACH ) :
194  case ( EHOSTUNREACH ): return StandardException(LOC,NotFoundError,"The host is unreachable.");
195  case ( ENOTSOCK ) : return StandardException(LOC,InvalidObjectError,"This is not a socket file descriptor.");
196  case ( ETIMEDOUT ) : return StandardException(LOC,TimeOutError,"Timed out.");
197  default : {
198  std::ostringstream ostream;
199  ostream << "Unknown error [" << errno << "]";
200  return StandardException(loc, UnknownError, ostream.str());
201  }
202  }
203 
204 }
205 
206 
207 } // namespace devices
208 } // namespace ecl
209 
210 #endif /* ECL_IS_POSIX */
211 #endif /* !ECL_IS_APPLE */
212 
ecl::OpenError
OpenError
ecl::ConnectionError
ConnectionError
ecl::WriteError
WriteError
ecl::NotFoundError
NotFoundError
ecl::BusyError
BusyError
ecl::UsageError
UsageError
ecl::OutOfResourcesError
OutOfResourcesError
ecl::SystemFailureError
SystemFailureError
ecl::MemoryError
MemoryError
ecl::TimeOutError
TimeOutError
ecl::ConnectionRefusedError
ConnectionRefusedError
ecl::InterruptedError
InterruptedError
ecl::InvalidObjectError
InvalidObjectError
ecl::InvalidArgError
InvalidArgError
ecl::PermissionsError
PermissionsError
standard_exception.hpp
ecl::UnknownError
UnknownError
ecl::NotSupportedError
NotSupportedError
ecl::OutOfRangeError
OutOfRangeError
ecl::BlockingError
BlockingError
ecl
Embedded control libraries.


ecl_devices
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:45