snmp_transport.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef _SNMP_TRANSPORT_H
  2. #define _SNMP_TRANSPORT_H
  3. /*
  4. * Portions of this file are copyrighted by:
  5. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  6. * Use is subject to license terms specified in the COPYING file
  7. * distributed with the Net-SNMP package.
  8. */
  9. #include <sys/types.h>
  10. #ifdef HAVE_SYS_SOCKET_H
  11. #ifdef solaris2
  12. #define _XPG4_2
  13. #endif
  14. #if defined(aix4) || defined(aix5) || defined(aix6) || defined(aix7)
  15. #define _LINUX_SOURCE_COMPAT
  16. #endif
  17. #include <sys/socket.h>
  18. #ifdef solaris2
  19. # ifndef CMSG_SPACE
  20. # define CMSG_SPACE(l) \
  21. ((unsigned int)_CMSG_HDR_ALIGN(sizeof (struct cmsghdr) + (l)))
  22. # define CMSG_LEN(l) (_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)) + (l))
  23. # endif
  24. #endif
  25. #endif /* HAVE_SYS_SOCKET_H */
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #include <net-snmp/library/asn1.h>
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Some transport-type constants. */
  34. #ifndef NETSNMP_STREAM_QUEUE_LEN
  35. #define NETSNMP_STREAM_QUEUE_LEN 5
  36. #endif
  37. /* Some transport-type flags. */
  38. #define NETSNMP_TRANSPORT_FLAG_STREAM 0x01
  39. #define NETSNMP_TRANSPORT_FLAG_LISTEN 0x02
  40. #define NETSNMP_TRANSPORT_FLAG_TUNNELED 0x04
  41. #define NETSNMP_TRANSPORT_FLAG_TMSTATE 0x08 /* indicates opaque is a
  42. TSM tmStateReference */
  43. #define NETSNMP_TRANSPORT_FLAG_EMPTY_PKT 0x10
  44. #define NETSNMP_TRANSPORT_FLAG_OPENED 0x20 /* f_open called */
  45. #define NETSNMP_TRANSPORT_FLAG_SHARED 0x40
  46. #define NETSNMP_TRANSPORT_FLAG_HOSTNAME 0x80 /* for fmtaddr hook */
  47. /* The standard SNMP domains. */
  48. NETSNMP_IMPORT oid netsnmpUDPDomain[]; /* = { 1, 3, 6, 1, 6, 1, 1 }; */
  49. NETSNMP_IMPORT oid netsnmpCLNSDomain[]; /* = { 1, 3, 6, 1, 6, 1, 2 }; */
  50. NETSNMP_IMPORT oid netsnmpCONSDomain[]; /* = { 1, 3, 6, 1, 6, 1, 3 }; */
  51. NETSNMP_IMPORT oid netsnmpDDPDomain[]; /* = { 1, 3, 6, 1, 6, 1, 4 }; */
  52. NETSNMP_IMPORT oid netsnmpIPXDomain[]; /* = { 1, 3, 6, 1, 6, 1, 5 }; */
  53. NETSNMP_IMPORT size_t netsnmpUDPDomain_len;
  54. NETSNMP_IMPORT size_t netsnmpCLNSDomain_len;
  55. NETSNMP_IMPORT size_t netsnmpCONSDomain_len;
  56. NETSNMP_IMPORT size_t netsnmpDDPDomain_len;
  57. NETSNMP_IMPORT size_t netsnmpIPXDomain_len;
  58. /* Structure which stores transport security model specific parameters */
  59. /* isms-secshell-11 section 4.1 */
  60. /* contents documented in draft-ietf-isms-transport-security-model
  61. Section 3.2 */
  62. /* note: VACM only allows <= 32 so this is overkill till another ACM comes */
  63. #define NETSNMP_TM_MAX_SECNAME 256
  64. typedef union netsnmp_sockaddr_storage_u {
  65. struct sockaddr sa;
  66. struct sockaddr_in sin;
  67. #ifdef NETSNMP_ENABLE_IPV6
  68. struct sockaddr_in6 sin6;
  69. #endif
  70. } netsnmp_sockaddr_storage;
  71. typedef struct netsnmp_addr_pair_s {
  72. netsnmp_sockaddr_storage remote_addr;
  73. netsnmp_sockaddr_storage local_addr;
  74. } netsnmp_addr_pair;
  75. typedef struct netsnmp_indexed_addr_pair_s {
  76. netsnmp_sockaddr_storage remote_addr;
  77. netsnmp_sockaddr_storage local_addr;
  78. int if_index;
  79. } netsnmp_indexed_addr_pair;
  80. typedef struct netsnmp_tmStateReference_s {
  81. oid transportDomain[MAX_OID_LEN];
  82. size_t transportDomainLen;
  83. char securityName[NETSNMP_TM_MAX_SECNAME];
  84. size_t securityNameLen;
  85. int requestedSecurityLevel;
  86. int transportSecurityLevel;
  87. char sameSecurity;
  88. char sessionID[8];
  89. char have_addresses;
  90. netsnmp_indexed_addr_pair addresses;
  91. void *otherTransportOpaque; /* XXX: May have mem leak issues */
  92. } netsnmp_tmStateReference;
  93. #define NETSNMP_TSPEC_LOCAL 0x01 /* 1=server, 0=client */
  94. #define NETSNMP_TSPEC_PREBOUND 0x02 /* 1=bound by systemd, 0=needs bind in the library */
  95. struct netsnmp_container_s; /* forward decl */
  96. typedef struct netsnmp_tdomain_spec_s {
  97. const char *application; /* application name */
  98. const char *target; /* target as string */
  99. u_int flags;
  100. const char *default_domain; /* default domain */
  101. const char *default_target; /* default target */
  102. const char *source; /* source as string iff remote */
  103. struct netsnmp_container_s *transport_config; /* extra config */
  104. } netsnmp_tdomain_spec;
  105. /* Structure which defines the transport-independent API. */
  106. struct snmp_session;
  107. typedef struct netsnmp_transport_s {
  108. /* The transport domain object identifier. */
  109. const oid *domain;
  110. int domain_length; /* In sub-IDs, not octets. */
  111. /* Local transport address (in relevant SNMP-style encoding). */
  112. void *local;
  113. int local_length; /* In octets. */
  114. /* Remote transport address (in relevant SNMP-style encoding). */
  115. void *remote;
  116. int remote_length; /* In octets. */
  117. /* The actual socket. */
  118. int sock;
  119. /* Flags (see #definitions above). */
  120. unsigned int flags;
  121. /* Protocol-specific opaque data pointer. */
  122. void *data;
  123. int data_length;
  124. /* Maximum size of PDU that can be sent/received by this transport. */
  125. size_t msgMaxSize;
  126. #ifdef FOR_STANDARDS_COMPLIANCE_OR_FUTURE_USE
  127. /* TM state reference per ISMS WG solution */
  128. netsnmp_tmStateReference *tmStateRef;
  129. #endif
  130. /* tunneled transports */
  131. struct netsnmp_transport_s * base_transport;
  132. /* Callbacks. Arguments are:
  133. *
  134. * "this" pointer, fd, buf, size, *opaque, *opaque_length
  135. */
  136. int (*f_recv) (struct netsnmp_transport_s *, void *,
  137. int, void **, int *);
  138. int (*f_send) (struct netsnmp_transport_s *, const void *,
  139. int, void **, int *);
  140. int (*f_close) (struct netsnmp_transport_s *);
  141. /* Optional: opening can occur during creation if more appropriate */
  142. struct netsnmp_transport_s * (*f_open) (struct netsnmp_transport_s *);
  143. /* This callback is only necessary for stream-oriented transports. */
  144. int (*f_accept) (struct netsnmp_transport_s *);
  145. /* Optional callback to format a transport address. */
  146. char *(*f_fmtaddr)(struct netsnmp_transport_s *, const void *,
  147. int);
  148. /* Optional callback to support extra configuration token/value pairs */
  149. /* return non-zero on error */
  150. int (*f_config)(struct netsnmp_transport_s *, const char *,
  151. const char *);
  152. /* Optional callback that is called after the first transport is
  153. cloned to the second */
  154. int (*f_copy)(const struct netsnmp_transport_s *,
  155. struct netsnmp_transport_s *);
  156. /* Setup initial session config if special things are needed */
  157. int (*f_setup_session)(struct netsnmp_transport_s *,
  158. struct snmp_session *);
  159. /* allocated host name identifier; used by configuration system
  160. to load localhost.conf for host-specific configuration */
  161. u_char *identifier; /* udp:localhost:161 -> "localhost" */
  162. /* Duplicate the remote address in the format required by SNMP-TARGET-MIB */
  163. void (*f_get_taddr)(struct netsnmp_transport_s *t,
  164. void **addr, size_t *addr_len);
  165. } netsnmp_transport;
  166. typedef struct netsnmp_transport_list_s {
  167. netsnmp_transport *transport;
  168. struct netsnmp_transport_list_s *next;
  169. } netsnmp_transport_list;
  170. typedef struct netsnmp_tdomain_s {
  171. const oid *name;
  172. size_t name_length;
  173. const char **prefix;
  174. /*
  175. * The f_create_from_tstring and f_create_from_tstring_new fields are
  176. * deprecated, please do not use them for new code and try to migrate
  177. * old code away from using them.
  178. */
  179. netsnmp_transport *(*f_create_from_tstring) (const char *, int);
  180. /* @o and @o_len define an address in the format used by SNMP-TARGET-MIB */
  181. netsnmp_transport *(*f_create_from_ostring) (const void *o, size_t o_len,
  182. int local);
  183. struct netsnmp_tdomain_s *next;
  184. /** deprecated, please do not use it */
  185. netsnmp_transport *(*f_create_from_tstring_new) (const char *, int,
  186. const char*);
  187. netsnmp_transport *(*f_create_from_tspec) (netsnmp_tdomain_spec *);
  188. } netsnmp_tdomain;
  189. void init_snmp_transport(void);
  190. void shutdown_snmp_transport(void);
  191. /* Some utility functions. */
  192. NETSNMP_IMPORT
  193. char *netsnmp_transport_peer_string(netsnmp_transport *t, const void *data,
  194. int len);
  195. int netsnmp_transport_send(netsnmp_transport *t, const void *data, int len,
  196. void **opaque, int *olength);
  197. int netsnmp_transport_recv(netsnmp_transport *t, void *data, int len,
  198. void **opaque, int *olength);
  199. int netsnmp_transport_add_to_list(netsnmp_transport_list **transport_list,
  200. netsnmp_transport *transport);
  201. int netsnmp_transport_remove_from_list(netsnmp_transport_list **transport_list,
  202. netsnmp_transport *transport);
  203. NETSNMP_IMPORT
  204. int netsnmp_sockaddr_size(const struct sockaddr *sa);
  205. /*
  206. * Return an exact (deep) copy of t, or NULL if there is a memory allocation
  207. * problem (for instance).
  208. */
  209. netsnmp_transport *netsnmp_transport_copy(const netsnmp_transport *t);
  210. /* Free an netsnmp_transport. */
  211. NETSNMP_IMPORT
  212. void netsnmp_transport_free(netsnmp_transport *t);
  213. #ifndef FEATURE_REMOVE_TRANSPORT_CACHE
  214. /** transport cache support */
  215. NETSNMP_IMPORT
  216. netsnmp_transport *netsnmp_transport_cache_get(int af, int type, int local,
  217. const netsnmp_sockaddr_storage *bind_addr);
  218. NETSNMP_IMPORT
  219. int netsnmp_transport_cache_save(int af, int type, int local,
  220. const netsnmp_sockaddr_storage *addr,
  221. netsnmp_transport *t);
  222. NETSNMP_IMPORT
  223. int netsnmp_transport_cache_remove(netsnmp_transport *t);
  224. #endif /* FEATURE_REMOVE_TRANSPORT_CACHE */
  225. /*
  226. * If the passed oid (in_oid, in_len) corresponds to a supported transport
  227. * domain, return 1; if not return 0. If out_oid is not NULL and out_len is
  228. * not NULL, then the "internal" oid which should be used to identify this
  229. * domain (e.g. in pdu->tDomain etc.) is written to *out_oid and its length to
  230. * *out_len.
  231. */
  232. NETSNMP_IMPORT
  233. int netsnmp_tdomain_support(const oid *in_oid, size_t in_len,
  234. const oid **out_oid, size_t *out_len);
  235. int netsnmp_tdomain_register(netsnmp_tdomain *domain);
  236. int netsnmp_tdomain_unregister(netsnmp_tdomain *domain);
  237. NETSNMP_IMPORT
  238. void netsnmp_clear_tdomain_list(void);
  239. void netsnmp_tdomain_init(void);
  240. NETSNMP_IMPORT
  241. netsnmp_transport *netsnmp_tdomain_transport(const char *str,
  242. int local,
  243. const char *default_domain);
  244. NETSNMP_IMPORT
  245. netsnmp_transport *netsnmp_tdomain_transport_full(const char *application,
  246. const char *str,
  247. int local,
  248. const char *default_domain,
  249. const char *default_target);
  250. NETSNMP_IMPORT
  251. netsnmp_transport *netsnmp_tdomain_transport_oid(const oid * dom,
  252. size_t dom_len,
  253. const u_char * o,
  254. size_t o_len,
  255. int local);
  256. NETSNMP_IMPORT
  257. netsnmp_transport *netsnmp_tdomain_transport_tspec(netsnmp_tdomain_spec *tspec);
  258. NETSNMP_IMPORT
  259. netsnmp_transport*
  260. netsnmp_transport_open_client(const char* application, const char* str);
  261. NETSNMP_IMPORT
  262. netsnmp_transport*
  263. netsnmp_transport_open_server(const char* application, const char* str);
  264. netsnmp_transport*
  265. netsnmp_transport_open(const char* application, const char* str, int local);
  266. typedef struct netsnmp_transport_config_s {
  267. char *key;
  268. char *value;
  269. } netsnmp_transport_config;
  270. NETSNMP_IMPORT
  271. int netsnmp_transport_config_compare(netsnmp_transport_config *left,
  272. netsnmp_transport_config *right);
  273. NETSNMP_IMPORT
  274. netsnmp_transport_config *netsnmp_transport_create_config(const char *key,
  275. const char *value);
  276. #ifndef NETSNMP_FEATURE_REMOVE_FILTER_SOURCE
  277. NETSNMP_IMPORT
  278. void netsnmp_transport_parse_filterType(const char *word, char *cptr);
  279. NETSNMP_IMPORT
  280. int netsnmp_transport_filter_add(const char *addrtxt);
  281. NETSNMP_IMPORT
  282. int netsnmp_transport_filter_remove(const char *addrtxt);
  283. NETSNMP_IMPORT
  284. int netsnmp_transport_filter_check(const char *addrtxt);
  285. NETSNMP_IMPORT
  286. void netsnmp_transport_filter_cleanup(void);
  287. #endif /* NETSNMP_FEATURE_REMOVE_FILTER_SOURCE */
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif/*_SNMP_TRANSPORT_H*/