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 (c) 1998 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
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 <stdio.h>
31*0Sstevel@tonic-gate #include <ctype.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <sys/utsname.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/socket.h>
37*0Sstevel@tonic-gate #include <netinet/in.h>
38*0Sstevel@tonic-gate #include <arpa/inet.h>
39*0Sstevel@tonic-gate #include <netdb.h>
40*0Sstevel@tonic-gate #include "snmp_msg.h"
41*0Sstevel@tonic-gate #include "impl.h"
42*0Sstevel@tonic-gate #include "asn1.h"
43*0Sstevel@tonic-gate #include "snmp.h"
44*0Sstevel@tonic-gate #include "error.h"
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /********************************************************************/
48*0Sstevel@tonic-gate
pdu_type_string(u_char type)49*0Sstevel@tonic-gate char *pdu_type_string(u_char type)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate static char buffer[50];
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate switch(type)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate case GET_REQ_MSG:
57*0Sstevel@tonic-gate sprintf(buffer, "GET_REQ_MSG (0x%x)", type);
58*0Sstevel@tonic-gate break;
59*0Sstevel@tonic-gate case GETNEXT_REQ_MSG:
60*0Sstevel@tonic-gate sprintf(buffer, "GETNEXT_REQ_MSG (0x%x)", type);
61*0Sstevel@tonic-gate break;
62*0Sstevel@tonic-gate case GET_RSP_MSG:
63*0Sstevel@tonic-gate sprintf(buffer, "GET_RSP_MSG (0x%x)", type);
64*0Sstevel@tonic-gate break;
65*0Sstevel@tonic-gate case SET_REQ_MSG:
66*0Sstevel@tonic-gate sprintf(buffer, "SET_REQ_MSG (0x%x)", type);
67*0Sstevel@tonic-gate break;
68*0Sstevel@tonic-gate case TRP_REQ_MSG:
69*0Sstevel@tonic-gate sprintf(buffer, "TRP_MSG (0x%x)", type);
70*0Sstevel@tonic-gate break;
71*0Sstevel@tonic-gate default:
72*0Sstevel@tonic-gate sprintf(buffer, "UNKNOWN! (0x%x)", type);
73*0Sstevel@tonic-gate break;
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate return buffer;
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate /********************************************************************/
81*0Sstevel@tonic-gate
asn1_type_string(u_char type)82*0Sstevel@tonic-gate char *asn1_type_string(u_char type)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate static char buffer[50];
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate switch(type)
88*0Sstevel@tonic-gate {
89*0Sstevel@tonic-gate case ASN_INTEGER:
90*0Sstevel@tonic-gate sprintf(buffer, "INTEGER (0x%x)", type);
91*0Sstevel@tonic-gate break;
92*0Sstevel@tonic-gate case COUNTER:
93*0Sstevel@tonic-gate sprintf(buffer, "COUNTER (0x%x)", type);
94*0Sstevel@tonic-gate break;
95*0Sstevel@tonic-gate case GAUGE:
96*0Sstevel@tonic-gate sprintf(buffer, "GAUGE (0x%x)", type);
97*0Sstevel@tonic-gate break;
98*0Sstevel@tonic-gate case TIMETICKS:
99*0Sstevel@tonic-gate sprintf(buffer, "TIMETICKS (0x%x)", type);
100*0Sstevel@tonic-gate break;
101*0Sstevel@tonic-gate case ASN_OCTET_STR:
102*0Sstevel@tonic-gate sprintf(buffer, "OCTET STRING (0x%x)", type);
103*0Sstevel@tonic-gate break;
104*0Sstevel@tonic-gate case IPADDRESS:
105*0Sstevel@tonic-gate sprintf(buffer, "IP ADDRESS (0x%x)", type);
106*0Sstevel@tonic-gate break;
107*0Sstevel@tonic-gate case OPAQUE:
108*0Sstevel@tonic-gate sprintf(buffer, "OPAQUE (0x%x)", type);
109*0Sstevel@tonic-gate break;
110*0Sstevel@tonic-gate case ASN_OBJECT_ID:
111*0Sstevel@tonic-gate sprintf(buffer, "OBJECT IDENTIFIER (0x%x)", type);
112*0Sstevel@tonic-gate break;
113*0Sstevel@tonic-gate case ASN_NULL:
114*0Sstevel@tonic-gate sprintf(buffer, "NULL (0x%x)", type);
115*0Sstevel@tonic-gate break;
116*0Sstevel@tonic-gate default:
117*0Sstevel@tonic-gate sprintf(buffer, "UNKNOWN! (0x%x)", type);
118*0Sstevel@tonic-gate break;
119*0Sstevel@tonic-gate }
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate return buffer;
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate /********************************************************************/
126*0Sstevel@tonic-gate
error_status_string(int status)127*0Sstevel@tonic-gate char *error_status_string(int status)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate static char buffer[50];
130*0Sstevel@tonic-gate
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate switch(status)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate case SNMP_ERR_NOERROR:
135*0Sstevel@tonic-gate sprintf(buffer, "noError(%d)", status);
136*0Sstevel@tonic-gate break;
137*0Sstevel@tonic-gate case SNMP_ERR_TOOBIG:
138*0Sstevel@tonic-gate sprintf(buffer, "tooBig(%d)", status);
139*0Sstevel@tonic-gate break;
140*0Sstevel@tonic-gate case SNMP_ERR_NOSUCHNAME:
141*0Sstevel@tonic-gate sprintf(buffer, "noSuchName(%d)", status);
142*0Sstevel@tonic-gate break;
143*0Sstevel@tonic-gate case SNMP_ERR_BADVALUE:
144*0Sstevel@tonic-gate sprintf(buffer, "badValue(%d)", status);
145*0Sstevel@tonic-gate break;
146*0Sstevel@tonic-gate case SNMP_ERR_READONLY:
147*0Sstevel@tonic-gate sprintf(buffer, "readOnly(%d)", status);
148*0Sstevel@tonic-gate break;
149*0Sstevel@tonic-gate case SNMP_ERR_GENERR:
150*0Sstevel@tonic-gate sprintf(buffer, "genErr(%d)", status);
151*0Sstevel@tonic-gate break;
152*0Sstevel@tonic-gate default:
153*0Sstevel@tonic-gate sprintf(buffer, "UNKNOWN! (%d)", status);
154*0Sstevel@tonic-gate break;
155*0Sstevel@tonic-gate }
156*0Sstevel@tonic-gate
157*0Sstevel@tonic-gate return buffer;
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate /********************************************************************/
162*0Sstevel@tonic-gate
generic_trap_string(int generic)163*0Sstevel@tonic-gate char *generic_trap_string(int generic)
164*0Sstevel@tonic-gate {
165*0Sstevel@tonic-gate static char buffer[50];
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate switch(generic)
169*0Sstevel@tonic-gate {
170*0Sstevel@tonic-gate case SNMP_TRAP_COLDSTART:
171*0Sstevel@tonic-gate sprintf(buffer, "coldStart(%d)", generic);
172*0Sstevel@tonic-gate break;
173*0Sstevel@tonic-gate case SNMP_TRAP_WARMSTART:
174*0Sstevel@tonic-gate sprintf(buffer, "warmStart(%d)", generic);
175*0Sstevel@tonic-gate break;
176*0Sstevel@tonic-gate case SNMP_TRAP_LINKDOWN:
177*0Sstevel@tonic-gate sprintf(buffer, "linkDown(%d)", generic);
178*0Sstevel@tonic-gate break;
179*0Sstevel@tonic-gate case SNMP_TRAP_LINKUP:
180*0Sstevel@tonic-gate sprintf(buffer, "linkUp(%d)", generic);
181*0Sstevel@tonic-gate break;
182*0Sstevel@tonic-gate case SNMP_TRAP_AUTHFAIL:
183*0Sstevel@tonic-gate sprintf(buffer, "authentificationFailure(%d)", generic);
184*0Sstevel@tonic-gate break;
185*0Sstevel@tonic-gate case SNMP_TRAP_EGPNEIGHBORLOSS:
186*0Sstevel@tonic-gate sprintf(buffer, "egpNeighborLoss(%d)", generic);
187*0Sstevel@tonic-gate break;
188*0Sstevel@tonic-gate case SNMP_TRAP_ENTERPRISESPECIFIC:
189*0Sstevel@tonic-gate sprintf(buffer, "enterpriseSpecific(%d)", generic);
190*0Sstevel@tonic-gate break;
191*0Sstevel@tonic-gate default:
192*0Sstevel@tonic-gate sprintf(buffer, "UNKNOWN! (%d)", generic);
193*0Sstevel@tonic-gate break;
194*0Sstevel@tonic-gate }
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate return buffer;
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate /********************************************************************/
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate /* we should check if the buffer is not too small */
203*0Sstevel@tonic-gate
SSAOidString(Oid * oid)204*0Sstevel@tonic-gate char *SSAOidString(Oid *oid)
205*0Sstevel@tonic-gate {
206*0Sstevel@tonic-gate static char buffer[1000];
207*0Sstevel@tonic-gate int i;
208*0Sstevel@tonic-gate int32_t len;
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate if(oid == NULL)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate sprintf(buffer, "oid is NULL!");
214*0Sstevel@tonic-gate return buffer;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate sprintf(buffer, "");
218*0Sstevel@tonic-gate
219*0Sstevel@tonic-gate if(oid->len == 0)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate return buffer;
222*0Sstevel@tonic-gate }
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gate for(i = 0; i < oid->len - 1; i++) {
225*0Sstevel@tonic-gate /* LINTED */
226*0Sstevel@tonic-gate len = (int32_t)strlen(buffer);
227*0Sstevel@tonic-gate sprintf(&(buffer[len]), "%lu.", oid->subids[i]);
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate /* LINTED */
230*0Sstevel@tonic-gate len = (int32_t)strlen(buffer);
231*0Sstevel@tonic-gate sprintf(&(buffer[len]), "%lu", oid->subids[oid->len - 1]);
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate return buffer;
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gate /********************************************************************/
238*0Sstevel@tonic-gate
timeval_string(struct timeval * tv)239*0Sstevel@tonic-gate char *timeval_string(struct timeval *tv)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate static char buffer[50];
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate if(tv == NULL)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate sprintf(buffer, "tv is NULL!");
247*0Sstevel@tonic-gate return buffer;
248*0Sstevel@tonic-gate }
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate sprintf(buffer, "%ld sec %ld usec", tv->tv_sec, tv->tv_usec);
251*0Sstevel@tonic-gate return buffer;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate /********************************************************************/
256*0Sstevel@tonic-gate
ip_address_string(IPAddress * ip_address)257*0Sstevel@tonic-gate char *ip_address_string(IPAddress *ip_address)
258*0Sstevel@tonic-gate {
259*0Sstevel@tonic-gate static char buffer[50];
260*0Sstevel@tonic-gate struct hostent *hp;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gate if(ip_address == NULL)
264*0Sstevel@tonic-gate {
265*0Sstevel@tonic-gate sprintf(buffer, "BUG: ip_address_string(): ip_address is NULL");
266*0Sstevel@tonic-gate return buffer;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate hp = gethostbyaddr((char *) &(ip_address->s_addr), 4, AF_INET);
270*0Sstevel@tonic-gate if(hp)
271*0Sstevel@tonic-gate {
272*0Sstevel@tonic-gate sprintf(buffer, "%s", hp->h_name);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate else
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate sprintf(buffer, "%s", inet_ntoa(*ip_address));
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate return buffer;
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate /********************************************************************/
284*0Sstevel@tonic-gate
address_string(Address * address)285*0Sstevel@tonic-gate char *address_string(Address *address)
286*0Sstevel@tonic-gate {
287*0Sstevel@tonic-gate static char buffer[50];
288*0Sstevel@tonic-gate struct hostent *hp;
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate
291*0Sstevel@tonic-gate if(address == NULL)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate sprintf(buffer, "BUG: address_string(): address is NULL");
294*0Sstevel@tonic-gate return buffer;
295*0Sstevel@tonic-gate }
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate hp = gethostbyaddr((char *) &(address->sin_addr.s_addr), 4, AF_INET);
298*0Sstevel@tonic-gate if(hp)
299*0Sstevel@tonic-gate {
300*0Sstevel@tonic-gate sprintf(buffer, "%s.%d", hp->h_name, address->sin_port);
301*0Sstevel@tonic-gate }
302*0Sstevel@tonic-gate else
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate sprintf(buffer, "%s.%d", inet_ntoa(address->sin_addr), address->sin_port);
305*0Sstevel@tonic-gate }
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate return buffer;
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate /********************************************************************/
312*0Sstevel@tonic-gate
SSAStringCpy(String * string1,String * string2,char * error_label)313*0Sstevel@tonic-gate int SSAStringCpy(String *string1, String *string2, char *error_label)
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate error_label[0] = '\0';
316*0Sstevel@tonic-gate
317*0Sstevel@tonic-gate if(string1 == NULL)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringCpy(): string1 is NULL");
320*0Sstevel@tonic-gate return -1;
321*0Sstevel@tonic-gate }
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gate if(string2 == NULL)
324*0Sstevel@tonic-gate {
325*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringCpy(): string2 is NULL");
326*0Sstevel@tonic-gate return -1;
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate if(string1->chars)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringCpy(): string1->chars is not NULL");
332*0Sstevel@tonic-gate return -1;
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate if(string1->len)
336*0Sstevel@tonic-gate {
337*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringCpy(): string1->len is not 0");
338*0Sstevel@tonic-gate return -1;
339*0Sstevel@tonic-gate }
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate if(string2->len == 0)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate return 0;
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate string1->chars = (u_char *) malloc(string2->len);
347*0Sstevel@tonic-gate if(string1->chars == NULL)
348*0Sstevel@tonic-gate {
349*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_ALLOC);
350*0Sstevel@tonic-gate return -1;
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate memcpy(string1->chars, string2->chars, string2->len);
354*0Sstevel@tonic-gate string1->len = string2->len;
355*0Sstevel@tonic-gate
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate return 0;
358*0Sstevel@tonic-gate }
359*0Sstevel@tonic-gate
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate /********************************************************************/
362*0Sstevel@tonic-gate
SSAStringZero(String * string)363*0Sstevel@tonic-gate void SSAStringZero(String *string)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate if(string == NULL)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate (void)fprintf(stderr, "BUG: SSAStringZero(): string is NULL");
368*0Sstevel@tonic-gate return;
369*0Sstevel@tonic-gate }
370*0Sstevel@tonic-gate
371*0Sstevel@tonic-gate if(string->chars)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate free(string->chars);
374*0Sstevel@tonic-gate string->chars = NULL;
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate string->len = 0;
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate
379*0Sstevel@tonic-gate return;
380*0Sstevel@tonic-gate }
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gate /********************************************************************/
384*0Sstevel@tonic-gate
SSAStringInit(String * string,u_char * chars,int len,char * error_label)385*0Sstevel@tonic-gate int SSAStringInit(String *string, u_char *chars, int len, char *error_label)
386*0Sstevel@tonic-gate {
387*0Sstevel@tonic-gate error_label[0] = '\0';
388*0Sstevel@tonic-gate
389*0Sstevel@tonic-gate if(string == NULL)
390*0Sstevel@tonic-gate {
391*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringInit(): string is NULL");
392*0Sstevel@tonic-gate return -1;
393*0Sstevel@tonic-gate }
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate if(string->chars != NULL)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringInit(): string->chars is not NULL");
398*0Sstevel@tonic-gate return -1;
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate
401*0Sstevel@tonic-gate if(string->len != 0)
402*0Sstevel@tonic-gate {
403*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAStringInit(): string->len is not 0");
404*0Sstevel@tonic-gate return -1;
405*0Sstevel@tonic-gate }
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gate if(len != 0)
408*0Sstevel@tonic-gate {
409*0Sstevel@tonic-gate string->chars = (u_char *) malloc(len);
410*0Sstevel@tonic-gate if(string->chars == NULL)
411*0Sstevel@tonic-gate {
412*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_ALLOC);
413*0Sstevel@tonic-gate return -1;
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate memcpy(string->chars, chars, len);
416*0Sstevel@tonic-gate string->len = len;
417*0Sstevel@tonic-gate }
418*0Sstevel@tonic-gate
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gate return 0;
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gate /********************************************************************/
425*0Sstevel@tonic-gate
426*0Sstevel@tonic-gate /*
427*0Sstevel@tonic-gate * SSAOidCmp() returns:
428*0Sstevel@tonic-gate *
429*0Sstevel@tonic-gate * 0 if oid1 == oid2
430*0Sstevel@tonic-gate * 1 if oid1 > oid2
431*0Sstevel@tonic-gate * -1 if oid1 < oid2
432*0Sstevel@tonic-gate */
433*0Sstevel@tonic-gate
SSAOidCmp(Oid * oid1,Oid * oid2)434*0Sstevel@tonic-gate int SSAOidCmp(Oid *oid1, Oid *oid2)
435*0Sstevel@tonic-gate {
436*0Sstevel@tonic-gate int min;
437*0Sstevel@tonic-gate int i;
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gate
440*0Sstevel@tonic-gate if(oid1 == NULL)
441*0Sstevel@tonic-gate {
442*0Sstevel@tonic-gate (void)fprintf(stderr, "BUG: SSAOidCmp(): oid1 is NULL");
443*0Sstevel@tonic-gate return -2;
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate
446*0Sstevel@tonic-gate if(oid2 == NULL)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate (void)fprintf(stderr, "BUG: SSAOidCmp(): oid2 is NULL");
449*0Sstevel@tonic-gate return -2;
450*0Sstevel@tonic-gate }
451*0Sstevel@tonic-gate
452*0Sstevel@tonic-gate min = MIN(oid1->len, oid2->len);
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gate for(i = 0; i < min; i++)
455*0Sstevel@tonic-gate {
456*0Sstevel@tonic-gate if(oid1->subids[i] > oid2->subids[i])
457*0Sstevel@tonic-gate {
458*0Sstevel@tonic-gate return 1;
459*0Sstevel@tonic-gate }
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gate if(oid1->subids[i] < oid2->subids[i])
462*0Sstevel@tonic-gate {
463*0Sstevel@tonic-gate return -1;
464*0Sstevel@tonic-gate }
465*0Sstevel@tonic-gate }
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gate if(oid1->len == oid2->len)
468*0Sstevel@tonic-gate {
469*0Sstevel@tonic-gate return 0;
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate else
472*0Sstevel@tonic-gate if(oid1->len > oid2->len)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate return 1;
475*0Sstevel@tonic-gate }
476*0Sstevel@tonic-gate else
477*0Sstevel@tonic-gate {
478*0Sstevel@tonic-gate return -1;
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gate /********************************************************************/
484*0Sstevel@tonic-gate
SSAOidCpy(Oid * oid1,Oid * oid2,char * error_label)485*0Sstevel@tonic-gate int SSAOidCpy(Oid *oid1, Oid *oid2, char *error_label)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate error_label[0] = '\0';
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gate if(oid1 == NULL) {
490*0Sstevel@tonic-gate (void)sprintf(error_label, "BUG: SSAOidCpy(): oid1 is NULL");
491*0Sstevel@tonic-gate return -1;
492*0Sstevel@tonic-gate }
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate if(oid2 == NULL) {
495*0Sstevel@tonic-gate (void)sprintf(error_label, "BUG: SSAOidCpy(): oid2 is NULL");
496*0Sstevel@tonic-gate return -1;
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate if(oid2->len == 0) {
500*0Sstevel@tonic-gate return 0;
501*0Sstevel@tonic-gate }
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gate if(oid1->subids) {
504*0Sstevel@tonic-gate (void)sprintf(error_label, "BUG: SSAOidCpy(): oid1->subids is not NULL");
505*0Sstevel@tonic-gate return -1;
506*0Sstevel@tonic-gate }
507*0Sstevel@tonic-gate
508*0Sstevel@tonic-gate if(oid1->len) {
509*0Sstevel@tonic-gate (void)sprintf(error_label, "BUG: SSAOidCpy(): oid1->len is not 0");
510*0Sstevel@tonic-gate return -1;
511*0Sstevel@tonic-gate }
512*0Sstevel@tonic-gate
513*0Sstevel@tonic-gate
514*0Sstevel@tonic-gate oid1->subids = (Subid *) malloc(oid2->len * (int32_t)sizeof(Subid));
515*0Sstevel@tonic-gate if(oid1->subids == NULL) {
516*0Sstevel@tonic-gate (void)sprintf(error_label, ERR_MSG_ALLOC);
517*0Sstevel@tonic-gate return -1;
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate
520*0Sstevel@tonic-gate (void)memcpy(oid1->subids, oid2->subids, oid2->len * (int32_t)sizeof(Subid));
521*0Sstevel@tonic-gate oid1->len = oid2->len;
522*0Sstevel@tonic-gate
523*0Sstevel@tonic-gate return 0;
524*0Sstevel@tonic-gate }
525*0Sstevel@tonic-gate
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate /********************************************************************/
528*0Sstevel@tonic-gate
SSAOidDup(Oid * oid,char * error_label)529*0Sstevel@tonic-gate Oid *SSAOidDup(Oid *oid, char *error_label)
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate Oid *new = NULL;
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate
534*0Sstevel@tonic-gate error_label[0] = '\0';
535*0Sstevel@tonic-gate
536*0Sstevel@tonic-gate if(oid == NULL)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAOidDup(): oid is NULL");
539*0Sstevel@tonic-gate return NULL;
540*0Sstevel@tonic-gate }
541*0Sstevel@tonic-gate
542*0Sstevel@tonic-gate new = (Oid *) malloc(sizeof(Oid));
543*0Sstevel@tonic-gate if(new == NULL)
544*0Sstevel@tonic-gate {
545*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_ALLOC);
546*0Sstevel@tonic-gate return NULL;
547*0Sstevel@tonic-gate }
548*0Sstevel@tonic-gate memset(new, 0, sizeof(Oid));
549*0Sstevel@tonic-gate
550*0Sstevel@tonic-gate if(SSAOidCpy(new, oid, error_label))
551*0Sstevel@tonic-gate {
552*0Sstevel@tonic-gate free(new);
553*0Sstevel@tonic-gate return NULL;
554*0Sstevel@tonic-gate }
555*0Sstevel@tonic-gate
556*0Sstevel@tonic-gate return new;
557*0Sstevel@tonic-gate }
558*0Sstevel@tonic-gate
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate /********************************************************************/
561*0Sstevel@tonic-gate
SSAOidNew()562*0Sstevel@tonic-gate Oid *SSAOidNew()
563*0Sstevel@tonic-gate {
564*0Sstevel@tonic-gate Oid *oid = NULL;
565*0Sstevel@tonic-gate oid = (Oid *) malloc(sizeof(Oid));
566*0Sstevel@tonic-gate oid->subids = NULL;
567*0Sstevel@tonic-gate oid->len = 0;
568*0Sstevel@tonic-gate return (oid);
569*0Sstevel@tonic-gate }
570*0Sstevel@tonic-gate
571*0Sstevel@tonic-gate /********************************************************************/
572*0Sstevel@tonic-gate
SSAOidZero(Oid * oid)573*0Sstevel@tonic-gate void SSAOidZero(Oid *oid)
574*0Sstevel@tonic-gate {
575*0Sstevel@tonic-gate if(oid == NULL)
576*0Sstevel@tonic-gate {
577*0Sstevel@tonic-gate (void)fprintf(stderr, "BUG: SSAOidZero(): oid is NULL");
578*0Sstevel@tonic-gate return;
579*0Sstevel@tonic-gate }
580*0Sstevel@tonic-gate
581*0Sstevel@tonic-gate if(oid->subids)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate free(oid->subids);
584*0Sstevel@tonic-gate oid->subids = NULL;
585*0Sstevel@tonic-gate }
586*0Sstevel@tonic-gate oid->len = 0;
587*0Sstevel@tonic-gate }
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate
590*0Sstevel@tonic-gate /********************************************************************/
591*0Sstevel@tonic-gate
SSAOidFree(Oid * oid)592*0Sstevel@tonic-gate void SSAOidFree(Oid *oid)
593*0Sstevel@tonic-gate {
594*0Sstevel@tonic-gate if(oid == NULL) {
595*0Sstevel@tonic-gate return;
596*0Sstevel@tonic-gate }
597*0Sstevel@tonic-gate
598*0Sstevel@tonic-gate SSAOidZero(oid);
599*0Sstevel@tonic-gate free(oid);
600*0Sstevel@tonic-gate }
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gate
603*0Sstevel@tonic-gate /********************************************************************/
604*0Sstevel@tonic-gate
SSAOidInit(Oid * oid,Subid * subids,int len,char * error_label)605*0Sstevel@tonic-gate int SSAOidInit(Oid *oid, Subid *subids, int len, char *error_label)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate error_label[0] = '\0';
608*0Sstevel@tonic-gate
609*0Sstevel@tonic-gate if(oid == NULL)
610*0Sstevel@tonic-gate {
611*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAOidInit(): oid is NULL");
612*0Sstevel@tonic-gate return -1;
613*0Sstevel@tonic-gate }
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate if(oid->subids != NULL)
616*0Sstevel@tonic-gate {
617*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAOidInit(): oid->subids is not NULL");
618*0Sstevel@tonic-gate return -1;
619*0Sstevel@tonic-gate }
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gate if(oid->len != 0)
622*0Sstevel@tonic-gate {
623*0Sstevel@tonic-gate sprintf(error_label, "BUG: SSAOidInit(): oid->len is not 0");
624*0Sstevel@tonic-gate return -1;
625*0Sstevel@tonic-gate }
626*0Sstevel@tonic-gate
627*0Sstevel@tonic-gate if(len != 0)
628*0Sstevel@tonic-gate {
629*0Sstevel@tonic-gate oid->subids = (Subid *) malloc(len * (int32_t)sizeof(Subid));
630*0Sstevel@tonic-gate if(oid->subids == NULL)
631*0Sstevel@tonic-gate {
632*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_ALLOC);
633*0Sstevel@tonic-gate return -1;
634*0Sstevel@tonic-gate }
635*0Sstevel@tonic-gate (void)memcpy(oid->subids, subids, len * (int32_t)sizeof(Subid));
636*0Sstevel@tonic-gate oid->len = len;
637*0Sstevel@tonic-gate }
638*0Sstevel@tonic-gate
639*0Sstevel@tonic-gate
640*0Sstevel@tonic-gate return 0;
641*0Sstevel@tonic-gate }
642*0Sstevel@tonic-gate
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate /********************************************************************/
645*0Sstevel@tonic-gate
get_my_ip_address(IPAddress * my_ip_address,char * error_label)646*0Sstevel@tonic-gate int get_my_ip_address(IPAddress *my_ip_address, char *error_label)
647*0Sstevel@tonic-gate {
648*0Sstevel@tonic-gate struct utsname name;
649*0Sstevel@tonic-gate struct hostent *hp;
650*0Sstevel@tonic-gate
651*0Sstevel@tonic-gate
652*0Sstevel@tonic-gate error_label[0] = '\0';
653*0Sstevel@tonic-gate
654*0Sstevel@tonic-gate if(uname(&name) == -1)
655*0Sstevel@tonic-gate {
656*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_UNAME,
657*0Sstevel@tonic-gate errno_string());
658*0Sstevel@tonic-gate return -1;
659*0Sstevel@tonic-gate }
660*0Sstevel@tonic-gate
661*0Sstevel@tonic-gate if((hp = gethostbyname(name.nodename)) == NULL)
662*0Sstevel@tonic-gate {
663*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_GETHOSTBYNAME,
664*0Sstevel@tonic-gate name.nodename, h_errno_string());
665*0Sstevel@tonic-gate return -1;
666*0Sstevel@tonic-gate }
667*0Sstevel@tonic-gate
668*0Sstevel@tonic-gate if(hp->h_length != 4)
669*0Sstevel@tonic-gate {
670*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_HOSTENT_BAD_IP_LENGTH,
671*0Sstevel@tonic-gate hp->h_length);
672*0Sstevel@tonic-gate return -1;
673*0Sstevel@tonic-gate }
674*0Sstevel@tonic-gate
675*0Sstevel@tonic-gate if(*hp->h_addr_list == NULL)
676*0Sstevel@tonic-gate {
677*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_HOSTENT_MISSING_IP_ADDRESS);
678*0Sstevel@tonic-gate return -1;
679*0Sstevel@tonic-gate }
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gate memcpy(&my_ip_address->s_addr, *hp->h_addr_list, 4);
682*0Sstevel@tonic-gate
683*0Sstevel@tonic-gate
684*0Sstevel@tonic-gate return 0;
685*0Sstevel@tonic-gate }
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate
688*0Sstevel@tonic-gate /********************************************************************/
689*0Sstevel@tonic-gate
name_to_ip_address(char * name,IPAddress * ip_address,char * error_label)690*0Sstevel@tonic-gate int name_to_ip_address(char *name, IPAddress *ip_address, char *error_label)
691*0Sstevel@tonic-gate {
692*0Sstevel@tonic-gate error_label[0] = '\0';
693*0Sstevel@tonic-gate
694*0Sstevel@tonic-gate
695*0Sstevel@tonic-gate if(name == NULL)
696*0Sstevel@tonic-gate {
697*0Sstevel@tonic-gate sprintf(error_label, "BUG: name_to_ip_address(): name is NULL");
698*0Sstevel@tonic-gate return -1;
699*0Sstevel@tonic-gate }
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gate if(ip_address == NULL)
702*0Sstevel@tonic-gate {
703*0Sstevel@tonic-gate sprintf(error_label, "BUG: name_to_ip_address(): ip_address is NULL");
704*0Sstevel@tonic-gate return -1;
705*0Sstevel@tonic-gate }
706*0Sstevel@tonic-gate
707*0Sstevel@tonic-gate /* try to find the IP address from the name */
708*0Sstevel@tonic-gate if(isdigit(name[0]))
709*0Sstevel@tonic-gate {
710*0Sstevel@tonic-gate if((int) (ip_address->s_addr = inet_addr(name)) == -1)
711*0Sstevel@tonic-gate {
712*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_BAD_IP_ADDRESS, name);
713*0Sstevel@tonic-gate return -1;
714*0Sstevel@tonic-gate }
715*0Sstevel@tonic-gate }
716*0Sstevel@tonic-gate else
717*0Sstevel@tonic-gate {
718*0Sstevel@tonic-gate struct hostent *hp;
719*0Sstevel@tonic-gate
720*0Sstevel@tonic-gate
721*0Sstevel@tonic-gate hp = gethostbyname(name);
722*0Sstevel@tonic-gate if(hp == NULL)
723*0Sstevel@tonic-gate {
724*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_BAD_HOSTNAME, name);
725*0Sstevel@tonic-gate return -1;
726*0Sstevel@tonic-gate }
727*0Sstevel@tonic-gate
728*0Sstevel@tonic-gate if(hp->h_length != 4)
729*0Sstevel@tonic-gate {
730*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_HOSTENT_BAD_IP_LENGTH,
731*0Sstevel@tonic-gate hp->h_length);
732*0Sstevel@tonic-gate return -1;
733*0Sstevel@tonic-gate }
734*0Sstevel@tonic-gate
735*0Sstevel@tonic-gate if(*hp->h_addr_list == NULL)
736*0Sstevel@tonic-gate {
737*0Sstevel@tonic-gate sprintf(error_label, ERR_MSG_HOSTENT_MISSING_IP_ADDRESS);
738*0Sstevel@tonic-gate return -1;
739*0Sstevel@tonic-gate }
740*0Sstevel@tonic-gate
741*0Sstevel@tonic-gate memcpy(&(ip_address->s_addr), *hp->h_addr_list, 4);
742*0Sstevel@tonic-gate }
743*0Sstevel@tonic-gate
744*0Sstevel@tonic-gate
745*0Sstevel@tonic-gate return 0;
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate
SSAStringToChar(String str)748*0Sstevel@tonic-gate char *SSAStringToChar(String str)
749*0Sstevel@tonic-gate {
750*0Sstevel@tonic-gate static char buffer[100];
751*0Sstevel@tonic-gate
752*0Sstevel@tonic-gate buffer[0] = '\0';
753*0Sstevel@tonic-gate memcpy(buffer,str.chars,str.len);
754*0Sstevel@tonic-gate return buffer;
755*0Sstevel@tonic-gate }
756*0Sstevel@tonic-gate
757*0Sstevel@tonic-gate /* error return NULL, success return Oid ptr */
SSAOidStrToOid(char * name,char * error_label)758*0Sstevel@tonic-gate Oid *SSAOidStrToOid (char *name, char *error_label)
759*0Sstevel@tonic-gate {
760*0Sstevel@tonic-gate Oid *name_oid;
761*0Sstevel@tonic-gate Subid *subids;
762*0Sstevel@tonic-gate int len = 0;
763*0Sstevel@tonic-gate int i;
764*0Sstevel@tonic-gate char *num_c;
765*0Sstevel@tonic-gate
766*0Sstevel@tonic-gate for (i=0; name[i] != '\0'; i++) {
767*0Sstevel@tonic-gate if (name[i] == '.')
768*0Sstevel@tonic-gate len++;
769*0Sstevel@tonic-gate else if (!isdigit(name[i])) {
770*0Sstevel@tonic-gate (void)fprintf(stderr, "%s is not a valid oid name\n", name);
771*0Sstevel@tonic-gate return(NULL);
772*0Sstevel@tonic-gate }
773*0Sstevel@tonic-gate }
774*0Sstevel@tonic-gate
775*0Sstevel@tonic-gate if (!len ) {
776*0Sstevel@tonic-gate (void)fprintf(stderr,"%s is not a valid oid name\n", name);
777*0Sstevel@tonic-gate return (NULL); /* not a valid name */
778*0Sstevel@tonic-gate }
779*0Sstevel@tonic-gate
780*0Sstevel@tonic-gate len++;
781*0Sstevel@tonic-gate subids = (Subid *) malloc(len * (int32_t)sizeof(Subid));
782*0Sstevel@tonic-gate if (subids == NULL) {
783*0Sstevel@tonic-gate (void)fprintf(stderr,"cannot malloc\n");
784*0Sstevel@tonic-gate return (NULL) ;
785*0Sstevel@tonic-gate }
786*0Sstevel@tonic-gate if ((num_c = strtok(name, "."))== NULL) {
787*0Sstevel@tonic-gate free(subids);
788*0Sstevel@tonic-gate return (NULL);
789*0Sstevel@tonic-gate }
790*0Sstevel@tonic-gate i = 0;
791*0Sstevel@tonic-gate /* LINTED */
792*0Sstevel@tonic-gate subids[i] = (Subid) atol(num_c);
793*0Sstevel@tonic-gate i++;
794*0Sstevel@tonic-gate while (( num_c = strtok(NULL, ".")) != NULL ) {
795*0Sstevel@tonic-gate /* LINTED */
796*0Sstevel@tonic-gate subids[i] = (Subid) atol(num_c);
797*0Sstevel@tonic-gate i++;
798*0Sstevel@tonic-gate }
799*0Sstevel@tonic-gate
800*0Sstevel@tonic-gate name_oid = SSAOidNew();
801*0Sstevel@tonic-gate (void)SSAOidInit(name_oid, subids, len, error_label);
802*0Sstevel@tonic-gate free(subids);
803*0Sstevel@tonic-gate return(name_oid);
804*0Sstevel@tonic-gate }
805*0Sstevel@tonic-gate
806