stun 1.12.11devel
Loading...
Searching...
No Matches
stun_common.h
Go to the documentation of this file.
1/*
2 * This file is part of the Sofia-SIP package
3 *
4 * Copyright (C) 2005 Nokia Corporation.
5 *
6 * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 */
24
25#ifndef STUN_COMMON_H
27#define STUN_COMMON_H
28
40
41SOFIA_BEGIN_DECLS
42
43/* Define Message Types */
44#define BINDING_REQUEST 0x0001
45#define BINDING_RESPONSE 0x0101
46#define BINDING_ERROR_RESPONSE 0x0111
47#define SHARED_SECRET_REQUEST 0x0002
48#define SHARED_SECRET_RESPONSE 0x0102
49#define SHARED_SECRET_ERROR_RESPONSE 0x0112
50
51/* Define Attribute Types */
52#define MAPPED_ADDRESS 0x0001
53#define RESPONSE_ADDRESS 0x0002
54#define CHANGE_REQUEST 0x0003
55#define SOURCE_ADDRESS 0x0004
56#define CHANGED_ADDRESS 0x0005
57#define USERNAME 0x0006
58#define PASSWORD 0x0007
59#define MESSAGE_INTEGRITY 0x0008
60#define ERROR_CODE 0x0009
61#define UNKNOWN_ATTRIBUTES 0x000a
62#define REFLECTED_FROM 0x000b
63#define STUN_A_REALM 0x0014 /* XXX: check value in 3489bis-05+ */
64#define STUN_A_NONCE 0x0015 /* XXX: check value in 3489bis-05+ */
65#define STUN_A_XOR_MAPPED_ADDRESS 0x0020
66#define STUN_A_FINGERPRINT 0x0023
67#define STUN_A_SERVER 0x8022
68#define STUN_A_ALTERNATE_SERVER 0x8023
69#define STUN_A_REFRESH_INTERVAL 0x8024
70
71/* Defines for mandatory and optional attributes */
72#define STUN_A_LAST_MANDATORY 0x0023
75#define STUN_A_OPTIONAL 0x7fff
76
77/* Compability attribute types */
78#define STUN_A_ALTERNATE_SERVER_DEP 0x000e
79#define STUN_A_BUGGYSERVER_XORONLY 0x0021
80#define STUN_A_BUGGYSERVER_SERVER 0x0022
81#define LARGEST_ATTRIBUTE STUN_A_LAST_MANDATORY
82#define OPTIONAL_ATTRIBUTE STUN_A_OPTIONAL
84/* Stun response codes */
85#define STUN_400_BAD_REQUEST 400
86#define STUN_401_UNAUTHORIZED 401
87#define STUN_420_UNKNOWN_ATTRIBUTE 420
88#define STUN_430_STALE_CREDENTIALS 430
89#define STUN_431_INTEGRITY_CHECK_FAILURE 431
90#define STUN_432_MISSING_USERNAME 432
91#define STUN_433_USE_TLS 433
92#define STUN_500_SERVER_ERROR 500
93#define STUN_600_GLOBAL_FAILURE 600
94
95/* flags for CHANGE_REQUEST */
96#define STUN_CR_CHANGE_IP 0x0004
97#define STUN_CR_CHANGE_PORT 0x0002
98
99/* mask for ERROR_CODE */
100#define STUN_EC_CLASS 0x0070
101#define STUN_EC_NUM 0x000F
102
103#define RAND_MAX_16 65535
104
105#define STUN_TID_BYTES 16
106
107/* other protocol specific parameters */
108#define STUN_MAX_RETRX 5 /* should be 8? */
109#define STUN_MAX_RETRX_INT 1600
111#define STUN_DEFAULT_PORT 3478
113/*
114 * STUN header format
115 */
116 /*
117 0 1 2 3
118 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 | message type | message length |
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 | |
123 | Transaction ID |
124 | |
125 | |
126 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127 */
128struct stun_buffer_s {
129 unsigned char *data;
130 unsigned size;
131};
132
133typedef struct stun_buffer_s stun_buffer_t;
134
135typedef struct {
136 uint16_t msg_type;
137 uint16_t msg_len;
138 uint8_t tran_id[16];
139} stun_hdr_t;
140
141typedef struct stun_attr_s {
142 uint16_t attr_type;
143 void *pattr;
144 stun_buffer_t enc_buf;
145 struct stun_attr_s *next;
146} stun_attr_t;
147
148typedef struct {
149 stun_hdr_t stun_hdr;
150 stun_attr_t *stun_attr;
151 stun_buffer_t enc_buf;
152} stun_msg_t;
153
154/* stun attribute definition */
155/* stun_sockaddr_t is used for:
156 MAPPED_ADDRESS
157 RESPONSE_ADDRESS
158 SOURCE_ADDRESS
159 CHANGED_ADDRESS
160 REFLECTED_FROM
161*/
162typedef struct sockaddr_in stun_attr_sockaddr_t;
163
164/* CHANGE_REQUEST attribute */
165typedef struct stun_attr_uint32_s {
166 uint32_t value;
167} stun_attr_uint32_t;
168
169typedef stun_attr_uint32_t stun_attr_changerequest_t;
170
171/* ERROR_CODE attribute */
172typedef struct {
173 int code;
174 char *phrase;
175} stun_attr_errorcode_t;
176
177/* USERNAME attribute */
178/* typedef struct {
179 stun_buffer_t *uname;
180} stun_attr_username_t;
181*/
182typedef stun_buffer_t stun_attr_username_t;
183
184/* PASSWORD attribute */
185typedef stun_buffer_t stun_attr_password_t;
186
187/* UNKNOWN_ATTRIBUTES attribute */
188typedef struct stun_attr_unknownattributes_s{
189 uint16_t attr_type[2];
190 struct stun_attr_unknownattributes_s *next;
191} stun_attr_unknownattributes_t;
192
193/* Common functions */
194int stun_parse_message(stun_msg_t *msg);
195int stun_parse_attribute(stun_msg_t *msg, unsigned char *p, size_t left_len);
196int stun_parse_attr_address(stun_attr_t *attr, const unsigned char *p, unsigned len);
197int stun_parse_attr_error_code(stun_attr_t *attr, const unsigned char *p, unsigned len);
198int stun_parse_attr_unknown_attributes(stun_attr_t *attr, const unsigned char *p, unsigned len);
199int stun_parse_attr_uint32(stun_attr_t *attr, const unsigned char *p, unsigned len);
200int stun_parse_attr_buffer(stun_attr_t *attr, const unsigned char *p, unsigned len);
201
202stun_attr_t *stun_get_attr(stun_attr_t *attr, uint16_t attr_type);
203
204int stun_encode_address(stun_attr_t *attr);
205int stun_encode_uint32(stun_attr_t *attr);
206int stun_encode_buffer(stun_attr_t *attr);
207int stun_encode_error_code(stun_attr_t *attr);
208int stun_encode_message_integrity(stun_attr_t *attr, unsigned char *buf, int len, stun_buffer_t *pwd);
209int stun_encode_type_len(stun_attr_t *attr, uint16_t len);
210int stun_encode_response_address(stun_attr_t *attr);
211
212int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd);
213
214int stun_copy_buffer(stun_buffer_t *p, stun_buffer_t *p2);
215void stun_init_buffer(stun_buffer_t *p);
216int stun_free_buffer(stun_buffer_t *p);
217int stun_free_message(stun_msg_t *msg);
218
219int stun_init_message(stun_msg_t *msg);
220/* int stun_send_message(int sockfd, struct sockaddr_in *to_addr, stun_msg_t *msg, stun_buffer_t *pwd); */
221int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd);
222
223char const *stun_response_phrase(int status);
224void debug_print(stun_buffer_t *buf);
225char const *stun_attr_phrase(uint16_t type);
226
234char *stun_determine_ip_address(int family);
235
236SOFIA_END_DECLS
237
238#endif /* !defined STUN_COMMON_H */
int stun_encode_type_len(stun_attr_t *attr, uint16_t len)
this function allocates the enc_buf, fills in type, length
Definition stun_common.c:500
int stun_encode_address(stun_attr_t *attr)
The set of functions encodes the corresponding attribute to network format, and save the result to th...
Definition stun_common.c:377
stun_attr_t * stun_get_attr(stun_attr_t *attr, uint16_t attr_type)
scan thru attribute list and return the next requested attr
Definition stun_common.c:313
char * stun_determine_ip_address(int family)
Determines and returns local IP address.
Definition stun_common.c:788
int stun_encode_message(stun_msg_t *msg, stun_buffer_t *pwd)
Send a STUN message.
Definition stun_common.c:685
int stun_validate_message_integrity(stun_msg_t *msg, stun_buffer_t *pwd)
Validate the message integrity based on given STUN password 'pwd'.
Definition stun_common.c:521
SU_U32_T uint32_t
SU_U16_T uint16_t
SU_U8_T uint8_t

Sofia-SIP 1.12.11devel - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.