1 /*
2  * Portions of this file are subject to the following copyright(s).  See
3  * the Net-SNMP's COPYING file for more details and other copyrights
4  * that may apply:
5  *
6  * Portions of this file are copyrighted by:
7  * Copyright (c) 2016 VMware, Inc. All rights reserved.
8  * Use is subject to license terms specified in the COPYING file
9  * distributed with the Net-SNMP package.
10  */
11 
12 #ifndef NET_SNMP_TYPES_H
13 #define NET_SNMP_TYPES_H
14 
15     /**
16      *  Definitions of data structures, used within the library API.
17      */
18 
19 #include <stdio.h>
20 
21 #ifndef NET_SNMP_CONFIG_H
22 #error "Please include <net-snmp/net-snmp-config.h> before this file"
23 #endif
24 
25 #include <sys/types.h>
26 
27 #if defined(WIN32) && !defined(cygwin)
28 typedef HANDLE netsnmp_pid_t;
29 #define NETSNMP_NO_SUCH_PROCESS INVALID_HANDLE_VALUE
30 #else
31 /*
32  * Note: on POSIX-compliant systems, pid_t is defined in <sys/types.h>.
33  * And if pid_t has not been defined in <sys/types.h>, AC_TYPE_PID_T ensures
34  * that a pid_t definition is present in net-snmp-config.h.
35  */
36 typedef pid_t netsnmp_pid_t;
37 #define NETSNMP_NO_SUCH_PROCESS -1
38 #endif
39 
40 #include <net-snmp/library/oid.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #ifndef HAVE_SOCKLEN_T
47 #ifdef WIN32
48 typedef int socklen_t;
49 #else
50 typedef u_int socklen_t;
51 #endif
52 #endif
53 
54 #ifndef HAVE_SSIZE_T
55 #if defined(__INT_MAX__) && __INT_MAX__ == 2147483647
56 typedef int ssize_t;
57 #else
58 typedef long ssize_t;
59 #endif
60 #endif
61 
62 #ifndef HAVE_NFDS_T
63 typedef unsigned long int nfds_t;
64 #endif
65 
66 #ifdef HAVE_PCRE_H
67 /*
68  * Abstract the pcre typedef such that not all *.c files have to include
69  * <pcre.h>.
70  */
71 typedef struct {
72     void *regex_ptr;
73 } netsnmp_regex_ptr;
74 #endif
75 
76     /*
77      *  For the initial release, this will just refer to the
78      *  relevant UCD header files.
79      *    In due course, the types and structures relevant to the
80      *  Net-SNMP API will be identified, and defined here directly.
81      *
82      *  But for the time being, this header file is primarily a placeholder,
83      *  to allow application writers to adopt the new header file names.
84      */
85 
86 typedef union {
87    long           *integer;
88    u_char         *string;
89    oid            *objid;
90    u_char         *bitstring;
91    struct counter64 *counter64;
92 #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES
93    float          *floatVal;
94    double         *doubleVal;
95    /*
96     * t_union *unionVal;
97     */
98 #endif                          /* NETSNMP_WITH_OPAQUE_SPECIAL_TYPES */
99 } netsnmp_vardata;
100 
101 
102 #define MAX_OID_LEN	    128 /* max subid's in an oid */
103 
104 /** @typedef struct variable_list netsnmp_variable_list
105  * Typedefs the variable_list struct into netsnmp_variable_list */
106 /** @struct variable_list
107  * The netsnmp variable list binding structure, it's typedef'd to
108  * netsnmp_variable_list.
109  */
110 typedef struct variable_list {
111    /** NULL for last variable */
112    struct variable_list *next_variable;
113    /** Object identifier of variable */
114    oid            *name;
115    /** number of subid's in name */
116    size_t          name_length;
117    /** ASN type of variable */
118    u_char          type;
119    /** value of variable */
120     netsnmp_vardata val;
121    /** the length of the value to be copied into buf */
122    size_t          val_len;
123    /** buffer to hold the OID */
124    oid             name_loc[MAX_OID_LEN];
125    /** 90 percentile < 40. */
126    u_char          buf[40];
127    /** (Opaque) hook for additional data */
128    void           *data;
129    /** callback to free above */
130    void            (*dataFreeHook)(void *);
131    int             index;
132 } netsnmp_variable_list;
133 
134 
135 /** @typedef struct snmp_pdu to netsnmp_pdu
136  * Typedefs the snmp_pdu struct into netsnmp_pdu */
137 /** @struct snmp_pdu
138  * The snmp protocol data unit.
139  */
140 typedef struct snmp_pdu {
141 
142 #define non_repeaters	errstat
143 #define max_repetitions errindex
144 
145     /*
146      * Protocol-version independent fields
147      */
148     /** snmp version */
149     long            version;
150     /** Type of this PDU */
151     int             command;
152     /** Request id - note: incremented for each retry */
153     long            reqid;
154     /** Message id for V3 messages note: incremented for each retry */
155     long            msgid;
156     /** Unique ID for incoming transactions */
157     long            transid;
158     /** Session id for AgentX messages */
159     long            sessid;
160     /** Error status (non_repeaters in GetBulk) */
161     long            errstat;
162     /** Error index (max_repetitions in GetBulk) */
163     long            errindex;
164     /** Uptime */
165     u_long          time;
166     u_long          flags;
167 
168     int             securityModel;
169     /** noAuthNoPriv, authNoPriv, authPriv */
170     int             securityLevel;
171     int             msgParseModel;
172 
173     /** smallest of max for transport, v3 msgMaxSize and local cfg. */
174     long            msgMaxSize;
175 
176     /**
177      * Transport-specific opaque data.  This replaces the IP-centric address
178      * field.
179      */
180 
181     void           *transport_data;
182     int             transport_data_length;
183 
184     /**
185      * The actual transport domain.  This SHOULD NOT BE FREE()D.
186      */
187 
188     const oid      *tDomain;
189     size_t          tDomainLen;
190 
191     netsnmp_variable_list *variables;
192 
193 
194     /*
195      * SNMPv1 & SNMPv2c fields
196      */
197     /** community for outgoing requests. */
198     u_char         *community;
199     /** length of community name. */
200     size_t          community_len;
201 
202     /*
203      * Trap information
204      */
205     /** System OID */
206     oid            *enterprise;
207     size_t          enterprise_length;
208     /** trap type */
209     long            trap_type;
210     /** specific type */
211     long            specific_type;
212     /** This is ONLY used for v1 TRAPs  */
213     unsigned char   agent_addr[4];
214 
215     /*
216      *  SNMPv3 fields
217      */
218     /** context snmpEngineID */
219     u_char         *contextEngineID;
220     /** Length of contextEngineID */
221     size_t          contextEngineIDLen;
222     /** authoritative contextName */
223     char           *contextName;
224     /** Length of contextName */
225     size_t          contextNameLen;
226     /** authoritative snmpEngineID for security */
227     u_char         *securityEngineID;
228     /** Length of securityEngineID */
229     size_t          securityEngineIDLen;
230     /** on behalf of this principal */
231     char           *securityName;
232     /** Length of securityName. */
233     size_t          securityNameLen;
234 
235     /*
236      * AgentX fields
237      *      (also uses SNMPv1 community field)
238      */
239     int             priority;
240     int             range_subid;
241 
242     void           *securityStateRef;
243 } netsnmp_pdu;
244 
245 
246 /**
247  * @typedef struct snmp_session netsnmp_session
248  * Typedefs the snmp_session struct into netsnmp_session.
249  */
250 typedef struct snmp_session netsnmp_session;
251 
252 /** for openssl this should match up with EVP_MAX_MD_SIZE */
253 #define USM_AUTH_KU_LEN     64
254 #define USM_PRIV_KU_LEN     64
255 
256 typedef int        (*snmp_callback) (int, netsnmp_session *, int,
257                                           netsnmp_pdu *, void *);
258 typedef int     (*netsnmp_callback) (int, netsnmp_session *, int,
259                                           netsnmp_pdu *, void *);
260 
261 struct netsnmp_container_s;
262 
263 #ifndef NETSNMP_NO_TRAP_STATS
264     /*
265      * trap/inform statistics.
266      *
267      * all times are sysuptime
268      */
269 typedef struct netsnmp_trap_stats_s {
270     u_long   sent_count;
271     u_long   sent_last_sent;
272 
273     u_long   sent_fail_count;
274     u_long   sent_last_fail;
275 
276     u_long   ack_count;
277     u_long   ack_last_rcvd;
278 
279     u_long   sec_err_count;
280     u_long   sec_err_last;
281 
282     u_long   timeouts;
283     u_long   sent_last_timeout;
284 } netsnmp_trap_stats;
285 #endif /* NETSNMP_NO_TRAP_STATS */
286 
287 /** @struct snmp_session
288  * The snmp session structure.
289  */
290 struct snmp_session {
291     /*
292      * Protocol-version independent fields
293      */
294     /** snmp version */
295     long            version;
296     /** Number of retries before timeout. */
297     int             retries;
298     /** Number of uS until first timeout, then exponential backoff */
299     long            timeout;
300     u_long          flags;
301     struct snmp_session *subsession;
302     struct snmp_session *next;
303 
304     /** name or address of default peer (may include transport specifier and/or port number) */
305     char           *peername;
306     /** UDP port number of peer. (NO LONGER USED - USE peername INSTEAD) */
307     u_short         remote_port NETSNMP_ATTRIBUTE_DEPRECATED;
308     /** My Domain name or dotted IP address, 0 for default */
309     char           *localname;
310     /** My UDP port number, 0 for default, picked randomly */
311     u_short         local_port;
312     /**
313      * Authentication function or NULL if null authentication is used
314      */
315     u_char         *(*authenticator) (u_char *, size_t *, u_char *, size_t);
316     /** Function to interpret incoming data */
317     netsnmp_callback callback;
318     /**
319      * Pointer to data that the callback function may consider important
320      */
321     void           *callback_magic;
322     /** copy of system errno */
323     int             s_errno;
324     /** copy of library errno */
325     int             s_snmp_errno;
326     /** Session id - AgentX only */
327     long            sessid;
328 
329     /*
330      * SNMPv1 & SNMPv2c fields
331      */
332     /** community for outgoing requests. */
333     u_char         *community;
334     /** Length of community name. */
335     size_t          community_len;
336     /**  Largest message to try to receive.  */
337     size_t          rcvMsgMaxSize;
338     /**  Largest message to try to send.  */
339     size_t          sndMsgMaxSize;
340 
341     /*
342      * SNMPv3 fields
343      */
344     /** are we the authoritative engine? */
345     u_char          isAuthoritative;
346     /** authoritative snmpEngineID */
347     u_char         *contextEngineID;
348     /** Length of contextEngineID */
349     size_t          contextEngineIDLen;
350     /** initial engineBoots for remote engine */
351     u_int           engineBoots;
352     /** initial engineTime for remote engine */
353     u_int           engineTime;
354     /** authoritative contextName */
355     char           *contextName;
356     /** Length of contextName */
357     size_t          contextNameLen;
358     /** authoritative snmpEngineID */
359     u_char         *securityEngineID;
360     /** Length of contextEngineID */
361     size_t          securityEngineIDLen;
362     /** on behalf of this principal */
363     char           *securityName;
364     /** Length of securityName. */
365     size_t          securityNameLen;
366 
367     /** auth protocol oid */
368     oid            *securityAuthProto;
369     /** Length of auth protocol oid */
370     size_t          securityAuthProtoLen;
371     /** Ku for auth protocol XXX */
372     u_char          securityAuthKey[USM_AUTH_KU_LEN];
373     /** Length of Ku for auth protocol */
374     size_t          securityAuthKeyLen;
375     /** Kul for auth protocol */
376     u_char          *securityAuthLocalKey;
377     /** Length of Kul for auth protocol XXX */
378     size_t          securityAuthLocalKeyLen;
379 
380     /** priv protocol oid */
381     oid            *securityPrivProto;
382     /** Length of priv protocol oid */
383     size_t          securityPrivProtoLen;
384     /** Ku for privacy protocol XXX */
385     u_char          securityPrivKey[USM_PRIV_KU_LEN];
386     /** Length of Ku for priv protocol */
387     size_t          securityPrivKeyLen;
388     /** Kul for priv protocol */
389     u_char          *securityPrivLocalKey;
390     /** Length of Kul for priv protocol XXX */
391     size_t          securityPrivLocalKeyLen;
392 
393     /** snmp security model, v1, v2c, usm */
394     int             securityModel;
395     /** noAuthNoPriv, authNoPriv, authPriv */
396     int             securityLevel;
397     /** target param name */
398     char           *paramName;
399 #ifndef NETSNMP_NO_TRAP_STATS
400     netsnmp_trap_stats *trap_stats;
401 #endif /* NETSNMP_NO_TRAP_STATS */
402 
403     /**
404      * security module specific
405      */
406     void           *securityInfo;
407 
408     /**
409      * transport specific configuration
410      */
411    struct netsnmp_container_s *transport_configuration;
412 
413     /**
414      * use as you want data
415      *
416      *     used by 'SNMP_FLAGS_RESP_CALLBACK' handling in the agent
417      * XXX: or should we add a new field into this structure?
418      */
419     void           *myvoid;
420 };
421 
422 
423 #include <net-snmp/library/types.h>
424 #include <net-snmp/definitions.h>
425 #include <net-snmp/library/snmp_api.h>
426 
427 #ifdef __cplusplus
428 }
429 #endif
430 
431 #endif                          /* NET_SNMP_TYPES_H */
432