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