blob: df4f0e6fc94eddbed90784e9e5f428bd1411f92b [file] [log] [blame]
James Kuszmaul871d0712021-01-17 11:30:43 -08001/**
2 * @file cand.c Common ICE Candidates
3 *
4 * Copyright (C) 2010 Creytiv.com
5 */
6#include <string.h>
7#include <re_types.h>
8#include <re_fmt.h>
9#include <re_mem.h>
10#include <re_mbuf.h>
11#include <re_list.h>
12#include <re_tmr.h>
13#include <re_sa.h>
14#include <re_net.h>
15#include <re_sys.h>
16#include <re_stun.h>
17#include <re_udp.h>
18#include <re_tcp.h>
19#include <re_ice.h>
20#include <re_trice.h>
21#include "trice.h"
22
23
24const char *ice_tcptype_name(enum ice_tcptype tcptype)
25{
26 switch (tcptype) {
27
28 case ICE_TCP_ACTIVE: return "active";
29 case ICE_TCP_PASSIVE: return "passive";
30 case ICE_TCP_SO: return "so";
31 default: return "???";
32 }
33}
34
35
36/*
37 Local Remote
38 Candidate Candidate
39 ---------------------------
40 tcp-so tcp-so
41 tcp-active tcp-passive
42 tcp-passive tcp-active
43
44 */
45enum ice_tcptype ice_tcptype_reverse(enum ice_tcptype type)
46{
47 switch (type) {
48
49 case ICE_TCP_SO: return ICE_TCP_SO;
50 case ICE_TCP_ACTIVE: return ICE_TCP_PASSIVE;
51 case ICE_TCP_PASSIVE: return ICE_TCP_ACTIVE;
52 default: return (enum ice_tcptype)-1;
53 }
54}
55
56
57enum ice_cand_type ice_cand_type_base(enum ice_cand_type type)
58{
59 switch (type) {
60
61 case ICE_CAND_TYPE_HOST: return ICE_CAND_TYPE_HOST;
62 case ICE_CAND_TYPE_SRFLX: return ICE_CAND_TYPE_HOST;
63 case ICE_CAND_TYPE_PRFLX: return ICE_CAND_TYPE_HOST;
64 case ICE_CAND_TYPE_RELAY: return ICE_CAND_TYPE_RELAY;
65 default: return type;
66 }
67}
68
69
70int trice_cand_print(struct re_printf *pf, const struct ice_cand_attr *cand)
71{
72 int err = 0;
73
74 if (!cand)
75 return 0;
76
77 err |= re_hprintf(pf, "%s|%s", ice_cand_type2name(cand->type),
78 net_proto2name(cand->proto));
79
80 if (cand->proto == IPPROTO_TCP) {
81
82 err |= re_hprintf(pf, ".%s", ice_tcptype_name(cand->tcptype));
83 }
84
85 err |= re_hprintf(pf, "|%J", &cand->addr);
86
87 return err;
88}