UDPInterface.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "util/Linker.h"
  24. Linker_require("interface/UDPInterface.c")
  25. struct UDPInterface
  26. {
  27. struct Sockaddr* addr;
  28. };
  29. /**
  30. * @param base the LibEvent context.
  31. * @param bindAddr a string representation of the address to bind to such as "0.0.0.0:12345".
  32. * @param allocator the memory allocator for this message.
  33. * @param exHandler the handler to deal with whatever exception arises.
  34. * Exceptions:
  35. * UDPInterface_new_PARSE_ADDRESS_FAILED Couldn't parse bindAddr as an ip address and port
  36. * UDPInterface_new_FAILED_CREATING_EVENT Failed creating the event or registering it with
  37. * the libevent event base (shouldn't happen)
  38. * UDPInterface_new_SOCKET_FAILED Failed calling socket(), check EVUTIL_SOCKET_ERROR()
  39. * UDPInterface_new_PROTOCOL_NOT_SUPPORTED Only IPv4 is supported as an underlying protocol
  40. * UDPInterface_new_BIND_FAILED Failed calling bind(), check EVUTIL_SOCKET_ERROR()
  41. *
  42. * @param logger
  43. * @param ic the controller which this interface should register with
  44. * and use when starting connections.
  45. * @return a new UDPInterface.
  46. */
  47. #define UDPInterface_new_PARSE_ADDRESS_FAILED -1
  48. #define UDPInterface_new_FAILED_CREATING_EVENT -2
  49. #define UDPInterface_new_SOCKET_FAILED -3
  50. #define UDPInterface_new_PROTOCOL_NOT_SUPPORTED -4
  51. #define UDPInterface_new_BIND_FAILED -5
  52. struct UDPInterface* UDPInterface_new(struct EventBase* base,
  53. struct Sockaddr* bindAddr,
  54. struct Allocator* allocator,
  55. struct Except* exHandler,
  56. struct Log* logger,
  57. struct InterfaceController* ic);
  58. /**
  59. * Begin an outgoing connection.
  60. *
  61. * @param address the ipv4 address and udp port to connect to, expressed as address:port.
  62. * @param cryptoKey the node's public key, this is required to send it traffic.
  63. * @param password if specified, the password for authenticating with the other node.
  64. * @param udpif the UDP interface.
  65. * @return 0 on success
  66. * UDPInterface_beginConnection_OUT_OF_SPACE if there is no space to store the entry.
  67. * UDPInterface_beginConnection_BAD_KEY invalid (non-cjdns) cryptoKey
  68. * UDPInterface_beginConnection_BAD_ADDRESS failed to parse ip address and port.
  69. * UDPInterface_beginConnection_ADDRESS_MISMATCH address not same protocol as UDP socket.
  70. * UDPInterface_beginConnection_UNKNOWN_ERROR something failed in InterfaceController.
  71. */
  72. #define UDPInterface_beginConnection_OUT_OF_SPACE -1
  73. #define UDPInterface_beginConnection_BAD_KEY -2
  74. #define UDPInterface_beginConnection_BAD_ADDRESS -3
  75. #define UDPInterface_beginConnection_ADDRESS_MISMATCH -4
  76. #define UDPInterface_beginConnection_UNKNOWN_ERROR -5
  77. int UDPInterface_beginConnection(const char* address,
  78. uint8_t cryptoKey[32],
  79. String* password,
  80. struct UDPInterface* udpif);
  81. #endif