2 #include <sys/socket.h>
5 #include <netinet/in.h>
16 static uint8_t toeplitz_key[KEYLEN];
17 static uint32_t hash_table[HASHLEN][256];
19 static void toeplitz_init(uint32_t[][256], int, const uint8_t[], int);
20 static void getaddrport(char *, uint32_t *, uint16_t *);
23 usage(const char *cmd)
25 fprintf(stderr, "%s [-s s1_hex [-s s2_hex]] [-p] [-m mask] "
26 "addr1.port1 addr2.port2\n", cmd);
31 main(int argc, char *argv[])
33 uint32_t saddr, daddr;
34 uint16_t sport, dport;
37 const char *cmd = argv[0];
38 uint8_t seeds[2] = { 0x6d, 0x5a };
45 while ((opt = getopt(argc, argv, "s:pm:")) != -1) {
50 seeds[i++] = strtoul(optarg, NULL, 16);
58 mask = strtoul(optarg, NULL, 16);
71 for (i = 0; i < KEYLEN; ++i) {
73 toeplitz_key[i] = seeds[1];
75 toeplitz_key[i] = seeds[0];
78 getaddrport(argv[0], &saddr, &sport);
79 getaddrport(argv[1], &daddr, &dport);
81 toeplitz_init(hash_table, HASHLEN, toeplitz_key, KEYLEN);
83 res = hash_table[0][(saddr >> 0) & 0xff];
84 res ^= hash_table[1][(saddr >> 8) & 0xff];
85 res ^= hash_table[2][(saddr >> 16) & 0xff];
86 res ^= hash_table[3][(saddr >> 24) & 0xff];
87 res ^= hash_table[4][(daddr >> 0) & 0xff];
88 res ^= hash_table[5][(daddr >> 8) & 0xff];
89 res ^= hash_table[6][(daddr >> 16) & 0xff];
90 res ^= hash_table[7][(daddr >> 24) & 0xff];
92 res ^= hash_table[8][(sport >> 0) & 0xff];
93 res ^= hash_table[9][(sport >> 8) & 0xff];
94 res ^= hash_table[10][(dport >> 0) & 0xff];
95 res ^= hash_table[11][(dport >> 8) & 0xff];
98 printf("0x%08x, masked 0x%08x\n", res, res & mask);
103 toeplitz_init(uint32_t cache[][256], int cache_len,
104 const uint8_t key_str[], int key_strlen)
108 if (key_strlen < cache_len + (int)sizeof(uint32_t))
111 for (i = 0; i < cache_len; ++i) {
113 int j, b, shift, val;
115 bzero(key, sizeof(key));
118 * Calculate 32bit keys for one byte; one key for each bit.
120 for (b = 0; b < NBBY; ++b) {
121 for (j = 0; j < 32; ++j) {
125 bit = (i * NBBY) + b + j;
127 k = key_str[bit / NBBY];
128 shift = NBBY - (bit % NBBY) - 1;
129 if (k & (1 << shift))
130 key[b] |= 1 << (31 - j);
135 * Cache the results of all possible bit combination of
138 for (val = 0; val < 256; ++val) {
141 for (b = 0; b < NBBY; ++b) {
142 shift = NBBY - b - 1;
143 if (val & (1 << shift))
152 getaddrport(char *ap_str, uint32_t *addr, uint16_t *port0)
157 p = strrchr(ap_str, '.');
159 fprintf(stderr, "invalid addr.port %s\n", ap_str);
166 port = strtoul(p, NULL, 10);
167 *port0 = htons(port);
169 if (inet_pton(AF_INET, ap_str, addr) <= 0) {
170 fprintf(stderr, "invalid addr %s\n", ap_str);