00001
00002
00003 #include "common.h"
00004 #include "internet.h"
00005
00006 static char const rcsid[] = "$Id: internet.c,v 1.10 2005/04/10 14:41:23 markd Exp $";
00007
00008 boolean internetIsDottedQuad(char *s)
00009
00010 {
00011 int i;
00012 if (!isdigit(s[0]))
00013 return FALSE;
00014 for (i=0; i<3; ++i)
00015 {
00016 s = strchr(s, '.');
00017 if (s == NULL)
00018 return FALSE;
00019 s += 1;
00020 if (!isdigit(s[0]))
00021 return FALSE;
00022 }
00023 return TRUE;
00024 }
00025
00026 bits32 internetHostIp(char *hostName)
00027
00028
00029 {
00030 struct hostent *hostent;
00031 bits32 ret;
00032 if (internetIsDottedQuad(hostName))
00033 {
00034 internetDottedQuadToIp(hostName, &ret);
00035 }
00036 else
00037 {
00038 hostent = gethostbyname(hostName);
00039 if (hostent == NULL)
00040 {
00041 warn("Couldn't find host %s. h_errno %d", hostName, h_errno);
00042 return 0;
00043 }
00044 memcpy(&ret, hostent->h_addr_list[0], sizeof(ret));
00045 ret = ntohl(ret);
00046 }
00047 return ret;
00048 }
00049
00050 boolean internetFillInAddress(char *hostName, int port, struct sockaddr_in *address)
00051
00052 {
00053 ZeroVar(address);
00054 address->sin_family = AF_INET;
00055 address->sin_port = htons(port);
00056 if (hostName == NULL)
00057 address->sin_addr.s_addr = INADDR_ANY;
00058 else
00059 {
00060 if ((address->sin_addr.s_addr = htonl(internetHostIp(hostName))) == 0)
00061 return FALSE;
00062 }
00063 return TRUE;
00064 }
00065
00066 boolean internetIpToDottedQuad(bits32 ip, char dottedQuad[17])
00067
00068
00069
00070 {
00071 #ifndef __CYGWIN32__
00072 struct in_addr ia;
00073 zeroBytes(dottedQuad, 17);
00074 ZeroVar(&ia);
00075 ia.s_addr = htonl(ip);
00076 if (inet_ntop(AF_INET, &ia, dottedQuad, 16) == NULL)
00077 {
00078 warn("conversion problem on 0x%x in internetIpToDottedQuad: %s",
00079 ip, strerror(errno));
00080 return FALSE;
00081 }
00082 return TRUE;
00083 #else
00084 warn("Sorry, internetIpToDottedQuad not supported in Windows.");
00085 return FALSE;
00086 #endif
00087 }
00088
00089 boolean internetDottedQuadToIp(char *dottedQuad, bits32 *retIp)
00090
00091
00092
00093 {
00094 #ifndef __CYGWIN32__
00095 struct in_addr ia;
00096 if (inet_pton(AF_INET, dottedQuad, &ia) < 0)
00097 {
00098 warn("internetDottedQuadToIp problem on %s: %s", dottedQuad, strerror(errno));
00099 return FALSE;
00100 }
00101 *retIp = ntohl(ia.s_addr);
00102 return TRUE;
00103 #else
00104 warn("Sorry, internetDottedQuadToIp not supported in Windows.");
00105 return FALSE;
00106 #endif
00107 }
00108
00109 void internetParseDottedQuad(char *dottedQuad, unsigned char quad[4])
00110
00111 {
00112 char *s = dottedQuad;
00113 int i;
00114 if (!internetIsDottedQuad(s))
00115 errAbort("%s is not a dotted quad", s);
00116 for (i=0; i<4; ++i)
00117 {
00118 quad[i] = atoi(s);
00119 s = strchr(s, '.') + 1;
00120 }
00121 }
00122
00123 void internetUnpackIp(bits32 packed, unsigned char unpacked[4])
00124
00125
00126 {
00127 int i;
00128 for (i=3; i>=0; --i)
00129 {
00130 unpacked[i] = (packed&0xff);
00131 packed >>= 8;
00132 }
00133 }
00134
00135 boolean internetIpInSubnet(unsigned char unpackedIp[4], unsigned char subnet[4])
00136
00137 {
00138 int i;
00139 for (i=0; i<4; ++i)
00140 {
00141 unsigned char c = subnet[i];
00142 if (c == 255)
00143 return TRUE;
00144 if (c != unpackedIp[i])
00145 return FALSE;
00146 }
00147 return TRUE;
00148 }
00149