xref: /onnv-gate/usr/src/cmd/agents/snmp/snmplib/trap.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <unistd.h>
31*0Sstevel@tonic-gate #include <sys/types.h>
32*0Sstevel@tonic-gate #include <netinet/in.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <sys/socket.h>
35*0Sstevel@tonic-gate #include <errno.h>
36*0Sstevel@tonic-gate #include <syslog.h>
37*0Sstevel@tonic-gate #include <string.h>
38*0Sstevel@tonic-gate #include <arpa/inet.h>
39*0Sstevel@tonic-gate #include <netdb.h>
40*0Sstevel@tonic-gate #include <nlist.h>
41*0Sstevel@tonic-gate #include "snmp_msg.h"
42*0Sstevel@tonic-gate #include "impl.h"
43*0Sstevel@tonic-gate #include "trace.h"
44*0Sstevel@tonic-gate #include "snmp.h"
45*0Sstevel@tonic-gate #include "pdu.h"
46*0Sstevel@tonic-gate #include "request.h"
47*0Sstevel@tonic-gate #include "trap.h"
48*0Sstevel@tonic-gate #include "error.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate /***** GLOBAL VARIABLES *****/
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate char *trap_community = NULL;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate Subid sun_subids[] = { 1, 3, 6, 1, 4, 1, 42, 2, 1, 1 };
56*0Sstevel@tonic-gate Oid sun_oid = { sun_subids, 10 };
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /***** LOCAL TYPES *****/
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate typedef struct _Trap_Destinator {
62*0Sstevel@tonic-gate 	struct _Trap_Destinator	*next_trap_destinator;
63*0Sstevel@tonic-gate 	char			*name;
64*0Sstevel@tonic-gate 	IPAddress		ip_address;
65*0Sstevel@tonic-gate } Trap_Destinator;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /***** LOCAL VARIABLES *****/
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static Oid *default_enterprise = NULL;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate static Trap_Destinator *first_trap_destinator = NULL;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /********************************************************************/
76*0Sstevel@tonic-gate 
trap_init(Oid * enterprise,char * error_label)77*0Sstevel@tonic-gate int trap_init(Oid *enterprise, char *error_label)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	error_label[0] = '\0';
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	if(enterprise == NULL)
82*0Sstevel@tonic-gate 	{
83*0Sstevel@tonic-gate 		sprintf(error_label, "BUG: trap_init(): enterprise is NULL");
84*0Sstevel@tonic-gate 		return -1;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate 	SSAOidFree(default_enterprise);
88*0Sstevel@tonic-gate 	default_enterprise = NULL;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	default_enterprise = SSAOidDup(enterprise, error_label);
91*0Sstevel@tonic-gate 	if(default_enterprise == NULL)
92*0Sstevel@tonic-gate 	{
93*0Sstevel@tonic-gate 		return -1;
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	return 0;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /********************************************************************/
101*0Sstevel@tonic-gate 
trap_send(IPAddress * ip_address,Oid * enterprise,int generic,int specific,SNMP_variable * variables,char * error_label)102*0Sstevel@tonic-gate int trap_send(IPAddress *ip_address, Oid *enterprise, int generic, int specific, SNMP_variable *variables, char *error_label)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	static int my_ip_address_initialized = False;
105*0Sstevel@tonic-gate 	static IPAddress my_ip_address;
106*0Sstevel@tonic-gate 	struct sockaddr_in me;
107*0Sstevel@tonic-gate 	int sd;
108*0Sstevel@tonic-gate 	Address address;
109*0Sstevel@tonic-gate 	SNMP_pdu *pdu;
110*0Sstevel@tonic-gate 	SNMP_variable *last_variable = NULL;
111*0Sstevel@tonic-gate 	SNMP_variable *new_variable;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	error_label[0] = '\0';
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	if(my_ip_address_initialized == False)
117*0Sstevel@tonic-gate 	{
118*0Sstevel@tonic-gate 		if(get_my_ip_address(&my_ip_address, error_label))
119*0Sstevel@tonic-gate 		{
120*0Sstevel@tonic-gate 			return -1;
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		my_ip_address_initialized = True;
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	pdu = snmp_pdu_new(error_label);
127*0Sstevel@tonic-gate 	if(pdu == NULL)
128*0Sstevel@tonic-gate 	{
129*0Sstevel@tonic-gate 		return -1;
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	/* version, community */
133*0Sstevel@tonic-gate 	pdu->version = SNMP_VERSION_1;
134*0Sstevel@tonic-gate 	if(trap_community == NULL)
135*0Sstevel@tonic-gate 	{
136*0Sstevel@tonic-gate 		pdu->community = strdup("public");
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	else
139*0Sstevel@tonic-gate 	{
140*0Sstevel@tonic-gate 		pdu->community = strdup(trap_community);
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	if(pdu->community == NULL)
143*0Sstevel@tonic-gate 	{
144*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_ALLOC);
145*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
146*0Sstevel@tonic-gate 		return -1;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* type */
150*0Sstevel@tonic-gate 	pdu->type = TRP_REQ_MSG;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/* enterprise */
153*0Sstevel@tonic-gate 	if(enterprise == NULL)
154*0Sstevel@tonic-gate 	{
155*0Sstevel@tonic-gate 		if(default_enterprise)
156*0Sstevel@tonic-gate 		{
157*0Sstevel@tonic-gate 			enterprise = default_enterprise;
158*0Sstevel@tonic-gate 		}
159*0Sstevel@tonic-gate 		else
160*0Sstevel@tonic-gate 		{
161*0Sstevel@tonic-gate 			enterprise = &sun_oid;
162*0Sstevel@tonic-gate 		}
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	if(SSAOidCpy(&(pdu->enterprise), enterprise, error_label))
165*0Sstevel@tonic-gate 	{
166*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
167*0Sstevel@tonic-gate 		return -1;
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/* agent_addr */
171*0Sstevel@tonic-gate 	pdu->ip_agent_addr.s_addr = my_ip_address.s_addr;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	/* generic, specific */
174*0Sstevel@tonic-gate 	pdu->generic = generic;
175*0Sstevel@tonic-gate 	pdu->specific = specific;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/* time_stamp */
178*0Sstevel@tonic-gate 	pdu->time_stamp = request_sysUpTime(error_label, NULL);
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/* first_variable */
181*0Sstevel@tonic-gate 	while(variables)
182*0Sstevel@tonic-gate 	{
183*0Sstevel@tonic-gate 		new_variable = snmp_variable_dup(variables, error_label);
184*0Sstevel@tonic-gate 		if(new_variable == NULL)
185*0Sstevel@tonic-gate 		{
186*0Sstevel@tonic-gate 			snmp_pdu_free(pdu);
187*0Sstevel@tonic-gate 			return -1;
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 		if(last_variable)
191*0Sstevel@tonic-gate 		{
192*0Sstevel@tonic-gate 			last_variable->next_variable = new_variable;
193*0Sstevel@tonic-gate 		}
194*0Sstevel@tonic-gate 		else
195*0Sstevel@tonic-gate 		{
196*0Sstevel@tonic-gate 			pdu->first_variable = new_variable;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		last_variable = new_variable;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 		variables = variables->next_variable;
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	/* sd */
205*0Sstevel@tonic-gate 	sd = socket(AF_INET, SOCK_DGRAM, 0);
206*0Sstevel@tonic-gate 	if(sd < 0)
207*0Sstevel@tonic-gate 	{
208*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_SOCKET, errno_string());
209*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
210*0Sstevel@tonic-gate 		return -1;
211*0Sstevel@tonic-gate 	}
212*0Sstevel@tonic-gate 	me.sin_family = AF_INET;
213*0Sstevel@tonic-gate 	me.sin_addr.s_addr = INADDR_ANY;
214*0Sstevel@tonic-gate 	me.sin_port = htons(0);
215*0Sstevel@tonic-gate 	if(bind(sd, (struct sockaddr *)&me, sizeof(me)) != 0)
216*0Sstevel@tonic-gate 	{
217*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_BIND, errno_string());
218*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
219*0Sstevel@tonic-gate 		(void)close(sd);
220*0Sstevel@tonic-gate 		return -1;
221*0Sstevel@tonic-gate 	}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	/* address */
225*0Sstevel@tonic-gate 	address.sin_family = AF_INET;
226*0Sstevel@tonic-gate 	address.sin_addr.s_addr = ip_address->s_addr;
227*0Sstevel@tonic-gate 	address.sin_port = SNMP_TRAP_PORT;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	if(snmp_pdu_send(sd, &address, pdu, error_label))
230*0Sstevel@tonic-gate 	{
231*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
232*0Sstevel@tonic-gate 		(void)close(sd);
233*0Sstevel@tonic-gate 		return -1;
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	snmp_pdu_free(pdu);
236*0Sstevel@tonic-gate 	(void)close(sd);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	return 0;
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate /**********************************************************************/
242*0Sstevel@tonic-gate 
trap_send_raw(IPAddress * ip_address,IPAddress my_ip_addr,char * community,int i_flag,Oid * enterprise,int generic,int specific,int trap_port,uint32_t time_stamp,SNMP_variable * variables,char * error_label)243*0Sstevel@tonic-gate int trap_send_raw(IPAddress *ip_address, IPAddress my_ip_addr,
244*0Sstevel@tonic-gate 	char* community,int i_flag,Oid *enterprise,int generic,
245*0Sstevel@tonic-gate 	int specific,int trap_port,uint32_t time_stamp,
246*0Sstevel@tonic-gate 	SNMP_variable *variables,char *error_label)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	static int my_ip_address_initialized = False;
249*0Sstevel@tonic-gate 	static IPAddress my_ip_address;
250*0Sstevel@tonic-gate 	struct sockaddr_in me;
251*0Sstevel@tonic-gate 	int sd;
252*0Sstevel@tonic-gate 	Address address;
253*0Sstevel@tonic-gate 	SNMP_pdu *pdu;
254*0Sstevel@tonic-gate 	SNMP_variable *last_variable = NULL;
255*0Sstevel@tonic-gate 	SNMP_variable *new_variable;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	error_label[0] = '\0';
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	if (!i_flag) {
261*0Sstevel@tonic-gate 		if(my_ip_address_initialized == False)
262*0Sstevel@tonic-gate 		{
263*0Sstevel@tonic-gate 			if(get_my_ip_address(&my_ip_address, error_label))
264*0Sstevel@tonic-gate 			{
265*0Sstevel@tonic-gate 				return -1;
266*0Sstevel@tonic-gate 			}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 			my_ip_address_initialized = True;
269*0Sstevel@tonic-gate 		}
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate 	pdu = snmp_pdu_new(error_label);
273*0Sstevel@tonic-gate 	if(pdu == NULL)
274*0Sstevel@tonic-gate 	{
275*0Sstevel@tonic-gate 		return -1;
276*0Sstevel@tonic-gate 	}
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	/* version, community */
279*0Sstevel@tonic-gate 	pdu->version = SNMP_VERSION_1;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	if(community == NULL)
282*0Sstevel@tonic-gate 	{
283*0Sstevel@tonic-gate 		pdu->community = strdup("public");
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 	else
286*0Sstevel@tonic-gate 	{
287*0Sstevel@tonic-gate 		pdu->community = strdup(community);
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 	if(pdu->community == NULL)
290*0Sstevel@tonic-gate 	{
291*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_ALLOC);
292*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
293*0Sstevel@tonic-gate 		return -1;
294*0Sstevel@tonic-gate 	}
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	/* type */
297*0Sstevel@tonic-gate 	pdu->type = TRP_REQ_MSG;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	/* enterprise */
300*0Sstevel@tonic-gate 	if(enterprise == NULL)
301*0Sstevel@tonic-gate 	{
302*0Sstevel@tonic-gate 		if(default_enterprise)
303*0Sstevel@tonic-gate 		{
304*0Sstevel@tonic-gate 			enterprise = default_enterprise;
305*0Sstevel@tonic-gate 		}
306*0Sstevel@tonic-gate 		else
307*0Sstevel@tonic-gate 		{
308*0Sstevel@tonic-gate 			enterprise = &sun_oid;
309*0Sstevel@tonic-gate 		}
310*0Sstevel@tonic-gate 	}
311*0Sstevel@tonic-gate 	if(SSAOidCpy(&(pdu->enterprise), enterprise, error_label))
312*0Sstevel@tonic-gate 	{
313*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
314*0Sstevel@tonic-gate 		return -1;
315*0Sstevel@tonic-gate 	}
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate  	/* agent_addr */
318*0Sstevel@tonic-gate 	if (!i_flag) {
319*0Sstevel@tonic-gate 		pdu->ip_agent_addr.s_addr = my_ip_address.s_addr;
320*0Sstevel@tonic-gate 	}
321*0Sstevel@tonic-gate 	else {
322*0Sstevel@tonic-gate 		pdu->ip_agent_addr.s_addr = my_ip_addr.s_addr;
323*0Sstevel@tonic-gate 	}
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	/* generic, specific */
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	pdu->generic = generic;
328*0Sstevel@tonic-gate 	pdu->specific = specific;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	/* time_stamp */
331*0Sstevel@tonic-gate 	if (time_stamp == -1U)
332*0Sstevel@tonic-gate 		pdu->time_stamp = request_sysUpTime(error_label, community); /* default */
333*0Sstevel@tonic-gate 	else
334*0Sstevel@tonic-gate 		pdu->time_stamp = time_stamp;
335*0Sstevel@tonic-gate 
336*0Sstevel@tonic-gate 	/* first_variable */
337*0Sstevel@tonic-gate 	while(variables)
338*0Sstevel@tonic-gate 	{
339*0Sstevel@tonic-gate 		new_variable = snmp_variable_dup(variables, error_label);
340*0Sstevel@tonic-gate 		if(new_variable == NULL)
341*0Sstevel@tonic-gate 		{
342*0Sstevel@tonic-gate 			snmp_pdu_free(pdu);
343*0Sstevel@tonic-gate 			return -1;
344*0Sstevel@tonic-gate 		}
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate 		if(last_variable)
347*0Sstevel@tonic-gate 		{
348*0Sstevel@tonic-gate 			last_variable->next_variable = new_variable;
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 		else
351*0Sstevel@tonic-gate 		{
352*0Sstevel@tonic-gate 			pdu->first_variable = new_variable;
353*0Sstevel@tonic-gate 		}
354*0Sstevel@tonic-gate 		last_variable = new_variable;
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 		variables = variables->next_variable;
357*0Sstevel@tonic-gate 	}
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 
360*0Sstevel@tonic-gate 	/* sd */
361*0Sstevel@tonic-gate 	sd = socket(AF_INET, SOCK_DGRAM, 0);
362*0Sstevel@tonic-gate 	if(sd < 0)
363*0Sstevel@tonic-gate 	{
364*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_SOCKET, errno_string());
365*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
366*0Sstevel@tonic-gate 		return -1;
367*0Sstevel@tonic-gate 	}
368*0Sstevel@tonic-gate 	me.sin_family = AF_INET;
369*0Sstevel@tonic-gate 	me.sin_addr.s_addr = INADDR_ANY;
370*0Sstevel@tonic-gate 	me.sin_port = htons(0);
371*0Sstevel@tonic-gate 	if(bind(sd, (struct sockaddr *)&me, sizeof(me)) != 0)
372*0Sstevel@tonic-gate 	{
373*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_BIND, errno_string());
374*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
375*0Sstevel@tonic-gate 		(void)close(sd);
376*0Sstevel@tonic-gate 		return -1;
377*0Sstevel@tonic-gate 	}
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	/* address */
381*0Sstevel@tonic-gate 	address.sin_family = AF_INET;
382*0Sstevel@tonic-gate 	address.sin_addr.s_addr = ip_address->s_addr;
383*0Sstevel@tonic-gate 	if (trap_port == -1)
384*0Sstevel@tonic-gate 		address.sin_port = SNMP_TRAP_PORT; /* default */
385*0Sstevel@tonic-gate 	else
386*0Sstevel@tonic-gate 		/* LINTED */
387*0Sstevel@tonic-gate 		address.sin_port = (short)trap_port;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if(snmp_pdu_send(sd, &address, pdu, error_label))
390*0Sstevel@tonic-gate 	{
391*0Sstevel@tonic-gate 		snmp_pdu_free(pdu);
392*0Sstevel@tonic-gate 		(void)close(sd);
393*0Sstevel@tonic-gate 		return -1;
394*0Sstevel@tonic-gate 	}
395*0Sstevel@tonic-gate 	snmp_pdu_free(pdu);
396*0Sstevel@tonic-gate 	(void)close(sd);
397*0Sstevel@tonic-gate 
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	return 0;
400*0Sstevel@tonic-gate }
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate /**********************************************************************/
trap_send_with_more_para(IPAddress * ip_address,IPAddress my_ip_addr,char * community,int i_flag,Oid * enterprise,int generic,int specific,int trap_port,uint32_t time_stamp,SNMP_variable * variables,char * error_label)403*0Sstevel@tonic-gate int trap_send_with_more_para(IPAddress *ip_address,
404*0Sstevel@tonic-gate 							 IPAddress my_ip_addr,
405*0Sstevel@tonic-gate 							 char *community,
406*0Sstevel@tonic-gate 							 int i_flag,
407*0Sstevel@tonic-gate 							 Oid *enterprise,
408*0Sstevel@tonic-gate 							 int generic,
409*0Sstevel@tonic-gate 							 int specific,
410*0Sstevel@tonic-gate 							 int trap_port,
411*0Sstevel@tonic-gate 							 uint32_t time_stamp,
412*0Sstevel@tonic-gate 							 SNMP_variable *variables,
413*0Sstevel@tonic-gate 							 char *error_label)
414*0Sstevel@tonic-gate {
415*0Sstevel@tonic-gate  return(trap_send_raw(ip_address,my_ip_addr,community,i_flag,enterprise,generic,
416*0Sstevel@tonic-gate 	specific,trap_port,time_stamp, variables,error_label));
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate /********************************************************************/
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate /*
424*0Sstevel@tonic-gate  *	returns 0 if OK
425*0Sstevel@tonic-gate  *		1 if error
426*0Sstevel@tonic-gate  *		-1 if fatal error
427*0Sstevel@tonic-gate  */
428*0Sstevel@tonic-gate 
trap_destinator_add(char * name,char * error_label)429*0Sstevel@tonic-gate int trap_destinator_add(char *name, char *error_label)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	IPAddress ip_address;
432*0Sstevel@tonic-gate 	Trap_Destinator *new;
433*0Sstevel@tonic-gate 	Trap_Destinator *d;
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	error_label[0] = '\0';
437*0Sstevel@tonic-gate 
438*0Sstevel@tonic-gate 	if(name == NULL)
439*0Sstevel@tonic-gate 	{
440*0Sstevel@tonic-gate 		sprintf(error_label, "BUG: trap_destinator_add(): name is NULL");
441*0Sstevel@tonic-gate 		return -1;
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 	if(name_to_ip_address(name, &ip_address, error_label))
445*0Sstevel@tonic-gate 	{
446*0Sstevel@tonic-gate 		return 1;
447*0Sstevel@tonic-gate 	}
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	/* check if this trap destinator does not already exist */
450*0Sstevel@tonic-gate 	for(d = first_trap_destinator; d; d = d->next_trap_destinator)
451*0Sstevel@tonic-gate 	{
452*0Sstevel@tonic-gate 		if(ip_address.s_addr == d->ip_address.s_addr)
453*0Sstevel@tonic-gate 		{
454*0Sstevel@tonic-gate 			sprintf(error_label, ERR_MSG_TRAP_DEST_DUP,
455*0Sstevel@tonic-gate 				name);
456*0Sstevel@tonic-gate 			return 1;
457*0Sstevel@tonic-gate 		}
458*0Sstevel@tonic-gate 	}
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 	/* allocate, initialize and link the new trap destinator */
462*0Sstevel@tonic-gate 	new = (Trap_Destinator *) malloc(sizeof(Trap_Destinator));
463*0Sstevel@tonic-gate 	if(new == NULL)
464*0Sstevel@tonic-gate 	{
465*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_ALLOC);
466*0Sstevel@tonic-gate 		return -1;
467*0Sstevel@tonic-gate 	}
468*0Sstevel@tonic-gate 	new->next_trap_destinator = NULL;
469*0Sstevel@tonic-gate 	new->name = NULL;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 	new->name = strdup(name);
472*0Sstevel@tonic-gate 	if(new->name == NULL)
473*0Sstevel@tonic-gate 	{
474*0Sstevel@tonic-gate 		sprintf(error_label, ERR_MSG_ALLOC);
475*0Sstevel@tonic-gate 		free(new);
476*0Sstevel@tonic-gate 		return -1;
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 	new->ip_address.s_addr = ip_address.s_addr;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	new->next_trap_destinator = first_trap_destinator;
482*0Sstevel@tonic-gate 	first_trap_destinator = new;
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate 	return 0;
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate 
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate /********************************************************************/
490*0Sstevel@tonic-gate 
delete_trap_destinator_list()491*0Sstevel@tonic-gate void delete_trap_destinator_list()
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 	Trap_Destinator *next;
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	while(first_trap_destinator)
497*0Sstevel@tonic-gate 	{
498*0Sstevel@tonic-gate 		next = first_trap_destinator->next_trap_destinator;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 		if(first_trap_destinator->name)
501*0Sstevel@tonic-gate 		{
502*0Sstevel@tonic-gate 			free(first_trap_destinator->name);
503*0Sstevel@tonic-gate 		}
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 		free(first_trap_destinator);
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate 		first_trap_destinator = next;
508*0Sstevel@tonic-gate 	}
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 	first_trap_destinator = NULL;
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate /********************************************************************/
515*0Sstevel@tonic-gate 
trace_trap_destinators()516*0Sstevel@tonic-gate void trace_trap_destinators()
517*0Sstevel@tonic-gate {
518*0Sstevel@tonic-gate 	Trap_Destinator *d;
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 	trace("TRAP RECIPIENTS:\n");
522*0Sstevel@tonic-gate 	trace("-----------------\n");
523*0Sstevel@tonic-gate 	for(d = first_trap_destinator; d; d = d->next_trap_destinator)
524*0Sstevel@tonic-gate 	{
525*0Sstevel@tonic-gate 		trace("%-30s %-20s\n",
526*0Sstevel@tonic-gate 			d->name,
527*0Sstevel@tonic-gate 			inet_ntoa(d->ip_address));
528*0Sstevel@tonic-gate 	}
529*0Sstevel@tonic-gate 	trace("\n");
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate /********************************************************************/
534*0Sstevel@tonic-gate /* ARGSUSED */
trap_send_to_all_destinators7(int i_flag,Oid * enterprise,int generic,int specific,uint32_t time_stamp,SNMP_variable * variables,char * error_label)535*0Sstevel@tonic-gate int trap_send_to_all_destinators7( int i_flag, Oid *enterprise, int generic,
536*0Sstevel@tonic-gate                                    int specific, uint32_t time_stamp,
537*0Sstevel@tonic-gate                                    SNMP_variable *variables, char *error_label)
538*0Sstevel@tonic-gate {
539*0Sstevel@tonic-gate         Trap_Destinator *d;
540*0Sstevel@tonic-gate         IPAddress my_ip_addr;
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate 	(void)memset(&my_ip_addr, 0, sizeof(IPAddress));
543*0Sstevel@tonic-gate 
544*0Sstevel@tonic-gate         error_label[0] = '\0';
545*0Sstevel@tonic-gate 
546*0Sstevel@tonic-gate         for(d = first_trap_destinator; d; d = d->next_trap_destinator)
547*0Sstevel@tonic-gate         {
548*0Sstevel@tonic-gate                 if(trap_send_with_more_para(&(d->ip_address), my_ip_addr, NULL, 0,enterprise, generic, specific, SNMP_TRAP_PORT,time_stamp,variables, error_label))
549*0Sstevel@tonic-gate                 {
550*0Sstevel@tonic-gate                         return -1;
551*0Sstevel@tonic-gate                 }
552*0Sstevel@tonic-gate         }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate         return 0;
555*0Sstevel@tonic-gate }
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 
trap_send_to_all_destinators(Oid * enterprise,int generic,int specific,SNMP_variable * variables,char * error_label)559*0Sstevel@tonic-gate int trap_send_to_all_destinators(Oid *enterprise, int generic, int specific, SNMP_variable *variables, char *error_label)
560*0Sstevel@tonic-gate {
561*0Sstevel@tonic-gate 	Trap_Destinator *d;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate 	error_label[0] = '\0';
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate 	for(d = first_trap_destinator; d; d = d->next_trap_destinator)
567*0Sstevel@tonic-gate 	{
568*0Sstevel@tonic-gate 		if(trap_send(&(d->ip_address), enterprise, generic, specific, variables, error_label))
569*0Sstevel@tonic-gate 		{
570*0Sstevel@tonic-gate 			return -1;
571*0Sstevel@tonic-gate 		}
572*0Sstevel@tonic-gate 	}
573*0Sstevel@tonic-gate 
574*0Sstevel@tonic-gate 	return 0;
575*0Sstevel@tonic-gate }
576*0Sstevel@tonic-gate 
577