Replaced inet_addr() with inet_aton(), which is a better function

anyway but isn't implemented in winsock, removed use of INADDR_NONE
which some platforms don't define except on winsock which does
define it, and changed SOL_TCP to IPPROTO_TCP which should work on
more platforms.
This commit is contained in:
crs 2002-10-20 22:36:24 +00:00
parent 285cc3abc0
commit 8f229393b8
3 changed files with 34 additions and 17 deletions

View File

@ -29,7 +29,6 @@ int (PASCAL FAR *CNetwork::ioctl)(CNetwork::Socket s, int cmd, void FAR *);
int (PASCAL FAR *CNetwork::getpeername)(CNetwork::Socket s, CNetwork::Address FAR *name, CNetwork::AddressLength FAR * namelen);
int (PASCAL FAR *CNetwork::getsockname)(CNetwork::Socket s, CNetwork::Address FAR *name, CNetwork::AddressLength FAR * namelen);
int (PASCAL FAR *CNetwork::getsockopt)(CNetwork::Socket s, int level, int optname, void FAR * optval, CNetwork::AddressLength FAR *optlen);
unsigned long (PASCAL FAR *CNetwork::inet_addr)(const char FAR * cp);
int (PASCAL FAR *CNetwork::listen)(CNetwork::Socket s, int backlog);
ssize_t (PASCAL FAR *CNetwork::read)(CNetwork::Socket s, void FAR * buf, size_t len);
ssize_t (PASCAL FAR *CNetwork::recv)(CNetwork::Socket s, void FAR * buf, size_t len, int flags);
@ -198,7 +197,7 @@ CNetwork::init2(
setfunc(getpeername, getpeername, int (PASCAL FAR *)(Socket s, Address FAR *name, AddressLength FAR * namelen));
setfunc(getsockname, getsockname, int (PASCAL FAR *)(Socket s, Address FAR *name, AddressLength FAR * namelen));
setfunc(getsockopt, getsockopt, int (PASCAL FAR *)(Socket s, int level, int optname, void FAR * optval, AddressLength FAR *optlen));
setfunc(inet_addr, inet_addr, unsigned long (PASCAL FAR *)(const char FAR * cp));
setfunc(inet_addr_n, inet_addr, unsigned long (PASCAL FAR *)(const char FAR * cp));
setfunc(inet_ntoa_n, inet_ntoa, char FAR * (PASCAL FAR *)(struct in_addr in));
setfunc(listen, listen, int (PASCAL FAR *)(Socket s, int backlog));
setfunc(recv, recv, ssize_t (PASCAL FAR *)(Socket s, void FAR * buf, size_t len, int flags));
@ -238,6 +237,22 @@ CNetwork::write2(Socket s, const void FAR* buf, size_t len)
return send(s, buf, len, 0);
}
int PASCAL FAR
CNetwork::inet_aton(const char FAR * cp, InternetAddress FAR * addr)
{
assert(addr != NULL);
// fake it with inet_addr
unsigned long inetAddr = inet_addr_n(cp);
if (inetAddr == INADDR_NONE) {
return 0;
}
else {
*(addr->s_addr) = inetAddr;
return 1;
}
}
CString PASCAL FAR
CNetwork::inet_ntoa(struct in_addr in)
{
@ -387,7 +402,7 @@ CNetwork::setnodelay(CNetwork::Socket s, bool nodelay)
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if !defined(TCP_NODELAY) || !defined(SOL_TCP)
#if !defined(TCP_NODELAY)
# include <netinet/tcp.h>
#endif
@ -444,7 +459,6 @@ CNetwork::init()
setfunc(getpeername, getpeername, int (PASCAL FAR *)(Socket s, Address FAR *name, AddressLength FAR * namelen));
setfunc(getsockname, getsockname, int (PASCAL FAR *)(Socket s, Address FAR *name, AddressLength FAR * namelen));
setfunc(getsockopt, getsockopt, int (PASCAL FAR *)(Socket s, int level, int optname, void FAR * optval, AddressLength FAR *optlen));
setfunc(inet_addr, inet_addr, unsigned long (PASCAL FAR *)(const char FAR * cp));
setfunc(listen, listen, int (PASCAL FAR *)(Socket s, int backlog));
#if HAVE_POLL
setfunc(poll, poll, int (PASCAL FAR *)(CNetwork::PollEntry fds[], int nfds, int timeout));
@ -483,6 +497,12 @@ CNetwork::getsockerror2(void)
return errno;
}
int PASCAL FAR
CNetwork::inet_aton(const char FAR * cp, InternetAddress FAR * addr)
{
return ::inet_aton(cp, addr);
}
CString PASCAL FAR
CNetwork::inet_ntoa(struct in_addr in)
{
@ -628,7 +648,7 @@ int PASCAL FAR
CNetwork::setnodelay(CNetwork::Socket s, bool nodelay)
{
int flag = nodelay ? 1 : 0;
setsockopt(s, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
}
#endif

View File

@ -55,10 +55,12 @@ public:
typedef SOCKET Socket;
typedef struct sockaddr Address;
typedef int AddressLength;
typedef struct in_addr InternetAddress;
#elif UNIX_LIKE
typedef int Socket;
typedef struct sockaddr Address;
typedef socklen_t AddressLength;
typedef struct in_addr InternetAddress;
#endif
#if WINDOWS_LIKE || !HAVE_POLL
@ -214,7 +216,7 @@ public:
static int (PASCAL FAR *getpeername)(Socket s, Address FAR *name, AddressLength FAR * namelen);
static int (PASCAL FAR *getsockname)(Socket s, Address FAR *name, AddressLength FAR * namelen);
static int (PASCAL FAR *getsockopt)(Socket s, int level, int optname, void FAR * optval, AddressLength FAR *optlen);
static unsigned long (PASCAL FAR *inet_addr)(const char FAR * cp);
static int PASCAL FAR inet_aton(const char FAR * cp, InternetAddress FAR * addr);
static CString PASCAL FAR inet_ntoa(struct in_addr in);
static int (PASCAL FAR *listen)(Socket s, int backlog);
static ssize_t (PASCAL FAR *read)(Socket s, void FAR * buf, size_t len);
@ -260,6 +262,7 @@ private:
static int (PASCAL FAR *__WSAFDIsSet)(CNetwork::Socket, fd_set FAR *);
static int (PASCAL FAR *select)(int nfds, fd_set FAR *readfds, fd_set FAR *writefds, fd_set FAR *exceptfds, const struct timeval FAR *timeout);
static char FAR * (PASCAL FAR *inet_ntoa_n)(struct in_addr in);
static unsigned long (PASCAL FAR *inet_addr_n)(const char FAR * cp);
static struct hostent FAR * (PASCAL FAR *gethostbyaddr_n)(const char FAR * addr, int len, int type);
static struct hostent FAR * (PASCAL FAR *gethostbyname_n)(const char FAR * name);
static struct servent FAR * (PASCAL FAR *getservbyport_n)(int port, const char FAR * proto);

View File

@ -103,9 +103,9 @@ CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
}
// if hostname is empty then use wildcard address
if (hostname.empty()) {
struct sockaddr_in* inetAddress = reinterpret_cast<
struct sockaddr_in*>(&m_address);
if (hostname.empty()) {
inetAddress->sin_family = AF_INET;
inetAddress->sin_port = CNetwork::swaphtons(port);
inetAddress->sin_addr.s_addr = INADDR_ANY;
@ -114,13 +114,9 @@ CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
}
// convert dot notation to address
unsigned long addr = CNetwork::inet_addr(hostname.c_str());
if (addr != INADDR_NONE) {
struct sockaddr_in* inetAddress = reinterpret_cast<
struct sockaddr_in*>(&m_address);
if (CNetwork::inet_aton(hostname.c_str(), &inetAddress->sin_addr) != 0) {
inetAddress->sin_family = AF_INET;
inetAddress->sin_port = CNetwork::swaphtons(port);
inetAddress->sin_addr.s_addr = addr;
memset(inetAddress->sin_zero, 0, sizeof(inetAddress->sin_zero));
return;
}
@ -143,8 +139,6 @@ CNetworkAddress::CNetworkAddress(const CString& hostname_, UInt16 port) :
throw XSocketAddress(XSocketAddress::kUnknown, hostname, port);
}
struct sockaddr_in* inetAddress = reinterpret_cast<
struct sockaddr_in*>(&m_address);
inetAddress->sin_family = hostInfo.m_addressType;
inetAddress->sin_port = CNetwork::swaphtons(port);
memcpy(&inetAddress->sin_addr, hostInfo.m_addresses[0],