1
0

UDPInterface.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef UDPInterface_H
  16. #define UDPInterface_H
  17. #include "interface/Interface.h"
  18. #include "interface/InterfaceController.h"
  19. #include "memory/Allocator.h"
  20. #include "util/events/EventBase.h"
  21. #include "util/platform/Sockaddr.h"
  22. #include "util/log/Log.h"
  23. struct UDPInterface
  24. {
  25. struct Sockaddr* addr;
  26. };
  27. /**
  28. * @param base the LibEvent context.
  29. * @param bindAddr a string representation of the address to bind to such as "0.0.0.0:12345".
  30. * @param allocator the memory allocator for this message.
  31. * @param exHandler the handler to deal with whatever exception arises.
  32. * Exceptions:
  33. * UDPInterface_new_PARSE_ADDRESS_FAILED Couldn't parse bindAddr as an ip address and port
  34. * UDPInterface_new_FAILED_CREATING_EVENT Failed creating the event or registering it with
  35. * the libevent event base (shouldn't happen)
  36. * UDPInterface_new_SOCKET_FAILED Failed calling socket(), check EVUTIL_SOCKET_ERROR()
  37. * UDPInterface_new_PROTOCOL_NOT_SUPPORTED Only IPv4 is supported as an underlying protocol
  38. * UDPInterface_new_BIND_FAILED Failed calling bind(), check EVUTIL_SOCKET_ERROR()
  39. *
  40. * @param logger
  41. * @param ic the controller which this interface should register with
  42. * and use when starting connections.
  43. * @return a new UDPInterface.
  44. */
  45. #define UDPInterface_new_PARSE_ADDRESS_FAILED -1
  46. #define UDPInterface_new_FAILED_CREATING_EVENT -2
  47. #define UDPInterface_new_SOCKET_FAILED -3
  48. #define UDPInterface_new_PROTOCOL_NOT_SUPPORTED -4
  49. #define UDPInterface_new_BIND_FAILED -5
  50. struct UDPInterface* UDPInterface_new(struct EventBase* base,
  51. struct Sockaddr* bindAddr,
  52. struct Allocator* allocator,
  53. struct Except* exHandler,
  54. struct Log* logger,
  55. struct InterfaceController* ic);
  56. /**
  57. * Begin an outgoing connection.
  58. *
  59. * @param address the ipv4 address and udp port to connect to, expressed as address:port.
  60. * @param cryptoKey the node's public key, this is required to send it traffic.
  61. * @param password if specified, the password for authenticating with the other node.
  62. * @param udpif the UDP interface.
  63. * @return 0 on success
  64. * UDPInterface_beginConnection_OUT_OF_SPACE if there is no space to store the entry.
  65. * UDPInterface_beginConnection_BAD_KEY invalid (non-cjdns) cryptoKey
  66. * UDPInterface_beginConnection_BAD_ADDRESS failed to parse ip address and port.
  67. * UDPInterface_beginConnection_ADDRESS_MISMATCH address not same protocol as UDP socket.
  68. * UDPInterface_beginConnection_UNKNOWN_ERROR something failed in InterfaceController.
  69. */
  70. #define UDPInterface_beginConnection_OUT_OF_SPACE -1
  71. #define UDPInterface_beginConnection_BAD_KEY -2
  72. #define UDPInterface_beginConnection_BAD_ADDRESS -3
  73. #define UDPInterface_beginConnection_ADDRESS_MISMATCH -4
  74. #define UDPInterface_beginConnection_UNKNOWN_ERROR -5
  75. int UDPInterface_beginConnection(const char* address,
  76. uint8_t cryptoKey[32],
  77. String* password,
  78. struct UDPInterface* udpif);
  79. #endif