blob: 98fdc894298213dd769a30f7cb6dba8897135d7b [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#ifndef __PJ_ADDR_RESOLV_H__
21#define __PJ_ADDR_RESOLV_H__
22
23/**
24 * @file addr_resolv.h
Benny Prijono594e4c52006-09-14 18:51:01 +000025 * @brief IP address resolution.
Benny Prijono5dcb38d2005-11-21 01:55:47 +000026 */
27
Benny Prijono594e4c52006-09-14 18:51:01 +000028#include <pj/sock.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000029
30PJ_BEGIN_DECL
31
32/**
33 * @defgroup pj_addr_resolve Network Address Resolution
34 * @ingroup PJ_IO
35 * @{
36 *
37 * This module provides function to resolve Internet address of the
38 * specified host name. To resolve a particular host name, application
39 * can just call #pj_gethostbyname().
40 *
41 * Example:
42 * <pre>
43 * ...
44 * pj_hostent he;
45 * pj_status_t rc;
46 * pj_str_t host = pj_str("host.example.com");
47 *
48 * rc = pj_gethostbyname( &host, &he);
49 * if (rc != PJ_SUCCESS) {
50 * char errbuf[80];
51 * pj_strerror( rc, errbuf, sizeof(errbuf));
52 * PJ_LOG(2,("sample", "Unable to resolve host, error=%s", errbuf));
53 * return rc;
54 * }
55 *
56 * // process address...
57 * addr.sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
58 * ...
59 * </pre>
60 *
61 * It's pretty simple really...
62 */
63
64/** This structure describes an Internet host address. */
65typedef struct pj_hostent
66{
67 char *h_name; /**< The official name of the host. */
68 char **h_aliases; /**< Aliases list. */
69 int h_addrtype; /**< Host address type. */
70 int h_length; /**< Length of address. */
71 char **h_addr_list; /**< List of addresses. */
72} pj_hostent;
73
74/** Shortcut to h_addr_list[0] */
75#define h_addr h_addr_list[0]
76
Benny Prijonoc16c6e32007-11-18 14:53:47 +000077/**
78 * This structure describes address information pj_getaddrinfo().
79 */
80typedef struct pj_addrinfo
81{
82 char ai_canonname[PJ_MAX_HOSTNAME]; /**< Canonical name for host*/
83 pj_sockaddr ai_addr; /**< Binary address. */
84} pj_addrinfo;
85
86
Benny Prijono5dcb38d2005-11-21 01:55:47 +000087/**
88 * This function fills the structure of type pj_hostent for a given host name.
Benny Prijono62b86eb2007-12-01 08:52:57 +000089 * For host resolution function that also works with IPv6, please see
90 * #pj_getaddrinfo().
Benny Prijono5dcb38d2005-11-21 01:55:47 +000091 *
Benny Prijono5f199172009-08-22 11:18:50 +000092 * @param name Host name to resolve. Specifying IPv4 address here
93 * may fail on some platforms (e.g. Windows)
Benny Prijono62b86eb2007-12-01 08:52:57 +000094 * @param he The pj_hostent structure to be filled. Note that
95 * the pointers in this structure points to temporary
96 * variables which value will be reset upon subsequent
97 * invocation.
Benny Prijono5dcb38d2005-11-21 01:55:47 +000098 *
99 * @return PJ_SUCCESS, or the appropriate error codes.
100 */
101PJ_DECL(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he);
102
103
Benny Prijono594e4c52006-09-14 18:51:01 +0000104/**
105 * Resolve the primary IP address of local host.
106 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000107 * @param af The desired address family to query. Valid values
108 * are pj_AF_INET() or pj_AF_INET6().
109 * @param addr On successful resolution, the address family and address
110 * part of this socket address will be filled up with the host
111 * IP address, in network byte order. Other parts of the socket
112 * address are untouched.
Benny Prijono594e4c52006-09-14 18:51:01 +0000113 *
114 * @return PJ_SUCCESS on success, or the appropriate error code.
115 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000116PJ_DECL(pj_status_t) pj_gethostip(int af, pj_sockaddr *addr);
Benny Prijono594e4c52006-09-14 18:51:01 +0000117
118
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000119/**
Benny Prijono4c8fb532007-11-26 05:36:18 +0000120 * Get the IP address of the default interface. Default interface is the
121 * interface of the default route.
122 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000123 * @param af The desired address family to query. Valid values
124 * are pj_AF_INET() or pj_AF_INET6().
125 * @param addr On successful resolution, the address family and address
126 * part of this socket address will be filled up with the host
127 * IP address, in network byte order. Other parts of the socket
128 * address are untouched.
Benny Prijono4c8fb532007-11-26 05:36:18 +0000129 *
130 * @return PJ_SUCCESS on success, or the appropriate error code.
131 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000132PJ_DECL(pj_status_t) pj_getdefaultipinterface(int af,
133 pj_sockaddr *addr);
Benny Prijono4c8fb532007-11-26 05:36:18 +0000134
135
136/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000137 * This function translates the name of a service location (for example,
138 * a host name) and returns a set of addresses and associated information
139 * to be used in creating a socket with which to address the specified
140 * service.
141 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000142 * @param af The desired address family to query. Valid values
143 * are pj_AF_INET(), pj_AF_INET6(), or pj_AF_UNSPEC().
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000144 * @param name Descriptive name or an address string, such as host
145 * name.
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000146 * @param count On input, it specifies the number of elements in
147 * \a ai array. On output, this will be set with the
148 * number of address informations found for the
149 * specified name.
Benny Prijono62b86eb2007-12-01 08:52:57 +0000150 * @param ai Array of address info to be filled with the information
151 * about the host.
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000152 *
153 * @return PJ_SUCCESS on success, or the appropriate error code.
154 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000155PJ_DECL(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *name,
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000156 unsigned *count, pj_addrinfo ai[]);
157
158
159
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000160/** @} */
161
162PJ_END_DECL
163
164#endif /* __PJ_ADDR_RESOLV_H__ */
165