blob: 51ba779a7e8d55eb584f6d0ee28edea661abd963 (
plain)
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
|
/* Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*!
* \file sockaddr.h
*
* \author Marek Vavrusa <marek.vavrusa@nic.cz>
*
* \brief Socket address abstraction layer.
*
* \addtogroup common_lib
* @{
*/
#ifndef _KNOTD_SOCKADDR_H_
#define _KNOTD_SOCKADDR_H_
/* BSD IPv6 */
#ifndef __POSIX_VISIBLE
#define __POSIX_VISIBLE = 200112
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
/*! \brief Universal socket address. */
typedef struct sockaddr_t {
int family; /*!< Address family. */
struct sockaddr* ptr; /*!< Pointer to used sockaddr. */
socklen_t len; /*!< Length of used sockaddr. */
union {
struct sockaddr_in addr4; /*!< IPv4 sockaddr. */
#ifndef DISABLE_IPV6
struct sockaddr_in6 addr6; /*!< IPv6 sockaddr. */
#endif
};
} sockaddr_t;
/*! \brief Maximum address length in string format. */
#ifdef DISABLE_IPV6
#define SOCKADDR_STRLEN INET_ADDRSTRLEN
#else
#define SOCKADDR_STRLEN INET6_ADDRSTRLEN
#endif
/*!
* \brief Initialize address structure.
*
* Members ptr and len will be initialized to correct address family.
*
* \param addr Socket address structure.
* \param af Requested address family.
*
* \retval 0 on success.
* \retval -1 on unsupported address family (probably INET6).
*/
int sockaddr_init(sockaddr_t *addr, int af);
/*!
* \brief Update internal pointers according to length.
*
* \param addr Socket address structure.
*
* \retval 0 on success.
* \retval -1 on invalid size.
*/
int sockaddr_update(sockaddr_t *addr);
/*!
* \brief Set address and port.
*
* \param dst Target address structure.
* \param family Address family.
* \param addr IP address in string format.
* \param port Port.
*
* \retval 0 if addr is not valid address in string format.
* \retval positive value in case of success.
* \retval -1 on error.
* \see inet_pton(3)
*/
int sockaddr_set(sockaddr_t *dst, int family, const char* addr, int port);
/*!
* \brief Return string representation of socket address.
*
* \param addr Socket address structure.
* \param dst Destination for string representation.
* \param size Maximum number of written bytes.
*
* \retval 0 on success.
* \retval -1 on invalid parameters.
*/
int sockaddr_tostr(sockaddr_t *addr, char *dst, size_t size);
/*!
* \brief Return port number from address.
*
* \param addr Socket address structure.
*
* \retval Port number on success.
* \retval -1 on errors.
*/
int sockaddr_portnum(sockaddr_t *addr);
#endif /* _KNOTD_SOCKADDR_H_ */
/*! @} */
|