move some functions to separate library, add type for bit array element

This commit is contained in:
Alexander Zubkov
2021-11-30 20:49:51 +01:00
parent 1d3675b848
commit 1d0c8c8998
6 changed files with 142 additions and 105 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/plag
/*.o

View File

@@ -1,5 +1,13 @@
plag: plag.c config.h
cc -O3 -o plag plag.c
CC=gcc
CFLAGS= -O3
all: plag
plag: plag.c lib.o lib.h config.h
$(CC) $(CFLAGS) -o $@ plag.c lib.o
lib.o: lib.c lib.h
$(CC) $(CFLAGS) -o $@ -c lib.c
clean:
rm plag
rm plag lib.o

View File

@@ -1,6 +1,11 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <stdint.h>
#define SIZE (10 * 1<<20)
#define BUFLEN 1024
typedef uint32_t pos_t;
typedef int mask_t;
#endif /* !CONFIG_H */

99
lib.c Normal file
View File

@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <error.h>
#include <errno.h>
#include "config.h"
#include "lib.h"
void bits2ip4(bits b, mask_t m, char *s, int size) {
// convert bits to in4_addr
struct in4_addr ip;
for (int i = 0; i < 4; ++i) {
ip.s4_addr[i] = 0;
for (int j = 0; j < 8; ++j) {
ip.s4_addr[i] <<= 1;
int p = i*8 + j;
ip.s4_addr[i] |= (p >= m) ? 0 : b[p];
}
}
// get test representation of ip
char s_ip[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, &ip, s_ip, INET_ADDRSTRLEN)) error(1, errno, "bits2ip4");
// print ip with mask to buffer
int res = snprintf(s, size, "%s/%d", s_ip, m);
// if printing to buffer has failed
if (res < 0 || res >= size) error(1, errno, "bits2ip4");
}
void bits2ip6(bits b, mask_t m, char *s, int size) {
// convert bits to in6_addr
struct in6_addr ip;
for (int i = 0; i < 16; ++i) {
ip.s6_addr[i] = 0;
for (int j = 0; j < 8; ++j) {
ip.s6_addr[i] <<= 1;
int p = i*8 + j;
ip.s6_addr[i] |= (p >= m) ? 0 : b[p];
}
}
// get test representation of ip
char s_ip[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, &ip, s_ip, INET6_ADDRSTRLEN)) error(1, errno, "bits2ip6");
// print ip with mask to buffer
int res = snprintf(s, size, "%s/%d", s_ip, m);
// if printing to buffer has failed
if (res < 0 || res >= size) error(1, errno, "bits2ip6");
}
void ip42bits(char *s, bits b, mask_t *m) {
// split ip and mask
char *p = strchr(s, '/');
if (p) {
*p = 0;
++p;
unsigned long l = strtol(p, &p, 10);
if (*p || l > IP4LEN) error(1, errno, "ip42bits: bad mask");
*m = l;
} else {
*m = IP4LEN;
}
// convert ip to binary
struct in4_addr ip;
if (!inet_pton(AF_INET, s, &ip)) error(1, errno, "ip42bits");
// convert binary ip to bits
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 8; ++j) {
int p = i*8 + 8-1-j;
b[p] = (p > *m) ? 0 : (ip.s4_addr[i] & 1);
ip.s4_addr[i] >>= 1;
}
}
}
void ip62bits(char *s, bits b, mask_t *m) {
// split ip and mask
char *p = strchr(s, '/');
if (p) {
*p = 0;
++p;
unsigned long l = strtol(p, &p, 10);
if (*p || l > IP6LEN) error(1, errno, "ip62bits: bad mask");
*m = l;
} else {
*m = IP6LEN;
}
// convert ip to binary
struct in6_addr ip;
if (!inet_pton(AF_INET6, s, &ip)) error(1, errno, "ip62bits");
// convert binary ip to bits
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 8; ++j) {
int p = i*8 + 8-1-j;
b[p] = (p > *m) ? 0 : (ip.s6_addr[i] & 1);
ip.s6_addr[i] >>= 1;
}
}
}

23
lib.h Normal file
View File

@@ -0,0 +1,23 @@
#ifndef LIB_H
#define LIB_H
#include "config.h"
#define IP4LEN 32
#define IP6LEN 128
#define IPMAXLEN IP6LEN
#define IPMAXB (IPMAXLEN/8)
struct in4_addr { unsigned char s4_addr[4]; };
typedef int bit_t;
typedef bit_t *bits;
void bits2ip4(bits b, mask_t m, char *s, int size);
void bits2ip6(bits b, mask_t m, char *s, int size);
void ip42bits(char *s, bits b, mask_t *m);
void ip62bits(char *s, bits b, mask_t *m);
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif /* !LIB_H */

105
plag.c
View File

@@ -1,23 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <error.h>
#include <errno.h>
#include "config.h"
#define IP4LEN 32
#define IP6LEN 128
#define IPMAXLEN IP6LEN
struct in4_addr {
unsigned char s4_addr[4];
};
#include "lib.h"
struct node { pos_t a[2]; };
typedef struct node *iptree;
typedef int *bits;
typedef void (*bitsfun)(bits, mask_t);
pos_t maxpos = 0;
@@ -146,103 +137,13 @@ void iptree_traverse_rec(iptree t, bitsfun f, pos_t h, bits b, mask_t m) {
}
void iptree_traverse(iptree t, pos_t h, bitsfun f) {
int b[IPMAXLEN];
bit_t b[IPMAXLEN];
if (t[h].a[0]) {
// tree is not empty
iptree_traverse_rec(t, f, t[h].a[0], b, 0);
}
}
void bits2ip4(bits b, mask_t m, char *s, int size) {
// convert bits to in4_addr
struct in4_addr ip;
for (int i = 0; i < 4; ++i) {
ip.s4_addr[i] = 0;
for (int j = 0; j < 8; ++j) {
ip.s4_addr[i] <<= 1;
int p = i*8 + j;
ip.s4_addr[i] |= (p >= m) ? 0 : b[p];
}
}
// get test representation of ip
char s_ip[INET_ADDRSTRLEN];
if (!inet_ntop(AF_INET, &ip, s_ip, INET_ADDRSTRLEN)) error(1, errno, "bits2ip4");
// print ip with mask to buffer
int res = snprintf(s, size, "%s/%d", s_ip, m);
// if printing to buffer has failed
if (res < 0 || res >= size) error(1, errno, "bits2ip4");
}
void bits2ip6(bits b, mask_t m, char *s, int size) {
// convert bits to in6_addr
struct in6_addr ip;
for (int i = 0; i < 16; ++i) {
ip.s6_addr[i] = 0;
for (int j = 0; j < 8; ++j) {
ip.s6_addr[i] <<= 1;
int p = i*8 + j;
ip.s6_addr[i] |= (p >= m) ? 0 : b[p];
}
}
// get test representation of ip
char s_ip[INET6_ADDRSTRLEN];
if (!inet_ntop(AF_INET6, &ip, s_ip, INET6_ADDRSTRLEN)) error(1, errno, "bits2ip6");
// print ip with mask to buffer
int res = snprintf(s, size, "%s/%d", s_ip, m);
// if printing to buffer has failed
if (res < 0 || res >= size) error(1, errno, "bits2ip6");
}
void ip42bits(char *s, bits b, mask_t *m) {
// split ip and mask
char *p = strchr(s, '/');
if (p) {
*p = 0;
++p;
unsigned long l = strtol(p, &p, 10);
if (*p || l > IP4LEN) error(1, errno, "ip42bits: bad mask");
*m = l;
} else {
*m = IP4LEN;
}
// convert ip to binary
struct in4_addr ip;
if (!inet_pton(AF_INET, s, &ip)) error(1, errno, "ip42bits");
// convert binary ip to bits
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 8; ++j) {
int p = i*8 + 8-1-j;
b[p] = (p > *m) ? 0 : (ip.s4_addr[i] & 1);
ip.s4_addr[i] >>= 1;
}
}
}
void ip62bits(char *s, bits b, mask_t *m) {
// split ip and mask
char *p = strchr(s, '/');
if (p) {
*p = 0;
++p;
unsigned long l = strtol(p, &p, 10);
if (*p || l > IP6LEN) error(1, errno, "ip62bits: bad mask");
*m = l;
} else {
*m = IP6LEN;
}
// convert ip to binary
struct in6_addr ip;
if (!inet_pton(AF_INET6, s, &ip)) error(1, errno, "ip62bits");
// convert binary ip to bits
for (int i = 0; i < 16; ++i) {
for (int j = 0; j < 8; ++j) {
int p = i*8 + 8-1-j;
b[p] = (p > *m) ? 0 : (ip.s6_addr[i] & 1);
ip.s6_addr[i] >>= 1;
}
}
}
void print4(bits b, mask_t m) {
char s_ip[BUFLEN];
bits2ip4(b, m, s_ip, BUFLEN);
@@ -260,7 +161,7 @@ double nodes2mb(pos_t n) {
}
int main(void) {
int b[IPMAXLEN];
bit_t b[IPMAXLEN];
mask_t m;
char s[BUFLEN];