protocol_misc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. protocol_misc.c -- handle the meta-protocol, miscellaneous functions
  3. Copyright (C) 1999-2005 Ivo Timmermans,
  4. 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "conf.h"
  19. #include "connection.h"
  20. #include "logger.h"
  21. #include "meta.h"
  22. #include "net.h"
  23. #include "netutl.h"
  24. #include "protocol.h"
  25. #include "utils.h"
  26. int maxoutbufsize = 0;
  27. bool send_ping(connection_t *c) {
  28. c->status.pinged = true;
  29. c->last_ping_time = now;
  30. return send_request(c, "%d", PING);
  31. }
  32. bool ping_h(connection_t *c) {
  33. return send_pong(c);
  34. }
  35. bool send_pong(connection_t *c) {
  36. return send_request(c, "%d", PONG);
  37. }
  38. bool pong_h(connection_t *c) {
  39. c->status.pinged = false;
  40. /* Successful connection, reset timeout if this is an outgoing connection. */
  41. if(c->outgoing) {
  42. c->outgoing->timeout = 0;
  43. c->outgoing->cfg = NULL;
  44. if(c->outgoing->ai) {
  45. freeaddrinfo(c->outgoing->ai);
  46. }
  47. c->outgoing->ai = NULL;
  48. c->outgoing->aip = NULL;
  49. }
  50. return true;
  51. }
  52. /* Sending and receiving packets via TCP */
  53. bool send_tcppacket(connection_t *c, const vpn_packet_t *packet) {
  54. /* If there already is a lot of data in the outbuf buffer, discard this packet.
  55. We use a very simple Random Early Drop algorithm. */
  56. if(2.0 * c->outbuflen / (float)maxoutbufsize - 1 > (float)rand() / (float)RAND_MAX) {
  57. return true;
  58. }
  59. if(!send_request(c, "%d %hd", PACKET, packet->len)) {
  60. return false;
  61. }
  62. return send_meta(c, (char *)packet->data, packet->len) && flush_meta(c);
  63. }
  64. bool tcppacket_h(connection_t *c) {
  65. length_t len;
  66. if(sscanf(c->buffer, "%*d %hu", &len) != 1) {
  67. logger(LOG_ERR, "Got bad %s from %s (%s)", "PACKET", c->name,
  68. c->hostname);
  69. return false;
  70. }
  71. /* Set reqlen to len, this will tell receive_meta() that a tcppacket is coming. */
  72. c->tcplen = len;
  73. return true;
  74. }