SHARE
TWEET

Untitled

a guest Apr 24th, 2019 291 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <netdb.h>
  2. #define BUFLEN 1500
  3. #define STANDARD_PACKET_SIZE 1500
  4.  
  5. /*** client-side resolution library ***/
  6.  
  7. char *DNS_IP;
  8. int DNS_PORT;
  9.  
  10. /**
  11.  * Initialize your client DNS library with the IP address and port number of
  12.  * your DNS server.
  13.  *
  14.  * @param  dns_ip  The IP address of the DNS server.
  15.  * @param  dns_port  The port number of the DNS server.
  16.  *
  17.  * @return 0 on success, -1 otherwise
  18.  */
  19. int init_mydns(const char *dns_ip, unsigned int dns_port) {
  20.  
  21.     // set the IP and port, so, resolve can run with those
  22.     // without the caller explicity mentioning that
  23.     DNS_PORT = dns_port;
  24.     DNS_IP   = dns_ip;
  25. }
  26.  
  27.  
  28. /**
  29.  * Resolve a DNS name using your custom DNS server.
  30.  *
  31.  * Whenever your proxy needs to open a connection to a web server, it calls
  32.  * resolve() as follows:
  33.  *
  34.  * struct addrinfo *result;
  35.  * int rc = resolve("video.cs.cmu.edu", "8080", null, &result);
  36.  * if (rc != 0) {
  37.  *     // handle error
  38.  * }
  39.  * // connect to address in result
  40.  * free(result);
  41.  *
  42.  *
  43.  * @param  node  The hostname to resolve.
  44.  * @param  service  The desired port number as a string.
  45.  * @param  hints  Should be null. resolve() ignores this parameter.
  46.  * @param  res  The result. resolve() should allocate a struct addrinfo, which
  47.  * the caller is responsible for freeing.
  48.  *
  49.  * @return 0 on success, -1 otherwise
  50.  */
  51.  
  52. int resolve(const char *node, const char *service,
  53.             const struct addrinfo *hints, struct addrinfo **res) {
  54.  
  55.     // What's the point of service?
  56.  
  57.     /** init **/
  58.  
  59.     /* buffer and packets vars */
  60.     struct sockaddr_in from;
  61.     socklen_t fromlen;
  62.     char data_received[BUFLEN];
  63.     uint8_t QNAME[BUFLEN];
  64.     char str_buf[BUFLEN];
  65.     fromlen = sizeof(from);
  66.     int flags = 0;
  67.  
  68.     /* constructing values for the query */
  69.     struct sockaddr_in DNS_addr;
  70.     bzero(&DNS_addr, sizeof(DNS_addr));
  71.     fromlen = sizeof(DNS_addr);
  72.     DNS_addr.sin_family = AF_INET;
  73.     DNS_addr.sin_addr.s_addr = htonl(DNS_IP);
  74.     DNS_addr.sin_port = htons(DNS_PORT);
  75.     strcpy(QNAME, node);
  76.  
  77.     /* create a socket */
  78.     int sock;
  79.     if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)) == -1) {
  80.         perror("could not create socket");
  81.         return -1;
  82.     }
  83.  
  84.     /* clients queue serving status */
  85.     int recieved_DNS = false;
  86.  
  87.     /* timeouts */
  88.         struct timeval timeout={3, 0}; //set timeout for 3 seconds
  89.         int recvlen;
  90.         dns_msg_s *recieved_DNS_msg;
  91.     /****/
  92.  
  93.     // Send UDP packet to DNS unitl you get a response, with 3 sec timeout
  94.     while (recieved_DNS) {
  95.  
  96.         /* listen for packets with 3 seconds timeout */
  97.         setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,(char*)&timeout,sizeof(struct timeval));
  98.        
  99.         static dns_msg_s dns_msg;
  100.         // Generate a DNS query and send it to the DNS server
  101.         generate_dns_query(&dns_msg, QNAME);
  102.         sendto(sock, (char *)dns_msg, STANDARD_PACKET_SIZE, flags, (struct sockaddr *) &DNS_addr, (fromlen) );
  103.         recvfrom(sock, data_received, STANDARD_PACKET_SIZE, flags, (struct sockaddr *) &from, &(fromlen) );
  104.  
  105.         recieved_DNS_msg = (dns_msg_s *)data_received;
  106.  
  107.         // Make sure no error bit, get the data and return it
  108.         get_question_details( (dns_msg_s *)(&data_received), str_buf);
  109.  
  110.         // Check if RCODE == 0 (there's no error), there's an answer, and it's addresed to the same QNAME requested
  111.         if ( (((dns_msg *)(&data_received)->layer2) & 0xF) == 0 &&
  112.              ((dns_msg *)(&data_received)->ANCOUNT) > 0 && strcmp(QNAME, str_buf) == 0 ) {
  113.             get_dns_answer_RDATA(&recieved_DNS_msg, str_buf);
  114.         }
  115.         else {
  116.             return -1;
  117.         }
  118.  
  119.     }
  120.  
  121.     return 1;
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top