monotone

monotone Mtn Source Tree

Root/netxx/resolve_gethostbyname.cxx

1/*
2 * Copyright (C) 2001-2004 Peter J Jones (pjones@pmade.org)
3 * All Rights Reserved
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 * 3. Neither the name of the Author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33/** @file
34 * This file contains the implementation of the resolve_hostname function
35 * using the gethostbyname system call.
36**/
37
38// common header
39#include "common.h"
40
41// Netxx includes
42#include "resolve.h"
43#include "netxx/types.h"
44#include "sockaddr.h"
45
46// standard includes
47#include <cstring>
48
49//####################################################################
50void Netxx::resolve_hostname (const char *hostname, port_type port, bool use_ipv6, std::vector<Peer> &addrs)
51{
52 { // first try to see if this address is already an IPv4 address
53SockAddr saddr(AF_INET, port);
54sockaddr_in *sai = reinterpret_cast<sockaddr_in*>(saddr.get_sa());;
55
56if (inet_pton(AF_INET, hostname, &(sai->sin_addr)) == 1) {
57 addrs.push_back(Peer(hostname, port, sai, saddr.get_sa_size()));
58 return;
59}
60 }
61
62#ifndef NETXX_NO_INET6
63 if (use_ipv6) { // now try to see if this address is already an IPv6 address
64SockAddr saddr(AF_INET6, port);
65sockaddr_in6 *sai6 = reinterpret_cast<sockaddr_in6*>(saddr.get_sa());
66
67if (inet_pton(AF_INET6, hostname, &(sai6->sin6_addr)) == 1) {
68 addrs.push_back(Peer(hostname, port, sai6, saddr.get_sa_size()));
69 return;
70}
71 }
72#endif
73
74 /*
75 * if we got this far then the address must really be a hostname so
76 * we are going to have to call gethostbyname and either pull it out
77 * of the hosts file or go to DNS. And don't forget NIS and WINS and
78 * ...
79 */
80 hostent *he; // WARNING not MT safe
81 if ( (he = gethostbyname(hostname)) == 0) {
82std::string error("name resolution failure for "); error += hostname;
83// HACK: Winsock uses a totally different error reporting mechanism.
84#ifdef WIN32
85 error += ": "; error += str_error(get_last_error());
86#else
87error += ": "; error += hstrerror(h_errno);
88#endif
89throw NetworkException(error);
90 }
91
92 for (char **ii = he->h_addr_list; *ii != 0; ++ii) {
93switch (he->h_addrtype) {
94 case AF_INET:
95 {
96SockAddr saddr(AF_INET, port);
97sockaddr_in *sai = reinterpret_cast<sockaddr_in*>(saddr.get_sa());
98std::memcpy(&(sai->sin_addr), *ii, sizeof(sai->sin_addr));
99addrs.push_back(Peer(he->h_name, port, sai, saddr.get_sa_size()));
100 }
101 break;
102
103#ifndef NETXX_NO_INET6
104 case AF_INET6:
105 {
106SockAddr saddr(AF_INET6, port);
107sockaddr_in6 *sai6 = reinterpret_cast<sockaddr_in6*>(saddr.get_sa());
108std::memcpy(&(sai6->sin6_addr), *ii, sizeof(sai6->sin6_addr));
109addrs.push_back(Peer(he->h_name, port, sai6, saddr.get_sa_size()));
110 }
111 break;
112#endif
113}
114 }
115}
116//####################################################################

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status