From ca9e46c937e75df43d2f80f0d957fabb07892c29 Mon Sep 17 00:00:00 2001 From: Ben Burwell Date: Mon, 29 Apr 2013 16:13:34 -0400 Subject: Init --- own_ip.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 own_ip.h (limited to 'own_ip.h') diff --git a/own_ip.h b/own_ip.h new file mode 100644 index 0000000..7021c11 --- /dev/null +++ b/own_ip.h @@ -0,0 +1,64 @@ +/** + * Localchat + * Ben Burwell + * + * Gets host IP + */ + +void get_own_ip() { + + char name_buf[256]; + unsigned int buf_len = 256; + struct hostent *host; + int ret_code; + + struct addrinfo hints; + struct addrinfo *result; + struct addrinfo *rptr; // Pointer to linked list. + struct sockaddr_in *ipv4; + void * addr; + char ipstr[64]; + + // Get the name of the host. + ret_code = gethostname(name_buf, buf_len); + if (ret_code < 0) { + exit(1); + } + + if (strcmp(name_buf, "mathcs") == 0) { + + strcpy(my_ip, "192.168.130.10"); + + } else { + + // Get the IP addresses of this host. + memset((void *) &hints, 0, (size_t) sizeof(hints)); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + hints.ai_protocol = 0; + hints.ai_addr = NULL; + hints.ai_next = NULL; + + ret_code = getaddrinfo(name_buf, NULL, &hints, &result); + if (ret_code != 0) { + perror("Error from getaddrinfo\n"); + exit(1); + } + + rptr = result; + while (rptr != NULL) { + ipv4 = (struct sockaddr_in *) rptr->ai_addr; + addr = &(ipv4->sin_addr); + inet_ntop(rptr->ai_family, addr, ipstr, sizeof(ipstr)); + + rptr = rptr->ai_next; + } + + strcpy(my_ip, ipstr); + + freeaddrinfo(result); // free the linked list + } + + return; +} \ No newline at end of file -- cgit v1.2.3