James Kuszmaul | 871d071 | 2021-01-17 11:30:43 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file ice.h Internal Interface to ICE |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | struct ice_tcpconn; |
| 9 | struct ice_conncheck; |
| 10 | |
| 11 | |
| 12 | /** |
| 13 | * Active Checklist. Only used by Full-ICE and Trickle-ICE |
| 14 | */ |
| 15 | struct ice_checklist { |
| 16 | struct trice *icem; /* parent */ |
| 17 | |
| 18 | struct tmr tmr_pace; /**< Timer for pacing STUN requests */ |
| 19 | uint32_t interval; /**< Interval in [ms] */ |
| 20 | struct stun *stun; /**< STUN Transport */ |
| 21 | struct list conncheckl; |
| 22 | bool is_running; /**< Checklist is running */ |
| 23 | |
| 24 | /* callback handlers */ |
| 25 | trice_estab_h *estabh; |
| 26 | trice_failed_h *failh; |
| 27 | void *arg; |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Defines an ICE media-stream |
| 33 | * |
| 34 | * NOTE: We try to follow the Resource Acquisition Is Initialization (RAII) |
| 35 | * programming idiom, which means: |
| 36 | * |
| 37 | * - at any time is the number of local/remote candidates correct |
| 38 | * - at any time is the checklist up to date (matching local/remote candidates) |
| 39 | * |
| 40 | */ |
| 41 | struct trice { |
| 42 | struct trice_conf conf; |
| 43 | enum ice_role lrole; /**< Local role */ |
| 44 | uint64_t tiebrk; /**< Tie-break value for roleconflict */ |
| 45 | |
| 46 | /* stun/authentication */ |
| 47 | char *lufrag; /**< Local Username fragment */ |
| 48 | char *lpwd; /**< Local Password */ |
| 49 | char *rufrag; /**< Remote Username fragment */ |
| 50 | char *rpwd; /**< Remote Password */ |
| 51 | |
| 52 | struct list lcandl; /**< local candidates (add order) */ |
| 53 | struct list rcandl; /**< remote candidates (add order) */ |
| 54 | struct list checkl; /**< Check List of cand pairs (sorted) */ |
| 55 | struct list validl; /**< Valid List of cand pairs (sorted) */ |
| 56 | struct list reqbufl; /**< buffered incoming requests */ |
| 57 | |
| 58 | struct ice_checklist *checklist; |
| 59 | |
| 60 | struct list connl; /**< TCP-connections for all components */ |
| 61 | |
| 62 | char *sw; |
| 63 | |
| 64 | /* Port range */ |
| 65 | struct { |
| 66 | uint16_t min; |
| 67 | uint16_t max; |
| 68 | } ports; |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Holds an unhandled STUN request message that will be handled once |
| 74 | * the role has been determined. |
| 75 | */ |
| 76 | struct trice_reqbuf { |
| 77 | struct le le; /**< list element */ |
| 78 | struct ice_lcand *lcand; /**< corresponding local candidate */ |
| 79 | void *sock; /**< request's socket */ |
| 80 | struct sa src; /**< source address */ |
| 81 | struct stun_msg *req; /**< buffered STUN request */ |
| 82 | size_t presz; /**< number of bytes in preamble */ |
| 83 | }; |
| 84 | |
| 85 | |
| 86 | /* return TRUE if handled */ |
| 87 | typedef bool (tcpconn_frame_h)(struct trice *icem, |
| 88 | struct tcp_conn *tc, struct sa *src, |
| 89 | struct mbuf *mb, void *arg); |
| 90 | |
| 91 | /** |
| 92 | * Defines a TCP-connection from local-adress to remote-address |
| 93 | * |
| 94 | * - one TCP-connection can be shared by multiple candidate pairs |
| 95 | * |
| 96 | * - one TCP-connection is always created by the Local Candidate |
| 97 | */ |
| 98 | struct ice_tcpconn { |
| 99 | struct trice *icem; /* parent */ |
| 100 | struct le le; |
| 101 | struct tcp_conn *tc; |
| 102 | struct shim *shim; |
| 103 | struct sa laddr; |
| 104 | struct sa paddr; |
| 105 | unsigned compid; |
| 106 | int layer; |
| 107 | bool active; |
| 108 | bool estab; |
| 109 | |
| 110 | tcpconn_frame_h *frameh; |
| 111 | void *arg; |
| 112 | }; |
| 113 | |
| 114 | struct ice_conncheck { |
| 115 | struct le le; |
| 116 | struct ice_candpair *pair; /* pointer */ |
| 117 | struct stun_ctrans *ct_conn; |
| 118 | struct trice *icem; /* owner */ |
| 119 | bool use_cand; |
| 120 | bool term; |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | /* cand */ |
| 125 | int trice_add_lcandidate(struct ice_lcand **candp, |
| 126 | struct trice *icem, struct list *lst, |
| 127 | unsigned compid, char *foundation, int proto, |
| 128 | uint32_t prio, const struct sa *addr, |
| 129 | const struct sa *base_addr, |
| 130 | enum ice_cand_type type, |
| 131 | const struct sa *rel_addr, |
| 132 | enum ice_tcptype tcptype); |
| 133 | int trice_lcands_debug(struct re_printf *pf, const struct list *lst); |
| 134 | int trice_rcands_debug(struct re_printf *pf, const struct list *lst); |
| 135 | |
| 136 | |
| 137 | /* candpair */ |
| 138 | int trice_candpair_alloc(struct ice_candpair **cpp, struct trice *icem, |
| 139 | struct ice_lcand *lcand, struct ice_rcand *rcand); |
| 140 | void trice_candpair_prio_order(struct list *lst, bool controlling); |
| 141 | void trice_candpair_make_valid(struct trice *icem, struct ice_candpair *pair); |
| 142 | void trice_candpair_failed(struct ice_candpair *cp, int err, uint16_t scode); |
| 143 | void trice_candpair_set_state(struct ice_candpair *cp, |
| 144 | enum ice_candpair_state state); |
| 145 | bool trice_candpair_iscompleted(const struct ice_candpair *cp); |
| 146 | bool trice_candpair_cmp_fnd(const struct ice_candpair *cp1, |
| 147 | const struct ice_candpair *cp2); |
| 148 | struct ice_candpair *trice_candpair_find(const struct list *lst, |
| 149 | const struct ice_lcand *lcand, |
| 150 | const struct ice_rcand *rcand); |
| 151 | int trice_candpair_with_local(struct trice *icem, struct ice_lcand *lcand); |
| 152 | int trice_candpair_with_remote(struct trice *icem, struct ice_rcand *rcand); |
| 153 | const char *trice_candpair_state2name(enum ice_candpair_state st); |
| 154 | |
| 155 | |
| 156 | /* STUN server */ |
| 157 | int trice_stund_recv(struct trice *icem, struct ice_lcand *lcand, |
| 158 | void *sock, const struct sa *src, |
| 159 | struct stun_msg *req, size_t presz); |
| 160 | int trice_stund_recv_role_set(struct trice *icem, struct ice_lcand *lcand, |
| 161 | void *sock, const struct sa *src, |
| 162 | struct stun_msg *req, size_t presz); |
| 163 | |
| 164 | |
| 165 | /* ICE media */ |
| 166 | void trice_switch_local_role(struct trice *ice); |
| 167 | void trice_printf(struct trice *icem, const char *fmt, ...); |
| 168 | void trice_tracef(struct trice *icem, int color, const char *fmt, ...); |
| 169 | |
| 170 | |
| 171 | /* ICE checklist */ |
| 172 | int trice_checklist_debug(struct re_printf *pf, |
| 173 | const struct ice_checklist *ic); |
| 174 | void trice_conncheck_schedule_check(struct trice *icem); |
| 175 | int trice_checklist_update(struct trice *icem); |
| 176 | void trice_checklist_refresh(struct trice *icem); |
| 177 | |
| 178 | |
| 179 | /* ICE conncheck */ |
| 180 | int trice_conncheck_stun_request(struct ice_checklist *ic, |
| 181 | struct ice_conncheck *cc, |
| 182 | struct ice_candpair *cp, void *sock, |
| 183 | bool cc_use_cand); |
| 184 | int trice_conncheck_trigged(struct trice *icem, struct ice_candpair *pair, |
| 185 | void *sock, bool use_cand); |
| 186 | int trice_conncheck_debug(struct re_printf *pf, |
| 187 | const struct ice_conncheck *cc); |
| 188 | |
| 189 | |
| 190 | /* TCP connections */ |
| 191 | |
| 192 | |
| 193 | int trice_conn_alloc(struct list *connl, struct trice *icem, unsigned compid, |
| 194 | bool active, const struct sa *laddr, const struct sa *peer, |
| 195 | struct tcp_sock *ts, int layer, |
| 196 | tcpconn_frame_h *frameh, void *arg); |
| 197 | struct ice_tcpconn *trice_conn_find(struct list *connl, unsigned compid, |
| 198 | const struct sa *laddr, |
| 199 | const struct sa *peer); |
| 200 | int trice_conn_debug(struct re_printf *pf, const struct ice_tcpconn *conn); |
| 201 | |
| 202 | |
| 203 | bool trice_stun_process(struct trice *icem, struct ice_lcand *lcand, |
| 204 | int proto, void *sock, const struct sa *src, |
| 205 | struct mbuf *mb); |
| 206 | int trice_reqbuf_append(struct trice *icem, struct ice_lcand *lcand, |
| 207 | void *sock, const struct sa *src, |
| 208 | struct stun_msg *req, size_t presz); |