summaryrefslogtreecommitdiff
path: root/src/bin/cidr.c
blob: 1cef7d477441ede5b54e5f6829c99a7ed49143ad (plain) (blame)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main(int argc,char *argv[]) {
 unsigned int cidr;
 unsigned int net;
 unsigned int mask;
 if(argc < 2) {
  printf("usage: cidr address/cidr\n");
  printf("example: cidr 192.168.0.1/24\n");
  return 1;
 }
 if(!strchr(argv[1],'/')) cidr=32;
 else {
  cidr=atoi(strchr(argv[1],'/')+1);
  *strchr(argv[1],'/')=0;
 }
 net=htonl(inet_addr(argv[1]));
 printf("%d.%d.%d.%d ",net>>24&255,net>>16&255,net>>8&255,net&255);
 mask=~0;
 mask>>=(32-cidr);
 mask<<=(32-cidr);
 printf("%d.%d.%d.%d\n",mask>>24&255,mask>>16&255,mask>>8&255,mask&255);
 return 0;
}