… | |
… | |
153 | } |
153 | } |
154 | } |
154 | } |
155 | |
155 | |
156 | ////////////////////////////////////////////////////////////////////////////// |
156 | ////////////////////////////////////////////////////////////////////////////// |
157 | |
157 | |
158 | void pkt_queue::put (net_packet *p) |
158 | pkt_queue::pkt_queue (double max_ttl, int max_queue) |
|
|
159 | : max_ttl (max_ttl), max_queue (max_queue) |
159 | { |
160 | { |
160 | if (queue[i]) |
161 | queue = new pkt [max_queue]; |
161 | { |
|
|
162 | delete queue[i]; |
|
|
163 | j = (j + 1) % QUEUEDEPTH; |
|
|
164 | } |
|
|
165 | |
162 | |
166 | queue[i] = p; |
|
|
167 | |
|
|
168 | i = (i + 1) % QUEUEDEPTH; |
|
|
169 | } |
|
|
170 | |
|
|
171 | net_packet *pkt_queue::get () |
|
|
172 | { |
|
|
173 | net_packet *p = queue[j]; |
|
|
174 | |
|
|
175 | if (p) |
|
|
176 | { |
|
|
177 | queue[j] = 0; |
|
|
178 | j = (j + 1) % QUEUEDEPTH; |
|
|
179 | } |
|
|
180 | |
|
|
181 | return p; |
|
|
182 | } |
|
|
183 | |
|
|
184 | pkt_queue::pkt_queue () |
|
|
185 | { |
|
|
186 | memset (queue, 0, sizeof (queue)); |
|
|
187 | i = 0; |
163 | i = 0; |
188 | j = 0; |
164 | j = 0; |
|
|
165 | |
|
|
166 | expire.set<pkt_queue, &pkt_queue::expire_cb> (this); |
189 | } |
167 | } |
190 | |
168 | |
191 | pkt_queue::~pkt_queue () |
169 | pkt_queue::~pkt_queue () |
192 | { |
170 | { |
193 | for (i = QUEUEDEPTH; --i > 0; ) |
171 | while (net_packet *p = get ()) |
|
|
172 | delete p; |
|
|
173 | |
194 | delete queue[i]; |
174 | delete [] queue; |
|
|
175 | } |
|
|
176 | |
|
|
177 | void pkt_queue::expire_cb (ev::timer &w, int revents) |
|
|
178 | { |
|
|
179 | ev_tstamp expire = ev_now () - max_ttl; |
|
|
180 | |
|
|
181 | for (;;) |
|
|
182 | { |
|
|
183 | if (empty ()) |
|
|
184 | break; |
|
|
185 | |
|
|
186 | double diff = queue[j].tstamp - expire; |
|
|
187 | |
|
|
188 | if (diff >= 0.) |
|
|
189 | { |
|
|
190 | w.start (diff > 0.5 ? diff : 0.5); |
|
|
191 | break; |
|
|
192 | } |
|
|
193 | |
|
|
194 | delete get (); |
|
|
195 | } |
|
|
196 | } |
|
|
197 | |
|
|
198 | void pkt_queue::put (net_packet *p) |
|
|
199 | { |
|
|
200 | ev_tstamp now = ev_now (); |
|
|
201 | |
|
|
202 | // start expiry timer |
|
|
203 | if (empty ()) |
|
|
204 | expire.start (max_ttl); |
|
|
205 | |
|
|
206 | int ni = i + 1 == max_queue ? 0 : i + 1; |
|
|
207 | |
|
|
208 | if (ni == j) |
|
|
209 | delete get (); |
|
|
210 | |
|
|
211 | queue[i].pkt = p; |
|
|
212 | queue[i].tstamp = now; |
|
|
213 | |
|
|
214 | i = ni; |
|
|
215 | } |
|
|
216 | |
|
|
217 | net_packet *pkt_queue::get () |
|
|
218 | { |
|
|
219 | if (empty ()) |
|
|
220 | return 0; |
|
|
221 | |
|
|
222 | net_packet *p = queue[j].pkt; |
|
|
223 | queue[j].pkt = 0; |
|
|
224 | |
|
|
225 | j = j + 1 == max_queue ? 0 : j + 1; |
|
|
226 | |
|
|
227 | return p; |
195 | } |
228 | } |
196 | |
229 | |
197 | struct net_rateinfo |
230 | struct net_rateinfo |
198 | { |
231 | { |
199 | u32 host; |
232 | u32 host; |
… | |
… | |
590 | { |
623 | { |
591 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
624 | slog (L_TRACE, _("%s: possible connection establish (ictx %d, octx %d)"), conf->nodename, !!ictx, !!octx); |
592 | |
625 | |
593 | if (ictx && octx) |
626 | if (ictx && octx) |
594 | { |
627 | { |
595 | connectmode = conf->connectmode; |
|
|
596 | |
|
|
597 | // make sure rekeying timeouts are slightly asymmetric |
628 | // make sure rekeying timeouts are slightly asymmetric |
598 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
629 | ev::tstamp rekey_interval = ::conf.rekey + (conf->id > THISNODE->id ? 10 : 0); |
599 | rekey.start (rekey_interval, rekey_interval); |
630 | rekey.start (rekey_interval, rekey_interval); |
600 | keepalive.start (::conf.keepalive); |
631 | keepalive.start (::conf.keepalive); |
601 | |
632 | |
602 | // send queued packets |
633 | // send queued packets |
603 | if (ictx && octx) |
634 | if (ictx && octx) |
604 | { |
635 | { |
605 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
636 | while (tap_packet *p = (tap_packet *)data_queue.get ()) |
606 | { |
637 | { |
607 | send_data_packet (p); |
638 | if (p->len) send_data_packet (p); |
608 | delete p; |
639 | delete p; |
609 | } |
640 | } |
610 | |
641 | |
611 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
642 | while (vpn_packet *p = (vpn_packet *)vpn_queue.get ()) |
612 | { |
643 | { |
613 | send_vpn_packet (p, si, IPTOS_RELIABILITY); |
644 | if (p->len) send_vpn_packet (p, si, IPTOS_RELIABILITY); |
614 | delete p; |
645 | delete p; |
615 | } |
646 | } |
616 | } |
647 | } |
617 | } |
648 | } |
618 | else |
649 | else |
… | |
… | |
754 | && conf != THISNODE |
785 | && conf != THISNODE |
755 | && connectmode != conf_node::C_NEVER |
786 | && connectmode != conf_node::C_NEVER |
756 | && connectmode != conf_node::C_DISABLED |
787 | && connectmode != conf_node::C_DISABLED |
757 | && !w.is_active ()) |
788 | && !w.is_active ()) |
758 | { |
789 | { |
|
|
790 | // a bit hacky, if ondemand, and packets are no longer queued, then reset the connection |
|
|
791 | // and stop trying. should probably be handled by a per-connection expire handler. |
|
|
792 | if (connectmode == conf_node::C_ONDEMAND && vpn_queue.empty () && data_queue.empty ()) |
|
|
793 | { |
|
|
794 | reset_connection (); |
|
|
795 | return; |
|
|
796 | } |
|
|
797 | |
759 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
798 | ev::tstamp retry_int = ev::tstamp (retry_cnt & 3 |
760 | ? (retry_cnt & 3) + 1 |
799 | ? (retry_cnt & 3) + 1 |
761 | : 1 << (retry_cnt >> 2)); |
800 | : 1 << (retry_cnt >> 2)); |
762 | |
801 | |
763 | reset_si (); |
802 | reset_si (); |
… | |
… | |
1265 | |
1304 | |
1266 | return filename; |
1305 | return filename; |
1267 | } |
1306 | } |
1268 | |
1307 | |
1269 | connection::connection (struct vpn *vpn, conf_node *conf) |
1308 | connection::connection (struct vpn *vpn, conf_node *conf) |
1270 | : vpn(vpn), conf(conf) |
1309 | : vpn(vpn), conf(conf), |
1271 | #if ENABLE_DNS |
1310 | #if ENABLE_DNS |
1272 | , dns (0) |
1311 | dns (0), |
1273 | #endif |
1312 | #endif |
|
|
1313 | data_queue(conf->max_ttl, conf->max_queue), |
|
|
1314 | vpn_queue(conf->max_ttl, conf->max_queue) |
1274 | { |
1315 | { |
1275 | rekey .set<connection, &connection::rekey_cb > (this); |
1316 | rekey .set<connection, &connection::rekey_cb > (this); |
1276 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1317 | keepalive .set<connection, &connection::keepalive_cb > (this); |
1277 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1318 | establish_connection.set<connection, &connection::establish_connection_cb> (this); |
1278 | |
1319 | |
… | |
… | |
1280 | retry_cnt = 0; |
1321 | retry_cnt = 0; |
1281 | |
1322 | |
1282 | if (!conf->protocols) // make sure some protocol is enabled |
1323 | if (!conf->protocols) // make sure some protocol is enabled |
1283 | conf->protocols = PROT_UDPv4; |
1324 | conf->protocols = PROT_UDPv4; |
1284 | |
1325 | |
1285 | connectmode = conf_node::C_ALWAYS; // initial setting |
1326 | connectmode = conf->connectmode; |
|
|
1327 | |
|
|
1328 | // queue a dummy packet to force an initial connection attempt |
|
|
1329 | if (connectmode != conf_node::C_ALWAYS && connectmode != conf_node::C_DISABLED) |
|
|
1330 | { |
|
|
1331 | net_packet *p = new net_packet; |
|
|
1332 | p->len = 0; |
|
|
1333 | vpn_queue.put (p); |
|
|
1334 | } |
|
|
1335 | |
1286 | reset_connection (); |
1336 | reset_connection (); |
1287 | } |
1337 | } |
1288 | |
1338 | |
1289 | connection::~connection () |
1339 | connection::~connection () |
1290 | { |
1340 | { |