diff --git a/datastructs/ringbuffer/ring.h b/datastructs/ringbuffer/ring.h index 8db996c..bc95f55 100644 --- a/datastructs/ringbuffer/ring.h +++ b/datastructs/ringbuffer/ring.h @@ -2,9 +2,9 @@ #define RING_H /* A ring buffer for multi-threaded programs. A single producer pushes - * objects into the ring, while a single consumer pops them off for - * processing. If the ring is full (i.e., the consumer is slower than the - * producer), then the push operation fails. +* objects into the ring, while a single consumer pops them off for +* processing. If the ring is full (i.e., the consumer is slower than the +* producer), then the push operation fails. */ struct ring @@ -18,16 +18,16 @@ struct ring /* Create a ring buffer with the specified size. Return the ring or NULL - if there is not enough memory to create. */ +if there is not enough memory to create. */ struct ring* ring_create(int size); /* Add an entry to the ring. - Return 0 on success, or a sensible error code if ring is full +Return 0 on success, or a sensible error code if ring is full */ int ring_push(struct ring *rb, void* data); /* Remove an entry from the ring. - Return a pointer to the data, or NULL if empty +Return a pointer to the data, or NULL if empty */ void* ring_pop(struct ring *rb); diff --git a/datastructs/ringbuffer/test/ring_test.c b/datastructs/ringbuffer/test/ring_test.c index 4fb5ae2..ce8633a 100644 --- a/datastructs/ringbuffer/test/ring_test.c +++ b/datastructs/ringbuffer/test/ring_test.c @@ -9,7 +9,7 @@ with exceeding push and pop options #include "../ring.h" -int + int main() { //set buffer size as 5, first push 3 elements, and then pop 2 elements, then push 6 elements, at last pop 12 elements diff --git a/sockets/.test.c.swp b/sockets/.test.c.swp new file mode 100644 index 0000000..63dcc5a Binary files /dev/null and b/sockets/.test.c.swp differ diff --git a/sockets/client-tcp b/sockets/client-tcp new file mode 100755 index 0000000..fac0cfa Binary files /dev/null and b/sockets/client-tcp differ diff --git a/sockets/ip b/sockets/ip new file mode 100755 index 0000000..009eaab Binary files /dev/null and b/sockets/ip differ diff --git a/sockets/iplookup.c b/sockets/iplookup.c index e66edab..0f539b9 100644 --- a/sockets/iplookup.c +++ b/sockets/iplookup.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -9,66 +10,69 @@ int main(int argc , char *argv[]) { int sockfd; + struct addrinfo *res = 0; //Setting addrinfo struct for getaddrinfo. struct addrinfo hints, *servinfo, *p; struct sockaddr_in *h; + char *hostname = malloc(sizeof(char) * 1000), *ip = malloc(sizeof(char) * 100);; int rc, ctr; - + //if argc size is less than 2, then the hostname was not included as a command line input. if(argc <3){ printf("Error: Missing Hostname/IP Address or flag to decide IP->Hostname or Hostname->IP\n"); exit(1); } //if command line input is "-h", then hostname was given. - if (strcmp(argv[1], "-h") == 0) { - char *hostname = argv[2]; //hostname from command line input. - char ip[100]; //set up to take in the IPv4 address later. + int o; + while((o = getopt (argc, argv, "h:i:")) != -1){ + switch(o){ + case 'h': + hostname = optarg; //hostname from command line input. + + memset(&hints, 0, sizeof hints); //setting space for hints. + hints.ai_family = AF_INET; //setting the family type to IPv4 only. + hints.ai_socktype = SOCK_STREAM; //setting socket type to streams. + + //if the return value is not zero, there is an error. + if((rc = getaddrinfo(hostname, "http", &hints, &servinfo)) != 0) + { + perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. + exit(-1); + } + + ctr = 1; + //find the first connection from the results + for(p = servinfo; p != NULL; p = p->ai_next) + { + h = (struct sockaddr_in *) p->ai_addr; //set h to the pointer of the sockaddr struct. ai_addr is a pointer to a filled-in socket address struct. + strcpy(ip, inet_ntoa(h->sin_addr)); //sin_addr holds the IPv4 address. this is copied to ip. + printf("%s - IPv4 address #%d: %s\n" ,hostname , ctr, ip); + ctr++; + } + break; - memset(&hints, 0, sizeof hints); //setting space for hints. - hints.ai_family = AF_INET; //setting the family type to IPv4 only. - hints.ai_socktype = SOCK_STREAM; //setting socket type to streams. - - //if the return value is not zero, there is an error. - if((rc = getaddrinfo(hostname, "http", &hints, &servinfo)) != 0) - { - perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. - exit(-1); - } - - ctr = 1; - //find the first connection from the results - for(p = servinfo; p != NULL; p = p->ai_next) - { - h = (struct sockaddr_in *) p->ai_addr; //set h to the pointer of the sockaddr struct. ai_addr is a pointer to a filled-in socket address struct. - strcpy(ip, inet_ntoa(h->sin_addr)); //sin_addr holds the IPv4 address. this is copied to ip. - printf("%s - IPv4 address #%d: %s\n" ,hostname , ctr, ip); - ctr++; - } + case 'i': + + ip = optarg; //command line input + printf("Command line input: %s\n", ip); //output to show user their input. - } - //otherwise, if command line input is "-i", then IPv4 address was given. - else if(strcmp(argv[1], "-i") == 0){ - struct addrinfo *res=0; //Setting addrinfo struct for getaddrinfo. - int rc; //used for error-checking later on. - - char *ip = argv[2]; //command line input - char hostname[1000]; //variable to store output - printf("Command line input: %s\n", ip); //output to show user their input. - - rc = getaddrinfo(ip, 0, 0, &res); //getaddrinfo is used to supply parameters to getnameinfo. - //error checking getaddrinfo. - if(rc != 0) - { - perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. - exit(-1); - } - - rc = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, 1000, 0, 0, 0); - //error checking getnameinfo. - if(rc != 0) - { - perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. - exit(-1); - } - printf("Hostname found: %s\n", hostname); - } + rc = getaddrinfo(ip, 0, 0, &res); //getaddrinfo is used to supply parameters to getnameinfo. + //error checking getaddrinfo. + if(rc != 0) + { + perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. + exit(-1); + } + + rc = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, 1000, 0, 0, 0); + //error checking getnameinfo. + if(rc != 0) + { + perror(gai_strerror(rc)); //print the error. gai_strerror translates the error to readable human text. + exit(-1); + } + printf("Hostname found: %s\n", hostname); + break; + + } + } } diff --git a/sockets/server-tcp b/sockets/server-tcp new file mode 100755 index 0000000..2079602 Binary files /dev/null and b/sockets/server-tcp differ