hyperbitblock.c - hbb - hyperbitblock
HTML git clone git://kroovy.de/hbb
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
hyperbitblock.c (1661B)
---
1 #define _POSIX_SOURCE
2 #include <signal.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/mman.h>
8 #include <sys/types.h>
9 #include <sys/uio.h>
10 #include <unistd.h>
11
12 #include <ctype.h>
13 #include <fcntl.h>
14
15 int
16 is_bit_set(unsigned value, unsigned bitindex)
17 {
18 return (value & (1 << bitindex)) != 0;
19 }
20
21 int
22 str_to_ip(char *s, uint8_t *v4)
23 {
24 uint8_t oct=0;
25
26 int i,j;
27
28 j=0;
29 for (i=0 ;s[i] != '\0'; i++) {
30 if (s[i] == '.') {
31 v4[j] = oct;
32 oct = 0;
33 j++;
34
35 } else {
36 oct *= 10;
37 oct += (int)s[i] - '0';
38 }
39 }
40 v4[j] = oct;
41
42 return 0;
43 }
44
45 int
46 main(void)
47 {
48 int fd;
49 FILE* db;
50 int i, res, ret=1;
51 uint8_t v4[4] = {0,0,0,0};
52 uint8_t* arr = malloc(4294967296);
53 uint32_t i32 = 0;
54
55 char c;
56 char line[64];
57
58 fd = open("mynamedpipe", O_RDWR);
59 db = fopen("bit.db", "r");
60
61 int cnt=0;
62 while (ret > 0) {
63 ret = fread(arr, 1024, 1, db);
64 cnt++;
65 }
66
67 //set some values
68 arr[1067179] = 255;
69 arr[309791169] = 255;
70 arr[3663462608] = 255;
71
72 nextline:
73 for (i=0; (res = read(fd, &c, 1)) > 0; i++) { //consider readline/getline
74 line[i] = c;
75 if (c == '\n') {
76 line[i] = '\0';
77 break;
78 }
79 c = '\0';
80 }
81 if (strncmp(line, "q", 1) == 0) {
82 free(arr);
83 return 0;
84 }
85
86 str_to_ip(line, v4);
87 i32 = (v4[0] << 24) | (v4[1] << 16) | (v4[2] << 8) | v4[3];
88 printf("%u.%u.%u.%u\t-->\t%u\t-->\t%u\n",v4[0],v4[1],v4[2],v4[3],i32,arr[i32]);
89 goto nextline;
90
91 close(fd);
92 fclose(db);
93 }