File: host.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (130 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download | duplicates (4)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* $Id: host.c,v 1.5 2003/07/12 10:25:41 mhe Exp $
 *
 * host.c -- DNS lookups of hostnames
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See COPYING for more details.
 */

#include "syshdr.h"
#include "host.h"
#include "gvars.h"

/* saved reply from getservent */
static int ftp_servent_port = -1; /* -1 == not initialized */

Host *host_create(const url_t *urlp)
{
	Host *hostp;

	hostp = (Host *)xmalloc(sizeof(Host));

	hostp->hep = 0;
	hostp->hostname = xstrdup(urlp->hostname);
	hostp->port = urlp->port; /* host byte order */
	if(hostp->port <= 0)
		hostp->port = -1;
	else
		hostp->port = htons(hostp->port); /* to network byte order */

	return hostp;
}

void host_destroy(Host *hostp)
{
	if(hostp) {
		free(hostp->ipnum);
		free(hostp->hostname);
		free(hostp->ohostname);
		free(hostp);
	}
}

/* returns 0 on success, -1 on failure, (sets h_errno)
 */
int host_lookup(Host *hostp)
{
	struct in_addr ia;

	if(!hostp->hostname) {
/*		h_errno = HOST_NOT_FOUND;*/
		return -1;
	}

	/* check if host is given in numbers-and-dots notation */
	/* FIXME: should check if inet_aton is not present -> use inet_addr() */
	if(inet_aton(hostp->hostname, &ia)) {
		if(gvReverseDNS)
			hostp->hep = gethostbyaddr((char *)&ia, sizeof(ia), AF_INET);
		if(hostp->hep == 0) {
			hostp->alt_h_length = sizeof(ia);
			memcpy((void *)&hostp->alt_h_addr, &ia, hostp->alt_h_length);
			hostp->ipnum = xstrdup(hostp->hostname);
			hostp->ohostname = xstrdup(hostp->ipnum);
		}
	}
	else {
		hostp->hep = gethostbyname(hostp->hostname);
		if(hostp->hep == 0)
			return -1;
	}

	if(hostp->hep) {
		struct in_addr tmp;
		memcpy(&tmp, hostp->hep->h_addr, hostp->hep->h_length);
		hostp->ipnum = xstrdup(inet_ntoa(tmp));
		hostp->ohostname = xstrdup(hostp->hep->h_name); /* official name of host */
	}

	/* let system pick port */
	if(hostp->port == -1) {
		if(ftp_servent_port == -1) {
			struct servent *sep;

			sep = getservbyname("ftp", "tcp");
			if(sep == 0)
				ftp_servent_port = 21;
			else
				ftp_servent_port = sep->s_port;
		}
		hostp->port = ftp_servent_port;
	}

	return 0;
}

/* returns port in network byte order */
unsigned short int host_getport(const Host *hostp)
{
	return hostp->port;
}

/* returns port in host byte order */
unsigned short int host_gethport(const Host *hostp)
{
	return ntohs(hostp->port);
}

/* returns IP number */
const char *host_getip(const Host *hostp)
{
	return hostp->ipnum;
}

/* returns name as passed to host_set() */
const char *host_getname(const Host *hostp)
{
	return hostp->hostname;
}

/* returns official name (as returned from gethostbyname()) */
const char *host_getoname(const Host *hostp)
{
	return hostp->ohostname;
}