Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Resplendent RPCs
- * CS 241 - Spring 2019
- */
- #include <arpa/inet.h>
- #include <errno.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/time.h>
- #include <unistd.h>
- #include <netdb.h>
- #include "common.h"
- #include "common.h"
- #include "dns_query.h"
- #include "dns_query_svc_impl.h"
- #define CACHE_FILE "cache_files/rpc_server_cache"
- char *contact_nameserver(query *argp, char *host, int port) {
- // Your code here
- // Look in the header file for a few more comments
- struct sockaddr_in a;
- memset(&a, 0, sizeof(struct sockaddr_in));
- a.sin_family = AF_INET;
- a.sin_port = htons((uint16_t)port);
- inet_pton(AF_INET, host, &(a.sin_addr));
- int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- ssize_t r;
- do {
- r = sendto(sockfd, argp->host, strlen(argp->host), 0, (struct sockaddr*)&(a), sizeof(a));
- } while (r == -1 && errno == EINTR);
- char *buffer = calloc(16, 1);
- struct sockaddr_storage addr;
- socklen_t size = sizeof(addr);
- do {
- r = recvfrom(sockfd, buffer, 15, 0, (struct sockaddr*)&(addr), &size);
- } while (r == -1 && errno == EAGAIN);
- if (strcmp(buffer, "-1.-1.-1.-1") == 0){ return NULL;}
- return buffer;
- }
- void create_response(query *argp, char *ipv4_address, response *res) {
- // Your code here
- // As before there are comments in the header file
- if (!ipv4_address){
- //set result fields
- res->success = 0;
- struct host_address *a = calloc(sizeof(struct host_address), 1);
- //set host_address fields
- a->host = calloc(strlen(argp->host)+1, 1);
- strcpy(a->host, argp->host);
- a->host_ipv4_address = calloc(sizeof(char*), 1);
- //set res address
- res->address = a;
- return;
- }
- res->success = 1;
- struct host_address *a = malloc(sizeof(struct host_address));
- //set host_address fields
- a->host = calloc(strlen(argp->host)+1, 1);
- strcpy(a->host, argp->host);
- a->host_ipv4_address = calloc(strlen(ipv4_address)+1, 1);
- strcpy(a->host_ipv4_address, ipv4_address);
- //set res address
- res->address = a;
- return;
- }
- // Stub code
- response *dns_query_1_svc(query *argp, struct svc_req *rqstp) {
- printf("Resolving query...\n");
- // check its cache, 'rpc_server_cache'
- // if it's in cache, return with response object with the ip address
- char *ipv4_address = check_cache_for_address(argp->host, CACHE_FILE);
- if (ipv4_address == NULL) {
- // not in the cache. contact authoritative servers like a recursive dns
- // server
- printf("Domain not found in server's cache. Contacting authoritative "
- "servers...\n");
- char *host = getenv("NAMESERVER_HOST");
- int port = strtol(getenv("NAMESERVER_PORT"), NULL, 10);
- ipv4_address = contact_nameserver(argp, host, port);
- } else {
- // it is in the server's cache; no need to ask the authoritative
- // servers.
- printf("Domain found in server's cache!\n");
- }
- static response res;
- xdr_free(xdr_response, &res); // Frees old memory in the response struct
- create_response(argp, ipv4_address, &res);
- free(ipv4_address);
- return &res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement