LineBuffer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * @file LineBuffer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <system/BLog.h>
  25. #include <flow/LineBuffer.h>
  26. #include <generated/blog_channel_LineBuffer.h>
  27. static void input_handler_done (LineBuffer *o, int data_len)
  28. {
  29. DebugObject_Access(&o->d_obj);
  30. ASSERT(data_len > 0)
  31. ASSERT(data_len <= o->buf_size - o->buf_used)
  32. // update buffer
  33. o->buf_used += data_len;
  34. // look for newline
  35. int i;
  36. for (i = o->buf_used - data_len; i < o->buf_used; i++) {
  37. if (o->buf[i] == o->nl_char) {
  38. break;
  39. }
  40. }
  41. if (i < o->buf_used || o->buf_used == o->buf_size) {
  42. if (i == o->buf_used) {
  43. BLog(BLOG_WARNING, "line too long");
  44. }
  45. // pass to output
  46. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  47. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  48. } else {
  49. // receive more data
  50. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  51. }
  52. }
  53. static void output_handler_done (LineBuffer *o)
  54. {
  55. DebugObject_Access(&o->d_obj);
  56. ASSERT(o->buf_consumed > 0)
  57. ASSERT(o->buf_consumed <= o->buf_used)
  58. // update buffer
  59. memmove(o->buf, o->buf + o->buf_consumed, o->buf_used - o->buf_consumed);
  60. o->buf_used -= o->buf_consumed;
  61. // look for newline
  62. int i;
  63. for (i = 0; i < o->buf_used; i++) {
  64. if (o->buf[i] == o->nl_char) {
  65. break;
  66. }
  67. }
  68. if (i < o->buf_used || o->buf_used == o->buf_size) {
  69. // pass to output
  70. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  71. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  72. } else {
  73. // receive more data
  74. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  75. }
  76. }
  77. int LineBuffer_Init (LineBuffer *o, StreamRecvInterface *input, PacketPassInterface *output, int buf_size, uint8_t nl_char)
  78. {
  79. ASSERT(buf_size > 0)
  80. ASSERT(PacketPassInterface_GetMTU(output) >= buf_size)
  81. // init arguments
  82. o->input = input;
  83. o->output = output;
  84. o->buf_size = buf_size;
  85. o->nl_char = nl_char;
  86. // init input
  87. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  88. // init output
  89. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  90. // set buffer empty
  91. o->buf_used = 0;
  92. // allocate buffer
  93. if (!(o->buf = malloc(o->buf_size))) {
  94. BLog(BLOG_ERROR, "malloc failed");
  95. goto fail0;
  96. }
  97. // start receiving
  98. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_size);
  99. DebugObject_Init(&o->d_obj);
  100. return 1;
  101. fail0:
  102. return 0;
  103. }
  104. void LineBuffer_Free (LineBuffer *o)
  105. {
  106. DebugObject_Free(&o->d_obj);
  107. // free buffer
  108. free(o->buf);
  109. }