ser_stun.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. *
  28. * History:
  29. * --------
  30. * 2006-10-13 created (vlada)
  31. */
  32. #ifndef _ser_stun_h
  33. #define _ser_stun_h
  34. #ifdef USE_STUN
  35. #include <openssl/sha.h>
  36. #include "str.h"
  37. #include "tcp_conn.h"
  38. #include "ip_addr.h"
  39. /* type redefinition */
  40. typedef unsigned char UCHAR_T;
  41. typedef unsigned short USHORT_T;
  42. typedef unsigned int UINT_T;
  43. typedef unsigned long ULONG_T;
  44. /* STUN message types supported by SER */
  45. #define BINDING_REQUEST 0x0001
  46. #define BINDING_RESPONSE 0x0101
  47. #define BINDING_ERROR_RESPONSE 0x0111
  48. /* common STUN attributes */
  49. #define MAPPED_ADDRESS_ATTR 0x0001
  50. #define USERNAME_ATTR 0x0006
  51. #define MESSAGE_INTEGRITY_ATTR 0x0008
  52. #define ERROR_CODE_ATTR 0x0009
  53. #define UNKNOWN_ATTRIBUTES_ATTR 0x000A
  54. /* STUN attributes defined by rfc5389 */
  55. #define REALM_ATTR 0x0014
  56. #define NONCE_ATTR 0x0015
  57. #define XOR_MAPPED_ADDRESS_ATTR 0x0020
  58. #define FINGERPRINT_ATTR 0x8028
  59. #define SOFTWARE_ATTR 0x8022
  60. #define ALTERNATE_SERVER_ATTR 0x8023
  61. /* STUN attributes defined by rfc3489 */
  62. #define RESPONSE_ADDRESS_ATTR 0x0002
  63. #define CHANGE_REQUEST_ATTR 0x0003
  64. #define SOURCE_ADDRESS_ATTR 0x0004
  65. #define CHANGED_ADDRESS_ATTR 0x0005
  66. #define REFLECTED_FROM_ATTR 0x000b
  67. /* STUN error codes supported by SER */
  68. #define RESPONSE_OK 200
  69. #define TRY_ALTERNATE_ERR 300
  70. #define BAD_REQUEST_ERR 400
  71. #define UNAUTHORIZED_ERR 401
  72. #define UNKNOWN_ATTRIBUTE_ERR 420
  73. #define STALE_CREDENTIALS_ERR 430
  74. #define INTEGRITY_CHECK_ERR 431
  75. #define MISSING_USERNAME_ERR 432
  76. #define USE_TLS_ERR 433
  77. #define MISSING_REALM_ERR 434
  78. #define MISSING_NONCE_ERR 435
  79. #define UNKNOWN_USERNAME_ERR 436
  80. #define STALE_NONCE_ERR 438
  81. #define SERVER_ERROR_ERR 500
  82. #define GLOBAL_FAILURE_ERR 600
  83. #define TRY_ALTERNATE_TXT "Try Alternate"
  84. #define BAD_REQUEST_TXT "Bad Request"
  85. #define UNAUTHORIZED_TXT "Unauthorized"
  86. #define UNKNOWN_ATTRIBUTE_TXT "Unknown Attribute"
  87. #define STALE_CREDENTIALS_TXT "Stale Credentials"
  88. #define INTEGRITY_CHECK_TXT "Integrity Check Failure"
  89. #define MISSING_USERNAME_TXT "Missing Username"
  90. #define USE_TLS_TXT "Use TLS"
  91. #define MISSING_REALM_TXT "Missing Realm"
  92. #define MISSING_NONCE_TXT "Missing Nonce"
  93. #define UNKNOWN_USERNAME_TXT "Unknown Username"
  94. #define STALE_NONCE_TXT "Stale Nonce"
  95. #define SERVER_ERROR_TXT "Server Error"
  96. #define GLOBAL_FAILURE_TXT "Global Failure"
  97. /* other stuff */
  98. #define MAGIC_COOKIE 0x2112A442
  99. #define MAGIC_COOKIE_2B 0x2112 /* because of XOR for port */
  100. #define MANDATORY_ATTR 0x7fff
  101. #define PAD4 4
  102. #define PAD64 64
  103. #define STUN_MSG_LEN 516
  104. #define IPV4_LEN 4
  105. #define IPV6_LEN 16
  106. #define IPV4_FAMILY 0x0001
  107. #define IPV6_FAMILY 0x0002
  108. #define FATAL_ERROR -1
  109. #define IP_ADDR 4
  110. #define XOR 1
  111. #define TRANSACTION_ID 12
  112. /** padd len to a multiple of sz.
  113. * sz must be a power of the form 2^k (e.g. 2, 4, 8, 16 ...)
  114. */
  115. #define PADD_TO(len, sz) (((len) + (sz)-1) & (~((sz) - 1)))
  116. #define PADDED_TO_FOUR(len) PADD_TO(len, 4)
  117. #define PADDED_TO_SIXTYFOUR(len) PADD_TO(len, 64)
  118. struct transaction_id {
  119. UINT_T magic_cookie;
  120. UCHAR_T id[TRANSACTION_ID];
  121. };
  122. struct stun_hdr {
  123. USHORT_T type;
  124. USHORT_T len;
  125. struct transaction_id id;
  126. };
  127. struct stun_ip_addr {
  128. USHORT_T family; /* 0x01: IPv4; 0x02: IPv6 */
  129. USHORT_T port;
  130. UINT_T ip[IP_ADDR];
  131. };
  132. struct stun_buffer {
  133. str buf;
  134. USHORT_T empty; /* number of free bytes in buf before
  135. * it'll be necessary to realloc the buf
  136. */
  137. };
  138. struct stun_unknown_att {
  139. USHORT_T type;
  140. struct stun_unknown_att* next;
  141. };
  142. struct stun_attr {
  143. USHORT_T type;
  144. USHORT_T len;
  145. };
  146. struct stun_msg {
  147. struct stun_hdr hdr;
  148. struct stun_ip_addr ip_addr; /* XOR values for rfc3489bis,
  149. normal values for rfc3489 */
  150. struct stun_buffer msg;
  151. UCHAR_T old; /* true: the format of message is in
  152. accordance with rfc3489 */
  153. };
  154. /*
  155. * stun functions called from ser
  156. */
  157. int stun_process_msg(char* buf, unsigned len, struct receive_info* ri);
  158. #endif /* USE_STUN */
  159. #endif /* _ser_stun_h */