blob: cad82730c6477f5043bd4c1dc87779c74c1965e8 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __PJ_ADDR_RESOLV_H__
20#define __PJ_ADDR_RESOLV_H__
21
22/**
23 * @file addr_resolv.h
Benny Prijono594e4c52006-09-14 18:51:01 +000024 * @brief IP address resolution.
Benny Prijono5dcb38d2005-11-21 01:55:47 +000025 */
26
Benny Prijono594e4c52006-09-14 18:51:01 +000027#include <pj/sock.h>
Benny Prijono5dcb38d2005-11-21 01:55:47 +000028
29PJ_BEGIN_DECL
30
31/**
32 * @defgroup pj_addr_resolve Network Address Resolution
33 * @ingroup PJ_IO
34 * @{
35 *
36 * This module provides function to resolve Internet address of the
37 * specified host name. To resolve a particular host name, application
38 * can just call #pj_gethostbyname().
39 *
40 * Example:
41 * <pre>
42 * ...
43 * pj_hostent he;
44 * pj_status_t rc;
45 * pj_str_t host = pj_str("host.example.com");
46 *
47 * rc = pj_gethostbyname( &host, &he);
48 * if (rc != PJ_SUCCESS) {
49 * char errbuf[80];
50 * pj_strerror( rc, errbuf, sizeof(errbuf));
51 * PJ_LOG(2,("sample", "Unable to resolve host, error=%s", errbuf));
52 * return rc;
53 * }
54 *
55 * // process address...
56 * addr.sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
57 * ...
58 * </pre>
59 *
60 * It's pretty simple really...
61 */
62
63/** This structure describes an Internet host address. */
64typedef struct pj_hostent
65{
66 char *h_name; /**< The official name of the host. */
67 char **h_aliases; /**< Aliases list. */
68 int h_addrtype; /**< Host address type. */
69 int h_length; /**< Length of address. */
70 char **h_addr_list; /**< List of addresses. */
71} pj_hostent;
72
73/** Shortcut to h_addr_list[0] */
74#define h_addr h_addr_list[0]
75
Benny Prijonoc16c6e32007-11-18 14:53:47 +000076/**
77 * This structure describes address information pj_getaddrinfo().
78 */
79typedef struct pj_addrinfo
80{
81 char ai_canonname[PJ_MAX_HOSTNAME]; /**< Canonical name for host*/
82 pj_sockaddr ai_addr; /**< Binary address. */
83} pj_addrinfo;
84
85
Benny Prijono5dcb38d2005-11-21 01:55:47 +000086/**
87 * This function fills the structure of type pj_hostent for a given host name.
Benny Prijono62b86eb2007-12-01 08:52:57 +000088 * For host resolution function that also works with IPv6, please see
89 * #pj_getaddrinfo().
Benny Prijono5dcb38d2005-11-21 01:55:47 +000090 *
Benny Prijono62b86eb2007-12-01 08:52:57 +000091 * @param name Host name, or IPv4 address in standard dot notation.
92 * @param he The pj_hostent structure to be filled. Note that
93 * the pointers in this structure points to temporary
94 * variables which value will be reset upon subsequent
95 * invocation.
Benny Prijono5dcb38d2005-11-21 01:55:47 +000096 *
97 * @return PJ_SUCCESS, or the appropriate error codes.
98 */
99PJ_DECL(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he);
100
101
Benny Prijono594e4c52006-09-14 18:51:01 +0000102/**
103 * Resolve the primary IP address of local host.
104 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000105 * @param af The desired address family to query. Valid values
106 * are pj_AF_INET() or pj_AF_INET6().
107 * @param addr On successful resolution, the address family and address
108 * part of this socket address will be filled up with the host
109 * IP address, in network byte order. Other parts of the socket
110 * address are untouched.
Benny Prijono594e4c52006-09-14 18:51:01 +0000111 *
112 * @return PJ_SUCCESS on success, or the appropriate error code.
113 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000114PJ_DECL(pj_status_t) pj_gethostip(int af, pj_sockaddr *addr);
Benny Prijono594e4c52006-09-14 18:51:01 +0000115
116
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000117/**
Benny Prijono4c8fb532007-11-26 05:36:18 +0000118 * Get the IP address of the default interface. Default interface is the
119 * interface of the default route.
120 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000121 * @param af The desired address family to query. Valid values
122 * are pj_AF_INET() or pj_AF_INET6().
123 * @param addr On successful resolution, the address family and address
124 * part of this socket address will be filled up with the host
125 * IP address, in network byte order. Other parts of the socket
126 * address are untouched.
Benny Prijono4c8fb532007-11-26 05:36:18 +0000127 *
128 * @return PJ_SUCCESS on success, or the appropriate error code.
129 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000130PJ_DECL(pj_status_t) pj_getdefaultipinterface(int af,
131 pj_sockaddr *addr);
Benny Prijono4c8fb532007-11-26 05:36:18 +0000132
133
134/**
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000135 * This function translates the name of a service location (for example,
136 * a host name) and returns a set of addresses and associated information
137 * to be used in creating a socket with which to address the specified
138 * service.
139 *
Benny Prijono62b86eb2007-12-01 08:52:57 +0000140 * @param af The desired address family to query. Valid values
141 * are pj_AF_INET(), pj_AF_INET6(), or pj_AF_UNSPEC().
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000142 * @param name Descriptive name or an address string, such as host
143 * name.
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000144 * @param count On input, it specifies the number of elements in
145 * \a ai array. On output, this will be set with the
146 * number of address informations found for the
147 * specified name.
Benny Prijono62b86eb2007-12-01 08:52:57 +0000148 * @param ai Array of address info to be filled with the information
149 * about the host.
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000150 *
151 * @return PJ_SUCCESS on success, or the appropriate error code.
152 */
Benny Prijono62b86eb2007-12-01 08:52:57 +0000153PJ_DECL(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *name,
Benny Prijonoc16c6e32007-11-18 14:53:47 +0000154 unsigned *count, pj_addrinfo ai[]);
155
156
157
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000158/** @} */
159
160PJ_END_DECL
161
162#endif /* __PJ_ADDR_RESOLV_H__ */
163