1 #include <net-snmp/net-snmp-config.h>
2
3 #include <net-snmp/library/snmpAAL5PVCDomain.h>
4
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <errno.h>
9
10 #if HAVE_STRING_H
11 #include <string.h>
12 #else
13 #include <strings.h>
14 #endif
15 #if HAVE_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #if HAVE_SYS_SOCKET_H
22 #include <sys/socket.h>
23 #endif
24 #include <atm.h>
25
26 #include <net-snmp/types.h>
27 #include <net-snmp/output_api.h>
28 #include <net-snmp/config_api.h>
29
30 #include <net-snmp/library/snmp_transport.h>
31 #include <net-snmp/library/tools.h>
32
33
34 oid netsnmp_AAL5PVCDomain[10] = { NETSNMP_ENTERPRISE_MIB, 3, 3, 3 };
35 static netsnmp_tdomain aal5pvcDomain;
36
37
38 /*
39 * Return a string representing the address in data, or else the "far end"
40 * address if data is NULL.
41 */
42
43 static char *
netsnmp_aal5pvc_fmtaddr(netsnmp_transport * t,const void * data,int len)44 netsnmp_aal5pvc_fmtaddr(netsnmp_transport *t, const void *data, int len)
45 {
46 const struct sockaddr_atmpvc *to = NULL;
47
48 if (data != NULL && len == sizeof(struct sockaddr_atmpvc)) {
49 to = (const struct sockaddr_atmpvc *) data;
50 } else if (t != NULL && t->data != NULL &&
51 t->data_length == sizeof(struct sockaddr_atmpvc)) {
52 to = (const struct sockaddr_atmpvc *) t->data;
53 }
54 if (to == NULL) {
55 return strdup("AAL5 PVC: unknown");
56 } else {
57 char *tmp;
58
59 if (asprintf(&tmp, "AAL5 PVC: %hd.%hd.%d", to->sap_addr.itf,
60 to->sap_addr.vpi, to->sap_addr.vci) < 0)
61 tmp = NULL;
62 return tmp;
63 }
64 }
65
66 static void
netsnmp_aal5pvc_get_taddr(netsnmp_transport * t,void ** addr,size_t * addr_len)67 netsnmp_aal5pvc_get_taddr(netsnmp_transport *t, void **addr, size_t *addr_len)
68 {
69 struct sockaddr_atmpvc *sa = t->remote;
70 unsigned char *p;
71
72 *addr_len = 8;
73 if (!(*addr = malloc(*addr_len)))
74 return;
75 p = *addr;
76 p[0] = sa->sap_addr.itf >> 8;
77 p[1] = sa->sap_addr.itf >> 0;
78 p[2] = sa->sap_addr.vpi >> 8;
79 p[3] = sa->sap_addr.vpi >> 0;
80 p[4] = sa->sap_addr.vci >> 24;
81 p[5] = sa->sap_addr.vci >> 16;
82 p[6] = sa->sap_addr.vci >> 8;
83 p[7] = sa->sap_addr.vci >> 0;
84 }
85
86 /*
87 * You can write something into opaque that will subsequently get passed back
88 * to your send function if you like. For instance, you might want to
89 * remember where a PDU came from, so that you can send a reply there...
90 */
91
92 static int
netsnmp_aal5pvc_recv(netsnmp_transport * t,void * buf,int size,void ** opaque,int * olength)93 netsnmp_aal5pvc_recv(netsnmp_transport *t, void *buf, int size,
94 void **opaque, int *olength)
95 {
96 int rc = -1;
97
98 if (t != NULL && t->sock >= 0) {
99 while (rc < 0) {
100 rc = recvfrom(t->sock, buf, size, 0, NULL, NULL);
101 if (rc < 0 && errno != EINTR) {
102 break;
103 }
104 }
105
106 if (rc >= 0) {
107 DEBUGIF("netsnmp_aal5pvc") {
108 char *str = netsnmp_aal5pvc_fmtaddr(t, NULL, 0);
109 DEBUGMSGTL(("netsnmp_aal5pvc",
110 "recv on fd %d got %d bytes (from %s)\n", t->sock,
111 rc, str));
112 free(str);
113 }
114 } else {
115 DEBUGMSGTL(("netsnmp_aal5pvc", "recv on fd %d err %d (\"%s\")\n",
116 t->sock, errno, strerror(errno)));
117 }
118 *opaque = NULL;
119 *olength = 0;
120 }
121 return rc;
122 }
123
124
125
126 static int
netsnmp_aal5pvc_send(netsnmp_transport * t,const void * buf,int size,void ** opaque,int * olength)127 netsnmp_aal5pvc_send(netsnmp_transport *t, const void *buf, int size,
128 void **opaque, int *olength)
129 {
130 int rc = -1;
131 const struct sockaddr *to = NULL;
132
133 if (opaque != NULL && *opaque != NULL &&
134 *olength == sizeof(struct sockaddr_atmpvc)) {
135 to = (const struct sockaddr *) (*opaque);
136 } else if (t != NULL && t->data != NULL &&
137 t->data_length == sizeof(struct sockaddr_atmpvc)) {
138 to = (const struct sockaddr *) (t->data);
139 }
140
141 if (to != NULL && t != NULL && t->sock >= 0) {
142 DEBUGIF("netsnmp_aal5pvc") {
143 char *str = netsnmp_aal5pvc_fmtaddr(NULL, to,
144 sizeof(struct sockaddr_atmpvc));
145 DEBUGMSGTL(("netsnmp_aal5pvc",
146 "send %d bytes from %p to %s on fd %d\n",
147 size, buf, str, t->sock));
148 free(str);
149 }
150 while (rc < 0) {
151 rc = sendto(t->sock, buf, size, 0, NULL, 0);
152 if (rc < 0 && errno != EINTR) {
153 break;
154 }
155 }
156 }
157 return rc;
158 }
159
160
161
162 static int
netsnmp_aal5pvc_close(netsnmp_transport * t)163 netsnmp_aal5pvc_close(netsnmp_transport *t)
164 {
165 int rc = -1;
166
167 if (t->sock >= 0) {
168 DEBUGMSGTL(("netsnmp_aal5pvc", "close fd %d\n", t->sock));
169 #ifndef HAVE_CLOSESOCKET
170 rc = close(t->sock);
171 #else
172 rc = closesocket(t->sock);
173 #endif
174 t->sock = -1;
175 }
176 return rc;
177 }
178
179
180
181 /*
182 * Open an AAL5 PVC transport for SNMP. Local is TRUE if addr is the local
183 * NSAP to bind to (i.e. this is a server-type session); otherwise addr is
184 * the remote NSAP to send things to.
185 */
186
187 netsnmp_transport *
netsnmp_aal5pvc_transport(const struct sockaddr_atmpvc * addr,int local)188 netsnmp_aal5pvc_transport(const struct sockaddr_atmpvc *addr, int local)
189 {
190 netsnmp_transport *t = NULL;
191
192 #ifdef NETSNMP_NO_LISTEN_SUPPORT
193 if (local)
194 return NULL;
195 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
196
197 if (addr == NULL || addr->sap_family != AF_ATMPVC) {
198 return NULL;
199 }
200
201 t = SNMP_MALLOC_TYPEDEF(netsnmp_transport);
202 if (t == NULL) {
203 return NULL;
204 }
205
206 DEBUGIF("netsnmp_aal5pvc") {
207 char *str = netsnmp_aal5pvc_fmtaddr(NULL, addr,
208 sizeof(struct sockaddr_atmpvc));
209 DEBUGMSGTL(("netsnmp_aal5pvc", "open %s %s\n",
210 local ? "local" : "remote", str));
211 free(str);
212 }
213
214 t->domain = netsnmp_AAL5PVCDomain;
215 t->domain_length =
216 sizeof(netsnmp_AAL5PVCDomain) / sizeof(netsnmp_AAL5PVCDomain[0]);
217
218 t->sock = socket(PF_ATMPVC, SOCK_DGRAM, 0);
219 if (t->sock < 0) {
220 DEBUGMSGTL(("netsnmp_aal5pvc","socket failed (%s)\n",strerror(errno)));
221 netsnmp_transport_free(t);
222 return NULL;
223 }
224 DEBUGMSGTL(("netsnmp_aal5pvc", "fd %d opened\n", t->sock));
225
226 {
227 /*
228 * Set up the QOS parameters.
229 */
230
231 struct atm_qos qos = { 0 };
232 qos.aal = ATM_AAL5;
233 qos.rxtp.traffic_class = ATM_UBR;
234 qos.rxtp.max_sdu = SNMP_MAX_LEN; /* Hmm -- this is a bit small? */
235 qos.txtp = qos.rxtp;
236
237 if (setsockopt(t->sock, SOL_ATM, SO_ATMQOS, &qos, sizeof(qos)) < 0) {
238 DEBUGMSGTL(("netsnmp_aal5pvc", "setsockopt failed (%s)\n",
239 strerror(errno)));
240 netsnmp_aal5pvc_close(t);
241 netsnmp_transport_free(t);
242 return NULL;
243 }
244 }
245
246 if (local) {
247 #ifndef NETSNMP_NO_LISTEN_SUPPORT
248 t->local_length = sizeof(*addr);
249 t->local = netsnmp_memdup(addr, sizeof(*addr));
250 if (t->local == NULL) {
251 netsnmp_transport_free(t);
252 return NULL;
253 }
254
255 if (bind(t->sock, (const struct sockaddr *)addr,
256 sizeof(struct sockaddr_atmpvc)) < 0) {
257 DEBUGMSGTL(("netsnmp_aal5pvc", "bind failed (%s)\n",
258 strerror(errno)));
259 netsnmp_aal5pvc_close(t);
260 netsnmp_transport_free(t);
261 return NULL;
262 }
263 #else /* NETSNMP_NO_LISTEN_SUPPORT */
264 return NULL;
265 #endif /* NETSNMP_NO_LISTEN_SUPPORT */
266 } else {
267 t->remote_length = sizeof(*addr);
268 t->remote = netsnmp_memdup(addr, sizeof(*addr));
269 if (t->remote == NULL) {
270 netsnmp_transport_free(t);
271 return NULL;
272 }
273
274 if (connect(t->sock, (const struct sockaddr *)addr,
275 sizeof(struct sockaddr_atmpvc)) < 0) {
276 DEBUGMSGTL(("netsnmp_aal5pvc", "connect failed (%s)\n",
277 strerror(errno)));
278 netsnmp_aal5pvc_close(t);
279 netsnmp_transport_free(t);
280 return NULL;
281 }
282 }
283
284 t->data = malloc(sizeof(struct sockaddr_atmpvc));
285 if (t->data == NULL) {
286 netsnmp_transport_free(t);
287 return NULL;
288 }
289 memcpy(t->data, addr, sizeof(struct sockaddr_atmpvc));
290 t->data_length = sizeof(struct sockaddr_atmpvc);
291
292 /*
293 * 16-bit length field in the trailer, no headers.
294 */
295
296 t->msgMaxSize = 0xffff;
297 t->f_recv = netsnmp_aal5pvc_recv;
298 t->f_send = netsnmp_aal5pvc_send;
299 t->f_close = netsnmp_aal5pvc_close;
300 t->f_accept = NULL;
301 t->f_fmtaddr = netsnmp_aal5pvc_fmtaddr;
302 t->f_get_taddr = netsnmp_aal5pvc_get_taddr;
303
304 return t;
305 }
306
307
308
309 netsnmp_transport *
netsnmp_aal5pvc_create_tstring(const char * str,int local,const char * default_target)310 netsnmp_aal5pvc_create_tstring(const char *str, int local,
311 const char *default_target)
312 {
313 struct sockaddr_atmpvc addr;
314
315 if (str == NULL || *str == '\0')
316 str = default_target;
317
318 if (str != NULL) {
319 addr.sap_family = AF_ATMPVC;
320
321 if (sscanf(str, "%hd.%hd.%d", &(addr.sap_addr.itf),
322 &(addr.sap_addr.vpi), &(addr.sap_addr.vci)) == 3) {
323 return netsnmp_aal5pvc_transport(&addr, local);
324 } else if (sscanf(str, "%hd.%d", &(addr.sap_addr.vpi),
325 &(addr.sap_addr.vci)) == 2) {
326 addr.sap_addr.itf = 0;
327 return netsnmp_aal5pvc_transport(&addr, local);
328 } else if (sscanf(str, "%d", &(addr.sap_addr.vci)) == 1) {
329 addr.sap_addr.itf = 0;
330 addr.sap_addr.vpi = 0;
331 return netsnmp_aal5pvc_transport(&addr, local);
332 } else {
333 return NULL;
334 }
335 } else {
336 return NULL;
337 }
338 }
339
netsnmp_aal5pvc_ostring_to_sockaddr(struct sockaddr_atmpvc * sa,const void * o,size_t o_len)340 static int netsnmp_aal5pvc_ostring_to_sockaddr(struct sockaddr_atmpvc *sa,
341 const void *o, size_t o_len)
342 {
343 const unsigned char *p = o;
344
345 if (o_len != 8)
346 return 0;
347
348 memset(sa, 0, sizeof(*sa));
349 sa->sap_family = AF_ATMPVC;
350 sa->sap_addr.itf = (p[0] << 8) + (p[1] << 0);
351 sa->sap_addr.vpi = (p[2] << 8) + (p[3] << 0);
352 sa->sap_addr.vci = (p[4] << 24) + (p[5] << 16) + (p[6] << 8) + (p[7] << 0);
353 return 1;
354 }
355
356 netsnmp_transport *
netsnmp_aal5pvc_create_ostring(const void * o,size_t o_len,int local)357 netsnmp_aal5pvc_create_ostring(const void *o, size_t o_len, int local)
358 {
359 struct sockaddr_atmpvc sa;
360
361 if (netsnmp_aal5pvc_ostring_to_sockaddr(&sa, o, o_len))
362 return netsnmp_aal5pvc_transport(&sa, local);
363 return NULL;
364 }
365
366
367
368 void
netsnmp_aal5pvc_ctor(void)369 netsnmp_aal5pvc_ctor(void)
370 {
371 aal5pvcDomain.name = netsnmp_AAL5PVCDomain;
372 aal5pvcDomain.name_length = sizeof(netsnmp_AAL5PVCDomain) / sizeof(oid);
373 aal5pvcDomain.prefix = (const char**)calloc(3, sizeof(char *));
374 aal5pvcDomain.prefix[0] = "aal5pvc";
375 aal5pvcDomain.prefix[1] = "pvc";
376
377 aal5pvcDomain.f_create_from_tstring_new = netsnmp_aal5pvc_create_tstring;
378 aal5pvcDomain.f_create_from_ostring = netsnmp_aal5pvc_create_ostring;
379
380 netsnmp_tdomain_register(&aal5pvcDomain);
381 }
382