… | |
… | |
70 | #define MAX_SEND_INTERVAL 5. // optimistic? |
70 | #define MAX_SEND_INTERVAL 5. // optimistic? |
71 | |
71 | |
72 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
72 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
73 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
73 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
74 | |
74 | |
75 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
75 | #define MAX_DOMAIN_SIZE 235 // 255 is legal limit, but bind doesn't compress well |
76 | // 240 leaves about 4 bytes of server reply data |
76 | // 240 leaves about 4 bytes of server reply data |
77 | // every request byte less give room for two reply bytes |
77 | // every request byte less give room for two reply bytes |
78 | |
78 | |
79 | #define SEQNO_MASK 0x3fff |
79 | #define SEQNO_MASK 0x3fff |
80 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
80 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
… | |
… | |
146 | assert (size < 256); |
146 | assert (size < 256); |
147 | } |
147 | } |
148 | |
148 | |
149 | #define MAX_DEC_LEN 500 |
149 | #define MAX_DEC_LEN 500 |
150 | #define MAX_ENC_LEN (MAX_DEC_LEN * 2) |
150 | #define MAX_ENC_LEN (MAX_DEC_LEN * 2) |
151 | #define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS) |
151 | #define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS + 1) |
152 | |
152 | |
153 | // ugly. minimum base is 16(!) |
153 | // ugly. minimum base is 16(!) |
154 | struct basecoder |
154 | struct basecoder |
155 | { |
155 | { |
156 | charmap cmap; |
156 | charmap cmap; |
… | |
… | |
167 | }; |
167 | }; |
168 | |
168 | |
169 | basecoder::basecoder (const char *cmap) |
169 | basecoder::basecoder (const char *cmap) |
170 | : cmap (cmap) |
170 | : cmap (cmap) |
171 | { |
171 | { |
|
|
172 | int decn = -1; |
|
|
173 | |
172 | for (unsigned int len = 0; len < MAX_DEC_LEN; ++len) |
174 | for (unsigned int len = 0; len < MAX_DEC_LEN; ++len) |
173 | { |
175 | { |
174 | u8 src [MAX_DEC_LEN]; |
176 | u8 src [MAX_DEC_LEN]; |
175 | u8 dst [MAX_ENC_LEN]; |
177 | u8 dst [MAX_ENC_LEN]; |
176 | |
178 | |
… | |
… | |
180 | mp_size_t n; |
182 | mp_size_t n; |
181 | |
183 | |
182 | n = mpn_set_str (m, src, len, 256); |
184 | n = mpn_set_str (m, src, len, 256); |
183 | n = mpn_get_str (dst, this->cmap.size, m, n); |
185 | n = mpn_get_str (dst, this->cmap.size, m, n); |
184 | |
186 | |
185 | for (int i = 0; !dst [i]; ++i) |
187 | for (int i = 0; n && !dst [i]; ++i, --n) |
186 | n--; |
188 | ; |
187 | |
189 | |
188 | enc_len [len] = n; |
190 | enc_len [len] = n; |
|
|
191 | while (decn < n) |
189 | dec_len [n] = len; |
192 | dec_len [++decn] = len; |
190 | } |
193 | } |
191 | } |
194 | } |
192 | |
195 | |
193 | unsigned int |
196 | unsigned int |
194 | basecoder::encode_len (unsigned int len) const |
197 | basecoder::encode_len (unsigned int len) const |
… | |
… | |
197 | } |
200 | } |
198 | |
201 | |
199 | unsigned int |
202 | unsigned int |
200 | basecoder::decode_len (unsigned int len) const |
203 | basecoder::decode_len (unsigned int len) const |
201 | { |
204 | { |
202 | while (len && !dec_len [len]) |
|
|
203 | --len; |
|
|
204 | |
|
|
205 | return dec_len [len]; |
205 | return dec_len [len]; |
206 | } |
206 | } |
207 | |
207 | |
208 | unsigned int |
208 | unsigned int |
209 | basecoder::encode (char *dst, u8 *src, unsigned int len) const |
209 | basecoder::encode (char *dst, u8 *src, unsigned int len) const |
… | |
… | |
364 | } |
364 | } |
365 | |
365 | |
366 | void |
366 | void |
367 | byte_stream::remove (int count) |
367 | byte_stream::remove (int count) |
368 | { |
368 | { |
369 | if (count > fill) |
|
|
370 | assert (count <= fill); |
369 | assert (count <= fill); |
371 | |
370 | |
372 | memmove (data, data + count, fill -= count); |
371 | memmove (data, data + count, fill -= count); |
373 | } |
372 | } |
374 | |
373 | |
375 | bool |
374 | bool |
… | |
… | |
395 | memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
394 | memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
396 | |
395 | |
397 | return true; |
396 | return true; |
398 | } |
397 | } |
399 | |
398 | |
|
|
399 | vpn_packet * |
400 | vpn_packet *byte_stream::get () |
400 | byte_stream::get () |
401 | { |
401 | { |
402 | unsigned int len; |
402 | unsigned int len; |
403 | |
403 | |
404 | for (;;) |
404 | for (;;) |
405 | { |
405 | { |
|
|
406 | if (fill < 2) |
|
|
407 | return 0; |
|
|
408 | |
406 | len = (data [0] << 8) | data [1]; |
409 | len = (data [0] << 8) | data [1]; |
407 | |
410 | |
408 | if (len <= MAXSIZE || fill < 2) |
411 | if (len <= MAXSIZE) |
409 | break; |
412 | break; |
410 | |
413 | |
411 | // TODO: handle this better than skipping, e.g. by reset |
414 | // TODO: handle this better than skipping, e.g. by reset |
412 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
415 | slog (L_DEBUG, _("DNS: corrupted packet (%02x %02x > %d) stream skipping a byte..."), data [0], data [1], MAXSIZE); |
413 | remove (1); |
416 | remove (1); |
414 | } |
417 | } |
415 | |
418 | |
416 | if (fill < len + 2) |
419 | if (fill < len + 2) |
417 | return 0; |
420 | return 0; |
… | |
… | |
590 | static |
593 | static |
591 | u16 next_id () |
594 | u16 next_id () |
592 | { |
595 | { |
593 | static u16 dns_id = 0; // TODO: should be per-vpn |
596 | static u16 dns_id = 0; // TODO: should be per-vpn |
594 | |
597 | |
|
|
598 | #if 1 |
595 | if (!dns_id) |
599 | if (!dns_id) |
596 | dns_id = time (0); |
600 | dns_id = time (0); |
597 | |
601 | |
598 | // the simplest lsfr with periodicity 65535 i could find |
602 | // the simplest lsfr with periodicity 65535 i could find |
599 | dns_id = (dns_id << 1) |
603 | dns_id = (dns_id << 1) |
… | |
… | |
601 | ^ (dns_id >> 2) |
605 | ^ (dns_id >> 2) |
602 | ^ (dns_id >> 4) |
606 | ^ (dns_id >> 4) |
603 | ^ (dns_id >> 15)) & 1); |
607 | ^ (dns_id >> 15)) & 1); |
604 | |
608 | |
605 | return dns_id; |
609 | return dns_id; |
|
|
610 | #else |
|
|
611 | dns_id++;//D |
|
|
612 | |
|
|
613 | return htons (dns_id); |
|
|
614 | #endif |
606 | } |
615 | } |
607 | |
616 | |
608 | struct dns_rcv; |
617 | struct dns_rcv; |
609 | struct dns_snd; |
618 | struct dns_snd; |
610 | |
619 | |