simple-socket library 1.1.5

InternetSocket.cpp

Go to the documentation of this file.
00001 #include "InternetSocket.h"
00002 
00003 #include <sys/socket.h>
00004 #include <arpa/inet.h>
00005 #include <netinet/in.h>
00006 #include <netdb.h>
00007 #include <cerrno>
00008 #include <cstring>
00009 
00010 using namespace NET;
00011 
00012 InternetSocket::InternetSocket( int type, int protocol)
00013 : SimpleSocket( INTERNET, type, protocol)
00014 {}
00015 
00016 InternetSocket::InternetSocket( int sockfd)
00017 : SimpleSocket(sockfd) {}
00018 
00019 void InternetSocket::connect( const std::string& foreignAddress, unsigned short foreignPort)
00020 {
00021     sockaddr_in addr;
00022     fillAddress( foreignAddress, foreignPort, addr);
00023 
00024     if( ::connect( m_socket, (sockaddr*) &addr, sizeof(addr)) < 0)
00025         throw SocketException("Connect failed (connect)");
00026 
00027     m_peerDisconnected = false;
00028 }
00029 
00030 void InternetSocket::bind( unsigned short localPort /* = 0 */)
00031 {
00032     sockaddr_in addr;
00033     addr.sin_family = AF_INET;
00034     addr.sin_addr.s_addr = INADDR_ANY;
00035     addr.sin_port = htons(localPort);
00036 
00037     if( ::bind( m_socket, (sockaddr*) &addr, sizeof(addr)) < 0)
00038         throw SocketException("Set of local port failed (bind)");
00039 }
00040 
00041 void InternetSocket::bind( const std::string& localAddress, unsigned short localPort /* = 0 */)
00042 {
00043     sockaddr_in addr;
00044     fillAddress( localAddress, localPort, addr);
00045 
00046     if( ::bind( m_socket, (sockaddr*) &addr, sizeof(addr)) < 0)
00047         throw SocketException("Set of local address and port failed (bind)");
00048 }
00049 
00050 std::string InternetSocket::getLocalAddress() const
00051 {
00052     sockaddr_in addr;
00053     socklen_t addr_len = sizeof(addr);
00054 
00055     if( getsockname( m_socket, (sockaddr*) &addr, &addr_len) < 0)
00056         throw SocketException("Fetch of local address failed (getsockname)");
00057 
00058     return inet_ntoa( addr.sin_addr);
00059 }
00060 
00061 unsigned short InternetSocket::getLocalPort() const
00062 {
00063     sockaddr_in addr;
00064     socklen_t addr_len = sizeof(addr);
00065 
00066     if( getsockname( m_socket, (sockaddr*) &addr, &addr_len) < 0)
00067         throw SocketException("Fetch of local port failed (getsockname)");
00068 
00069     return ntohs( addr.sin_port);
00070 }
00071 
00072 std::string InternetSocket::getForeignAddress() const
00073 {
00074     sockaddr_in addr;
00075     socklen_t addr_len = sizeof(addr);
00076 
00077     if( getpeername( m_socket, (sockaddr*) &addr, &addr_len) < 0)
00078         throw SocketException("Fetch of foreign address failed (getpeername)");
00079 
00080     return inet_ntoa( addr.sin_addr);
00081 }
00082 
00083 unsigned short InternetSocket::getForeignPort() const
00084 {
00085     sockaddr_in addr;
00086     socklen_t addr_len = sizeof(addr);
00087 
00088     if( getpeername( m_socket, (sockaddr*) &addr, &addr_len) < 0)
00089         throw SocketException("Fetch of foreign port failed (getpeername)");
00090 
00091     return ntohs( addr.sin_port);
00092 }
00093 
00094 void InternetSocket::fillAddress( const std::string& address, unsigned short port, sockaddr_in& addr)
00095 {
00096     addr.sin_family = AF_INET;
00097     addr.sin_port = htons(port);
00098 
00099     // Assume we have a simple ipv4 address
00100     if( inet_aton( address.c_str(), &addr.sin_addr)) return;
00101 
00102     // We need to resolve the address
00103     hostent* host = gethostbyname( address.c_str());
00104     if( host == 0)
00105     {
00106         // strerror() will not work for gethostbyname()
00107         throw SocketException("Failed to resolve address (gethostbyname)", false);
00108     }
00109     addr.sin_addr.s_addr = *reinterpret_cast<uint32_t*>( host->h_addr);
00110 }