File: vanessa_socket_host.c

package info (click to toggle)
vanessa-socket 0.0.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,568 kB
  • ctags: 121
  • sloc: sh: 7,818; ansic: 1,297; perl: 132; makefile: 110
file content (76 lines) | stat: -rw-r--r-- 2,753 bytes parent folder | download | duplicates (2)
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
/**********************************************************************
 * vanessa_socket_host.c                                  December 1999
 * Horms                                             horms@verge.net.au
 *
 * Operations on host names
 *
 * vanessa_socket
 * Library to simplify handling of TCP sockets
 * Copyright (C) 1999-2004  Horms
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307 USA
 *
 **********************************************************************/

#include "vanessa_socket.h"


/**********************************************************************
 * vanessa_socket_host_in_addr
 * A host is given as a string either as a host name or IP address
 * as a dotted quad. The host is used to seed an in_addr structure
 * with a binary representation of the IP address of the host
 * in network byte order.
 * pre: host: hostname or IP address
 *            If NULL then INADDR_ANY will be converted to network
 *            byte order and used as the address.
 *      in: pointer to an in_addr structure.
 *      flag: Flags. If the VANESSA_SOCKET_NO_LOOKUP bit is set then
 *            no hostname lookups will be performed. That is the
 *            host given as an argument should be an IP address
 * post: none
 * return: 0 on success
 *         -1 on error
 **********************************************************************/

int vanessa_socket_host_in_addr(const char *host,
				struct in_addr *in,
				const vanessa_socket_flag_t flag)
{
	struct hostent *hp;
	extern int h_errno;

	if (host == NULL) {
		in->s_addr = htonl(INADDR_ANY);
	} else if (flag & VANESSA_SOCKET_NO_LOOKUP) {
		if (inet_aton(host, in) == 0) {
			VANESSA_LOGGER_DEBUG_UNSAFE("invalid IP address (%s): "
					"Are you trying to resolve a hostname "
					"with no lookup enabled?", host);
			return (-1);
		}
	} else {
		if ((hp = gethostbyname(host)) == NULL) {
			VANESSA_LOGGER_DEBUG_UNSAFE("gethostbyname (%s): %s",
					host, 
					vanessa_logger_strherror(h_errno));
			return (-1);
		}
		memcpy(in, hp->h_addr, hp->h_length);
	}

	return (0);
}