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