aboutsummaryrefslogtreecommitdiff
path: root/own_ip.h
diff options
context:
space:
mode:
authorBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
committerBen Burwell <bburwell1@gmail.com>2013-04-29 16:13:34 -0400
commitca9e46c937e75df43d2f80f0d957fabb07892c29 (patch)
tree0d8ae2bc1d1e3e248c746586a5daf7cb58a79463 /own_ip.h
Init
Diffstat (limited to 'own_ip.h')
-rw-r--r--own_ip.h64
1 files changed, 64 insertions, 0 deletions
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