Asterisk - The Open Source Telephony Project  GIT-master-a24979a
stun.h
Go to the documentation of this file.
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 /*!
20  * \file
21  *
22  * \brief STUN support.
23  *
24  * STUN is defined in RFC 3489.
25  */
26 
27 #ifndef _ASTERISK_STUN_H
28 #define _ASTERISK_STUN_H
29 
30 #include "asterisk/network.h"
32 
33 #if defined(__cplusplus) || defined(c_plusplus)
34 extern "C" {
35 #endif
36 
37 /* STUN debug logging category name */
38 #define AST_LOG_CATEGORY_STUN "stun"
39 /* STUN packet debug logging category name */
40 #define AST_LOG_CATEGORY_STUN_PACKET "stun_packet"
41 
42 uintmax_t ast_debug_category_stun_id(void);
43 uintmax_t ast_debug_category_stun_packet_id(void);
44 
45 #define AST_DEBUG_CATEGORY_STUN ast_debug_category_stun_id() /* STUN debug logging category id */
46 #define AST_DEBUG_CATEGORY_STUN_PACKET ast_debug_category_stun_packet_id() /* STUN packet debug logging category id */
47 
48 /*!
49  * \brief Log debug level STUN information
50  *
51  * \param sublevel Debug output sublevel (>= 0)
52  * \param ... String format and any associated arguments
53  */
54 #define ast_debug_stun(sublevel, ...) \
55  ast_debug_category(sublevel, AST_DEBUG_CATEGORY_STUN, __VA_ARGS__)
56 
57 /* Is logging of stun packets allowed? */
58 #define ast_debug_stun_packet_is_allowed \
59  ast_debug_category_is_allowed(AST_LOG_CATEGORY_ENABLED, AST_DEBUG_CATEGORY_STUN_PACKET)
60 
61 static const int STANDARD_STUN_PORT = 3478;
62 
66 };
67 
68 struct stun_attr;
69 
70 /*!
71  * \brief Generic STUN request.
72  *
73  * \param s The socket used to send the request.
74  * \param dst If non null, the address of the STUN server.
75  * Only needed if the socket is not bound or connected.
76  * \param username If non null, add the username in the request.
77  * \param answer If non null, the function waits for a response and
78  * puts here the externally visible address.
79  *
80  * \details
81  * Send a generic STUN request to the server specified, possibly
82  * waiting for a reply and filling the answer parameter with the
83  * externally visible address. Note that in this case the
84  * request will be blocking.
85  *
86  * \note The interface may change slightly in the future.
87  *
88  * \retval 0 on success.
89  * \retval <0 on error.
90  * \retval >0 on timeout.
91  */
92 int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer);
93 
94 /*! \brief callback type to be invoked on stun responses. */
95 typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
96 
97 /*!
98  * \brief handle an incoming STUN message.
99  *
100  * \param s Socket to send any response to.
101  * \param src Address where packet came from.
102  * \param data STUN packet buffer to process.
103  * \param len Length of packet
104  * \param stun_cb If not NULL, callback for each STUN attribute.
105  * \param arg Arg to pass to callback.
106  *
107  * \details
108  * Do some basic sanity checks on packet size and content,
109  * try to extract a bit of information, and possibly reply.
110  * At the moment this only processes BIND requests, and returns
111  * the externally visible address of the request.
112  * If a callback is specified, invoke it with the attribute.
113  *
114  * \retval AST_STUN_ACCEPT if responed to a STUN request
115  * \retval AST_STUN_IGNORE
116  * \retval -1 on error
117  */
118 int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg);
119 
120 #if defined(__cplusplus) || defined(c_plusplus)
121 }
122 #endif
123 
124 #endif /* _ASTERISK_STUN_H */
static int answer(void *data)
Definition: chan_pjsip.c:675
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Wrapper for network related headers, masking differences between various operating systems....
Definition: stun.c:78
unsigned short attr
Definition: stun.c:79
uintmax_t ast_debug_category_stun_id(void)
Definition: stun.c:555
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
Generic STUN request.
Definition: stun.c:415
int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
handle an incoming STUN message.
Definition: stun.c:293
uintmax_t ast_debug_category_stun_packet_id(void)
Definition: stun.c:562
int() stun_cb_f(struct stun_attr *attr, void *arg)
callback type to be invoked on stun responses.
Definition: stun.h:95
static const int STANDARD_STUN_PORT
Definition: stun.h:61
ast_stun_result
Definition: stun.h:63
@ AST_STUN_ACCEPT
Definition: stun.h:65
@ AST_STUN_IGNORE
Definition: stun.h:64