2 #include "netinet/ipl.h"
6 static u_char sysuptime[] = { 6, 8, 0x2b, 6, 1, 2, 1, 1, 3, 0 };
8 * Enterprise number OID:
11 static u_char ipf_trap0_1[] = { 6, 10, 0x2b, 6, 1, 4, 1, 0xcd, 0x4c, 1, 1, 1 };
12 static u_char ipf_trap0_2[] = { 6, 10, 0x2b, 6, 1, 4, 1, 0xcd, 0x4c, 1, 1, 2 };
14 static int writeint __P((u_char *, int));
15 static int writelength __P((u_char *, u_int));
16 static int maketrap_v2 __P((char *, u_char *, int, u_char *, int));
17 static void snmpv2_destroy __P((void *));
18 static void *snmpv2_dup __P((void *));
19 static int snmpv2_match __P((void *, void *));
20 static void *snmpv2_parse __P((char **));
21 static void snmpv2_print __P((void *));
22 static int snmpv2_send __P((void *, ipmon_msg_t *));
25 int sendtrap_v2_0 __P((int, char *, char *, int));
27 static char def_community[] = "public"; /* ublic */
29 typedef struct snmpv2_opts_s {
36 struct sockaddr_in6 sin6;
38 struct sockaddr_in sin;
41 ipmon_saver_t snmpv2saver = {
45 snmpv2_match, /* match */
53 snmpv2_match(ctx1, ctx2)
56 snmpv2_opts_t *s1 = ctx1, *s2 = ctx2;
61 if (strcmp(s1->community, s2->community))
66 if (memcmp(&s1->sin6, &s2->sin6, sizeof(s1->sin6)))
71 if (memcmp(&s1->sin, &s2->sin, sizeof(s1->sin)))
83 snmpv2_opts_t *s = ctx;
94 snmpv2_opts_t *snmpv2 = ctx;
96 printf("%s ", snmpv2->community);
98 if (snmpv2->v6 == 1) {
101 printf("%s", inet_ntop(AF_INET6, &snmpv2->sin6.sin6_addr, buf,
102 sizeof(snmpv2->sin6.sin6_addr)));
106 printf("%s", inet_ntoa(snmpv2->sin.sin_addr));
112 snmpv2_parse(char **strings)
119 if (strings[0] == NULL || strings[0][0] == '\0')
121 if (strchr(*strings, ' ') == NULL)
124 str = strdup(*strings);
126 ctx = calloc(1, sizeof(*ctx));
132 s = strchr(str, ' ');
134 ctx->community = str;
145 if (strchr(s, ':') == NULL) {
146 result = inet_pton(AF_INET, s, &ctx->sin.sin_addr);
148 ctx->fd = socket(AF_INET, SOCK_DGRAM, 0);
150 ctx->sin.sin_family = AF_INET;
151 ctx->sin.sin_port = htons(162);
153 (struct sockaddr *)&ctx->sin,
154 sizeof(ctx->sin)) != 0) {
161 result = inet_pton(AF_INET6, s, &ctx->sin6.sin6_addr);
164 ctx->fd = socket(AF_INET6, SOCK_DGRAM, 0);
166 ctx->sin6.sin6_family = AF_INET6;
167 ctx->sin6.sin6_port = htons(162);
169 (struct sockaddr *)&ctx->sin6,
170 sizeof(ctx->sin6)) != 0) {
178 result = inet_aton(s, &ctx->sin.sin_addr);
180 ctx->fd = socket(AF_INET, SOCK_DGRAM, 0);
182 ctx->sin.sin_family = AF_INET;
183 ctx->sin.sin_port = htons(162);
184 if (connect(ctx->fd, (struct sockaddr *)&ctx->sin,
185 sizeof(ctx->sin)) != 0) {
209 snmpv2_opts_t *v2 = ctx;
224 snmpv2_send(ctx, msg)
228 snmpv2_opts_t *v2 = ctx;
230 return sendtrap_v2_0(v2->fd, v2->community,
231 msg->imm_msg, msg->imm_msglen);
234 writelength(buffer, value)
238 u_int n = htonl(value);
245 if (value > 0xffffff)
247 else if (value > 0xffff)
249 else if (value > 0xff)
254 *buffer = 0x80 | len;
256 bcopy((u_char *)&n + 4 - len, buffer + 1, len);
263 writeint(buffer, value)
276 *s++ = 0x80 | (n / 4194304);
277 n -= 4194304 * (n / 4194304);
280 *s++ = 0x80 | (n / 32768);
281 n -= 32768 * (n / 327678);
284 *s++ = 0x80 | (n / 128);
285 n -= (n / 128) * 128;
295 * First style of traps is:
296 * 1.3.6.1.4.1.9932.1.1
299 maketrap_v2(community, buffer, bufsize, msg, msglen)
306 u_char *s = buffer, *t, *pdulen;
316 if (community == NULL || *community == '\0')
317 community = def_community;
318 basesize += strlen(community) + msglen;
320 if (basesize + 8 > bufsize)
323 memset(buffer, 0xff, bufsize);
324 *s++ = 0x30; /* Sequence */
326 if (basesize - 1 >= 128) {
333 *s++ = 0x02; /* Integer32 */
334 *s++ = 0x01; /* length 1 */
335 *s++ = 0x01; /* version 2 */
336 *s++ = 0x04; /* octet string */
337 *s++ = strlen(community); /* length of "public" */
338 bcopy(community, s, s[-1]);
340 *s++ = 0xA7; /* PDU(7) */
342 if (basesize - (s - buffer) >= 128) {
350 *s++ = 0x2; /* integer */
351 *s++ = 0x4; /* len 4 */
352 *s++ = 0x0; /* noError */
353 *s++ = 0x0; /* noError */
354 *s++ = 0x0; /* noError */
355 *s++ = 0x0; /* noError */
358 *s++ = 0x2; /* integer */
359 *s++ = 0x1; /* len 1 */
360 *s++ = 0x0; /* noError */
363 *s++ = 0x2; /* integer */
364 *s++ = 0x1; /* len 1 */
365 *s++ = 0x0; /* noError */
367 *s++ = 0x30; /* sequence */
369 if (basesize - (s - buffer) >= 128) {
377 *s++ = 0x30; /* sequence */
378 *s++ = sizeof(sysuptime) + 6;
380 bcopy(sysuptime, s, sizeof(sysuptime));
381 s += sizeof(sysuptime);
383 *s++ = 0x43; /* Timestamp */
384 *s++ = 0x04; /* TimeTicks */
392 bcopy(ipf_trap0_1, t, sizeof(ipf_trap0_1));
393 t += sizeof(ipf_trap0_1);
395 *t++ = 0x2; /* Integer */
396 n = writeint(t + 1, IPFILTER_VERSION);
406 if (msglen + 1 + 1 + sizeof(ipf_trap0_2) >= 128)
411 if (msglen + 2 + 1 + sizeof(ipf_trap0_2) >= 128)
417 bcopy(ipf_trap0_2, t, sizeof(ipf_trap0_2));
418 t += sizeof(ipf_trap0_2);
420 *t++ = 0x4; /* Octet string */
421 n = writelength(t, msglen);
423 bcopy(msg, t, msglen);
426 len = t - s - trapmsglen;
429 len = t - varlen - varlensz;
430 writelength(varlen, len); /* pdu length */
432 len = t - pdulen - pdulensz;
433 writelength(pdulen, len); /* pdu length */
435 len = t - buffer - baselensz - 1;
436 writelength(buffer + 1, len); /* length of trap */
443 sendtrap_v2_0(fd, community, msg, msglen)
445 char *community, *msg;
452 n = maketrap_v2(community, buffer, sizeof(buffer),
453 (u_char *)msg, msglen);
455 return send(fd, buffer, n, 0);