DatagramSocketSource.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file DatagramSocketSource.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 PacketRecvInterface} source which receives packets from a datagram socket.
  25. */
  26. #ifndef BADVPN_FLOW_DATAGRAMSOCKETSOURCE_H
  27. #define BADVPN_FLOW_DATAGRAMSOCKETSOURCE_H
  28. #include <stdint.h>
  29. #include <misc/dead.h>
  30. #include <misc/debugin.h>
  31. #include <system/DebugObject.h>
  32. #include <system/BSocket.h>
  33. #include <system/BPending.h>
  34. #include <flow/PacketRecvInterface.h>
  35. #include <flow/error.h>
  36. #define DATAGRAMSOCKETSOURCE_ERROR_BSOCKET 1
  37. /**
  38. * A {@link PacketRecvInterface} source which receives packets from a datagram socket.
  39. */
  40. typedef struct {
  41. dead_t dead;
  42. FlowErrorReporter rep;
  43. BSocket *bsock;
  44. int mtu;
  45. PacketRecvInterface output;
  46. int out_have;
  47. uint8_t *out;
  48. BAddr last_addr;
  49. BIPAddr last_local_addr;
  50. BPending retry_job;
  51. DebugIn d_in_error;
  52. DebugObject d_obj;
  53. #ifndef NDEBUG
  54. int have_last_addr;
  55. #endif
  56. } DatagramSocketSource;
  57. /**
  58. * Initializes the object.
  59. *
  60. * @param s the object
  61. * @param rep error reporting data. Error code is an int. Possible error codes:
  62. * - DATAGRAMSOCKETSOURCE_ERROR_BSOCKET: {@link BSocket_RecvFromTo} failed
  63. * with an unhandled error code
  64. * On error, the object will continue to operate unless it is destroyed from
  65. * the error handler.
  66. * @param bsock datagram socket to read data from. The BSOCKET_READ event must be disabled.
  67. * * Takes over reading on the socket.
  68. * @param mtu maximum packet size. Must be >=0.
  69. * @param pg pending group
  70. */
  71. void DatagramSocketSource_Init (DatagramSocketSource *s, FlowErrorReporter rep, BSocket *bsock, int mtu, BPendingGroup *pg);
  72. /**
  73. * Frees the object.
  74. *
  75. * @param s the object
  76. */
  77. void DatagramSocketSource_Free (DatagramSocketSource *s);
  78. /**
  79. * Returns the output interface.
  80. *
  81. * @param s the object
  82. * @return output interface
  83. */
  84. PacketRecvInterface * DatagramSocketSource_GetOutput (DatagramSocketSource *s);
  85. /**
  86. * Returns the remote and local address of the last received packet.
  87. * At least one packet must have been received.
  88. *
  89. * @param s the object
  90. * @param addr where to put the remote address, if not NULL. The returned address
  91. * will be valid.
  92. * @param local_addr where to put the local address, if not NULL. The returned
  93. * address may be an invalid address.
  94. */
  95. void DatagramSocketSource_GetLastAddresses (DatagramSocketSource *s, BAddr *addr, BIPAddr *local_addr);
  96. #endif