4 * Community name parse/build routines.
6 /**********************************************************************
7 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
11 Permission to use, copy, modify, and distribute this software and its
12 documentation for any purpose and without fee is hereby granted,
13 provided that the above copyright notice appear in all copies and that
14 both that copyright notice and this permission notice appear in
15 supporting documentation, and that the name of CMU not be
16 used in advertising or publicity pertaining to distribution of the
17 software without specific, written prior permission.
19 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
20 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
22 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
23 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
24 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26 ******************************************************************/
28 #include <net-snmp/net-snmp-config.h>
32 #include "fp4/cmdmacro.h"
41 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
44 # include <sys/timeb.h>
46 # include <sys/time.h>
51 # include <sys/time.h>
57 #include <sys/select.h>
60 #include <netinet/in.h>
63 #include <arpa/inet.h>
78 #include <net-snmp/types.h>
79 #include <net-snmp/output_api.h>
80 #include <net-snmp/utilities.h>
82 #include <net-snmp/library/asn1.h>
83 #include <net-snmp/library/snmp_api.h>
84 #include <net-snmp/library/mib.h>
85 #include <net-snmp/library/md5.h>
86 #include <net-snmp/library/scapi.h>
92 /*******************************************************************-o-******
97 * *length (I/O) Bytes left in message.
98 * *psid (O) Community string.
99 * *slen (O) Length of community string.
100 * *version (O) Message version.
103 * Pointer to the remainder of data.
106 * Parse the header of a community string-based message such as that found
107 * in SNMPv1 and SNMPv2c.
110 snmp_comstr_parse(u_char * data,
112 u_char * psid, size_t * slen, long *version)
116 size_t origlen = *slen;
119 * Message is an ASN.1 SEQUENCE.
121 data = asn_parse_sequence(data, length, &type,
122 (ASN_SEQUENCE | ASN_CONSTRUCTOR),
129 * First field is the version.
131 DEBUGDUMPHEADER("recv", "SNMP version");
132 data = asn_parse_int(data, length, &type, &ver, sizeof(ver));
136 ERROR_MSG("bad parse of version");
141 * second field is the community string for SNMPv1 & SNMPv2c
143 DEBUGDUMPHEADER("recv", "community string");
144 data = asn_parse_string(data, length, &type, psid, slen);
147 ERROR_MSG("bad parse of community");
150 psid[SNMP_MIN(*slen, origlen - 1)] = '\0';
151 return (u_char *) data;
153 } /* end snmp_comstr_parse() */
158 /*******************************************************************-o-******
170 * Pointer into 'data' after built section.
173 * Build the header of a community string-based message such as that found
174 * in SNMPv1 and SNMPv2c.
176 * NOTE: The length of the message will have to be inserted later,
179 * NOTE: Version is an 'int'. (CMU had it as a long, but was passing
180 * in a *int. Grrr.) Assign version to verfix and pass in
181 * that to asn_build_int instead which expects a long. -- WH
184 snmp_comstr_build(u_char * data,
187 size_t * slen, long *version, size_t messagelen)
189 long verfix = *version;
192 size_t hlength = *length;
196 * Build the the message wrapper (note length will be inserted later).
199 asn_build_sequence(data, length,
200 (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), 0);
208 * Store the version field.
210 data = asn_build_int(data, length,
211 (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE |
212 ASN_INTEGER), &verfix, sizeof(verfix));
219 * Store the community string.
221 data = asn_build_string(data, length,
222 (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE |
223 ASN_OCTET_STR), psid,
233 asn_build_sequence(h1, &hlength,
234 (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR),
235 data - h1e + messagelen);
240 } /* end snmp_comstr_build() */