/src/rtpproxy/external/libre/src/stun/rep.c
Line | Count | Source |
1 | | /** |
2 | | * @file stun/rep.c STUN reply |
3 | | * |
4 | | * Copyright (C) 2010 Creytiv.com |
5 | | */ |
6 | | #include <re_types.h> |
7 | | #include <re_mem.h> |
8 | | #include <re_mbuf.h> |
9 | | #include <re_sa.h> |
10 | | #include <re_list.h> |
11 | | #include <re_stun.h> |
12 | | #include "stun.h" |
13 | | |
14 | | |
15 | | /** |
16 | | * Send a STUN response message |
17 | | * |
18 | | * @param proto Transport Protocol |
19 | | * @param sock Socket; UDP (struct udp_sock) or TCP (struct tcp_conn) |
20 | | * @param dst Destination network address |
21 | | * @param presz Number of bytes in preamble, if sending over TURN |
22 | | * @param req Matching STUN request |
23 | | * @param key Authentication key (optional) |
24 | | * @param keylen Number of bytes in authentication key |
25 | | * @param fp Use STUN Fingerprint attribute |
26 | | * @param attrc Number of attributes to encode (variable arguments) |
27 | | * @param ... Variable list of attribute-tuples |
28 | | * Each attribute has 2 arguments, attribute type and value |
29 | | * |
30 | | * @return 0 if success, otherwise errorcode |
31 | | */ |
32 | | int stun_reply(int proto, void *sock, const struct sa *dst, size_t presz, |
33 | | const struct stun_msg *req, const uint8_t *key, |
34 | | size_t keylen, bool fp, uint32_t attrc, ...) |
35 | 0 | { |
36 | 0 | struct mbuf *mb = NULL; |
37 | 0 | int err = ENOMEM; |
38 | 0 | va_list ap; |
39 | |
|
40 | 0 | if (!sock || !req) |
41 | 0 | return EINVAL; |
42 | | |
43 | 0 | mb = mbuf_alloc(256); |
44 | 0 | if (!mb) |
45 | 0 | goto out; |
46 | | |
47 | 0 | va_start(ap, attrc); |
48 | 0 | mb->pos = presz; |
49 | 0 | err = stun_msg_vencode(mb, stun_msg_method(req), |
50 | 0 | STUN_CLASS_SUCCESS_RESP, stun_msg_tid(req), |
51 | 0 | NULL, key, keylen, fp, 0x00, attrc, ap); |
52 | 0 | va_end(ap); |
53 | 0 | if (err) |
54 | 0 | goto out; |
55 | | |
56 | 0 | mb->pos = presz; |
57 | 0 | err = stun_send(proto, sock, dst, mb); |
58 | |
|
59 | 0 | out: |
60 | 0 | mem_deref(mb); |
61 | |
|
62 | 0 | return err; |
63 | 0 | } |
64 | | |
65 | | |
66 | | /** |
67 | | * Send a STUN error response |
68 | | * |
69 | | * @param proto Transport Protocol |
70 | | * @param sock Socket; UDP (struct udp_sock) or TCP (struct tcp_conn) |
71 | | * @param dst Destination network address |
72 | | * @param presz Number of bytes in preamble, if sending over TURN |
73 | | * @param req Matching STUN request |
74 | | * @param scode Status code |
75 | | * @param reason Reason string |
76 | | * @param key Authentication key (optional) |
77 | | * @param keylen Number of bytes in authentication key |
78 | | * @param fp Use STUN Fingerprint attribute |
79 | | * @param attrc Number of attributes to encode (variable arguments) |
80 | | * @param ... Variable list of attribute-tuples |
81 | | * Each attribute has 2 arguments, attribute type and value |
82 | | * |
83 | | * @return 0 if success, otherwise errorcode |
84 | | */ |
85 | | int stun_ereply(int proto, void *sock, const struct sa *dst, size_t presz, |
86 | | const struct stun_msg *req, uint16_t scode, |
87 | | const char *reason, const uint8_t *key, size_t keylen, |
88 | | bool fp, uint32_t attrc, ...) |
89 | 1.11k | { |
90 | 1.11k | struct stun_errcode ec; |
91 | 1.11k | struct mbuf *mb = NULL; |
92 | 1.11k | int err = ENOMEM; |
93 | 1.11k | va_list ap; |
94 | | |
95 | 1.11k | if (!sock || !req || !scode || !reason) |
96 | 0 | return EINVAL; |
97 | | |
98 | 1.11k | mb = mbuf_alloc(256); |
99 | 1.11k | if (!mb) |
100 | 0 | goto out; |
101 | | |
102 | 1.11k | ec.code = scode; |
103 | 1.11k | ec.reason = (char *)reason; |
104 | | |
105 | 1.11k | va_start(ap, attrc); |
106 | 1.11k | mb->pos = presz; |
107 | 1.11k | err = stun_msg_vencode(mb, stun_msg_method(req), STUN_CLASS_ERROR_RESP, |
108 | 1.11k | stun_msg_tid(req), &ec, key, keylen, |
109 | 1.11k | fp, 0x00, attrc, ap); |
110 | 1.11k | va_end(ap); |
111 | 1.11k | if (err) |
112 | 0 | goto out; |
113 | | |
114 | 1.11k | mb->pos = presz; |
115 | 1.11k | err = stun_send(proto, sock, dst, mb); |
116 | | |
117 | 1.11k | out: |
118 | 1.11k | mem_deref(mb); |
119 | | |
120 | 1.11k | return err; |
121 | 1.11k | } |