| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#if HAVE_CONFIG_H |
| 11 |
#include <conf.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#if HAVE_UNISTD_H |
| 15 |
#include <unistd.h> |
| 16 |
#endif |
| 17 |
#if HAVE_STDLIB_H |
| 18 |
#include <stdlib.h> |
| 19 |
#endif |
| 20 |
#include <stdio.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <string.h> |
| 23 |
#include <xport.h> |
| 24 |
#include <tcp.h> |
| 25 |
|
| 26 |
|
| 27 |
#ifndef SZ_LINE |
| 28 |
#define SZ_LINE 1024 |
| 29 |
#endif |
| 30 |
|
| 31 |
|
| 32 |
#ifndef MYHOST |
| 33 |
#define MYHOST "XPA_HOST" |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
int use_localhost=0; |
| 39 |
|
| 40 |
static char myhost[SZ_LINE]; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static char ipstr[SZ_LINE]; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
#ifdef ANSI_FUNC |
| 76 |
int |
| 77 |
gethost ( |
| 78 |
char *xhost, |
| 79 |
int len |
| 80 |
) |
| 81 |
#else |
| 82 |
int gethost(xhost, len) |
| 83 |
char *xhost; |
| 84 |
int len; |
| 85 |
#endif |
| 86 |
{ |
| 87 |
char *s=NULL; |
| 88 |
struct hostent *hent; |
| 89 |
static int init=0; |
| 90 |
|
| 91 |
if( use_localhost == 0 ){ |
| 92 |
if( init == 0 ){ |
| 93 |
if( (s=(char *)getenv(MYHOST)) != NULL ){ |
| 94 |
strncpy(myhost, s, SZ_LINE-1); |
| 95 |
} else { |
| 96 |
gethostname(myhost, SZ_LINE-1); |
| 97 |
} |
| 98 |
init++; |
| 99 |
} |
| 100 |
strncpy(xhost, myhost, len-1); |
| 101 |
if( (hent = gethostbyname(xhost)) == NULL ){ |
| 102 |
return(-1); |
| 103 |
} |
| 104 |
strncpy(xhost, hent->h_name, len-1); |
| 105 |
} |
| 106 |
else{ |
| 107 |
strncpy(xhost, "localhost", len-1); |
| 108 |
} |
| 109 |
xhost[len-1] = '\0'; |
| 110 |
return(0); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
#ifdef ANSI_FUNC |
| 125 |
unsigned int |
| 126 |
gethostip ( |
| 127 |
char *xhost |
| 128 |
) |
| 129 |
#else |
| 130 |
unsigned int gethostip(xhost) |
| 131 |
char *xhost; |
| 132 |
#endif |
| 133 |
{ |
| 134 |
struct hostent *hostent; |
| 135 |
unsigned int ip; |
| 136 |
char temp[SZ_LINE]; |
| 137 |
int saveip=0; |
| 138 |
static unsigned int myip=0; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if( (xhost == NULL) || (*xhost == '\0') || !strcmp(xhost, "$host") ){ |
| 143 |
if( myip != 0 ) |
| 144 |
return(myip); |
| 145 |
saveip = 1; |
| 146 |
gethost(temp, SZ_LINE); |
| 147 |
} |
| 148 |
else if( !strcmp(xhost, "$localhost") ){ |
| 149 |
strcpy(temp, "localhost"); |
| 150 |
} else { |
| 151 |
strncpy(temp, xhost, SZ_LINE-1); |
| 152 |
temp[SZ_LINE-1] = '\0'; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
if( !strcmp(temp, "localhost") || !strcmp(temp, "localhost.localdomain") ){ |
| 157 |
ip = htonl(0x7F000001); |
| 158 |
goto done; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
if( (int)(ip = inet_addr(temp)) != -1 ){ |
| 166 |
goto done; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if( (hostent = gethostbyname(temp)) != NULL ){ |
| 174 |
memcpy(&ip, hostent->h_addr_list[0], (size_t)hostent->h_length); |
| 175 |
goto done; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
ip = 0; |
| 180 |
saveip = 0; |
| 181 |
|
| 182 |
done: |
| 183 |
ip = ntohl(ip); |
| 184 |
if( saveip ) myip = ip; |
| 185 |
return(ip); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
#ifdef ANSI_FUNC |
| 200 |
char * |
| 201 |
getiphost (unsigned int ip) |
| 202 |
#else |
| 203 |
char *getiphost(ip) |
| 204 |
unsigned int ip; |
| 205 |
#endif |
| 206 |
{ |
| 207 |
char *s; |
| 208 |
struct sockaddr_in sockbuf; |
| 209 |
|
| 210 |
if( ip == 0x7F000001 ){ |
| 211 |
strcpy(ipstr, "localhost"); |
| 212 |
return(ipstr); |
| 213 |
} |
| 214 |
|
| 215 |
sockbuf.sin_addr.s_addr = htonl(ip); |
| 216 |
s = (char *)inet_ntoa(sockbuf.sin_addr); |
| 217 |
if( s != NULL ){ |
| 218 |
strcpy(ipstr, s); |
| 219 |
return(ipstr); |
| 220 |
} |
| 221 |
else |
| 222 |
return(NULL); |
| 223 |
} |