Line data Source code
1 : /*
2 : Copyright (c) 2007, Adobe Systems, Incorporated
3 : All rights reserved.
4 :
5 : Redistribution and use in source and binary forms, with or without
6 : modification, are permitted provided that the following conditions are
7 : met:
8 :
9 : * Redistributions of source code must retain the above copyright
10 : notice, this list of conditions and the following disclaimer.
11 :
12 : * Redistributions in binary form must reproduce the above copyright
13 : notice, this list of conditions and the following disclaimer in the
14 : documentation and/or other materials provided with the distribution.
15 :
16 : * Neither the name of Adobe Systems, Network Resonance nor the names of its
17 : contributors may be used to endorse or promote products derived from
18 : this software without specific prior written permission.
19 :
20 : THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : */
32 :
33 :
34 :
35 : static char *RCSSTRING __UNUSED__="$Id: transport_addr_reg.c,v 1.2 2008/04/28 17:59:03 ekr Exp $";
36 :
37 : #include <csi_platform.h>
38 : #include <stdio.h>
39 : #include <string.h>
40 : #include <memory.h>
41 : #include <sys/types.h>
42 : #ifdef WIN32
43 : #include <winsock2.h>
44 : #else
45 : #include <strings.h>
46 : #include <unistd.h>
47 : #include <sys/socket.h>
48 : #include <netinet/in.h>
49 : #include <arpa/inet.h>
50 : #endif
51 : #include <assert.h>
52 : #include "nr_api.h"
53 : #include "util.h"
54 : #include "transport_addr.h"
55 : #include "transport_addr_reg.h"
56 :
57 : #ifndef INET6_ADDRSTRLEN
58 : #define INET6_ADDRSTRLEN 46 /* Value used by linux/BSD */
59 : #endif
60 :
61 : int
62 0 : nr_reg_get_transport_addr(NR_registry prefix, int keep, nr_transport_addr *addr)
63 : {
64 : int r,_status;
65 : unsigned int count;
66 0 : char *address = 0;
67 0 : UINT2 port = 0;
68 0 : char *ifname = 0;
69 0 : char *protocol = 0;
70 : int p;
71 :
72 0 : if ((r=NR_reg_get_child_count(prefix, &count)))
73 0 : ABORT(r);
74 :
75 0 : if (count == 0)
76 0 : ABORT(R_NOT_FOUND);
77 :
78 0 : if ((r=NR_reg_alloc2_string(prefix, "address", &address))) {
79 0 : if (r != R_NOT_FOUND)
80 0 : ABORT(r);
81 0 : address = 0;
82 : }
83 :
84 0 : if ((r=NR_reg_alloc2_string(prefix, "ifname", &ifname))) {
85 0 : if (r != R_NOT_FOUND)
86 0 : ABORT(r);
87 0 : ifname = 0;
88 : }
89 :
90 0 : if ((r=NR_reg_get2_uint2(prefix, "port", &port))) {
91 0 : if (r != R_NOT_FOUND)
92 0 : ABORT(r);
93 0 : port = 0;
94 : }
95 :
96 0 : if ((r=NR_reg_alloc2_string(prefix, "protocol", &protocol))) {
97 0 : if (r != R_NOT_FOUND)
98 0 : ABORT(r);
99 0 : p = IPPROTO_UDP;
100 :
101 0 : protocol = 0;
102 : }
103 : else {
104 0 : if (!strcasecmp("tcp", protocol))
105 0 : p = IPPROTO_TCP;
106 0 : else if (!strcasecmp("udp", protocol))
107 0 : p = IPPROTO_UDP;
108 : else
109 0 : ABORT(R_BAD_DATA);
110 : }
111 :
112 0 : if (!keep) memset(addr, 0, sizeof(*addr));
113 :
114 0 : if ((r=nr_str_port_to_transport_addr(address?address:"0.0.0.0", port, p, addr)))
115 0 : ABORT(r);
116 :
117 0 : if (ifname)
118 0 : strlcpy(addr->ifname, ifname, sizeof(addr->ifname));
119 :
120 0 : _status=0;
121 : abort:
122 0 : RFREE(protocol);
123 0 : RFREE(ifname);
124 0 : RFREE(address);
125 0 : return(_status);
126 : }
127 :
128 : int
129 0 : nr_reg_set_transport_addr(NR_registry prefix, int keep, nr_transport_addr *addr)
130 : {
131 : int r,_status;
132 :
133 0 : if (! keep) {
134 0 : if ((r=NR_reg_del(prefix)))
135 0 : ABORT(r);
136 : }
137 :
138 0 : switch (addr->ip_version) {
139 : case NR_IPV4:
140 0 : if (!nr_transport_addr_is_wildcard(addr)) {
141 0 : if ((r=NR_reg_set2_string(prefix, "address", inet_ntoa(addr->u.addr4.sin_addr))))
142 0 : ABORT(r);
143 : }
144 :
145 0 : if (addr->u.addr4.sin_port != 0) {
146 0 : if ((r=NR_reg_set2_uint2(prefix, "port", ntohs(addr->u.addr4.sin_port))))
147 0 : ABORT(r);
148 : }
149 0 : break;
150 :
151 : case NR_IPV6:
152 0 : if (!nr_transport_addr_is_wildcard(addr)) {
153 : char address[INET6_ADDRSTRLEN];
154 0 : if(!inet_ntop(AF_INET6, &addr->u.addr6.sin6_addr,address,sizeof(address))) {
155 0 : ABORT(R_BAD_DATA);
156 : }
157 :
158 0 : if ((r=NR_reg_set2_string(prefix, "address", address))) {
159 0 : ABORT(r);
160 : }
161 : }
162 :
163 0 : if (addr->u.addr6.sin6_port != 0) {
164 0 : if ((r=NR_reg_set2_uint2(prefix, "port", ntohs(addr->u.addr6.sin6_port))))
165 0 : ABORT(r);
166 : }
167 0 : break;
168 : default:
169 0 : ABORT(R_INTERNAL);
170 : break;
171 : }
172 :
173 : /* We abort if neither NR_IPV4 or NR_IPV6 above */
174 0 : switch (addr->protocol) {
175 : case IPPROTO_TCP:
176 0 : if ((r=NR_reg_set2_string(prefix, "protocol", "tcp")))
177 0 : ABORT(r);
178 0 : break;
179 : case IPPROTO_UDP:
180 0 : if ((r=NR_reg_set2_string(prefix, "protocol", "udp")))
181 0 : ABORT(r);
182 0 : break;
183 : default:
184 0 : UNIMPLEMENTED;
185 : break;
186 : }
187 :
188 0 : if (strlen(addr->ifname) > 0) {
189 0 : if ((r=NR_reg_set2_string(prefix, "ifname", addr->ifname)))
190 0 : ABORT(r);
191 : }
192 :
193 0 : _status=0;
194 : abort:
195 0 : if (_status)
196 0 : NR_reg_del(prefix);
197 0 : return _status;
198 : }
199 :
200 : int
201 0 : nr_reg_get_transport_addr2(NR_registry prefix, char *name, int keep, nr_transport_addr *addr)
202 : {
203 : int r, _status;
204 : NR_registry registry;
205 :
206 0 : if ((r=NR_reg_make_registry(prefix, name, registry)))
207 0 : ABORT(r);
208 :
209 0 : if ((r=nr_reg_get_transport_addr(registry, keep, addr)))
210 0 : ABORT(r);
211 :
212 0 : _status = 0;
213 : abort:
214 0 : return _status;
215 : }
216 :
217 : int
218 0 : nr_reg_set_transport_addr2(NR_registry prefix, char *name, int keep, nr_transport_addr *addr)
219 : {
220 : int r, _status;
221 : NR_registry registry;
222 :
223 0 : if ((r=NR_reg_make_registry(prefix, name, registry)))
224 0 : ABORT(r);
225 :
226 0 : if ((r=nr_reg_set_transport_addr(registry, keep, addr)))
227 0 : ABORT(r);
228 :
229 0 : _status = 0;
230 : abort:
231 0 : return _status;
232 : }
233 :
|