00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _IPA_IPA_H_
00016 #define _IPA_IPA_H_
00017
00031 #ifdef HAVE_CONFIG_H
00032 # include "config.h"
00033 #endif
00034
00035 #include <stdio.h>
00036
00037 #if STDC_HEADERS
00038 # include <stdlib.h>
00039 # include <stddef.h>
00040 #else
00041 # if HAVE_STDLIB_H
00042 # include <stdlib.h>
00043 # endif
00044 # if HAVE_MALLOC_H
00045 # include <malloc.h>
00046 # endif
00047 #endif
00048
00049 #if HAVE_INTTYPES_H
00050 # include <inttypes.h>
00051 #else
00052 # if HAVE_STDINT_H
00053 # include <stdint.h>
00054 # endif
00055 #endif
00056 #if HAVE_UNISTD_H
00057 # include <unistd.h>
00058 #endif
00059
00060 #if HAVE_LIMITS_H
00061 # include <limits.h>
00062 #endif
00063
00064 #if HAVE_STRING_H
00065 # if !STDC_HEADERS && HAVE_MEMORY_H
00066 # include <memory.h>
00067 # endif
00068 # include <string.h>
00069 #endif
00070 #if HAVE_STRINGS_H
00071 # include <strings.h>
00072 #endif
00073
00074 #if HAVE_STDARG_H
00075 # include <stdarg.h>
00076 #endif
00077
00078 #if HAVE_ERRNO_H
00079 # include <errno.h>
00080 #endif
00081
00082 #if HAVE_ARPA_INET_H
00083 # include <arpa/inet.h>
00084 #endif
00085
00086 #if HAVE_PWD_H
00087 # include <pwd.h>
00088 #endif
00089
00090
00091 #if !defined (PRIu32)
00092
00093 # define PRId32 "d"
00094 # define PRIi32 "i"
00095 # define PRIo32 "o"
00096 # define PRIu32 "u"
00097 # define PRIx32 "x"
00098 # define PRIX32 "X"
00099
00100 # define PRId16 PRId32
00101 # define PRIi16 PRIi32
00102 # define PRIo16 PRIo32
00103 # define PRIu16 PRIu32
00104 # define PRIx16 PRIx32
00105 # define PRIX16 PRIX32
00106
00107 # define PRId8 PRId32
00108 # define PRIi8 PRIi32
00109 # define PRIo8 PRIo32
00110 # define PRIu8 PRIu32
00111 # define PRIx8 PRIx32
00112 # define PRIX8 PRIX32
00113 #endif
00114 #if !defined (PRIu64)
00115 # if (SIZEOF_LONG >= 8)
00116 # define PRId64 "l" PRId32
00117 # define PRIi64 "l" PRIi32
00118 # define PRIo64 "l" PRIo32
00119 # define PRIu64 "l" PRIu32
00120 # define PRIx64 "l" PRIx32
00121 # define PRIX64 "l" PRIX32
00122 # else
00123 # define PRId64 "ll" PRId32
00124 # define PRIi64 "ll" PRIi32
00125 # define PRIo64 "ll" PRIo32
00126 # define PRIu64 "ll" PRIu32
00127 # define PRIx64 "ll" PRIx32
00128 # define PRIX64 "ll" PRIX32
00129 # endif
00130 #endif
00131
00132 #include <glib.h>
00133 #if GLIB_CHECK_VERSION(2, 6, 0)
00134 # include <glib/gstdio.h>
00135 #else
00136 # define g_debug(...) g_log(G_LOG_DOMAIN, \
00137 G_LOG_LEVEL_DEBUG, \
00138 __VA_ARGS__)
00139 #endif
00140 #include <airdbc/airdbc.h>
00141
00190 #define ENV_IPA_DB_URI "IPA_DB_URI"
00191
00192 #define IPA_CHECK_ERR( e, m, n ) \
00193 if (e != NULL) { \
00194 g_critical("%s: %s", m, (e && e->message) ? e->message : "" ); \
00195 g_clear_error(&e); \
00196 return n; \
00197 }
00198
00200 typedef enum _IPAStatus {
00201 IPA_OK = 0,
00202 IPA_ERR_NOTFOUND,
00203 IPA_ERR_FILEIO,
00204 IPA_ERR_SQL,
00205 IPA_ERR_INVALID,
00206 IPA_ERR_UNKNOWN
00207 } IPAStatus;
00208
00210 typedef enum _IPACatalogType
00211 {
00212 IPA_CAT_NONE = 0,
00213 IPA_CAT_SET,
00214 IPA_CAT_BAG,
00215 IPA_CAT_PMAP,
00216 IPA_CAT_MMAP
00217 } IPACatalogType;
00218
00219
00220 typedef enum _IPAContextState
00221 {
00222 IPA_STATE_INITIAL = 0,
00223 IPA_STATE_EXPORT,
00224 IPA_STATE_IMPORT,
00225 IPA_STATE_QUERY,
00226 IPA_STATE_QUERY_DONE
00227 } IPAContextState;
00228
00232 typedef struct _IPAContext {
00233 IPAContextState state;
00234 char *db_uri;
00235 uint64_t ds_id;
00236 AdbConnection *conn;
00237 AdbStatement *stmt;
00238 AdbResultSet *rs;
00239 GString *sql;
00240 IPACatalogType cat_type;
00241 uint8_t cat_width;
00242 uint8_t range_width;
00243 uint8_t label_width;
00244 uint8_t value_width;
00245 gboolean verbose;
00246 uint8_t displayfields;
00247 GScanner *scanner;
00248 char *uname;
00249 } IPAContext;
00250
00251 #define IPA_ADDR_LEN 16
00252 #define IPA_RANGE_LEN ((2 * IPA_ADDR_LEN) + 4)
00253 #define IPA_LABEL_LEN 1024
00254 #define IPA_VALUE_LEN 32
00255 #define IPA_CAT_LEN 64
00256
00257
00258 #define IPA_COL_CATALOG 1
00259 #define IPA_COL_START 2
00260 #define IPA_COL_END 4
00261 #define IPA_COL_RANGE 8
00262 #define IPA_COL_LABEL 16
00263 #define IPA_COL_VALUE 32
00264 #define IPA_COL_ALL 63
00265
00266
00272 typedef struct _IPAAssoc {
00273 char range[IPA_RANGE_LEN];
00274 uint32_t begin;
00275 uint32_t end;
00276 char label[IPA_LABEL_LEN];
00277 char value[IPA_VALUE_LEN];
00278 char catalog[IPA_CAT_LEN];
00279 char t1[IPA_VALUE_LEN];
00280 char t2[IPA_VALUE_LEN];
00281 } IPAAssoc;
00282
00283 typedef struct _IPAColumn {
00284 char *label;
00285 int width;
00286 gboolean align_left;
00287 char *val;
00288 } IPAColumn;
00289
00301 int ipa_create_context(
00302 IPAContext **ipa,
00303 char *db_uri,
00304 char *uname);
00305
00312 void ipa_destroy_context(
00313 IPAContext **ipa);
00314
00315
00321 void ipa_begin(
00322 IPAContext *ipa);
00323
00329 void ipa_commit(
00330 IPAContext *ipa);
00331
00337 void ipa_rollback(
00338 IPAContext *ipa);
00339
00351 int ipa_add_dataset(
00352 IPAContext *ipa,
00353 const char *catname,
00354 const char *catdesc,
00355 IPACatalogType type,
00356 const char *begin,
00357 const char *end);
00358
00372 int ipa_add_assoc(
00373 IPAContext *ipa,
00374 uint32_t addr1,
00375 uint32_t addr2,
00376 char *label,
00377 uint64_t value);
00378
00389 int ipa_add_cidr(
00390 IPAContext *ipa,
00391 uint32_t addr,
00392 uint32_t prefix,
00393 char *label,
00394 uint64_t value);
00395
00396
00406 int ipa_get_dataset(
00407 IPAContext *ipa,
00408 const char *catalog_name,
00409 const char *dataset_time);
00410
00418 int ipa_get_assoc(
00419 IPAContext *ipa,
00420 IPAAssoc *assoc);
00421
00422 int ipa_get_next_assoc(
00423 IPAContext *ipa,
00424 IPAAssoc *assoc);
00425
00426
00440 int ipa_do_assoc_query(
00441 IPAContext *ipa,
00442 const char *catalog,
00443 const char *range,
00444 const char *label,
00445 const char *value,
00446 const char *t1,
00447 const char *t2,
00448 int displayfields
00449 );
00450
00455 typedef enum _IPAOrderBy {
00456 IPA_ORDER_DEFAULT = 0,
00457 IPA_ORDER_CTIME,
00458 IPA_ORDER_MTIME
00459 } IPAOrderBy;
00460
00461 uint32_t ipa_mask_from_prefix(
00462 uint32_t pfx);
00463
00469 typedef enum _IPAQuerySymbol {
00470
00471 IPAQUERY_CATLIST = G_TOKEN_LAST + 1,
00472 IPAQUERY_SETLIST,
00473 IPAQUERY_FIND,
00474 IPAQUERY_NONE,
00475
00476 IPAQUERY_IN,
00477 IPAQUERY_ONLY,
00478 IPAQUERY_ADDR,
00479 IPAQUERY_LABEL,
00480 IPAQUERY_VALUE,
00481 IPAQUERY_AT,
00482 IPAQUERY_BEFORE,
00483 IPAQUERY_AFTER,
00484 IPAQUERY_BETWEEN,
00485 IPAQUERY_AND,
00486 IPAQUERY_SHOW
00487 } IPAQuerySymbol;
00488
00489
00490 typedef const struct _symbol_table {
00491 gchar *symbol_name;
00492 guint symbol_token;
00493 } symbol_table_t;
00494
00495 static symbol_table_t
00496 symbols_main[] = {
00497 { "catlist", IPAQUERY_CATLIST },
00498 { "setlist", IPAQUERY_SETLIST },
00499 { "find", IPAQUERY_FIND },
00500 { NULL, 0 }
00501 },
00502 *symbols_main_p = symbols_main,
00503 symbols_find[] = {
00504 { "in", IPAQUERY_IN },
00505 { "only", IPAQUERY_ONLY },
00506 { "addr", IPAQUERY_ADDR },
00507 { "label", IPAQUERY_LABEL },
00508 { "value", IPAQUERY_VALUE },
00509
00510 { "at", IPAQUERY_AT },
00511 { "before", IPAQUERY_BEFORE },
00512 { "after", IPAQUERY_AFTER },
00513 { "between", IPAQUERY_BETWEEN },
00514 { "and", IPAQUERY_AND },
00515
00516 { "show", IPAQUERY_SHOW },
00517 { NULL, 0 }
00518 }, *symbols_find_p = symbols_find,
00519 symbols_none[] = {
00520 { NULL, 0 }
00521 }, *symbols_none_p = symbols_none;
00522
00523
00524 #define IPA_ADD_COLUMN( A, l, w, a ) \
00525 { \
00526 if (A) { \
00527 IPAColumn col = { l, w, a, NULL }; \
00528 g_array_append_val(A, col); \
00529 } \
00530 }
00531
00532 int ipa_parse_query(
00533 IPAContext *ipa,
00534 char *query
00535 );
00536
00537 gboolean ipa_scan_addr(
00538 GScanner *scanner,
00539 uint32_t *addr);
00540
00541 gboolean ipa_scan_range(
00542 GScanner *scanner,
00543 uint32_t *a,
00544 uint32_t *b);
00545
00546
00547 gboolean ipa_ntop(
00548 int family,
00549 uint32_t addr,
00550 GString *str);
00551
00552
00553 #endif
00554
00555