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