sockaddr.h 2.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*  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/>.
 */
16 17 18 19 20 21 22 23 24 25 26
/*!
 * \file sockaddr.h
 *
 * \author Marek Vavrusa <marek.vavrusa@nic.cz>
 *
 * \brief Socket address abstraction layer.
 *
 * \addtogroup common_lib
 * @{
 */

Lubos Slovak's avatar
Lubos Slovak committed
27 28
#ifndef _KNOTD_SOCKADDR_H_
#define _KNOTD_SOCKADDR_H_
29

30 31 32 33 34
/* BSD IPv6 */
#ifndef __POSIX_VISIBLE
#define __POSIX_VISIBLE = 200112
#endif

35 36 37
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
38
#include <netinet/in.h>
39
#include <arpa/inet.h>
40
#include <stdint.h>
41

42 43 44 45
/* Subnet maximum prefix length. */
#define IPV4_PREFIXLEN 32
#define IPV6_PREFIXLEN 128

46 47 48
/* Address string "address[@port]" maximum length. */
#define SOCKADDR_STRLEN_EXT (1 + 6) /* '@', 5 digits number, \0 */
#define SOCKADDR_STRLEN (sizeof(struct sockaddr_un) + SOCKADDR_STRLEN_EXT)
49 50

/*!
51
 * \brief Calculate current structure length based on address family.
52
 *
53
 * \param ss Socket address storage.
54
 *
55
 * \return number of bytes or error code
56
 */
57
int sockaddr_len(const struct sockaddr_storage *ss);
58

59 60
/*!
 * \brief Set address and port.
61
 *
62
 * \param ss Socket address storage.
63
 * \param family Address family.
64
 * \param straddr IP address in string format.
65
 * \param port Port.
66
 *
67
 * \return KNOT_EOK on success or an error code
68
 */
69
int sockaddr_set(struct sockaddr_storage *ss, int family, const char *straddr, int port);
70

71 72 73
/*!
 * \brief Return string representation of socket address.
 *
74 75 76 77 78
 * \note String format: <address>[@<port>], f.e. '127.0.0.1@53'
 *
 * \param ss Socket address storage.
 * \param buf Destination for string representation.
 * \param maxlen Maximum number of written bytes.
79
 *
80
 * \return EOK on success, error code on failure
81
 */
82
int sockaddr_tostr(const struct sockaddr_storage *ss, char *buf, size_t maxlen);
83 84 85 86

/*!
 * \brief Return port number from address.
 *
87
 * \param ss Socket address storage.
88
 *
89
 * \return port number or error code
90
 */
91
int sockaddr_port(const struct sockaddr_storage *ss);
92

93
/*!
94 95 96 97
 * \brief Set port number.
 *
 * \param ss Socket address storage.
 *
98
 */
99
void sockaddr_port_set(struct sockaddr_storage *ss, uint16_t port);
100

101 102
/*!
 * \brief Get host FQDN address.
103 104
 *
 * \return hostname string or NULL
105 106 107
 */
char *sockaddr_hostname(void);

Lubos Slovak's avatar
Lubos Slovak committed
108
#endif /* _KNOTD_SOCKADDR_H_ */
109 110

/*! @} */