]>
Commit | Line | Data |
---|---|---|
43d4303e | 1 | |
2 | ||
85269fdf | 3 | /********************************************************************** |
4 | * | |
5 | * Copyright 1997 by Carnegie Mellon University | |
04a725db | 6 | * |
85269fdf | 7 | * All Rights Reserved |
04a725db | 8 | * |
85269fdf | 9 | * Permission to use, copy, modify, and distribute this software and its |
10 | * documentation for any purpose and without fee is hereby granted, | |
04a725db | 11 | * provided that the above copyright notice appear in all copies and that |
85269fdf | 12 | * both that copyright notice and this permission notice appear in |
04a725db | 13 | * supporting documentation, and that the name of CMU not be |
14 | * used in advertising or publicity pertaining to distribution of the | |
85269fdf | 15 | * software without specific, written prior permission. |
04a725db | 16 | * |
17 | * CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING | |
18 | * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL | |
19 | * CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR | |
20 | * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
21 | * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
22 | * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
23 | * SOFTWARE. | |
85269fdf | 24 | * |
25 | **********************************************************************/ | |
da2d50d1 | 26 | |
931ae822 | 27 | #include "config.h" |
28 | ||
85269fdf | 29 | #include <stdio.h> |
30 | ||
931ae822 | 31 | #if HAVE_UNISTD_H |
32 | #include <unistd.h> | |
33 | #endif | |
34 | #if HAVE_STDLIB_H | |
35 | #include <stdlib.h> | |
36 | #endif | |
37 | #if HAVE_SYS_TYPES_H | |
da2d50d1 | 38 | #include <sys/types.h> |
931ae822 | 39 | #endif |
40 | #if HAVE_CTYPE_H | |
41 | #include <ctype.h> | |
42 | #endif | |
43 | #if HAVE_GNUMALLOC_H | |
44 | #include <gnumalloc.h> | |
45 | #elif HAVE_MALLOC_H && !defined(_SQUID_FREEBSD_) && !defined(_SQUID_NEXT_) | |
46 | #include <malloc.h> | |
47 | #endif | |
48 | #if HAVE_MEMORY_H | |
49 | #include <memory.h> | |
50 | #endif | |
51 | #ifdef HAVE_STRING_H | |
52 | #include <string.h> | |
53 | #endif | |
54 | #ifdef HAVE_STRINGS_H | |
55 | #include <strings.h> | |
56 | #endif | |
57 | #if HAVE_BSTRING_H | |
58 | #include <bstring.h> | |
59 | #endif | |
60 | #if HAVE_SYS_SOCKET_H | |
da2d50d1 | 61 | #include <sys/socket.h> |
931ae822 | 62 | #endif |
63 | #if HAVE_NETINET_IN_H | |
64 | #include <netinet/in.h> | |
65 | #endif | |
66 | #if HAVE_ARPA_INET_H | |
da2d50d1 | 67 | #include <arpa/inet.h> |
da2d50d1 | 68 | #endif |
85269fdf | 69 | #if HAVE_SYS_TIME_H |
70 | #include <sys/time.h> | |
71 | #endif | |
72 | #if HAVE_NETDB_H | |
73 | #include <netdb.h> | |
74 | #endif | |
da2d50d1 | 75 | |
76 | #include "asn1.h" | |
77 | #include "snmp.h" | |
85269fdf | 78 | |
79 | #include "snmp-internal.h" | |
da2d50d1 | 80 | #include "snmp_impl.h" |
85269fdf | 81 | #include "snmp_session.h" |
85269fdf | 82 | #include "snmp_error.h" |
83 | #include "snmp_vars.h" | |
84 | #include "snmp_pdu.h" | |
85 | #include "snmp_msg.h" | |
86 | ||
da2d50d1 | 87 | #include "snmp_api.h" |
85269fdf | 88 | #include "snmp_api_error.h" |
89 | #include "snmp_api_util.h" | |
da2d50d1 | 90 | |
931ae822 | 91 | #include "util.h" |
92 | ||
85269fdf | 93 | /**********************************************************************/ |
da2d50d1 | 94 | |
627f6d02 | 95 | /* |
96 | * Takes a session and a pdu and serializes the ASN PDU into the area | |
97 | * pointed to by packet. out_length is the size of the data area available. | |
85269fdf | 98 | * Returns the length of the encoded packet in out_length. If an error |
99 | * occurs, -1 is returned. If all goes well, 0 is returned. | |
627f6d02 | 100 | */ |
101 | int | |
85269fdf | 102 | snmp_build(session, pdu, packet, out_length) |
43d4303e | 103 | struct snmp_session *session; |
104 | struct snmp_pdu *pdu; | |
105 | u_char *packet; | |
106 | int *out_length; | |
627f6d02 | 107 | { |
85269fdf | 108 | u_char *bufp; |
109 | ||
110 | bufp = snmp_msg_Encode(packet, out_length, | |
43d4303e | 111 | session->community, session->community_len, |
112 | session->Version, | |
113 | pdu); | |
114 | snmplib_debug(8, "LIBSNMP: snmp_build(): Packet len %d (requid %d)\n", | |
115 | *out_length, pdu->reqid); | |
85269fdf | 116 | |
117 | if (bufp == NULL) | |
43d4303e | 118 | return (-1); |
85269fdf | 119 | |
43d4303e | 120 | return (0); |
627f6d02 | 121 | } |
122 | ||
123 | /* | |
85269fdf | 124 | * Parses the packet recieved on the input session, and places the data into |
627f6d02 | 125 | * the input pdu. length is the length of the input packet. If any errors |
85269fdf | 126 | * are encountered, NULL is returned. If not, the community is. |
627f6d02 | 127 | */ |
43d4303e | 128 | u_char * |
129 | snmp_parse(struct snmp_session * session, | |
130 | struct snmp_pdu * pdu, | |
131 | u_char * data, | |
132 | int length) | |
627f6d02 | 133 | { |
85269fdf | 134 | u_char Community[128]; |
135 | u_char *bufp; | |
136 | int CommunityLen = 128; | |
137 | ||
138 | /* Decode the entire message. */ | |
43d4303e | 139 | data = snmp_msg_Decode(data, &length, |
140 | Community, &CommunityLen, | |
141 | &session->Version, pdu); | |
627f6d02 | 142 | if (data == NULL) |
43d4303e | 143 | return (NULL); |
627f6d02 | 144 | |
43d4303e | 145 | bufp = (u_char *) xmalloc(CommunityLen + 1); |
146 | if (bufp == NULL) | |
147 | return (NULL); | |
627f6d02 | 148 | |
b86202ac | 149 | strncpy((char *) bufp, (char *) Community, CommunityLen); |
150 | bufp[CommunityLen] = '\0'; | |
151 | ||
43d4303e | 152 | return (bufp); |
627f6d02 | 153 | } |