LineBuffer.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 <flow/LineBuffer.h>
  25. static void input_handler_done (LineBuffer *o, int data_len)
  26. {
  27. DebugObject_Access(&o->d_obj);
  28. ASSERT(data_len > 0)
  29. ASSERT(data_len <= o->buf_size - o->buf_used)
  30. // update buffer
  31. o->buf_used += data_len;
  32. // look for newline
  33. int i;
  34. for (i = o->buf_used - data_len; i < o->buf_used; i++) {
  35. if (o->buf[i] == o->nl_char) {
  36. break;
  37. }
  38. }
  39. if (i < o->buf_used || o->buf_used == o->buf_size) {
  40. // pass to output
  41. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  42. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  43. } else {
  44. // receive more data
  45. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  46. }
  47. }
  48. static void output_handler_done (LineBuffer *o)
  49. {
  50. DebugObject_Access(&o->d_obj);
  51. ASSERT(o->buf_consumed > 0)
  52. ASSERT(o->buf_consumed <= o->buf_used)
  53. // update buffer
  54. memmove(o->buf, o->buf + o->buf_consumed, o->buf_used - o->buf_consumed);
  55. o->buf_used -= o->buf_consumed;
  56. // look for newline
  57. int i;
  58. for (i = 0; i < o->buf_used; i++) {
  59. if (o->buf[i] == o->nl_char) {
  60. break;
  61. }
  62. }
  63. if (i < o->buf_used || o->buf_used == o->buf_size) {
  64. // pass to output
  65. o->buf_consumed = (i < o->buf_used ? i + 1 : i);
  66. PacketPassInterface_Sender_Send(o->output, o->buf, o->buf_consumed);
  67. } else {
  68. // receive more data
  69. StreamRecvInterface_Receiver_Recv(o->input, o->buf + o->buf_used, o->buf_size - o->buf_used);
  70. }
  71. }
  72. int LineBuffer_Init (LineBuffer *o, StreamRecvInterface *input, PacketPassInterface *output, int buf_size, uint8_t nl_char)
  73. {
  74. ASSERT(buf_size > 0)
  75. ASSERT(PacketPassInterface_GetMTU(output) >= buf_size)
  76. // init arguments
  77. o->input = input;
  78. o->output = output;
  79. o->buf_size = buf_size;
  80. o->nl_char = nl_char;
  81. // init input
  82. StreamRecvInterface_Receiver_Init(o->input, (StreamRecvInterface_handler_done)input_handler_done, o);
  83. // init output
  84. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  85. // set buffer empty
  86. o->buf_used = 0;
  87. // allocate buffer
  88. if (!(o->buf = malloc(o->buf_size))) {
  89. DEBUG("malloc failed");
  90. goto fail0;
  91. }
  92. // start receiving
  93. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_size);
  94. DebugObject_Init(&o->d_obj);
  95. return 1;
  96. fail0:
  97. return 0;
  98. }
  99. void LineBuffer_Free (LineBuffer *o)
  100. {
  101. DebugObject_Free(&o->d_obj);
  102. // free buffer
  103. free(o->buf);
  104. }