DatagramSocketSource.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <misc/dead.h>
  29. #include <system/DebugObject.h>
  30. #include <system/BSocket.h>
  31. #include <flow/error.h>
  32. #include <flow/PacketRecvInterface.h>
  33. #define DATAGRAMSOCKETSOURCE_ERROR_BSOCKET 1
  34. /**
  35. * A {@link PacketRecvInterface} source which receives packets from a datagram socket.
  36. */
  37. typedef struct {
  38. DebugObject d_obj;
  39. dead_t dead;
  40. FlowErrorReporter rep;
  41. BSocket *bsock;
  42. int mtu;
  43. PacketRecvInterface output;
  44. int out_have;
  45. uint8_t *out;
  46. BAddr last_addr;
  47. BIPAddr last_local_addr;
  48. #ifndef NDEBUG
  49. int have_last_addr;
  50. int in_error;
  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. */
  66. void DatagramSocketSource_Init (DatagramSocketSource *s, FlowErrorReporter rep, BSocket *bsock, int mtu);
  67. /**
  68. * Frees the object.
  69. *
  70. * @param s the object
  71. */
  72. void DatagramSocketSource_Free (DatagramSocketSource *s);
  73. /**
  74. * Returns the output interface.
  75. *
  76. * @param s the object
  77. * @return output interface
  78. */
  79. PacketRecvInterface * DatagramSocketSource_GetOutput (DatagramSocketSource *s);
  80. /**
  81. * Returns the remote and local address of the last received packet.
  82. * At least one packet must have been received.
  83. *
  84. * @param s the object
  85. * @param addr where to put the remote address, if not NULL. The returned address
  86. * will be valid.
  87. * @param local_addr where to put the local address, if not NULL. The returned
  88. * address may be an invalid address.
  89. */
  90. void DatagramSocketSource_GetLastAddresses (DatagramSocketSource *s, BAddr *addr, BIPAddr *local_addr);
  91. #endif