container.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /* Portions of this file are subject to the following copyright(s). See
  2. * the Net-SNMP's COPYING file for more details and other copyrights
  3. * that may apply:
  4. */
  5. /*
  6. * Portions of this file are copyrighted by:
  7. * Copyright (C) 2007 Apple, 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. * Portions of this file are copyrighted by:
  12. * Copyright (c) 2016 VMware, Inc. All rights reserved.
  13. * Use is subject to license terms specified in the COPYING file
  14. * distributed with the Net-SNMP package.
  15. */
  16. #ifndef NETSNMP_CONTAINER_H
  17. #define NETSNMP_CONTAINER_H
  18. /*
  19. * $Id$
  20. *
  21. * WARNING: This is a recently created file, and all of it's contents are
  22. * subject to change at any time.
  23. *
  24. * A basic container template. A generic way for code to store and
  25. * retrieve data. Allows for interchangable storage algorithms.
  26. */
  27. #ifndef NET_SNMP_CONFIG_H
  28. #error "Please include <net-snmp/net-snmp-config.h> before this file"
  29. #endif
  30. #include <stdlib.h> /* free() */
  31. #include <net-snmp/types.h>
  32. #include <net-snmp/library/snmp_logging.h>
  33. #include <net-snmp/library/tools.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /*************************************************************************
  38. *
  39. * function pointer definitions
  40. *
  41. *************************************************************************/
  42. struct netsnmp_container_s; /** forward declare */
  43. struct netsnmp_factory_s; /** forward declare */
  44. struct netsnmp_iterator_s; /** forward declare */
  45. /*
  46. * function for performing an operation on a container which
  47. * returns (maybe the same) container.
  48. */
  49. typedef struct netsnmp_container_s* (netsnmp_container_mod_op)
  50. (struct netsnmp_container_s *, void *context, u_int flags);
  51. /*
  52. * function for setting an option on a container
  53. */
  54. typedef int (netsnmp_container_option)(struct netsnmp_container_s *,
  55. int set, u_int flags);
  56. /*
  57. * function returning an int for an operation on a container
  58. */
  59. typedef int (netsnmp_container_rc)(struct netsnmp_container_s *);
  60. /*
  61. * function returning an iterator for a container
  62. */
  63. typedef struct netsnmp_iterator_s * (netsnmp_container_it)
  64. (struct netsnmp_container_s *);
  65. /*
  66. * function returning a size_t for an operation on a container
  67. */
  68. typedef size_t (netsnmp_container_size)(struct netsnmp_container_s *);
  69. /*
  70. * function returning an int for an operation on an object and
  71. * a container
  72. */
  73. typedef int (netsnmp_container_op)(struct netsnmp_container_s *,
  74. const void *data);
  75. /*
  76. * function returning an int for an operation on an object at a given
  77. * position in a container (for containers supporting direct access)
  78. */
  79. typedef int (netsnmp_container_da_op)(struct netsnmp_container_s *,
  80. size_t pos, void *data);
  81. /*
  82. * function returning an int and an object at a given position in a
  83. * container (for containers supporting direct access)
  84. */
  85. typedef int (netsnmp_container_da_op_rtn)(struct netsnmp_container_s *,
  86. size_t pos,
  87. void **data);
  88. /*
  89. * function returning an oject for an operation on an object and a
  90. * container
  91. */
  92. typedef void * (netsnmp_container_rtn)(struct netsnmp_container_s *,
  93. const void *data);
  94. /*
  95. * function with no return which acts on an object
  96. */
  97. typedef void (netsnmp_container_obj_func)(void *data, void *context);
  98. /*
  99. * function with no return which calls a function on an object
  100. */
  101. typedef void (netsnmp_container_func)(struct netsnmp_container_s *,
  102. netsnmp_container_obj_func *,
  103. void *context);
  104. /*
  105. * function returning an array of objects for an operation on an
  106. * ojbect and a container
  107. */
  108. typedef netsnmp_void_array * (netsnmp_container_set)
  109. (struct netsnmp_container_s *, void *data);
  110. /*
  111. * function returning an int for a comparison between two objects
  112. */
  113. typedef int (netsnmp_container_compare)(const void *lhs,
  114. const void *rhs);
  115. /*************************************************************************
  116. *
  117. * Basic container
  118. *
  119. *************************************************************************/
  120. typedef struct netsnmp_container_s {
  121. /*
  122. * pointer for container implementation
  123. */
  124. void * container_data;
  125. /*
  126. * returns the number of items in a container
  127. */
  128. netsnmp_container_size *get_size;
  129. /*
  130. * initialize a container
  131. */
  132. netsnmp_container_rc *init;
  133. /*
  134. * release memory used by a container.
  135. *
  136. * Note: if your data structures contained allocated
  137. * memory, you are responsible for releasing that
  138. * memory before calling this function!
  139. */
  140. netsnmp_container_rc *cfree;
  141. /*
  142. * add an entry to the container
  143. */
  144. netsnmp_container_op *insert;
  145. /*
  146. * add an entry to the container at a given position
  147. */
  148. netsnmp_container_da_op *insert_before;
  149. netsnmp_container_da_op *insert_after;
  150. /*
  151. * remove an entry from the container
  152. */
  153. netsnmp_container_op *remove;
  154. /*
  155. * remove an entry from the container at a given position
  156. */
  157. netsnmp_container_da_op_rtn *remove_at;
  158. /*
  159. * release memory for an entry from the container
  160. */
  161. netsnmp_container_op *release; /* NOTE: deprecated. Use free_item */
  162. netsnmp_container_obj_func *free_item;
  163. /*
  164. * find the entry in the container with the same key
  165. *
  166. * Note: do not change the key! If you need to
  167. * change a key, remove the entry, change the key,
  168. * and the re-add the entry.
  169. */
  170. netsnmp_container_rtn *find;
  171. /*
  172. * find the entry in the container with the next highest key
  173. *
  174. * If the key is NULL, return the first item in the container.
  175. */
  176. netsnmp_container_rtn *find_next;
  177. /*
  178. * get entry at the given index (for containers supporting direct access
  179. */
  180. netsnmp_container_da_op_rtn *get_at;
  181. /*
  182. * find all entries in the container which match the partial key
  183. * returns allocated memory (netsnmp_void_array). User is responsible
  184. * for releasing this memory (free(array->array), free(array)).
  185. * DO NOT FREE ELEMENTS OF THE ARRAY, because they are the same pointers
  186. * stored in the container.
  187. */
  188. netsnmp_container_set *get_subset;
  189. /*
  190. * function to return an iterator for the container
  191. */
  192. netsnmp_container_it *get_iterator;
  193. /*
  194. * function to call another function for each object in the container
  195. */
  196. netsnmp_container_func *for_each;
  197. /*
  198. * specialized version of for_each used to optimize cleanup.
  199. * clear the container, optionally calling a function for each item.
  200. */
  201. netsnmp_container_func *clear;
  202. /*
  203. * OPTIONAL function to filter inserts to the container
  204. * (intended for a secondary container, which only wants
  205. * a sub-set of the objects in the primary/parent container)
  206. * Returns:
  207. * 1 : filter matched (don't insert)
  208. * 0 : no match (insert)
  209. */
  210. netsnmp_container_op *insert_filter;
  211. /*
  212. * OPTIONAL function to duplicate a container. Defaults to a shallow
  213. * copy. Only the specified container is copied (i.e. sub-containers
  214. * not included).
  215. */
  216. netsnmp_container_mod_op *duplicate;
  217. /*
  218. * function to compare two object stored in the container.
  219. *
  220. * Returns:
  221. *
  222. * -1 LHS < RHS
  223. * 0 LHS = RHS
  224. * 1 LHS > RHS
  225. */
  226. netsnmp_container_compare *compare;
  227. /*
  228. * same as compare, but RHS will be a partial key
  229. */
  230. netsnmp_container_compare *ncompare;
  231. /*
  232. * function to set container options
  233. */
  234. netsnmp_container_option *options;
  235. /*
  236. * unique name for finding a particular container in a list
  237. */
  238. char *container_name;
  239. /*
  240. * sort count, for iterators to track (insert/delete
  241. * bumps counter, invalidates iterator)
  242. */
  243. u_long sync;
  244. /*
  245. * flags
  246. */
  247. u_int flags;
  248. /*
  249. * containers can contain other containers (additional indexes)
  250. */
  251. struct netsnmp_container_s *next, *prev;
  252. } netsnmp_container;
  253. /*
  254. * initialize/free a container of container factories. used by
  255. * netsnmp_container_find* functions.
  256. */
  257. NETSNMP_IMPORT
  258. void netsnmp_container_init_list(void);
  259. NETSNMP_IMPORT
  260. void netsnmp_container_free_list(void);
  261. /*
  262. * register a new container factory
  263. */
  264. int netsnmp_container_register_with_compare(const char* name,
  265. struct netsnmp_factory_s *f,
  266. netsnmp_container_compare *c);
  267. int netsnmp_container_register(const char* name, struct netsnmp_factory_s *f);
  268. /*
  269. * search for and create a container from a list of types or a
  270. * specific type.
  271. */
  272. NETSNMP_IMPORT
  273. netsnmp_container * netsnmp_container_find(const char *type_list);
  274. netsnmp_container * netsnmp_container_get(const char *type);
  275. /*
  276. * utility routines
  277. */
  278. NETSNMP_IMPORT
  279. void netsnmp_container_add_index(netsnmp_container *primary,
  280. netsnmp_container *new_index);
  281. struct netsnmp_factory_s *netsnmp_container_get_factory(const char *type);
  282. /*
  283. * common comparison routines
  284. */
  285. /** first data element is a 'netsnmp_index' */
  286. NETSNMP_IMPORT
  287. int netsnmp_compare_netsnmp_index(const void *lhs, const void *rhs);
  288. NETSNMP_IMPORT
  289. int netsnmp_ncompare_netsnmp_index(const void *lhs, const void *rhs);
  290. /** first data element is a 'char *' */
  291. int netsnmp_compare_cstring(const void * lhs, const void * rhs);
  292. int netsnmp_ncompare_cstring(const void * lhs, const void * rhs);
  293. /** useful for octet strings */
  294. NETSNMP_IMPORT
  295. int netsnmp_compare_mem(const char * lhs, size_t lhs_len,
  296. const char * rhs, size_t rhs_len);
  297. /** no structure, just 'char *' pointers */
  298. int netsnmp_compare_direct_cstring(const void * lhs, const void * rhs);
  299. int netsnmp_compare_long(const void * lhs, const void * rhs);
  300. int netsnmp_compare_ulong(const void * lhs, const void * rhs);
  301. int netsnmp_compare_int32(const void * lhs, const void * rhs);
  302. int netsnmp_compare_uint32(const void * lhs, const void * rhs);
  303. /** for_each callback to call free on data item */
  304. NETSNMP_IMPORT
  305. void netsnmp_container_simple_free(void *data, void *context);
  306. /*
  307. * container optionflags
  308. */
  309. #define CONTAINER_KEY_ALLOW_DUPLICATES 0x00000001
  310. #define CONTAINER_KEY_UNSORTED 0x00000002
  311. /* ... */
  312. #define CONTAINER_FLAG_INTERNAL_1 0x80000000
  313. #define CONTAINER_SET_OPTIONS(x,o,rc) do { \
  314. if (NULL==(x)->options) \
  315. rc = -1; \
  316. else { \
  317. rc = (x)->options(x, 1, o); \
  318. if (rc != -1 ) \
  319. (x)->flags |= o; \
  320. } \
  321. } while(0)
  322. #define CONTAINER_CHECK_OPTION(x,o,rc) do { \
  323. rc = x->flags & 0; \
  324. } while(0)
  325. /*
  326. * useful macros (x = container; k = key; c = user context)
  327. */
  328. #define CONTAINER_FIRST(x) (x)->find_next(x,NULL)
  329. #define CONTAINER_FIND(x,k) (x)->find(x,k)
  330. #define CONTAINER_NEXT(x,k) (x)->find_next(x,k)
  331. /*
  332. * GET_SUBSET returns allocated memory (netsnmp_void_array). User is responsible
  333. * for releasing this memory (free(array->array), free(array)).
  334. * DO NOT FREE ELEMENTS OF THE ARRAY, because they are the same pointers
  335. * stored in the container.
  336. */
  337. #define CONTAINER_GET_SUBSET(x,k) (x)->get_subset(x,k)
  338. #define CONTAINER_SIZE(x) (x)->get_size(x)
  339. #define CONTAINER_ITERATOR(x) (x)->get_iterator(x)
  340. #define CONTAINER_COMPARE(x,l,r) (x)->compare(l,r)
  341. #define CONTAINER_FOR_EACH(x,f,c) (x)->for_each(x,f,c)
  342. /*
  343. * insert k into all containers
  344. */
  345. NETSNMP_IMPORT
  346. int CONTAINER_INSERT(netsnmp_container *x, const void *k);
  347. /*
  348. * insert item before given position
  349. */
  350. NETSNMP_IMPORT
  351. int CONTAINER_INSERT_BEFORE(netsnmp_container *x, size_t pos, void *k);
  352. /*
  353. * remove k from all containers
  354. */
  355. NETSNMP_IMPORT
  356. int CONTAINER_REMOVE(netsnmp_container *x, const void *k);
  357. /*
  358. * remove item at given position
  359. */
  360. NETSNMP_IMPORT
  361. int CONTAINER_REMOVE_AT(netsnmp_container *x, size_t pos, void **k);
  362. /*
  363. * get item at given position
  364. */
  365. NETSNMP_IMPORT
  366. int CONTAINER_GET_AT(netsnmp_container *x, size_t pos, void **k);
  367. /*
  368. * duplicate container
  369. */
  370. NETSNMP_IMPORT
  371. netsnmp_container *CONTAINER_DUP(netsnmp_container *x, void *ctx,
  372. u_int flags);
  373. /*
  374. * clear all containers. When clearing the *first* container, and
  375. * *only* the first container, call the function f for each item.
  376. * After calling this function, all containers should be empty.
  377. */
  378. NETSNMP_IMPORT
  379. void CONTAINER_CLEAR(netsnmp_container *x, netsnmp_container_obj_func *f,
  380. void *c);
  381. /*
  382. * clear all containers. When clearing the *first* container, and
  383. * *only* the first container, call the free_item function for each item.
  384. * After calling this function, all containers should be empty.
  385. */
  386. NETSNMP_IMPORT
  387. void CONTAINER_FREE_ALL(netsnmp_container *x, void *c);
  388. /*
  389. * free all containers
  390. */
  391. NETSNMP_IMPORT
  392. int CONTAINER_FREE(netsnmp_container *x);
  393. NETSNMP_IMPORT
  394. netsnmp_container *SUBCONTAINER_FIND(netsnmp_container *x,
  395. const char* name);
  396. /*
  397. * INTERNAL utility routines for container implementations
  398. */
  399. void netsnmp_init_container(netsnmp_container *c,
  400. netsnmp_container_rc *init,
  401. netsnmp_container_rc *cfree,
  402. netsnmp_container_size *size,
  403. netsnmp_container_compare *cmp,
  404. netsnmp_container_op *ins,
  405. netsnmp_container_op *rem,
  406. netsnmp_container_rtn *fnd);
  407. /** Duplicate container meta-data. */
  408. int netsnmp_container_data_dup(netsnmp_container *dup,
  409. netsnmp_container *c);
  410. /*************************************************************************
  411. *
  412. * container iterator
  413. *
  414. *************************************************************************/
  415. /*
  416. * function returning an int for an operation on an iterator
  417. */
  418. typedef int (netsnmp_iterator_rc)(struct netsnmp_iterator_s *);
  419. /*
  420. * function returning an oject for an operation on an iterator
  421. */
  422. typedef void * (netsnmp_iterator_rtn)(struct netsnmp_iterator_s *);
  423. /*
  424. * iterator structure
  425. */
  426. typedef struct netsnmp_iterator_s {
  427. netsnmp_container *container;
  428. /*
  429. * sync from container when iterator created. used to invalidate
  430. * the iterator when the container changes.
  431. */
  432. u_long sync;
  433. /*
  434. * reset iterator position to beginning of container.
  435. */
  436. netsnmp_iterator_rc *reset;
  437. /*
  438. * release iterator and memory it uses
  439. */
  440. netsnmp_iterator_rc *release;
  441. /*
  442. * first, last and current DO NOT advance the iterator
  443. */
  444. netsnmp_iterator_rtn *first;
  445. netsnmp_iterator_rtn *curr;
  446. netsnmp_iterator_rtn *last;
  447. netsnmp_iterator_rtn *next;
  448. /*
  449. * remove will remove the item at the current position, then back up
  450. * the iterator to the previous item. That way next will move to the
  451. * item (the one that replaced the removed item.
  452. */
  453. netsnmp_iterator_rc *remove;
  454. } netsnmp_iterator;
  455. #define ITERATOR_FIRST(x) x->first(x)
  456. #define ITERATOR_NEXT(x) x->next(x)
  457. #define ITERATOR_LAST(x) x->last(x)
  458. #define ITERATOR_REMOVE(x) x->remove(x)
  459. #define ITERATOR_RELEASE(x) do { x->release(x); x = NULL; } while(0)
  460. #ifdef __cplusplus
  461. }
  462. #endif
  463. #endif /** NETSNMP_CONTAINER_H */