]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/raw_socket.cxx
SGPath: fix creating paths with permission checker.
[simgear.git] / simgear / io / raw_socket.cxx
index cf7b20f7bf873b869ace2287e9093c3dc68c19cb..c0c3ff22e9679dac0a34494df0d6d3aece9c5e68 100644 (file)
@@ -17,7 +17,7 @@
 
      You should have received a copy of the GNU Library General Public
      License along with this library; if not, write to the Free Software
-     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
 */
 
 #ifdef HAVE_CONFIG_H
@@ -63,6 +63,8 @@
 #include <simgear/structure/exception.hxx>
 #include <simgear/threads/SGThread.hxx>
 
+using std::string;
+
 namespace {
 
 class Resolver : public SGThread
@@ -311,6 +313,10 @@ bool IPAddress::lookupNonblocking(const char* host, IPAddress& addr)
 
 const char* IPAddress::getHost () const
 {
+    if (!addr) {
+        return NULL;
+    }
+    
   static char buf [32];
        long x = ntohl(addr->sin_addr.s_addr);
        sprintf(buf, "%d.%d.%d.%d",
@@ -321,21 +327,37 @@ const char* IPAddress::getHost () const
 
 unsigned int IPAddress::getIP () const
 {
+    if (!addr) {
+        return 0;
+    }
+    
        return addr->sin_addr.s_addr;
 }
 
 unsigned int IPAddress::getPort() const
 {
+    if (!addr) {
+        return 0;
+    }
+    
   return ntohs(addr->sin_port);
 }
 
 void IPAddress::setPort(int port)
 {
+    if (!addr) {
+        return;
+    }
+    
     addr->sin_port = htons(port);
 }
 
 unsigned int IPAddress::getFamily () const
 {
+    if (!addr) {
+        return 0;
+    }
+    
        return addr->sin_family;
 }
 
@@ -363,7 +385,11 @@ const char* IPAddress::getLocalHost ()
 
 bool IPAddress::getBroadcast () const
 {
-  return (addr->sin_addr.s_addr == INADDR_BROADCAST);
+    if (!addr) {
+        return false;
+    }
+    
+    return (addr->sin_addr.s_addr == INADDR_BROADCAST);
 }
 
 unsigned int IPAddress::getAddrLen() const
@@ -381,6 +407,11 @@ struct sockaddr* IPAddress::getAddr() const
     return (struct sockaddr*) addr;
 }
 
+bool IPAddress::isValid() const
+{
+    return (addr != NULL);
+}
+
 Socket::Socket ()
 {
   handle = -1 ;
@@ -423,6 +454,23 @@ bool Socket::open ( bool stream )
 #endif
   }
 
+#ifdef SO_NOSIGPIPE
+  // Do not generate SIGPIPE signal (which immediately terminates the program),
+  // instead ::send() will return -1 and errno will be set to EPIPE.
+  // SO_NOSIGPIPE should be available on Mac/BSD systems, but is not available
+  // within Posix/Linux.
+  // This only works for calls to ::send() but not for ::write():
+  // http://freebsd.1045724.n5.nabble.com/is-setsockopt-SO-NOSIGPIPE-work-tp4011054p4011055.html
+  int set = 1;
+  setsockopt(handle, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(set));
+#endif
+
+#ifndef MSG_NOSIGNAL
+# define MSG_NOSIGNAL 0
+#endif
+  // TODO supress SIGPIPE if neither SO_NOSIGPIPE nor MSG_NOSIGNAL is available
+  // http://krokisplace.blogspot.co.at/2010/02/suppressing-sigpipe-in-library.html
+
   return (handle != -1);
 }
 
@@ -561,7 +609,7 @@ int Socket::connect ( IPAddress* addr )
 int Socket::send (const void * buffer, int size, int flags)
 {
   assert ( handle != -1 ) ;
-  return ::send (handle, (const char*)buffer, size, flags);
+  return ::send (handle, (const char*)buffer, size, flags | MSG_NOSIGNAL);
 }
 
 
@@ -569,8 +617,12 @@ int Socket::sendto ( const void * buffer, int size,
                         int flags, const IPAddress* to )
 {
   assert ( handle != -1 ) ;
-  return ::sendto(handle,(const char*)buffer,size,flags,
-                         (const sockaddr*) to->getAddr(), to->getAddrLen());
+  return ::sendto( handle,
+                   (const char*)buffer,
+                   size,
+                   flags | MSG_NOSIGNAL,
+                   (const sockaddr*)to->getAddr(),
+                   to->getAddrLen() );
 }