DatagramSocketSource.h 3.1 KB

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