[4e65f42] | 1 | #include "config.h" |
---|
[49f147b] | 2 | #include "ipenc.h" |
---|
| 3 | #include "panon.h" |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <assert.h> |
---|
| 6 | #include <unistd.h> |
---|
| 7 | #include <string.h> |
---|
| 8 | |
---|
| 9 | #ifndef HAVE_STRLCPY |
---|
| 10 | static size_t strlcpy(char *dest, const char *src, size_t size) |
---|
| 11 | { |
---|
| 12 | size_t ret; |
---|
| 13 | for(ret=0;src[ret] && ret<size; ret++) { |
---|
| 14 | dest[ret]=src[ret]; |
---|
| 15 | } |
---|
| 16 | dest[ret++]='\0'; |
---|
| 17 | return ret; |
---|
| 18 | } |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | static enum enc_type_t enc_type = ENC_NONE; |
---|
| 22 | |
---|
| 23 | static uint32_t masks[33] = { |
---|
| 24 | 0x00000000, 0x80000000, 0xC0000000, 0xe0000000, 0xf0000000, |
---|
| 25 | 0xf8000000, 0xfC000000, 0xfe000000, 0xff000000, 0xff800000, |
---|
| 26 | 0xffC00000, 0xffe00000, 0xfff00000, 0xfff80000, 0xfffC0000, |
---|
| 27 | 0xfffe0000, 0xffff0000, 0xffff8000, 0xffffC000, 0xffffe000, |
---|
| 28 | 0xfffff000, 0xfffff800, 0xfffffC00, 0xfffffe00, 0xffffff00, |
---|
| 29 | 0xffffff80, 0xffffffC0, 0xffffffe0, 0xfffffff0, 0xfffffff8, |
---|
| 30 | 0xfffffffC, 0xfffffffe, 0xffffffff, |
---|
| 31 | }; |
---|
| 32 | |
---|
| 33 | static uint32_t prefix; |
---|
| 34 | static uint32_t netmask; |
---|
[a8f2692] | 35 | static void init_prefix(const char *key) |
---|
[49f147b] | 36 | { |
---|
| 37 | int a,b,c,d; |
---|
| 38 | int bits; |
---|
| 39 | sscanf(key,"%i.%i.%i.%i/%i", |
---|
| 40 | &a, &b, &c, &d, &bits); |
---|
| 41 | prefix=(a<<24) + (b<<16) + (c<<8) + d; |
---|
| 42 | assert(bits>=0 && bits<=32); |
---|
| 43 | netmask = masks[bits]; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | static uint32_t prefix_substitute(uint32_t ip) |
---|
| 47 | { |
---|
| 48 | return (prefix & netmask) | (ip & ~netmask); |
---|
| 49 | } |
---|
| 50 | |
---|
[a8f2692] | 51 | void enc_init(enum enc_type_t type, char *key) |
---|
[49f147b] | 52 | { |
---|
[a8f2692] | 53 | char cryptopan_key[32]; |
---|
[49f147b] | 54 | memset(cryptopan_key,0,sizeof(cryptopan_key)); |
---|
| 55 | enc_type = type; |
---|
| 56 | switch (enc_type) { |
---|
| 57 | case ENC_NONE: |
---|
| 58 | break; |
---|
| 59 | case ENC_PREFIX_SUBSTITUTION: |
---|
| 60 | init_prefix(key); |
---|
| 61 | break; |
---|
| 62 | case ENC_CRYPTOPAN: |
---|
| 63 | strlcpy(cryptopan_key,key,sizeof(cryptopan_key)); |
---|
| 64 | panon_init(cryptopan_key); |
---|
| 65 | break; |
---|
| 66 | default: |
---|
| 67 | assert(0 /* unknown encryption type */); |
---|
| 68 | _exit(1); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | uint32_t enc_ip(uint32_t orig_addr) |
---|
| 73 | { |
---|
| 74 | switch (enc_type) { |
---|
| 75 | case ENC_NONE: |
---|
| 76 | return orig_addr; |
---|
| 77 | case ENC_PREFIX_SUBSTITUTION: |
---|
| 78 | return prefix_substitute(orig_addr); |
---|
| 79 | case ENC_CRYPTOPAN: |
---|
| 80 | return cpp_anonymize(orig_addr); |
---|
| 81 | default: |
---|
| 82 | assert(0 /* unknown encryption type */); |
---|
| 83 | _exit(1); |
---|
| 84 | } |
---|
| 85 | } |
---|