James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file ind.c STUN Indication |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | #include <re_types.h> |
| 7 | #include <re_sys.h> |
| 8 | #include <re_mem.h> |
| 9 | #include <re_mbuf.h> |
| 10 | #include <re_sa.h> |
| 11 | #include <re_list.h> |
| 12 | #include <re_stun.h> |
| 13 | #include "stun.h" |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * Send a STUN Indication message |
| 18 | * |
| 19 | * @param proto Transport Protocol |
| 20 | * @param sock Socket; UDP (struct udp_sock) or TCP (struct tcp_conn) |
| 21 | * @param dst Destination network address |
| 22 | * @param presz Number of bytes in preamble, if sending over TURN |
| 23 | * @param method STUN Method |
| 24 | * @param key Authentication key (optional) |
| 25 | * @param keylen Number of bytes in authentication key |
| 26 | * @param fp Use STUN Fingerprint attribute |
| 27 | * @param attrc Number of attributes to encode (variable arguments) |
| 28 | * @param ... Variable list of attribute-tuples |
| 29 | * Each attribute has 2 arguments, attribute type and value |
| 30 | * |
| 31 | * @return 0 if success, otherwise errorcode |
| 32 | */ |
| 33 | int stun_indication(int proto, void *sock, const struct sa *dst, size_t presz, |
| 34 | uint16_t method, const uint8_t *key, size_t keylen, |
| 35 | bool fp, uint32_t attrc, ...) |
| 36 | { |
| 37 | uint8_t tid[STUN_TID_SIZE]; |
| 38 | struct mbuf *mb; |
| 39 | va_list ap; |
| 40 | uint32_t i; |
| 41 | int err; |
| 42 | |
| 43 | if (!sock) |
| 44 | return EINVAL; |
| 45 | |
| 46 | mb = mbuf_alloc(2048); |
| 47 | if (!mb) |
| 48 | return ENOMEM; |
| 49 | |
| 50 | for (i=0; i<STUN_TID_SIZE; i++) |
| 51 | tid[i] = rand_u32(); |
| 52 | |
| 53 | va_start(ap, attrc); |
| 54 | mb->pos = presz; |
| 55 | err = stun_msg_vencode(mb, method, STUN_CLASS_INDICATION, tid, NULL, |
| 56 | key, keylen, fp, 0x00, attrc, ap); |
| 57 | va_end(ap); |
| 58 | if (err) |
| 59 | goto out; |
| 60 | |
| 61 | mb->pos = presz; |
| 62 | err = stun_send(proto, sock, dst, mb); |
| 63 | |
| 64 | out: |
| 65 | mem_deref(mb); |
| 66 | |
| 67 | return err; |
| 68 | } |