forked from libswift/libswift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.cpp
673 lines (582 loc) · 20.5 KB
/
channel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* channel.cpp
* class representing a virtual connection to a peer. In addition,
* it contains generic functions for socket management (see sock_open
* class variable)
*
* Created by Victor Grishchenko on 3/6/09.
* Copyright 2009-2016 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
*
*/
#include <cassert>
#include "compat.h"
#include "swift.h"
#include "bin_utils.h"
using namespace std;
using namespace swift;
/*
* Class variables
*/
swift::tint now_t::now = Channel::Time();
tint Channel::start = now_t::now;
tint Channel::epoch = now_t::now/360000000LL*360000000LL; // make logs mergeable
uint64_t Channel::global_dgrams_up=0, Channel::global_dgrams_down=0,
Channel::global_raw_bytes_up=0, Channel::global_raw_bytes_down=0,
Channel::global_bytes_up=0, Channel::global_bytes_down=0;
sckrwecb_t Channel::sock_open[] = {};
int Channel::sock_count = 0;
swift::tint Channel::last_tick = 0;
int Channel::MAX_REORDERING = 4;
bool Channel::SELF_CONN_OK = false;
swift::tint Channel::TIMEOUT = TINT_SEC*60;
channels_t Channel::channels(1);
Address Channel::tracker;
FILE* Channel::debug_file = NULL;
tint Channel::MIN_PEX_REQUEST_INTERVAL = TINT_SEC;
Socks5Connection Channel::socks5_connection;
/*
* Instance methods
*/
Channel::Channel(ContentTransfer* transfer, int socket, Address peer_addr,bool peerissource) :
// Arno, 2011-10-03: Reordered to avoid g++ Wall warning
peer_(peer_addr), socket_(socket==INVALID_SOCKET?default_socket():socket), // FIXME
transfer_(transfer), own_id_mentioned_(false),
data_in_(TINT_NEVER,bin_t::NONE), data_in_dbl_(bin_t::NONE),
data_out_cap_(bin_t::ALL),hint_in_size_(0), hint_out_size_(0),
// Gertjan fix 996e21e8abfc7d88db3f3f8158f2a2c4fc8a8d3f
// "Changed PEX rate limiting to per channel limiting"
pex_requested_(false), // Ric: init var that wasn't initialiazed
last_pex_request_time_(0), next_pex_request_time_(0),
pex_request_outstanding_(false),
useless_pex_count_(0),
rtt_avg_(TINT_SEC), dev_avg_(0), dip_avg_(TINT_SEC),
last_send_time_(0), last_recv_time_(0), last_data_out_time_(0), last_data_in_time_(0),
last_loss_time_(0), next_send_time_(0), open_time_(NOW), cwnd_(1),
cwnd_count1_(0), send_interval_(TINT_SEC),
send_control_(PING_PONG_CONTROL), sent_since_recv_(0),
lastrecvwaskeepalive_(false), lastsendwaskeepalive_(false), // Arno: nap bug fix
live_have_no_hint_(false), // Arno: live speed opt
ack_rcvd_recent_(0),
ack_not_rcvd_recent_(0), owd_min_bin_(0), owd_min_bin_start_(NOW),
owd_cur_bin_(0), dgrams_sent_(0), dgrams_rcvd_(0),
raw_bytes_up_(0), raw_bytes_down_(0), bytes_up_(0), bytes_down_(0),
scheduled4del_(false),
direct_sending_(false),
peer_is_source_(peerissource),
hs_out_(NULL), hs_in_(NULL),
rtt_hint_tintbin_()
{
if (peer_==Address())
peer_ = tracker;
this->id_ = channels.size();
channels.push_back(this);
for(int i=0; i<4; i++) {
owd_min_bins_[i] = TINT_NEVER;
owd_current_[i] = TINT_NEVER;
}
evsend_ptr_ = new struct event;
evtimer_assign(evsend_ptr_,evbase,&Channel::LibeventSendCallback,this);
evtimer_add(evsend_ptr_,tint2tv(next_send_time_));
//LIVE
evsendlive_ptr_ = NULL;
// RATELIMIT
transfer_->GetChannels()->push_back(this);
hs_out_ = new Handshake();
if (transfer_->ttype() == FILE_TRANSFER)
hs_out_->cont_int_prot_ = POPT_CONT_INT_PROT_MERKLE;
else
hs_out_->cont_int_prot_ = POPT_CONT_INT_PROT_NONE; // PPSPTODO implement live schemes
dprintf("%s #%u init channel %s transfer %d\n",tintstr(),id_,peer_.str().c_str(), transfer_->td() );
//fprintf(stderr,"new Channel %d %s\n", id_, peer_.str().c_str() );
}
Channel::~Channel () {
dprintf("%s #%u dealloc channel\n",tintstr(),id_);
channels[id_] = NULL;
ClearEvents();
// RATELIMIT
if (transfer_ != NULL)
{
channels_t::iterator iter;
channels_t *channels = transfer_->GetChannels();
for (iter=channels->begin(); iter!=channels->end(); iter++)
{
if (*iter == this)
break;
}
channels->erase(iter);
}
if (hs_in_ != NULL)
delete hs_in_;
if (hs_out_ != NULL)
delete hs_out_;
}
void Channel::ClearEvents()
{
// Arno, 2013-02-01: Be safer, _del not just on pending.
if (evsend_ptr_ != NULL)
{
evtimer_del(evsend_ptr_);
delete evsend_ptr_;
evsend_ptr_ = NULL;
}
if (evsendlive_ptr_ != NULL)
{
evtimer_del(evsendlive_ptr_);
delete evsendlive_ptr_;
evsendlive_ptr_ = NULL;
}
}
HashTree * Channel::hashtree()
{
if (transfer()->ttype() == LIVE_TRANSFER)
return NULL;
else
return ((FileTransfer *)transfer_)->hashtree();
}
bool Channel::IsComplete() {
if (transfer()->ttype() == LIVE_TRANSFER)
return peer_is_source_;
// Check if peak hash bins are filled.
if (hashtree()->peak_count() == 0)
return false;
for(int i=0; i<hashtree()->peak_count(); i++) {
bin_t peak = hashtree()->peak(i);
if (!ack_in_.is_filled(peak))
return false;
}
return true;
}
uint16_t Channel::GetMyPort() {
Address addr;
// Arno, 2013-06-05: Retrieving addr, so use largest possible sockaddr
socklen_t addrlen = sizeof(struct sockaddr_storage);
if (getsockname(socket_, (struct sockaddr *)&addr.addr, &addrlen) < 0)
{
print_error("error on getsockname");
return 0;
}
else
return addr.port();
}
bool Channel::IsDiffSenderOrDuplicate(Address addr, uint32_t chid)
{
if (peer() != addr)
{
// Got message from different address than I send to
//
if (!own_id_mentioned_ && addr.is_private()) {
// Arno, 2012-02-27: Got HANDSHAKE reply from IANA private address,
// check for duplicate connections:
//
// When two peers A and B are behind the same firewall, they will get
// extB, resp. extA addresses from the tracker. They will both
// connect to their counterpart but because the incoming packet
// will be from the intNAT address the duplicates are not
// recognized.
//
// Solution: when the second datagram comes in (HANDSHAKE reply),
// see if you have had a first datagram from the same addr
// (HANDSHAKE). If so, close the channel if his port number is
// larger than yours (such that one channel remains).
//
recv_peer_ = addr;
Channel *c = transfer()->FindChannel(addr,this);
if (c == NULL)
return false;
// I already initiated a connection to this peer,
// this new incoming message would establish a duplicate.
// One must break the connection, decide using port
// number:
dprintf("%s #%u found duplicate channel to %s\n",
tintstr(),chid,addr.str().c_str());
if (addr.port() > GetMyPort()) {
dprintf("%s #%u closing duplicate channel to %s\n",
tintstr(),chid,addr.str().c_str());
return true;
}
}
else
{
// Received HANDSHAKE reply from other address than I sent
// HANDSHAKE to, and the address is not an IANA private
// address (=no NAT in play), so close.
dprintf("%s #%u invalid peer address %s!=%s\n",
tintstr(),chid,peer().str().c_str(),addr.str().c_str());
return true;
}
}
return false;
}
/*
* Class methods
*/
tint Channel::Time () {
//HiResTimeOfDay* tod = HiResTimeOfDay::Instance();
//tint ret = tod->getTimeUSec();
//DLOG(INFO)<<"now is "<<ret;
return now_t::now = usec_time();
}
// SOCKMGMT
evutil_socket_t Channel::Bind (Address address, sckrwecb_t callbacks) {
struct sockaddr_storage sa = address;
evutil_socket_t fd;
// Arno, 2013-06-05: MacOS X bind fails if sizeof(struct sockaddr_storage) is passed.
int len = address.get_real_sockaddr_length(), sndbuf=1<<20, rcvbuf=1<<20;
#define dbnd_ensure(x) { if (!(x)) { \
print_error("binding fails"); close_socket(fd); return INVALID_SOCKET; } }
dbnd_ensure ( (fd = socket(address.get_family(), SOCK_DGRAM, 0)) >= 0 );
dbnd_ensure( make_socket_nonblocking(fd) ); // FIXME may remove this
int enable = true;
dbnd_ensure ( setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
(setsockoptptr_t)&sndbuf, sizeof(int)) == 0 );
dbnd_ensure ( setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
(setsockoptptr_t)&rcvbuf, sizeof(int)) == 0 );
//setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (setsockoptptr_t)&enable, sizeof(int));
if (address.get_family() == AF_INET6)
{
// Arno, 2012-12-04: Enable IPv4 on this IPv6 socket, addresses
// show up as IPv4-mapped IPv6.
int no = 0;
dbnd_ensure ( setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (setsockoptptr_t)&no, sizeof(no)) == 0 );
}
dbnd_ensure ( ::bind(fd, (sockaddr*)&sa, len) == 0 );
callbacks.sock = fd;
sock_open[sock_count++] = callbacks;
return fd;
}
Address Channel::BoundAddress(evutil_socket_t sock) {
struct sockaddr_storage myaddr;
// Arno, 2013-06-05: Retrieving addr, so use largest possible sockaddr
socklen_t mylen = sizeof(struct sockaddr_storage);
int ret = getsockname(sock,(sockaddr*)&myaddr,&mylen);
if (ret >= 0) {
return Address(myaddr);
}
else {
return Address();
}
}
Address swift::BoundAddress(evutil_socket_t sock) {
return Channel::BoundAddress(sock);
}
int Channel::SendTo (evutil_socket_t sock, const Address& addr, struct evbuffer *evb) {
Address destination = addr;
int lengthLoss = 0;
// If we have a Socks5Connection use it to tunnel data through!
if(Channel::socks5_connection.isOpen()){
lengthLoss = Channel::socks5_connection.prependHeader(addr, evb);
destination = Channel::socks5_connection.getBindAddress();
} else if(Channel::socks5_connection.getBindAddress() != Address()){
printf("ERROR: Sending to %s:%d WITHOUT socks5\n", destination.ipstr(false).c_str(), destination.port());
}
int length = evbuffer_get_length(evb);
int r = sendto(sock,(const char *)evbuffer_pullup(evb, length),length,0,
(struct sockaddr*)&(destination.addr),destination.get_real_sockaddr_length());
// SCHAAP: 2012-06-16 - How about EAGAIN and EWOULDBLOCK? Do we just drop the packet then as well?
if (r<0) {
print_error("can't send");
evbuffer_drain(evb, length); // Arno: behaviour is to pretend the packet got lost
}
else
evbuffer_drain(evb,r);
global_dgrams_up++;
global_raw_bytes_up+=length - lengthLoss;
Time();
return r;
}
int Channel::RecvFrom (evutil_socket_t sock, Address& addr, struct evbuffer *evb) {
// Arno, 2013-06-05: Incoming addr, so use largest possible sockaddr
socklen_t addrlen = sizeof(struct sockaddr_storage);
struct evbuffer_iovec vec;
if (evbuffer_reserve_space(evb, SWIFT_MAX_RECV_DGRAM_SIZE, &vec, 1) < 0) {
print_error("error on evbuffer_reserve_space");
return 0;
}
int length = recvfrom (sock, (char *)vec.iov_base, SWIFT_MAX_RECV_DGRAM_SIZE, 0,
(struct sockaddr*)&(addr.addr), &addrlen);
if (length<0) {
length = 0;
// Linux and Windows report "ICMP port unreachable" if the dest port could
// not be reached:
// http://support.microsoft.com/kb/260018
// http://www.faqs.org/faqs/unix-faq/socket/
#ifdef _WIN32
if (WSAGetLastError() == 10054) // Sometimes errno == 2 ?!
#else
if (errno == ECONNREFUSED)
#endif
{
CloseChannelByAddress(addr);
}
else
print_error("error on recv");
}
vec.iov_len = length;
if (evbuffer_commit_space(evb, &vec, 1) < 0) {
length = 0;
print_error("error on evbuffer_commit_space");
}
global_dgrams_down++;
// If there is a SOCKS5 connection set we need to unwrap the packet!
if(addr == Channel::socks5_connection.getBindAddress()){
int result = Channel::socks5_connection.unwrapDatagram(addr, evb);
// printf("Got SOCKS5 packet from %s:%d via proxy at %s:%d\n", addr.ipstr().c_str(), addr.port(), Channel::socks5_connection.getBindAddress().ipstr().c_str(), Channel::socks5_connection.getBindAddress().port());
if(result > -1){
length -= result;
// printf("Got %d byte packet from %s:%d via SOCKS5 proxy at %s:%d\n", length, addr.ipv4str(), addr.port(), socks5_connection.getBindAddress().ipv4str(), socks5_connection.getBindAddress().port());
} else{
printf("Got corrupted SOCKS5 packet from %s:%d\n", socks5_connection.getBindAddress().ipstr(false).c_str(), socks5_connection.getBindAddress().port());
}
} else if(Channel::socks5_connection.getBindAddress() != Address()){
printf("Got %d bytes NON SOCKS5 from %s:%d\n", length, addr.ipstr(false).c_str(), addr.port());
}
global_raw_bytes_down+=length;
Time();
return length;
}
void swift::SetSocks5Connection(const Socks5Connection& tracker) {
Channel::socks5_connection = tracker;
}
void Channel::CloseSocket(evutil_socket_t sock) {
for(int i=0; i<sock_count; i++)
if (sock_open[i].sock==sock)
sock_open[i] = sock_open[--sock_count];
if (!close_socket(sock))
print_error("on closing a socket");
}
void Channel::Shutdown () {
while (sock_count--)
CloseSocket(sock_open[sock_count].sock);
}
void swift::SetTracker(const Address& tracker) {
Channel::tracker = tracker;
}
int Channel::DecodeID(int scrambled) {
return scrambled ^ (int)start;
}
int Channel::EncodeID(int unscrambled) {
return unscrambled ^ (int)start;
}
/*
* Utility methods
*/
const char* swift::tintstr (tint time) {
if (time==0)
time = now_t::now;
static char ret_str[4][32]; // wow
static int i;
i = (i+1) & 3;
if (time==TINT_NEVER)
return "NEVER";
time -= Channel::epoch;
assert(time>=0);
int hours = time/TINT_HOUR;
time %= TINT_HOUR;
int mins = time/TINT_MIN;
time %= TINT_MIN;
int secs = time/TINT_SEC;
time %= TINT_SEC;
int msecs = time/TINT_MSEC;
time %= TINT_MSEC;
int usecs = time/TINT_uSEC;
sprintf(ret_str[i],"%i_%02i_%02i_%03i_%03i",hours,mins,secs,msecs,usecs);
return ret_str[i];
}
int swift::evbuffer_add_string(struct evbuffer *evb, std::string str) {
return evbuffer_add(evb, str.c_str(), str.size());
}
int swift::evbuffer_add_8(struct evbuffer *evb, uint8_t b) {
return evbuffer_add(evb, &b, 1);
}
int swift::evbuffer_add_16be(struct evbuffer *evb, uint16_t w) {
uint16_t wbe = htons(w);
return evbuffer_add(evb, &wbe, 2);
}
int swift::evbuffer_add_32be(struct evbuffer *evb, uint32_t i) {
uint32_t ibe = htonl(i);
return evbuffer_add(evb, &ibe, 4);
}
int swift::evbuffer_add_64be(struct evbuffer *evb, uint64_t l) {
uint32_t lbe[2];
lbe[0] = htonl((uint32_t)(l>>32));
lbe[1] = htonl((uint32_t)(l&0xffffffff));
return evbuffer_add(evb, lbe, 8);
}
int swift::evbuffer_add_hash(struct evbuffer *evb, const Sha1Hash& hash) {
return evbuffer_add(evb, hash.bits, Sha1Hash::SIZE);
}
// PPSP
int swift::evbuffer_add_chunkaddr(struct evbuffer *evb, bin_t &b, popt_chunk_addr_t chunk_addr)
{
int ret = -1;
if (chunk_addr == POPT_CHUNK_ADDR_BIN32)
ret = evbuffer_add_32be(evb, bin_toUInt32(b));
else if (chunk_addr == POPT_CHUNK_ADDR_CHUNK32)
{
ret = evbuffer_add_32be(evb, (uint32_t)b.base_offset() );
ret = evbuffer_add_32be(evb, (uint32_t)(b.base_offset()+b.base_length()-1) ); // end is inclusive
}
return ret;
}
int swift::evbuffer_add_pexaddr(struct evbuffer *evb, Address& a)
{
int ret = -1;
if (a.get_family() == AF_INET)
{
ret = evbuffer_add_8(evb, SWIFT_PEX_RESv4);
ret = evbuffer_add_32be(evb, a.ipv4());
ret = evbuffer_add_16be(evb, a.port());
}
else
{
struct in6_addr ipv6 = a.ipv6();
ret = evbuffer_add_8(evb, SWIFT_PEX_RESv6);
for (int i=0; i<16; i++)
ret = evbuffer_add_8(evb, ipv6.s6_addr[i] );
ret = evbuffer_add_16be(evb, a.port());
}
return ret;
}
uint8_t swift::evbuffer_remove_8(struct evbuffer *evb) {
uint8_t b;
if (evbuffer_remove(evb, &b, 1) < 1)
return 0;
return b;
}
uint16_t swift::evbuffer_remove_16be(struct evbuffer *evb) {
uint16_t wbe;
if (evbuffer_remove(evb, &wbe, 2) < 2)
return 0;
return ntohs(wbe);
}
uint32_t swift::evbuffer_remove_32be(struct evbuffer *evb) {
uint32_t ibe;
if (evbuffer_remove(evb, &ibe, 4) < 4)
return 0;
return ntohl(ibe);
}
uint64_t swift::evbuffer_remove_64be(struct evbuffer *evb) {
uint32_t lbe[2];
if (evbuffer_remove(evb, lbe, 8) < 8)
return 0;
uint64_t l = ntohl(lbe[0]);
l<<=32;
l |= ntohl(lbe[1]);
return l;
}
Sha1Hash swift::evbuffer_remove_hash(struct evbuffer* evb) {
char bits[Sha1Hash::SIZE];
if (evbuffer_remove(evb, bits, Sha1Hash::SIZE) < Sha1Hash::SIZE)
return Sha1Hash::ZERO;
return Sha1Hash(false, bits);
}
// PPSP
binvector swift::evbuffer_remove_chunkaddr(struct evbuffer *evb, popt_chunk_addr_t chunk_addr)
{
binvector bv;
if (chunk_addr == POPT_CHUNK_ADDR_BIN32)
{
bin_t pos = bin_fromUInt32(evbuffer_remove_32be(evb));
bv.push_back(pos);
}
else if (chunk_addr == POPT_CHUNK_ADDR_CHUNK32)
{
uint32_t schunk = evbuffer_remove_32be(evb);
uint32_t echunk = evbuffer_remove_32be(evb);
if (schunk <= echunk) // Bad input protection
swift::chunk32_to_bin32(schunk,echunk,&bv);
}
return bv;
}
Address swift::evbuffer_remove_pexaddr(struct evbuffer *evb, int family)
{
int ret = -1;
if (family == AF_INET)
{
uint32_t ipv4 = evbuffer_remove_32be(evb);
uint16_t port = evbuffer_remove_16be(evb);
Address addr(ipv4,port);
return addr;
}
else
{
struct in6_addr ipv6;
for (int i=0; i<16; i++)
ipv6.s6_addr[i] = evbuffer_remove_8(evb);
uint16_t port = evbuffer_remove_16be(evb);
Address addr(ipv6,port);
return addr;
}
}
/** Convert a chunk32 chunk specification to a list of bins. A chunk32 spec is
* a (start chunk ID, end chunk ID) pair, where chunk ID is just a numbering
* from 0 to N of all chunks, equivalent to the leaves in a bin tree. This
* method finds which bins describe this range.
*/
void swift::chunk32_to_bin32(uint32_t schunk, uint32_t echunk, binvector *bvptr)
{
bin_t s(0,schunk);
bin_t e(0,echunk);
bin_t cur = s;
while (true)
{
// Move up in tree till we exceed either start or end. If so, the
// previous node belongs to the range description. Next, we start at
// the left most chunk in the subtree next to the previous node, and see
// how far up we can go there.
//fprintf(stderr,"\ncur %s par left %s par right %s\n", cur.str().c_str(), cur.parent().base_left().str().c_str(), cur.parent().base_right().str().c_str());
if (cur.parent().base_left() < s || cur.parent().base_right() > e)
{
/*if (cur.parent().base_left() < s)
fprintf(stderr,"parent %s left %s before s, add %s\n", cur.parent().str().c_str(), cur.parent().base_left().str().c_str(), cur.str().c_str() );
if (cur.parent().base_right() > e)
fprintf(stderr,"parent %s right %s exceeds e, add %s\n", cur.parent().str().c_str(), cur.parent().base_right().str().c_str(), cur.str().c_str() );
*/
bvptr->push_back(cur);
if (cur.parent().base_left() < s)
cur = bin_t(0,cur.parent().base_right().layer_offset()+1);
else
cur = bin_t(0,cur.base_right().layer_offset()+1);
//fprintf(stderr,"newcur %s\n", cur.str().c_str() );
if (cur >= e)
{
if (cur == e)
{
// fprintf(stderr,"adding e %s\n", cur.str().c_str() );
bvptr->push_back(e);
}
break;
}
}
else
cur = cur.parent();
}
}
/*
* Calculate the complement of 2 bins, where origbin covers cancelbin.
* I.e., origbin is turned into a list of base bins that are covered by
* origbin but not by cancelbin.
*/
binvector swift::bin_fragment(bin_t &origbin, bin_t &cancelbin)
{
// origbin covers cancelbin
// Easy: just split into base bins
binvector bv;
bin_t origsbase = origbin.base_left();
bin_t origebase = origbin.base_right();
bin_t cansbase = cancelbin.base_left();
bin_t canebase = cancelbin.base_right();
bin_t curbin = origsbase;
while (curbin < cansbase)
{
bv.push_back(curbin);
curbin = bin_t(0,curbin.base_offset()+1);
}
curbin = bin_t(0,canebase.base_offset()+1);
while (curbin <= origebase)
{
bv.push_back(curbin);
curbin = bin_t(0,curbin.base_offset()+1);
}
return bv;
}