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
|
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "md5.h"
static void
usage()
{
fprintf(stderr, "usage: ipmunge [-s data] templ mask\n");
exit(2);
}
static int
md5_file(FILE* f, MD5_CTX *ctx)
{
#define BUF_SIZE 128
unsigned char buf[BUF_SIZE];
int r, read = 0;
while(!feof(f))
{
r = fread(buf, 1, BUF_SIZE, f);
if(ferror(f))
err(1, "couldn't read input data");
read += r;
MD5Update(ctx, buf, r);
}
return read;
}
int
main(int argc, char* argv[])
{
MD5_CTX ctx;
struct in_addr templ;
struct in_addr mask;
struct in_addr result;
const char* data = NULL;
int ch, l, i;
int count = 1;
memset(&result, 0, sizeof(result));
memset(&mask, 0, sizeof(mask));
memset(&templ, 0, sizeof(templ));
while((ch = getopt(argc, argv, "s:")) != -1)
{
switch(ch)
{
case 's':
data = optarg;
break;
case '?':
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if(argc != 2)
usage();
if(!inet_aton(argv[0], &templ))
errx(1, "invalid template address: %s", argv[0]);
if(!inet_aton(argv[1], &mask))
errx(1, "invalid template mask: %s", argv[1]);
MD5Init(&ctx);
if(data)
{
l = strlen(data);
MD5Update(&ctx, data, l);
}
else
{
l = md5_file(stdin, &ctx);
}
if(l > 0)
{
unsigned char md[MD5_DIGEST_LENGTH];
MD5Final(md, &ctx);
memcpy(&result, md, sizeof(result));
}
result.s_addr = result.s_addr & ~mask.s_addr;
result.s_addr = result.s_addr | (templ.s_addr & mask.s_addr);
printf("%s\n", inet_ntoa(result));
return 0;
}
|