1/*
2 * net.h
3 *
4 * DNS Resolver definitions
5 *
6 * a Net::DNS like library for C
7 *
8 * (c) NLnet Labs, 2005-2006
9 *
10 * See the file LICENSE for the license
11 */
12
13#ifndef LDNS_NET_H
14#define LDNS_NET_H
15
16#include <ldns/ldns.h>
17@include_sys_socket_h@
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#define LDNS_DEFAULT_TIMEOUT_SEC 5
24#define LDNS_DEFAULT_TIMEOUT_USEC 0
25
26/**
27 * \file
28 *
29 * Contains functions to send and receive packets over a network.
30 */
31
32/**
33 * Sends a buffer to an ip using udp and return the respons as a ldns_pkt
34 * \param[in] qbin the ldns_buffer to be send
35 * \param[in] to the ip addr to send to
36 * \param[in] tolen length of the ip addr
37 * \param[in] timeout the timeout value for the network
38 * \param[out] answersize size of the packet
39 * \param[out] result packet with the answer
40 * \return status
41 */
42ldns_status ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize);
43
44/**
45 * Send an udp query and don't wait for an answer but return
46 * the socket
47 * \param[in] qbin the ldns_buffer to be send
48 * \param[in] to the ip addr to send to
49 * \param[in] tolen length of the ip addr
50 * \param[in] timeout *unused*, was the timeout value for the network
51 * \return the socket used
52 */
53int ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
54
55/**
56 * Send an tcp query and don't wait for an answer but return
57 * the socket
58 * \param[in] qbin the ldns_buffer to be send
59 * \param[in] to the ip addr to send to
60 * \param[in] tolen length of the ip addr
61 * \param[in] timeout the timeout value for the connect attempt
62 * \return the socket used
63 */
64int ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
65
66/**
67 * Sends a buffer to an ip using tcp and return the respons as a ldns_pkt
68 * \param[in] qbin the ldns_buffer to be send
69 * \param[in] qbin the ldns_buffer to be send
70 * \param[in] to the ip addr to send to
71 * \param[in] tolen length of the ip addr
72 * \param[in] timeout the timeout value for the network
73 * \param[out] answersize size of the packet
74 * \param[out] result packet with the answer
75 * \return status
76 */
77ldns_status ldns_tcp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize);
78
79/**
80 * Sends ptk to the nameserver at the resolver object. Returns the data
81 * as a ldns_pkt
82 * 
83 * \param[out] pkt packet received from the nameserver
84 * \param[in] r the resolver to use 
85 * \param[in] query_pkt the query to send
86 * \return status
87 */
88ldns_status ldns_send(ldns_pkt **pkt, ldns_resolver *r, const ldns_pkt *query_pkt);
89
90/**
91 * Sends and ldns_buffer (presumably containing a packet to the nameserver at the resolver object. Returns the data
92 * as a ldns_pkt
93 * 
94 * \param[out] pkt packet received from the nameserver
95 * \param[in] r the resolver to use 
96 * \param[in] qb the buffer to send
97 * \param[in] tsig_mac the tsig MAC to authenticate the response with (NULL to do no TSIG authentication)
98 * \return status
99 */
100ldns_status ldns_send_buffer(ldns_pkt **pkt, ldns_resolver *r, ldns_buffer *qb, ldns_rdf *tsig_mac);
101
102/**
103 * Create a tcp socket to the specified address
104 * \param[in] to ip and family
105 * \param[in] tolen length of to
106 * \param[in] timeout timeout for the connect attempt
107 * \return a socket descriptor
108 */
109int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
110
111/**
112 * Create a udp socket to the specified address
113 * \param[in] to ip and family
114 * \param[in] timeout *unused*, was timeout for the socket
115 * \return a socket descriptor
116 */
117int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout);
118
119/**
120 * send a query via tcp to a server. Don't want for the answer
121 *
122 * \param[in] qbin the buffer to send
123 * \param[in] sockfd the socket to use
124 * \param[in] to which ip to send it
125 * \param[in] tolen socketlen
126 * \return number of bytes sent
127 */
128ssize_t ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen);
129
130/**
131 * send a query via udp to a server. Don;t want for the answer
132 *
133 * \param[in] qbin the buffer to send
134 * \param[in] sockfd the socket to use
135 * \param[in] to which ip to send it
136 * \param[in] tolen socketlen
137 * \return number of bytes sent
138 */
139ssize_t ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen);
140
141/**
142 * Gives back a raw packet from the wire and reads the header data from the given
143 * socket. Allocates the data (of size size) itself, so don't forget to free
144 *
145 * \param[in] sockfd the socket to read from
146 * \param[out] size the number of bytes that are read
147 * \param[in] timeout the time allowed between packets.
148 * \return the data read
149 */
150uint8_t *ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout);
151
152/**
153 * This routine may block. Use ldns_tcp_read_wire_timeout, it checks timeouts.
154 * Gives back a raw packet from the wire and reads the header data from the given
155 * socket. Allocates the data (of size size) itself, so don't forget to free
156 *
157 * \param[in] sockfd the socket to read from
158 * \param[out] size the number of bytes that are read
159 * \return the data read
160 */
161uint8_t *ldns_tcp_read_wire(int sockfd, size_t *size);
162
163/**
164 * Gives back a raw packet from the wire and reads the header data from the given
165 * socket. Allocates the data (of size size) itself, so don't forget to free
166 *
167 * \param[in] sockfd the socket to read from
168 * \param[in] fr the address of the client (if applicable)
169 * \param[in] *frlen the lenght of the client's addr (if applicable)
170 * \param[out] size the number of bytes that are read
171 * \return the data read
172 */
173uint8_t *ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *fr, socklen_t *frlen);
174
175/**
176 * returns the native sockaddr representation from the rdf.
177 * \param[in] rd the ldns_rdf to operate on
178 * \param[in] port what port to use. 0 means; use default (53)
179 * \param[out] size what is the size of the sockaddr_storage
180 * \return struct sockaddr* the address in the format so other
181 * functions can use it (sendto)
182 */
183struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size);
184
185/**
186 * returns an rdf with the sockaddr info. works for ip4 and ip6
187 * \param[in] sock the struct sockaddr_storage to convert
188 * \param[in] port what port was used. When NULL this is not set
189 * \return ldns_rdf* wth the address
190 */
191ldns_rdf * ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port);
192
193/**
194 * Prepares the resolver for an axfr query
195 * The query is sent and the answers can be read with ldns_axfr_next
196 * \param[in] resolver the resolver to use
197 * \param[in] domain the domain to exfr
198 * \param[in] c the class to use
199 * \return ldns_status the status of the transfer
200 */
201ldns_status ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class c);
202
203#ifdef __cplusplus
204}
205#endif
206
207#endif  /* LDNS_NET_H */
208