aboutsummaryrefslogtreecommitdiffstats
path: root/src/ipcalc.c
blob: fc8d3d5cd2613e61246909159a0b13f2d70788bf (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <popt.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

typedef unsigned int int32;

int main(int argc, char ** argv) {
    int showBroadcast = 0, showNetwork = 0, showHostname = 0, showNetmask = 0;
    int beSilent = 0;
    int rc;
    poptContext optCon;
    char * ipStr, * netmaskStr, * chptr;
    int32 ip, netmask, network, broadcast;
    struct hostent * hostinfo;
    char errBuf[250];
    struct poptOption optionsTable[] = {
	    { "broadcast", '\0', 0, &showBroadcast, 0 },
	    { "hostname", '\0', 0, &showHostname, 0 },
	    { "netmask", '\0', 0, &showNetmask, 0 },
	    { "network", '\0', 0, &showNetwork, 0 },
	    { "silent", '\0', 0, &beSilent, 0 },
	    { NULL, '\0', 0, 0, 0 },
    };

    optCon = poptGetContext("ipcalc", argc, argv, optionsTable,0);
    poptReadDefaultConfig(optCon, 1);

    if ((rc = poptGetNextOpt(optCon)) < -1) {
	if (!beSilent)
	    fprintf(stderr, "ipcalc: bad argument %s: %s\n", 
		    poptBadOption(optCon, POPT_BADOPTION_NOALIAS), 
		    poptStrerror(rc));
	return 1;
    }

    if (!(ipStr = poptGetArg(optCon))) {
	if (!beSilent)
	    fprintf(stderr, "ipcalc: ip address expected\n");
	return 1;
    }

    if (showBroadcast || showNetwork) {
	if (!(netmaskStr = poptGetArg(optCon))) {
	    if (!beSilent)
		fprintf(stderr, "ipcalc: netmask expected\n");
	    return 1;
	}

	if (!inet_aton(netmaskStr, (struct in_addr *) &netmask)) {
	    if (!beSilent)
		fprintf(stderr, "ipcalc: bad netmask: %s\n", netmaskStr);
	    return 1;
	}
    }

    if ((chptr = poptGetArg(optCon))) {
	if (!beSilent)
	    fprintf(stderr, "ipcalc: unexpected argument: %s\n", chptr);
	return 1;
    }

    poptFreeContext(optCon);

    if (!inet_aton(ipStr, (struct in_addr *) &ip)) {
	if (!beSilent)
	    fprintf(stderr, "ipcalc: bad ip address: %s\n", ipStr);
	return 1;
    }

    if (showNetmask) {
	if (((ntohl(ip) & 0xFF000000) >> 24) <= 127)
	    chptr = "255.0.0.0";
	else if (((ntohl(ip) & 0xFF000000) >> 24) <= 191)
	    chptr = "255.255.0.0";
	else 
	    chptr = "255.255.255.0";

	printf("NETMASK=%s\n", chptr);
    }

    if (showBroadcast) {
	broadcast = (ip & netmask) | ~netmask;
	printf("BROADCAST=%s\n", inet_ntoa(*((struct in_addr *) &broadcast)));
    }

    if (showNetwork) {
	network = ip & netmask;
	printf("NETWORK=%s\n", inet_ntoa(*((struct in_addr *) &network)));
    }

    if (showHostname) {
	hostinfo = gethostbyaddr((char *) &ip, sizeof(ip), AF_INET);
	if (!hostinfo) {
	    if (!beSilent) {
		sprintf(errBuf, "ipcalc: cannot find hostname for %s", ipStr);
		herror(errBuf);
	    }

	    return 1;
	}

	printf("HOSTNAME=%s\n", hostinfo->h_name);
    }
	

    return 0;
}