stun.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2008, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file stun.h
  20. * \brief STUN support.
  21. *
  22. * STUN is defined in RFC 3489.
  23. */
  24. #ifndef _ASTERISK_STUN_H
  25. #define _ASTERISK_STUN_H
  26. #include "asterisk/network.h"
  27. #if defined(__cplusplus) || defined(c_plusplus)
  28. extern "C" {
  29. #endif
  30. static const int STANDARD_STUN_PORT = 3478;
  31. enum ast_stun_result {
  32. AST_STUN_IGNORE = 0,
  33. AST_STUN_ACCEPT,
  34. };
  35. struct stun_attr;
  36. /*!
  37. * \brief Generic STUN request.
  38. *
  39. * \param s The socket used to send the request.
  40. * \param dst If non null, the address of the STUN server.
  41. * Only needed if the socket is not bound or connected.
  42. * \param username If non null, add the username in the request.
  43. * \param answer If non null, the function waits for a response and
  44. * puts here the externally visible address.
  45. *
  46. * \details
  47. * Send a generic STUN request to the server specified, possibly
  48. * waiting for a reply and filling the answer parameter with the
  49. * externally visible address. Note that in this case the
  50. * request will be blocking.
  51. *
  52. * \note The interface may change slightly in the future.
  53. *
  54. * \retval 0 on success.
  55. * \retval <0 on error.
  56. * \retval >0 on timeout.
  57. */
  58. int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer);
  59. /*! \brief callback type to be invoked on stun responses. */
  60. typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
  61. /*!
  62. * \brief handle an incoming STUN message.
  63. *
  64. * \param s Socket to send any response to.
  65. * \param src Address where packet came from.
  66. * \param data STUN packet buffer to process.
  67. * \param len Length of packet
  68. * \param stun_cb If not NULL, callback for each STUN attribute.
  69. * \param arg Arg to pass to callback.
  70. *
  71. * \details
  72. * Do some basic sanity checks on packet size and content,
  73. * try to extract a bit of information, and possibly reply.
  74. * At the moment this only processes BIND requests, and returns
  75. * the externally visible address of the request.
  76. * If a callback is specified, invoke it with the attribute.
  77. *
  78. * \retval AST_STUN_ACCEPT if responed to a STUN request
  79. * \retval AST_STUN_IGNORE
  80. * \retval -1 on error
  81. */
  82. int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg);
  83. #if defined(__cplusplus) || defined(c_plusplus)
  84. }
  85. #endif
  86. #endif /* _ASTERISK_STUN_H */