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