Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions datastructs/ringbuffer/ring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion datastructs/ringbuffer/test/ring_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added sockets/.test.c.swp
Binary file not shown.
Binary file added sockets/client-tcp
Binary file not shown.
Binary file added sockets/ip
Binary file not shown.
106 changes: 55 additions & 51 deletions sockets/iplookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,77 @@
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <arpa/inet.h>

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;

}
}
}
Binary file added sockets/server-tcp
Binary file not shown.