/src/rtpproxy/src/rtpp_bindargs.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2004-2006 Maxim Sobolev <sobomax@FreeBSD.org> |
3 | | * Copyright (c) 2006-2026 Sippy Software, Inc., http://www.sippysoft.com |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions |
8 | | * are met: |
9 | | * 1. Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * 2. Redistributions in binary form must reproduce the above copyright |
12 | | * notice, this list of conditions and the following disclaimer in the |
13 | | * documentation and/or other materials provided with the distribution. |
14 | | * |
15 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 | | * SUCH DAMAGE. |
26 | | * |
27 | | */ |
28 | | |
29 | | #include <string.h> |
30 | | #include <netdb.h> |
31 | | #include <sys/socket.h> |
32 | | #include <assert.h> |
33 | | |
34 | | #include "config_pp.h" |
35 | | #include "rtpp_str.h" |
36 | | #include "rtpp_types.h" |
37 | | #include "rtpp_log.h" |
38 | | #include "rtpp_log_obj.h" |
39 | | #include "rtpp_bindaddr.h" |
40 | | #include "rtpp_bindargs.h" |
41 | | #include "rtpp_bindaddrs.h" |
42 | | #include "rtpp_cfg.h" |
43 | | |
44 | | void |
45 | | bindarg_parse(struct bindarg *bap, char *barg, int is_v6) |
46 | 0 | { |
47 | 0 | char *cp = strchr(barg, '@'); |
48 | 0 | if (cp != NULL) { |
49 | 0 | cp[0] = '\0'; |
50 | 0 | bap->params.label = rtpp_str_const_i(barg); |
51 | 0 | barg = cp + 1; |
52 | 0 | } |
53 | 0 | cp = strchr(barg, '='); |
54 | 0 | if (cp != NULL) { |
55 | 0 | cp[0] = '\0'; |
56 | 0 | cp += 1; |
57 | 0 | bap->params.advaddr = rtpp_str_const_i(cp); |
58 | 0 | } |
59 | 0 | bap->h = barg; |
60 | 0 | bap->is_v6 = is_v6; |
61 | 0 | } |
62 | | |
63 | | static int |
64 | | ba_firstidx(const struct bindarg bargs[], int banum, int v6) |
65 | 0 | { |
66 | 0 | for (int i = 0; i < banum; i++) { |
67 | 0 | if (bargs[i].is_v6 == v6) |
68 | 0 | return i; |
69 | 0 | } |
70 | 0 | return -1; |
71 | 0 | } |
72 | | |
73 | | int |
74 | | init_bindaddrs(struct rtpp_cfg *cfsp, struct bindarg bargs[], int banum, |
75 | | const struct rtpp_bindaddr_params iebparams[2]) |
76 | 2 | { |
77 | 2 | int i; |
78 | 2 | char *bh1; |
79 | 2 | const char *errmsg; |
80 | | |
81 | 2 | if (banum == 0) { |
82 | 2 | bargs[0].h = "*"; |
83 | 2 | banum = 1; |
84 | 2 | } else { |
85 | 0 | struct bindarg iaddr = {0}, eaddr = {0}; |
86 | 0 | int f_idx[2] = {ba_firstidx(bargs, banum, 0), |
87 | 0 | ba_firstidx(bargs, banum, 1)}; |
88 | 0 | for (int j = 0; j < 2; j++) { |
89 | 0 | i = f_idx[j]; |
90 | 0 | if (i < 0) |
91 | 0 | continue; |
92 | 0 | bh1 = strchr(bargs[i].h, '/'); |
93 | 0 | if (bh1 != NULL) { |
94 | 0 | if (eaddr.h != NULL) { |
95 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "either IPv4 or IPv6 should be configured for external " |
96 | 0 | "interface in bridging mode, not both"); |
97 | 0 | return (-1); |
98 | 0 | } |
99 | 0 | cfsp->bmode = 1; |
100 | 0 | bh1[0] = '\0'; |
101 | 0 | eaddr = bargs[i]; |
102 | 0 | eaddr.h = bh1 + 1; |
103 | 0 | if (bargs[i].h == bh1) { |
104 | 0 | bargs[i].h = NULL; |
105 | 0 | } |
106 | 0 | } |
107 | 0 | } |
108 | 0 | if (cfsp->bmode) { |
109 | 0 | for (int j = 0; j < 2; j++) { |
110 | 0 | i = f_idx[j]; |
111 | 0 | if (i < 0) |
112 | 0 | continue; |
113 | 0 | if (bargs[i].h == NULL) |
114 | 0 | continue; |
115 | 0 | if (iaddr.h != NULL) { |
116 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "either IPv4 or IPv6 should be configured for internal " |
117 | 0 | "interface in bridging mode, not both"); |
118 | 0 | return (-1); |
119 | 0 | } |
120 | 0 | iaddr = bargs[i]; |
121 | 0 | bargs[i].h = NULL; |
122 | 0 | } |
123 | 0 | assert(eaddr.h != NULL); |
124 | 0 | if (iaddr.h == NULL) { |
125 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "incomplete configuration of the bridging mode - exactly " |
126 | 0 | "2 listen addresses required"); |
127 | 0 | return (-1); |
128 | 0 | } |
129 | 0 | if (iebparams[0].advaddr.s != NULL && iebparams[1].advaddr.s == NULL) { |
130 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "two advertised addresses are required for internal " |
131 | 0 | "and external interfaces in bridging mode"); |
132 | 0 | return (-1); |
133 | 0 | } |
134 | 0 | for (i = 0; i < 2; i++) { |
135 | 0 | const struct bindarg *bap = (i == 0) ? &iaddr : &eaddr; |
136 | 0 | if (bap->params.label.s != NULL) { |
137 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "bridging address(es) cannot have a label"); |
138 | 0 | return (-1); |
139 | 0 | } |
140 | 0 | if (bap->params.advaddr.s != NULL) { |
141 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "bridging address(es) cannot have an advertized" |
142 | 0 | "address, use matching -A argument"); |
143 | 0 | return (-1); |
144 | 0 | } |
145 | 0 | int rmode = AI_ADDRCONFIG | AI_PASSIVE; |
146 | 0 | rmode |= cfsp->no_resolve ? AI_NUMERICHOST : 0; |
147 | 0 | cfsp->bindaddr[i] = CALL_SMETHOD(cfsp->bindaddrs_cf, |
148 | 0 | host2, bap->h, bap->is_v6 ? AF_INET6 : AF_INET, rmode, &errmsg, |
149 | 0 | &iebparams[i]); |
150 | 0 | if (cfsp->bindaddr[i] == NULL) { |
151 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "host2bindaddr(%s): %s", bap->h, errmsg); |
152 | 0 | return (-1); |
153 | 0 | } |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | 4 | for (i = 0; i < banum; i++) { |
159 | 2 | const struct rtpp_bindaddr *baddr; |
160 | 2 | if (bargs[i].h == NULL) |
161 | 0 | continue; |
162 | 2 | if (cfsp->bindaddr[0] != NULL && bargs[i].params.label.s == NULL) { |
163 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "address label is missing"); |
164 | 0 | return (-1); |
165 | 0 | } |
166 | 2 | if (strchr(bargs[i].h, '/') != NULL) { |
167 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "only the first %s listening address can have external part", |
168 | 0 | bargs[i].is_v6 ? "IPv6" : "IPv4"); |
169 | 0 | return (-1); |
170 | 0 | } |
171 | 2 | int rmode = AI_ADDRCONFIG | AI_PASSIVE; |
172 | 2 | rmode |= cfsp->no_resolve ? AI_NUMERICHOST : 0; |
173 | 2 | baddr = CALL_SMETHOD(cfsp->bindaddrs_cf, |
174 | 2 | host2, bargs[i].h, bargs[i].is_v6 ? AF_INET6 : AF_INET, rmode, |
175 | 2 | &errmsg, &bargs[i].params); |
176 | 2 | if (baddr == NULL) { |
177 | 0 | RTPP_LOG(cfsp->glog, RTPP_LOG_ERR, "host2bindaddr(%s): %s", bargs[i].h, errmsg); |
178 | 0 | return (-1); |
179 | 0 | } |
180 | 2 | if (cfsp->bindaddr[0] == NULL) |
181 | 2 | cfsp->bindaddr[0] = baddr; |
182 | 2 | } |
183 | | |
184 | 2 | assert(cfsp->bindaddr[0] != NULL && cfsp->bindaddr[0]->addr != NULL); |
185 | 2 | return (0); |
186 | 2 | } |