4 * Copyright (C) 2001-2003 FhG Fokus
6 * This file is part of ser, a free SIP server.
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
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:
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.
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
30 * 2006-10-13 created (vlada)
39 #include <openssl/sha.h>
45 /* type redefinition */
46 typedef unsigned char UCHAR_T;
47 typedef unsigned short USHORT_T;
48 typedef unsigned int UINT_T;
49 typedef unsigned long ULONG_T;
51 /* STUN message types supported by SER */
52 #define BINDING_REQUEST 0x0001
53 #define BINDING_RESPONSE 0x0101
54 #define BINDING_ERROR_RESPONSE 0x0111
56 /* common STUN attributes */
57 #define MAPPED_ADDRESS_ATTR 0x0001
58 #define USERNAME_ATTR 0x0006
59 #define MESSAGE_INTEGRITY_ATTR 0x0008
60 #define ERROR_CODE_ATTR 0x0009
61 #define UNKNOWN_ATTRIBUTES_ATTR 0x000A
63 /* STUN attributes defined by rfc5389 */
64 #define REALM_ATTR 0x0014
65 #define NONCE_ATTR 0x0015
66 #define XOR_MAPPED_ADDRESS_ATTR 0x0020
67 #define FINGERPRINT_ATTR 0x8028
68 #define SOFTWARE_ATTR 0x8022
69 #define ALTERNATE_SERVER_ATTR 0x8023
71 /* STUN attributes defined by rfc3489 */
72 #define RESPONSE_ADDRESS_ATTR 0x0002
73 #define CHANGE_REQUEST_ATTR 0x0003
74 #define SOURCE_ADDRESS_ATTR 0x0004
75 #define CHANGED_ADDRESS_ATTR 0x0005
76 #define REFLECTED_FROM_ATTR 0x000b
78 /* STUN error codes supported by SER */
79 #define RESPONSE_OK 200
80 #define TRY_ALTERNATE_ERR 300
81 #define BAD_REQUEST_ERR 400
82 #define UNAUTHORIZED_ERR 401
83 #define UNKNOWN_ATTRIBUTE_ERR 420
84 #define STALE_CREDENTIALS_ERR 430
85 #define INTEGRITY_CHECK_ERR 431
86 #define MISSING_USERNAME_ERR 432
87 #define USE_TLS_ERR 433
88 #define MISSING_REALM_ERR 434
89 #define MISSING_NONCE_ERR 435
90 #define UNKNOWN_USERNAME_ERR 436
91 #define STALE_NONCE_ERR 438
92 #define SERVER_ERROR_ERR 500
93 #define GLOBAL_FAILURE_ERR 600
95 #define TRY_ALTERNATE_TXT "Try Alternate"
96 #define BAD_REQUEST_TXT "Bad Request"
97 #define UNAUTHORIZED_TXT "Unauthorized"
98 #define UNKNOWN_ATTRIBUTE_TXT "Unknown Attribute"
99 #define STALE_CREDENTIALS_TXT "Stale Credentials"
100 #define INTEGRITY_CHECK_TXT "Integrity Check Failure"
101 #define MISSING_USERNAME_TXT "Missing Username"
102 #define USE_TLS_TXT "Use TLS"
103 #define MISSING_REALM_TXT "Missing Realm"
104 #define MISSING_NONCE_TXT "Missing Nonce"
105 #define UNKNOWN_USERNAME_TXT "Unknown Username"
106 #define STALE_NONCE_TXT "Stale Nonce"
107 #define SERVER_ERROR_TXT "Server Error"
108 #define GLOBAL_FAILURE_TXT "Global Failure"
112 #define MAGIC_COOKIE 0x2112A442
113 #define MAGIC_COOKIE_2B 0x2112 /* because of XOR for port */
114 #define MANDATORY_ATTR 0x7fff
117 #define STUN_MSG_LEN 516
120 #define IPV4_FAMILY 0x0001
121 #define IPV6_FAMILY 0x0002
122 #define FATAL_ERROR -1
125 #define TRANSACTION_ID 12
127 /** padd len to a multiple of sz.
128 * sz must be a power of the form 2^k (e.g. 2, 4, 8, 16 ...)
130 #define PADD_TO(len, sz) (((len) + (sz)-1) & (~((sz) - 1)))
132 #define PADDED_TO_FOUR(len) PADD_TO(len, 4)
133 #define PADDED_TO_SIXTYFOUR(len) PADD_TO(len, 64)
135 struct transaction_id {
137 UCHAR_T id[TRANSACTION_ID];
143 struct transaction_id id;
146 struct stun_ip_addr {
147 USHORT_T family; /* 0x01: IPv4; 0x02: IPv6 */
154 USHORT_T empty; /* number of free bytes in buf before
155 * it'll be necessary to realloc the buf
159 struct stun_unknown_att {
161 struct stun_unknown_att* next;
171 struct stun_ip_addr ip_addr; /* XOR values for rfc3489bis,
172 normal values for rfc3489 */
173 struct stun_buffer msg;
174 UCHAR_T old; /* true: the format of message is in
175 accordance with rfc3489 */
180 * stun functions called from ser
182 int stun_process_msg(char* buf, unsigned len, struct receive_info* ri);
184 #endif /* USE_STUN */
186 #endif /* _ser_stun_h */