aboutsummaryrefslogtreecommitdiff
path: root/own_ip.h
blob: 7021c11bcb557b3aea1496b228c8453615991700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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;
}