net.h revision 266114
1193323Sed/* 2193323Sed * net.h 3193323Sed * 4193323Sed * DNS Resolver definitions 5193323Sed * 6193323Sed * a Net::DNS like library for C 7193323Sed * 8193323Sed * (c) NLnet Labs, 2005-2006 9193323Sed * 10193323Sed * See the file LICENSE for the license 11193323Sed */ 12193323Sed 13193323Sed#ifndef LDNS_NET_H 14193323Sed#define LDNS_NET_H 15249423Sdim 16249423Sdim#include <ldns/ldns.h> 17193323Sed#include <sys/socket.h> 18288943Sdim 19251662Sdim#ifdef __cplusplus 20193323Sedextern "C" { 21203954Srdivacky#endif 22280031Sdim 23276479Sdim#define LDNS_DEFAULT_TIMEOUT_SEC 5 24276479Sdim#define LDNS_DEFAULT_TIMEOUT_USEC 0 25249423Sdim 26280031Sdim/** 27234353Sdim * \file 28249423Sdim * 29193323Sed * Contains functions to send and receive packets over a network. 30234353Sdim */ 31234353Sdim 32234353Sdim/** 33249423Sdim * Sends a buffer to an ip using udp and return the respons as a ldns_pkt 34288943Sdim * \param[in] qbin the ldns_buffer to be send 35193323Sed * \param[in] to the ip addr to send to 36193323Sed * \param[in] tolen length of the ip addr 37193323Sed * \param[in] timeout the timeout value for the network 38193323Sed * \param[out] answersize size of the packet 39193323Sed * \param[out] result packet with the answer 40261991Sdim * \return status 41198090Srdivacky */ 42193323Sedldns_status ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize); 43193323Sed 44193323Sed/** 45195098Sed * Send an udp query and don't wait for an answer but return 46195098Sed * the socket 47288943Sdim * \param[in] qbin the ldns_buffer to be send 48195098Sed * \param[in] to the ip addr to send to 49251662Sdim * \param[in] tolen length of the ip addr 50261991Sdim * \param[in] timeout *unused*, was the timeout value for the network 51234353Sdim * \return the socket used 52193323Sed */ 53193323Sedint ldns_udp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout); 54276479Sdim 55276479Sdim/** 56276479Sdim * Send an tcp query and don't wait for an answer but return 57276479Sdim * the socket 58276479Sdim * \param[in] qbin the ldns_buffer to be send 59218893Sdim * \param[in] to the ip addr to send to 60276479Sdim * \param[in] tolen length of the ip addr 61193323Sed * \param[in] timeout the timeout value for the connect attempt 62198090Srdivacky * \return the socket used 63288943Sdim */ 64198090Srdivackyint ldns_tcp_bgsend(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout); 65193323Sed 66198090Srdivacky/** 67288943Sdim * Sends a buffer to an ip using tcp and return the respons as a ldns_pkt 68288943Sdim * \param[in] qbin the ldns_buffer to be send 69198892Srdivacky * \param[in] qbin the ldns_buffer to be send 70193323Sed * \param[in] to the ip addr to send to 71193323Sed * \param[in] tolen length of the ip addr 72193323Sed * \param[in] timeout the timeout value for the network 73193323Sed * \param[out] answersize size of the packet 74193323Sed * \param[out] result packet with the answer 75288943Sdim * \return status 76193323Sed */ 77193323Sedldns_status ldns_tcp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout, size_t *answersize); 78198090Srdivacky 79276479Sdim/** 80193323Sed * Sends ptk to the nameserver at the resolver object. Returns the data 81193323Sed * as a ldns_pkt 82193323Sed * 83288943Sdim * \param[out] pkt packet received from the nameserver 84193323Sed * \param[in] r the resolver to use 85193323Sed * \param[in] query_pkt the query to send 86198090Srdivacky * \return status 87218893Sdim */ 88218893Sdimldns_status ldns_send(ldns_pkt **pkt, ldns_resolver *r, const ldns_pkt *query_pkt); 89243830Sdim 90288943Sdim/** 91193323Sed * Sends and ldns_buffer (presumably containing a packet to the nameserver at the resolver object. Returns the data 92193323Sed * as a ldns_pkt 93288943Sdim * 94288943Sdim * \param[out] pkt packet received from the nameserver 95218893Sdim * \param[in] r the resolver to use 96218893Sdim * \param[in] qb the buffer to send 97218893Sdim * \param[in] tsig_mac the tsig MAC to authenticate the response with (NULL to do no TSIG authentication) 98218893Sdim * \return status 99218893Sdim */ 100218893Sdimldns_status ldns_send_buffer(ldns_pkt **pkt, ldns_resolver *r, ldns_buffer *qb, ldns_rdf *tsig_mac); 101218893Sdim 102218893Sdim/** 103218893Sdim * Create a tcp socket to the specified address 104218893Sdim * \param[in] to ip and family 105193323Sed * \param[in] tolen length of to 106218893Sdim * \param[in] timeout timeout for the connect attempt 107296417Sdim * \return a socket descriptor 108296417Sdim */ 109296417Sdimint ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout); 110296417Sdim 111296417Sdim/** 112296417Sdim * Create a udp socket to the specified address 113218893Sdim * \param[in] to ip and family 114218893Sdim * \param[in] timeout *unused*, was timeout for the socket 115198892Srdivacky * \return a socket descriptor 116218893Sdim */ 117218893Sdimint ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout); 118193323Sed 119218893Sdim/** 120218893Sdim * send a query via tcp to a server. Don't want for the answer 121218893Sdim * 122193323Sed * \param[in] qbin the buffer to send 123193323Sed * \param[in] sockfd the socket to use 124276479Sdim * \param[in] to which ip to send it 125276479Sdim * \param[in] tolen socketlen 126276479Sdim * \return number of bytes sent 127198090Srdivacky */ 128198090Srdivackyssize_t ldns_tcp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen); 129193323Sed 130218893Sdim/** 131218893Sdim * send a query via udp to a server. Don;t want for the answer 132280031Sdim * 133221345Sdim * \param[in] qbin the buffer to send 134193323Sed * \param[in] sockfd the socket to use 135218893Sdim * \param[in] to which ip to send it 136193323Sed * \param[in] tolen socketlen 137218893Sdim * \return number of bytes sent 138288943Sdim */ 139288943Sdimssize_t ldns_udp_send_query(ldns_buffer *qbin, int sockfd, const struct sockaddr_storage *to, socklen_t tolen); 140288943Sdim 141288943Sdim/** 142288943Sdim * Gives back a raw packet from the wire and reads the header data from the given 143288943Sdim * socket. Allocates the data (of size size) itself, so don't forget to free 144288943Sdim * 145288943Sdim * \param[in] sockfd the socket to read from 146288943Sdim * \param[out] size the number of bytes that are read 147288943Sdim * \param[in] timeout the time allowed between packets. 148288943Sdim * \return the data read 149288943Sdim */ 150280031Sdimuint8_t *ldns_tcp_read_wire_timeout(int sockfd, size_t *size, struct timeval timeout); 151280031Sdim 152193323Sed/** 153193323Sed * This routine may block. Use ldns_tcp_read_wire_timeout, it checks timeouts. 154218893Sdim * Gives back a raw packet from the wire and reads the header data from the given 155218893Sdim * socket. Allocates the data (of size size) itself, so don't forget to free 156288943Sdim * 157221345Sdim * \param[in] sockfd the socket to read from 158288943Sdim * \param[out] size the number of bytes that are read 159288943Sdim * \return the data read 160288943Sdim */ 161193323Seduint8_t *ldns_tcp_read_wire(int sockfd, size_t *size); 162280031Sdim 163280031Sdim/** 164218893Sdim * Gives back a raw packet from the wire and reads the header data from the given 165193323Sed * socket. Allocates the data (of size size) itself, so don't forget to free 166193323Sed * 167193323Sed * \param[in] sockfd the socket to read from 168193323Sed * \param[in] fr the address of the client (if applicable) 169193323Sed * \param[in] *frlen the lenght of the client's addr (if applicable) 170193323Sed * \param[out] size the number of bytes that are read 171193323Sed * \return the data read 172280031Sdim */ 173280031Sdimuint8_t *ldns_udp_read_wire(int sockfd, size_t *size, struct sockaddr_storage *fr, socklen_t *frlen); 174280031Sdim 175193323Sed/** 176221345Sdim * returns the native sockaddr representation from the rdf. 177276479Sdim * \param[in] rd the ldns_rdf to operate on 178276479Sdim * \param[in] port what port to use. 0 means; use default (53) 179276479Sdim * \param[out] size what is the size of the sockaddr_storage 180276479Sdim * \return struct sockaddr* the address in the format so other 181276479Sdim * functions can use it (sendto) 182276479Sdim */ 183276479Sdimstruct sockaddr_storage * ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size); 184276479Sdim 185276479Sdim/** 186276479Sdim * returns an rdf with the sockaddr info. works for ip4 and ip6 187276479Sdim * \param[in] sock the struct sockaddr_storage to convert 188280031Sdim * \param[in] port what port was used. When NULL this is not set 189276479Sdim * \return ldns_rdf* wth the address 190276479Sdim */ 191276479Sdimldns_rdf * ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port); 192276479Sdim 193276479Sdim/** 194276479Sdim * Prepares the resolver for an axfr query 195276479Sdim * The query is sent and the answers can be read with ldns_axfr_next 196280031Sdim * \param[in] resolver the resolver to use 197276479Sdim * \param[in] domain the domain to exfr 198218893Sdim * \param[in] c the class to use 199193323Sed * \return ldns_status the status of the transfer 200296417Sdim */ 201193323Sedldns_status ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class c); 202203954Srdivacky 203203954Srdivacky#ifdef __cplusplus 204203954Srdivacky} 205193323Sed#endif 206288943Sdim 207193323Sed#endif /* LDNS_NET_H */ 208193323Sed