summaryrefslogtreecommitdiff
path: root/src/utils/common/netio.h
blob: ac0e98d0a9f93b698c1848233c8c948bcc3d857a (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*  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 netio.h
 *
 * \author Daniel Salzman <daniel.salzman@nic.cz>
 *
 * \brief Networking abstraction for utilities.
 *
 * \addtogroup knot_utils
 * @{
 */

#ifndef _UTILS__NETIO_H_
#define _UTILS__NETIO_H_

#include <stdint.h>			// uint_t
#include <netdb.h>			// addrinfo

#include "common/lists.h"		// node
#include "utils/common/params.h"	// params_t

/*! \brief Structure containing server information. */
typedef struct {
	/*! List node (for list container). */
	node	n;
	/*! Name or address of the server. */
	char	*name;
	/*! Name or number of the service. */
	char	*service;
} server_t;

typedef struct {
	/*! Socket descriptor. */
	int	sockfd;

	/*! IP protocol type. */
	int	iptype;
	/*! Socket type. */
	int	socktype;
	/*! Timeout for all network operations. */
	int	wait;

	/*! Local interface parameters. */
	const server_t *local;
	/*! Remote server parameters. */
	const server_t *remote;

	/*! Local description string (used for logging). */
	char *local_str;
	/*! Remote description string (used for logging). */
	char *remote_str;

	/*! Output from getaddrinfo for remote server. If the server is
	 *  specified using domain name, this structure may contain more
	 *  results.
	 */
	struct addrinfo *remote_info;
	/*! Currently used result from remote_info. */
	struct addrinfo *srv;
	/*! Output from getaddrinfo for local address. Only first result is
	 *  used.
	 */
	struct addrinfo *local_info;
} net_t;

/*!
 * \brief Creates and fills server structure.
 *
 * \param name		Address or host name.
 * \param service	Port number or service name.
 *
 * \retval server	if success.
 * \retval NULL		if error.
 */
server_t* server_create(const char *name, const char *service);

/*!
 * \brief Destroys server structure.
 *
 * \param server	Server structure to destroy.
 */
void server_free(server_t *server);

/*!
 * \brief Translates enum IP version type to int version.
 *
 * \param ip		IP version to convert.
 *
 * \retval AF_INET, AF_INET6 or AF_UNSPEC.
 */
int get_iptype(const ip_t ip);

/*!
 * \brief Translates enum IP protocol type to int version in context to the
 *        current DNS query type.
 *
 * \param proto		IP protocol type to convert.
 * \param type		DNS query type number.
 *
 * \retval SOCK_STREAM or SOCK_DGRAM.
 */
int get_socktype(const protocol_t proto, const uint16_t type);

/*!
 * \brief Translates int socket type to the common string one.
 *
 * \param socktype	Socket type (SOCK_STREAM or SOCK_DGRAM).
 *
 * \retval "TCP" or "UDP".
 */
const char* get_sockname(const int socktype);

/*!
 * \brief Initializes network structure and resolves local and remote addresses.
 *
 * \param local		Local address and service description.
 * \param remote	Remote address and service description.
 * \param iptype	IP version.
 * \param socktype	Socket type.
 * \param wait		Network timeout interval.
 * \param net		Network structure to initialize.
 *
 * \retval KNOT_EOK	if success.
 * \retval errcode	if error.
 */
int net_init(const server_t *local,
             const server_t *remote,
             const int      iptype,
             const int      socktype,
             const int      wait,
             net_t          *net);

/*!
 * \brief Creates socket and connects (if TCP) to remote address specified
 *        by net->srv.
 *
 * \param net		Connection parameters.
 *
 * \retval KNOT_EOK	if success.
 * \retval errcode	if error.
 */
int net_connect(net_t *net);

/*!
 * \brief Sends data to connected remote server.
 *
 * \param net		Connection parameters.
 * \param buf		Data to send.
 * \param buf_len	Length of the data to send.
 *
 * \retval KNOT_EOK	if success.
 * \retval errcode	if error.
 */
int net_send(const net_t *net, const uint8_t *buf, const size_t buf_len);

/*!
 * \brief Receives data from connected remote server.
 *
 * \param net		Connection parameters.
 * \param buf		Buffer for incomming data.
 * \param buf_len	Length of the buffer.
 *
 * \retval >=0		length of successfully received data.
 * \retval errcode	if error.
 */
int net_receive(const net_t *net, uint8_t *buf, const size_t buf_len);

/*!
 * \brief Closes current network connection.
 *
 * \param net		Connection parameters.
 */
void net_close(net_t *net);

/*!
 * \brief Cleans up network structure.
 *
 * \param net		Connection parameters.
 */
void net_clean(net_t *net);

#endif // _UTILS__NETIO_H_

/*! @} */