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