DatagramSocketSink.h 3.2 KB

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