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