aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-10-27 16:50:20 -0600
committerJack Sangdahl <[email protected]>2024-10-27 16:50:20 -0600
commit571f79fee9bbe89f544f5453d36243e59c7dcb8e (patch)
tree566f35075593b6d854d199fc5f4702588a9765ba
parent6e6f4151b7f709fbaf62e40479c700a81436512f (diff)
downloadcsci3753-571f79fee9bbe89f544f5453d36243e59c7dcb8e.tar.gz
csci3753-571f79fee9bbe89f544f5453d36243e59c7dcb8e.zip
pa6: improvements
-rw-r--r--pa6_threaded_resolver/Makefile43
-rw-r--r--pa6_threaded_resolver/array.c65
-rw-r--r--pa6_threaded_resolver/array.h75
-rw-r--r--pa6_threaded_resolver/multi-lookup.c256
-rw-r--r--pa6_threaded_resolver/multi-lookup.h55
-rw-r--r--pa6_threaded_resolver/util.c108
-rw-r--r--pa6_threaded_resolver/util.h14
7 files changed, 263 insertions, 353 deletions
diff --git a/pa6_threaded_resolver/Makefile b/pa6_threaded_resolver/Makefile
index 80efc14..8c951e9 100644
--- a/pa6_threaded_resolver/Makefile
+++ b/pa6_threaded_resolver/Makefile
@@ -1,37 +1,14 @@
-# Makefile v1 for CSCI3753-F23 PA6
+OBJS = array.o util.o multi-lookup.o
+CFLAGS = -Wall -Wextra -g
-# Add any additional source files you'd like to submit by appending
-# .c filenames to the MSRCS line and .h filenames to the MHDRS line
-MSRCS = multi-lookup.c array.c
-MHDRS = multi-lookup.h array.h
+all: $(OBJS)
+ cc $(CFLAGS) -o multi-lookup $(OBJS)
-# Do not modify anything after this line
-CC = gcc
-CFLAGS = -Wextra -Wall -g -std=gnu99
-INCLUDES =
-LFLAGS =
-LIBS = -lpthread
+clean:
+ rm *.o *.log multi-lookup
-MAIN = multi-lookup
+test:
+ ./multi-lookup 10 10 requester.log resolver.log ../common/hostname_data/*.txt
-SRCS = $(MSRCS) util.c
-HDRS = $(MHDRS) util.h
-
-OBJS = $(SRCS:.c=.o)
-
-$(MAIN): $(OBJS)
- $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)
-
-%.o: %.c $(HDRS)
- $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
-
-.PHONY: clean
-clean:
- $(RM) *.o *~ $(MAIN)
-
-SUBMITFILES = $(MSRCS) $(MHDRS) Makefile README
-submit:
- @read -r -p "Enter your identikey username: " username; \
- echo; echo Bundling the following files for submission; \
- tar --transform "s|^|PA6-$$username/|" -cvf PA6-$$username.txt $(SUBMITFILES); \
- echo; echo Please upload the file PA6-$$username.txt to Canvas to complete your submission; echo
+array.o: array.h
+util.o: util.h
diff --git a/pa6_threaded_resolver/array.c b/pa6_threaded_resolver/array.c
index e43dbdf..2976bda 100644
--- a/pa6_threaded_resolver/array.c
+++ b/pa6_threaded_resolver/array.c
@@ -1,48 +1,39 @@
// Jack Sangdahl
// CSCI3753 SP24
-// PA6 Threaded Resolver
+// PA4 Shared Array
#include "array.h"
-int queue_init(queue *q, int queue_size) {
- q->data = malloc(queue_size * sizeof(node));
- q->maxsize = queue_size;
- q->front = 0;
- q->back = -1;
- q->size = 0;
- return EXIT_SUCCESS;
+int array_init(array *q) {
+ q->head = 0;
+ q->tail = 0;
+ pthread_mutex_init(&q->lock, NULL);
+ sem_init(&q->avail, 0, ARRAY_SIZE);
+ sem_init(&q->empty, 0, 0);
+ return 0;
}
-int queue_size(queue *q) { return q->size; }
-
-bool queue_full(queue *q) { return (q->size == q->maxsize); }
-
-int queue_push(queue *q, void *element) {
- if (queue_full(q)) {
- fprintf(stderr, "push error: queue full\n");
- return EXIT_FAILURE;
- }
- q->back = (q->back + 1) % q->maxsize;
- q->data[q->back].value = element;
- q->size++;
- return EXIT_SUCCESS;
+int array_put(array *q, char *hostname) {
+ sem_wait(&q->avail);
+ pthread_mutex_lock(&q->lock);
+ q->data[q->tail++] = hostname;
+ q->tail %= ARRAY_SIZE;
+ pthread_mutex_unlock(&q->lock);
+ sem_post(&q->empty);
+ return 0;
}
-void *queue_pop(queue *q) {
- if (!queue_size(q)) {
- fprintf(stderr, "pop error: queue empty\n");
- return NULL;
- }
- void *value = q->data[q->front].value;
- q->data[q->front].value = NULL;
- q->front = (q->front + 1) % q->maxsize;
- q->size--;
- return value;
+int array_get(array *q, char **hostname) {
+ sem_wait(&q->empty);
+ pthread_mutex_lock(&q->lock);
+ *hostname = q->data[q->head++];
+ q->head %= ARRAY_SIZE;
+ pthread_mutex_unlock(&q->lock);
+ sem_post(&q->avail);
+ return 0;
}
-void queue_free(queue *q) {
- while (q->size > 0) {
- void *val = queue_pop(q);
- free(val);
- }
- free(q->data);
+void array_free(array *q) {
+ pthread_mutex_destroy(&q->lock);
+ sem_destroy(&q->avail);
+ sem_destroy(&q->empty);
}
diff --git a/pa6_threaded_resolver/array.h b/pa6_threaded_resolver/array.h
index adceca8..ce5ef79 100644
--- a/pa6_threaded_resolver/array.h
+++ b/pa6_threaded_resolver/array.h
@@ -1,62 +1,23 @@
// Jack Sangdahl
// CSCI3753 SP24
-// PA6 Threaded Resolver
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
+// PA4 Shared Array
+#include <pthread.h>
+#include <semaphore.h>
-typedef struct {
- void *value;
-} node;
-
-typedef struct {
- node *data;
- int size;
- int maxsize;
- int front;
- int back;
-} queue;
-
-/**
- * @brief initialise the queue
- * @param q
- * @param size
- * @return int
- */
-int queue_init(queue *q, int size);
+#pragma once
-/**
- * @brief return size of queue
- * @param q
- * @return int
- */
-int queue_size(queue *q);
+#define ARRAY_SIZE 8
-/**
- * @brief return true if que size is
- * greater than ARRAY_SIZE. false otherwise
- * @param q
- * @return bool
- */
-bool queue_full(queue *q);
-
-/**
- * @brief add element to fifo queue. block when full
- * @param q
- * @param val
- * @return int
- */
-int queue_push(queue *q, void *val);
-
-/**
- * @brief return top element of queue in fifo order
- * return null pointer if queue empty
- * @param q
- */
-void *queue_pop(queue *q);
-
-/**
- * @brief free queue's resources
- * @param q
- */
-void queue_free(queue *q);
+typedef struct {
+ void *data[ARRAY_SIZE];
+ int head;
+ int tail;
+ pthread_mutex_t lock;
+ sem_t avail;
+ sem_t empty;
+} array;
+
+int array_init(array *q);
+int array_put(array *q, char *hostname);
+int array_get(array *q, char **hostname);
+void array_free(array *q);
diff --git a/pa6_threaded_resolver/multi-lookup.c b/pa6_threaded_resolver/multi-lookup.c
index 2956b8f..a7e02dd 100644
--- a/pa6_threaded_resolver/multi-lookup.c
+++ b/pa6_threaded_resolver/multi-lookup.c
@@ -2,171 +2,167 @@
// CSCI3753 SP24
// PA6 Threaded Resolver
#include "multi-lookup.h"
+#include "array.h"
+#include "util.h"
#include <sys/time.h>
int main(int argc, char **argv) {
if (argc < 6) {
fprintf(stderr,
"usage: %s <# requester> <# resolver> <requester log> <resolver "
- "log> [ <data file> ... ]\n",
+ "log> <data file(s)>\n",
argv[0]);
- return EXIT_FAILURE;
+ exit(EXIT_FAILURE);
} else if (argc > MAX_INPUT_FILES + 6) {
fprintf(stderr, "%s error: too many input files\n", argv[0]);
return EXIT_FAILURE;
}
- struct timeval proc_begin, proc_end;
- gettimeofday(&proc_begin, NULL);
- uint8_t req_thread = atoi(argv[1]);
- if (req_thread < 1) {
+ int req_thread_cnt = atoi(argv[1]);
+ if (req_thread_cnt < 1) {
fprintf(stderr, "%s error: minimum 1 requester thread\n", argv[0]);
return EXIT_FAILURE;
- } else if (req_thread > MAX_REQUESTER_THREADS) {
+ } else if (req_thread_cnt > MAX_REQUESTER_THREADS) {
fprintf(stderr, "%s error: requester threads cannot exceed %d\n", argv[0],
MAX_REQUESTER_THREADS);
return EXIT_FAILURE;
}
- uint8_t res_threads = atoi(argv[2]);
- if (res_threads < 1) {
+ int res_thread_cnt = atoi(argv[2]);
+ if (res_thread_cnt < 1) {
fprintf(stderr, "%s error: minimum 1 resolver thread\n", argv[0]);
return EXIT_FAILURE;
- } else if (res_threads > MAX_RESOLVER_THREADS) {
+ } else if (res_thread_cnt > MAX_RESOLVER_THREADS) {
fprintf(stderr, "%s error: resolver threads cannot exceed %d\n", argv[0],
MAX_RESOLVER_THREADS);
return EXIT_FAILURE;
}
- queue input_files;
- queue_init(&input_files, MAX_INPUT_FILES);
+ double start_time = get_time_sec();
+ array stack;
+ array_init(&stack);
+ struct requester_data req_data;
+ struct resolver_data res_data;
+ req_data.stack = &stack;
+ res_data.stack = &stack;
+ req_data.req_log = argv[3];
+ res_data.res_log = argv[4];
+ pthread_mutex_init(&res_data.res_log_lock, NULL);
+ pthread_mutex_init(&req_data.req_log_lock, NULL);
+ pthread_mutex_init(&res_data.term_lock, NULL);
+ req_data.res_thread_cnt = res_thread_cnt;
+ res_data.res_thread_cnt = res_thread_cnt;
+ int total_files = argc - 5;
+ char **filenames = (char **)malloc(total_files * sizeof(char *));
for (int i = 5; i < argc; i++) {
- if (access(argv[i], F_OK) == -1)
- fprintf(stderr, "%s error: invalid data file '%s'\n", argv[0], argv[i]);
- else
- queue_push(&input_files, strndup(argv[i], strlen(argv[i])));
- }
- if (!queue_size(&input_files)) {
- fprintf(stderr, "%s error: no valid data file given\n", argv[0]);
- return EXIT_FAILURE;
+ filenames[i - 5] = (char *)malloc((strlen(argv[i]) + 1) * sizeof(char));
+ strcpy(filenames[i - 5], argv[i]);
}
- pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t c_req = PTHREAD_COND_INITIALIZER;
- pthread_cond_t c_res = PTHREAD_COND_INITIALIZER;
- pthread_mutex_t input_lock = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t req_log_lock = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t res_log_lock = PTHREAD_MUTEX_INITIALIZER;
- bool req_done = 0;
- queue buffer;
- queue_init(&buffer, ARRAY_SIZE);
- request_data req_data;
- req_data.req_log = fopen(argv[3], "w");
- req_data.file_names = &input_files;
- req_data.buffer = &buffer;
- req_data.m = &m;
- req_data.c_req = &c_req;
- req_data.c_res = &c_res;
- req_data.input_lock = &input_lock;
- req_data.req_log_lock = &req_log_lock;
- resolve_data res_data;
- res_data.res_log = fopen(argv[4], "w");
- res_data.buffer = &buffer;
- res_data.m = &m;
- res_data.c_req = &c_req;
- res_data.c_res = &c_res;
- res_data.res_log_lock = &res_log_lock;
- res_data.req_done = &req_done;
- pthread_t request_tid[req_thread];
- pthread_t resolve_tid[res_threads];
- for (int i = 0; i < req_thread; i++)
- pthread_create(&request_tid[i], NULL, request, &req_data);
- for (int i = 0; i < res_threads; i++)
- pthread_create(&resolve_tid[i], NULL, resolve, &res_data);
- for (int i = 0; i < req_thread; i++)
- pthread_join(request_tid[i], NULL);
- pthread_mutex_lock(&m);
- req_done = 1;
- pthread_mutex_unlock(&m);
- for (int i = 0; i < res_threads; i++)
- pthread_join(resolve_tid[i], NULL);
- fclose(req_data.req_log);
- fclose(res_data.res_log);
- queue_free(&input_files);
- queue_free(&buffer);
- printf("Number of Requestor Threads: %d\n", req_thread);
- printf("Number of Resolver Threads: %d\n", res_threads);
- gettimeofday(&proc_end, NULL);
- fprintf(stdout, "%s: total time is %f seconds\n", argv[0],
- ELAPSED_SECONDS(proc_begin, proc_end));
- return EXIT_SUCCESS;
+ req_data.input_filenames = filenames;
+ req_data.total_files = total_files;
+ static int file_idx = 0;
+ pthread_mutex_init(&req_data.file_idx_lock, NULL);
+ req_data.file_idx = &file_idx;
+ pthread_t *requester_threads =
+ (pthread_t *)malloc(req_thread_cnt * sizeof(pthread_t));
+ pthread_t *resolver_threads =
+ (pthread_t *)malloc(res_thread_cnt * sizeof(pthread_t));
+ for (int i = 0; i < req_thread_cnt; i++)
+ pthread_create(&requester_threads[i], NULL, requester, (void *)&req_data);
+ for (int i = 0; i < res_thread_cnt; i++)
+ pthread_create(&resolver_threads[i], NULL, resolver, (void *)&res_data);
+ for (int i = 0; i < req_thread_cnt; i++)
+ pthread_join(requester_threads[i], NULL);
+ for (int i = 0; i < res_thread_cnt; i++)
+ pthread_join(resolver_threads[i], NULL);
+ pthread_mutex_destroy(&req_data.file_idx_lock);
+ pthread_mutex_destroy(&req_data.req_log_lock);
+ pthread_mutex_destroy(&res_data.res_log_lock);
+ pthread_mutex_destroy(&res_data.term_lock);
+ for (int i = 5; i < argc; i++)
+ free(filenames[i - 5]);
+ free(filenames);
+ array_free(&stack);
+ free(requester_threads);
+ free(resolver_threads);
+ double end_time = get_time_sec();
+ printf("%s: total time is %f seconds\n", argv[0],
+ (double)(end_time - start_time));
+ return 0;
}
-void *request(void *data) {
- request_data *req_data = (request_data *)data;
- uint8_t file_count = 0;
+void *requester(void *a) {
+ struct requester_data *data = (struct requester_data *)a;
+ double start_time = get_time_sec();
+ int files_serviced = 0;
+ char hostname[MAX_NAME_LENGTH];
+ FILE *input_file = NULL, *log_file = NULL;
while (1) {
- pthread_mutex_lock(req_data->input_lock);
- if (queue_size(req_data->file_names) > 0) {
- char *data_file = queue_pop(req_data->file_names);
- file_count++;
- pthread_mutex_unlock(req_data->input_lock);
- FILE *file = fopen(data_file, "r");
- char line[MAX_NAME_LENGTH];
- while (fgets(line, sizeof(line), file)) {
- pthread_mutex_lock(req_data->m);
- while (queue_full(req_data->buffer))
- pthread_cond_wait(req_data->c_req, req_data->m);
- pthread_mutex_lock(req_data->req_log_lock);
- fprintf(req_data->req_log, "%s", line);
- pthread_mutex_unlock(req_data->req_log_lock);
- queue_push(req_data->buffer, strdup(line));
- pthread_mutex_unlock(req_data->m);
- pthread_cond_signal(req_data->c_res);
- }
- fclose(file);
- free(data_file);
- } else {
- pthread_mutex_unlock(req_data->input_lock);
+ pthread_mutex_lock(&data->file_idx_lock);
+ int curr_file = *(data->file_idx);
+ if (curr_file >= data->total_files) {
+ pthread_mutex_unlock(&data->file_idx_lock);
break;
}
+ (*data->file_idx)++;
+ pthread_mutex_unlock(&data->file_idx_lock);
+ input_file = fopen(data->input_filenames[curr_file], "r");
+ if (!input_file) {
+ perror("failed to open data file");
+ return NULL;
+ }
}
- fprintf(stdout, "thread %ld serviced %d files\n", syscall(SYS_gettid),
- file_count);
- return 0;
+ while (fgets(hostname, sizeof(hostname), input_file)) {
+ hostname[strcspn(hostname, "\n")] = '\0';
+ array_put(data->stack, hostname);
+ pthread_mutex_lock(&data->req_log_lock);
+ log_file = fopen(data->req_log, "a");
+ fprintf(log_file, "%s\n", hostname);
+ fclose(log_file);
+ pthread_mutex_unlock(&data->req_log_lock);
+ }
+ fclose(input_file);
+ files_serviced++;
+ for (int i = 0; i < data->res_thread_cnt; i++)
+ array_put(data->stack, NULL);
+ double end_time = get_time_sec();
+ printf("thread %lx serviced %d files in %f seconds\n", (long)pthread_self(),
+ files_serviced, (double)(end_time - start_time));
+ return NULL;
}
-void *resolve(void *data) {
- resolve_data *res_data = (resolve_data *)data;
- char *ip = (char *)malloc(MAX_IP_LENGTH);
- uint16_t domain_cnt = 0;
- struct timeval thread_begin, thread_end;
- gettimeofday(&thread_begin, NULL);
+void *resolver(void *a) {
+ struct resolver_data *data = (struct resolver_data *)a;
+ double start_time = get_time_sec();
+ int hosts_resolved = 0;
+ char *hostname, ip_addr[MAX_IP_LENGTH];
+ FILE *log_file;
while (1) {
- pthread_mutex_lock(res_data->m);
- if (!(*(res_data->req_done)) || queue_size(res_data->buffer)) {
- while (!queue_size(res_data->buffer))
- pthread_cond_wait(res_data->c_res, res_data->m);
- char *domain = queue_pop(res_data->buffer);
- pthread_mutex_unlock(res_data->m);
- pthread_cond_signal(res_data->c_req);
- if (dnslookup(domain, ip, MAX_IP_LENGTH)) {
- pthread_mutex_lock(res_data->res_log_lock);
- fprintf(res_data->res_log, "%s, NOT_RESOLVED\n", domain);
- pthread_mutex_unlock(res_data->res_log_lock);
- domain_cnt++;
- } else {
- pthread_mutex_lock(res_data->res_log_lock);
- fprintf(res_data->res_log, "%s, %s\n", domain, ip);
- pthread_mutex_unlock(res_data->res_log_lock);
- domain_cnt++;
+ array_get(data->stack, &hostname);
+ if (!hostname) {
+ pthread_mutex_lock(&data->term_lock);
+ data->res_thread_cnt--;
+ if (!data->res_thread_cnt) {
+ pthread_mutex_unlock(&data->term_lock);
+ break;
}
- free(domain);
- } else {
- pthread_mutex_unlock(res_data->m);
- break;
+ pthread_mutex_unlock(&data->term_lock);
+ continue;
}
+ if (dnslookup(hostname, ip_addr, sizeof(ip_addr)))
+ strcpy(ip_addr, "NOT_RESOLVED");
+ pthread_mutex_lock(&data->res_log_lock);
+ log_file = fopen(data->res_log, "a");
+ fprintf(log_file, "%s, %s\n", hostname, ip_addr);
+ fclose(log_file);
+ pthread_mutex_unlock(&data->res_log_lock);
+ hosts_resolved++;
}
- free(ip);
- gettimeofday(&thread_end, NULL);
- fprintf(stdout, "thread %ld resolved %d hostnames in %f seconds\n",
- syscall(SYS_gettid), domain_cnt,
- ELAPSED_SECONDS(thread_begin, thread_end));
- return 0;
+ double end_time = get_time_sec();
+ printf("thread %lx resolved %d hosts in %f seconds\n", (long)pthread_self(),
+ hosts_resolved, (double)(end_time - start_time));
+ return NULL;
+}
+
+double get_time_sec(void) {
+ struct timeval time;
+ gettimeofday(&time, NULL);
+ return time.tv_sec + (time.tv_usec / 1000000.0);
}
diff --git a/pa6_threaded_resolver/multi-lookup.h b/pa6_threaded_resolver/multi-lookup.h
index 41e74d5..9433da9 100644
--- a/pa6_threaded_resolver/multi-lookup.h
+++ b/pa6_threaded_resolver/multi-lookup.h
@@ -2,43 +2,34 @@
// CSCI3753 SP24
// PA6 Threaded Resolver
#include "array.h"
-#include "util.h"
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/syscall.h>
-#include <sys/time.h>
-#include <unistd.h>
-#define ARRAY_SIZE 8
+#pragma once
+
#define MAX_INPUT_FILES 100
#define MAX_REQUESTER_THREADS 10
#define MAX_RESOLVER_THREADS 10
-#define MAX_NAME_LENGTH 16
+#define MAX_NAME_LENGTH 17
#define MAX_IP_LENGTH INET6_ADDRSTRLEN
-#define ELAPSED_SECONDS(a, b) \
- ((double)(b.tv_usec - a.tv_usec) / 1000000 + (double)(b.tv_sec - a.tv_sec))
-void *request(void *data);
-void *resolve(void *data);
+struct requester_data {
+ char *req_log;
+ char **input_filenames;
+ array *stack;
+ int *file_idx;
+ int total_files;
+ int res_thread_cnt;
+ pthread_mutex_t file_idx_lock;
+ pthread_mutex_t req_log_lock;
+};
-typedef struct {
- FILE *req_log;
- queue *file_names;
- queue *buffer;
- pthread_mutex_t *m;
- pthread_cond_t *c_req;
- pthread_cond_t *c_res;
- pthread_mutex_t *input_lock;
- pthread_mutex_t *req_log_lock;
-} request_data;
+struct resolver_data {
+ char *res_log;
+ array *stack;
+ int res_thread_cnt;
+ pthread_mutex_t term_lock;
+ pthread_mutex_t res_log_lock;
+};
-typedef struct {
- FILE *res_log;
- queue *buffer;
- pthread_mutex_t *m;
- pthread_cond_t *c_req;
- pthread_cond_t *c_res;
- pthread_mutex_t *res_log_lock;
- bool *req_done;
-} resolve_data;
+void *requester(void *a);
+void *resolver(void *a);
+double get_time_sec(void);
diff --git a/pa6_threaded_resolver/util.c b/pa6_threaded_resolver/util.c
index 0b4a825..04e9cb3 100644
--- a/pa6_threaded_resolver/util.c
+++ b/pa6_threaded_resolver/util.c
@@ -9,77 +9,73 @@
* Description:
* This file contains declarations of utility functions for
* Programming Assignment 5.
- *
+ *
*/
#include "util.h"
-int dnslookup(const char* hostname, char* firstIPstr, int maxSize){
+int dnslookup(const char *hostname, char *firstIPstr, int maxSize) {
- /* Local vars */
- struct addrinfo* headresult = NULL;
- struct addrinfo* result = NULL;
- struct sockaddr_in* ipv4sock = NULL;
- struct in_addr* ipv4addr = NULL;
- char ipv4str[INET_ADDRSTRLEN];
- char ipstr[INET6_ADDRSTRLEN];
- int addrError = 0;
+ /* Local vars */
+ struct addrinfo *headresult = NULL;
+ struct addrinfo *result = NULL;
+ struct sockaddr_in *ipv4sock = NULL;
+ struct in_addr *ipv4addr = NULL;
+ char ipv4str[INET_ADDRSTRLEN];
+ char ipstr[INET6_ADDRSTRLEN];
+ int addrError = 0;
- /* DEBUG: Print Hostname*/
+ /* DEBUG: Print Hostname*/
#ifdef UTIL_DEBUG
- fprintf(stderr, "%s\n", hostname);
+ fprintf(stderr, "%s\n", hostname);
#endif
-
- /* Lookup Hostname */
- addrError = getaddrinfo(hostname, NULL, NULL, &headresult);
- if(addrError){
- fprintf(stderr, "Error looking up Address: %s\n",
- gai_strerror(addrError));
- return UTIL_FAILURE;
- }
- /* Loop Through result Linked List */
- for(result=headresult; result != NULL; result = result->ai_next){
- /* Extract IP Address and Convert to String */
- if(result->ai_addr->sa_family == AF_INET){
- /* IPv4 Address Handling */
- ipv4sock = (struct sockaddr_in*)(result->ai_addr);
- ipv4addr = &(ipv4sock->sin_addr);
- if(!inet_ntop(result->ai_family, ipv4addr,
- ipv4str, sizeof(ipv4str))){
- perror("Error Converting IP to String");
- return UTIL_FAILURE;
- }
+
+ /* Lookup Hostname */
+ addrError = getaddrinfo(hostname, NULL, NULL, &headresult);
+ if (addrError) {
+ fprintf(stderr, "Error looking up Address: %s\n", gai_strerror(addrError));
+ return UTIL_FAILURE;
+ }
+ /* Loop Through result Linked List */
+ for (result = headresult; result != NULL; result = result->ai_next) {
+ /* Extract IP Address and Convert to String */
+ if (result->ai_addr->sa_family == AF_INET) {
+ /* IPv4 Address Handling */
+ ipv4sock = (struct sockaddr_in *)(result->ai_addr);
+ ipv4addr = &(ipv4sock->sin_addr);
+ if (!inet_ntop(result->ai_family, ipv4addr, ipv4str, sizeof(ipv4str))) {
+ perror("Error Converting IP to String");
+ return UTIL_FAILURE;
+ }
#ifdef UTIL_DEBUG
- fprintf(stdout, "%s\n", ipv4str);
+ fprintf(stdout, "%s\n", ipv4str);
#endif
- strncpy(ipstr, ipv4str, sizeof(ipstr));
- ipstr[sizeof(ipstr)-1] = '\0';
- }
- else if(result->ai_addr->sa_family == AF_INET6){
- /* IPv6 Handling */
+ strncpy(ipstr, ipv4str, sizeof(ipstr));
+ ipstr[sizeof(ipstr) - 1] = '\0';
+ } else if (result->ai_addr->sa_family == AF_INET6) {
+ /* IPv6 Handling */
#ifdef UTIL_DEBUG
- fprintf(stdout, "IPv6 Address: Not Handled\n");
+ fprintf(stdout, "IPv6 Address: Not Handled\n");
#endif
- strncpy(ipstr, "UNHANDELED", sizeof(ipstr));
- ipstr[sizeof(ipstr)-1] = '\0';
- }
- else{
- /* Unhandlded Protocol Handling */
+ strncpy(ipstr, "UNHANDELED", sizeof(ipstr));
+ ipstr[sizeof(ipstr) - 1] = '\0';
+ } else {
+ /* Unhandlded Protocol Handling */
#ifdef UTIL_DEBUG
- fprintf(stdout, "Unknown Protocol: Not Handled\n");
+ fprintf(stdout, "Unknown Protocol: Not Handled\n");
#endif
- strncpy(ipstr, "UNHANDELED", sizeof(ipstr));
- ipstr[sizeof(ipstr)-1] = '\0';
- }
- /* Save First IP Address */
- if(result==headresult){
- strncpy(firstIPstr, ipstr, maxSize);
- firstIPstr[maxSize-1] = '\0';
- }
+ strncpy(ipstr, "UNHANDELED", sizeof(ipstr));
+ ipstr[sizeof(ipstr) - 1] = '\0';
+ }
+ /* Save First IP Address */
+ if (result == headresult) {
+ strncpy(firstIPstr, ipstr, maxSize);
+ firstIPstr[maxSize - 1] = '\0';
}
+ }
- /* Cleanup */
- freeaddrinfo(headresult);
+ /* Cleanup */
+ freeaddrinfo(headresult);
- return UTIL_SUCCESS;
+ return UTIL_SUCCESS;
}
diff --git a/pa6_threaded_resolver/util.h b/pa6_threaded_resolver/util.h
index d0f7559..16433b7 100644
--- a/pa6_threaded_resolver/util.h
+++ b/pa6_threaded_resolver/util.h
@@ -9,7 +9,7 @@
* Description:
* This file contains declarations of utility functions for
* Programming Assignment 3.
- *
+ *
*/
#ifndef UTIL_H
@@ -18,15 +18,15 @@
/* Define the following to enable debug statments */
// #define UTIL_DEBUG
-#include <stdlib.h>
+#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <errno.h>
#include <arpa/inet.h>
-#include <sys/types.h>
-#include <sys/socket.h>
#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/types.h>
#define UTIL_FAILURE -1
#define UTIL_SUCCESS 0
@@ -35,8 +35,6 @@
* for hostname. IP address returned as string
* firstIPstr of size maxsize
*/
-int dnslookup(const char* hostname,
- char* firstIPstr,
- int maxSize);
+int dnslookup(const char *hostname, char *firstIPstr, int maxSize);
#endif