Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. /**
  2. * Resplendent RPCs
  3. * CS 241 - Spring 2019
  4. */
  5. #include <arpa/inet.h>
  6. #include <errno.h>
  7. #include <netinet/in.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <sys/socket.h>
  12. #include <sys/time.h>
  13. #include <unistd.h>
  14. #include <netdb.h>
  15.  
  16. #include "common.h"
  17. #include "common.h"
  18. #include "dns_query.h"
  19. #include "dns_query_svc_impl.h"
  20.  
  21. #define CACHE_FILE "cache_files/rpc_server_cache"
  22.  
  23. char *contact_nameserver(query *argp, char *host, int port) {
  24. // Your code here
  25. // Look in the header file for a few more comments
  26.  
  27. struct sockaddr_in a;
  28. memset(&a, 0, sizeof(struct sockaddr_in));
  29. a.sin_family = AF_INET;
  30. a.sin_port = htons((uint16_t)port);
  31.  
  32. inet_pton(AF_INET, host, &(a.sin_addr));
  33.  
  34. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  35. ssize_t r;
  36. do {
  37. r = sendto(sockfd, argp->host, strlen(argp->host), 0, (struct sockaddr*)&(a), sizeof(a));
  38. } while (r == -1 && errno == EINTR);
  39.  
  40. char *buffer = calloc(16, 1);
  41. struct sockaddr_storage addr;
  42. socklen_t size = sizeof(addr);
  43. do {
  44. r = recvfrom(sockfd, buffer, 15, 0, (struct sockaddr*)&(addr), &size);
  45. } while (r == -1 && errno == EAGAIN);
  46.  
  47. if (strcmp(buffer, "-1.-1.-1.-1") == 0){ return NULL;}
  48.  
  49. return buffer;
  50. }
  51.  
  52. void create_response(query *argp, char *ipv4_address, response *res) {
  53. // Your code here
  54. // As before there are comments in the header file
  55. if (!ipv4_address){
  56. //set result fields
  57. res->success = 0;
  58. struct host_address *a = calloc(sizeof(struct host_address), 1);
  59. //set host_address fields
  60. a->host = calloc(strlen(argp->host)+1, 1);
  61. strcpy(a->host, argp->host);
  62. a->host_ipv4_address = calloc(sizeof(char*), 1);
  63. //set res address
  64. res->address = a;
  65. return;
  66. }
  67.  
  68. res->success = 1;
  69. struct host_address *a = malloc(sizeof(struct host_address));
  70. //set host_address fields
  71. a->host = calloc(strlen(argp->host)+1, 1);
  72. strcpy(a->host, argp->host);
  73.  
  74. a->host_ipv4_address = calloc(strlen(ipv4_address)+1, 1);
  75. strcpy(a->host_ipv4_address, ipv4_address);
  76.  
  77. //set res address
  78. res->address = a;
  79. return;
  80.  
  81. }
  82.  
  83. // Stub code
  84.  
  85. response *dns_query_1_svc(query *argp, struct svc_req *rqstp) {
  86. printf("Resolving query...\n");
  87. // check its cache, 'rpc_server_cache'
  88. // if it's in cache, return with response object with the ip address
  89. char *ipv4_address = check_cache_for_address(argp->host, CACHE_FILE);
  90. if (ipv4_address == NULL) {
  91. // not in the cache. contact authoritative servers like a recursive dns
  92. // server
  93. printf("Domain not found in server's cache. Contacting authoritative "
  94. "servers...\n");
  95. char *host = getenv("NAMESERVER_HOST");
  96. int port = strtol(getenv("NAMESERVER_PORT"), NULL, 10);
  97. ipv4_address = contact_nameserver(argp, host, port);
  98. } else {
  99. // it is in the server's cache; no need to ask the authoritative
  100. // servers.
  101. printf("Domain found in server's cache!\n");
  102. }
  103.  
  104. static response res;
  105. xdr_free(xdr_response, &res); // Frees old memory in the response struct
  106. create_response(argp, ipv4_address, &res);
  107.  
  108. free(ipv4_address);
  109.  
  110. return &res;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement