ifndef INADDR_NONE define INADDR_NONE 0xffffffff endif int check_hostn

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
/****************************************************************************************************/
int
check_hostname (char *dest_url)
{
int port;
int i, j;
char *errpos;
char portnum[256];
char address[256];
long int lport;
struct hostent *host;
struct in_addr *addr;
struct servent *serv;
HOST = INADDR_NONE;
for (i = 7; dest_url[i] != ':' && dest_url[i]; i++)
;
if (dest_url[i] == ':')
{
for (i++, j = 0; dest_url[i] != '/'; i++)
portnum[j++] = dest_url[i];
portnum[j] = NULL_CHAR;
lport = strtol (portnum, &errpos, 0);
if ((errpos[0] != 0) || (lport < 1) || (lport > 65535))
usage ("#Invalid port address");
PORT = htons (lport);
}
else
PORT = htons ((long int) 80);
for (i = 7, j = 0; dest_url[i] != '/' && dest_url[i] != ':'; i++)
address[j++] = dest_url[i];
address[j] = NULL_CHAR;
for (i = 0; i < 3; i++)
{
/* First try it as aaa.bbb.ccc.ddd. */
HOST = inet_addr (address);
if (HOST != INADDR_NONE)
break;
else
{
host = gethostbyname (address);
if (host != NULL)
{
addr = (struct in_addr *) *host->h_addr_list;
HOST = addr->s_addr;
break;
}
else
sleep (10 * (i + 1));
}
}
if (HOST == INADDR_NONE)
usage ("#can't connect to remote host--invalid hostname");
}