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