DatagramSocketSink.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @file DatagramSocketSink.h
  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. * @section DESCRIPTION
  23. *
  24. * A {@link PacketPassInterface} sink which sends packets to a datagram socket.
  25. */
  26. #ifndef BADVPN_FLOW_DATAGRAMSOCKETSINK_H
  27. #define BADVPN_FLOW_DATAGRAMSOCKETSINK_H
  28. #include <stdint.h>
  29. #include <misc/dead.h>
  30. #include <system/DebugObject.h>
  31. #include <system/BSocket.h>
  32. #include <flow/PacketPassInterface.h>
  33. #include <flow/error.h>
  34. #define DATAGRAMSOCKETSINK_ERROR_BSOCKET 1
  35. #define DATAGRAMSOCKETSINK_ERROR_WRONGSIZE 2
  36. /**
  37. * A {@link PacketPassInterface} sink which sends packets to a datagram socket.
  38. */
  39. typedef struct {
  40. DebugObject d_obj;
  41. dead_t dead;
  42. FlowErrorReporter rep;
  43. BSocket *bsock;
  44. BAddr addr;
  45. BIPAddr local_addr;
  46. PacketPassInterface input;
  47. int in_len;
  48. uint8_t *in;
  49. #ifndef NDEBUG
  50. int in_error;
  51. #endif
  52. } DatagramSocketSink;
  53. /**
  54. * Initializes the sink.
  55. *
  56. * @param s the object
  57. * @param rep error reporting data. Error code is an int. Possible error codes:
  58. * - DATAGRAMSOCKETSINK_ERROR_BSOCKET: {@link BSocket_SendToFrom} failed
  59. * with an unhandled error code
  60. * - DATAGRAMSOCKETSINK_ERROR_WRONGSIZE: {@link BSocket_SendToFrom} succeeded,
  61. * but did not send all of the packet
  62. * On error, the object will continue to operate unless it is destroyed from
  63. * the error handler.
  64. * @param bsock datagram socket to write packets to. Registers a BSOCKET_WRITE handler which
  65. * must not be registered.
  66. * @param mtu maximum packet size. Must be >=0.
  67. * @param addr remote address. Must be recognized and valid. Passed to {@link BSocket_SendToFrom}.
  68. * @param local_addr source address. Must be recognized.
  69. * Passed to {@link BSocket_SendToFrom}.
  70. */
  71. void DatagramSocketSink_Init (DatagramSocketSink *s, FlowErrorReporter rep, BSocket *bsock, int mtu, BAddr addr, BIPAddr local_addr);
  72. /**
  73. * Frees the sink.
  74. *
  75. * @param s the object
  76. */
  77. void DatagramSocketSink_Free (DatagramSocketSink *s);
  78. /**
  79. * Returns the input interface.
  80. *
  81. * @param s the object
  82. * @return input interface
  83. */
  84. PacketPassInterface * DatagramSocketSink_GetInput (DatagramSocketSink *s);
  85. /**
  86. * Sets sending addresses.
  87. *
  88. * @param s the object
  89. * @param addr remote address. Must be recognized and valid. Passed to {@link BSocket_SendToFrom}.
  90. * @param local_addr source address. Must be recognized.
  91. * Passed to {@link BSocket_SendToFrom}.
  92. */
  93. void DatagramSocketSink_SetAddresses (DatagramSocketSink *s, BAddr addr, BIPAddr local_addr);
  94. #endif