/* * Copyright (c) 2003, 2004, 2006 Sendmail, Inc. and its suppliers. * All rights reserved. * * By using this file, you agree to the terms and conditions set * forth in the LICENSE file which can be found at the top level of * the sendmail distribution. * */ #include "sm/generic.h" SM_RCSID("@(#)$Id: t-mapip-0.c,v 1.17 2006/11/13 00:42:07 ca Exp $") #include "sm/error.h" #include "sm/sysexits.h" #include "sm/heap.h" #include "sm/memops.h" #include "sm/test.h" #include "sm/maps.h" #include "sm/mapc.h" #include "sm/map.h" #include "sm/mapclasses.h" #include "sm/bdb.h" #include "sm/io.h" int Verbose = 0; #define MAPC_TYPE "hash" #define MAPC_NAME "bdb1" #define MAPC_FILE "./bdb1.db" static void usage(const char *prg) { sm_io_fprintf(smioerr, "%s: usage: %s [options] key rhs [key rhs]...\n" , prg, prg ); } static sm_ret_T init_mapc_test(sm_maps_P *pmaps) { sm_ret_T ret; SM_REQUIRE(pmaps != NULL); ret = sm_maps_init(pmaps); SM_TEST(*pmaps != NULL); if (*pmaps == NULL) return ret; SM_TEST(sm_is_success(ret)); ret = sm_bdb_class_create(*pmaps); SM_TEST(sm_is_success(ret)); #if MTA_USE_TINYCDB ret = sm_cdb_class_create(*pmaps); SM_TEST(sm_is_success(ret)); #endif return ret; } int main(int argc, char *argv[]) { int c, list_mapc; uint taglen, keylen; char *key1, *rhs1, *colon; sm_ret_T ret; sm_maps_P maps; sm_map_P map; sm_cstr_P mtype, mname; sm_str_P rhs, tagp; sm_str_T tag; sm_map_key_T key; char *mapname, *mapfile, *maptype; list_mapc = -1; mapname = MAPC_NAME; mapfile = MAPC_FILE; maptype = MAPC_TYPE; while ((c = getopt(argc, argv, "F:l:n:T:")) != -1) { switch (c) { case 'F': SM_STRDUP_OPT(mapfile, optarg); break; case 'l': list_mapc = (int) strtol(optarg, NULL, 0); break; case 'n': SM_STRDUP_OPT(mapname, optarg); break; case 'T': SM_STRDUP_OPT(maptype, optarg); break; default: usage(argv[0]); exit(EX_USAGE); } } if (list_mapc >= 0) { sm_ret_T ret; ret = init_mapc_test(&maps); if (sm_is_err(ret)) exit(EX_OSERR); c = sm_mapc_list(smioout, maps, list_mapc, 0); return 0; } sm_test_begin(argc, argv, "test map ip"); argc -= optind; argv += optind; maps = NULL; mtype = mname = NULL; rhs = NULL; ret = init_mapc_test(&maps); SM_TEST(maps != NULL); if (maps == NULL) goto error; SM_TEST(sm_is_success(ret)); mtype = sm_cstr_scpyn0((const uchar *)maptype, strlen(maptype)); SM_TEST(mtype != NULL); if (mtype == NULL) goto error; mname = sm_cstr_scpyn0((const uchar *)mapname, strlen(mapname)); SM_TEST(mname != NULL); if (mname == NULL) goto error; rhs = sm_str_new(NULL, 256, 1024); SM_TEST(rhs != NULL); if (rhs == NULL) goto error; map = NULL; ret = sm_map_open(maps, mname, mtype, 0, mapfile, SMAP_MODE_RDONLY, &map, SMPO_END); SM_TEST(sm_is_success(ret)); if (!sm_is_success(ret)) goto error; /* perform some operations ... */ for (c = 0; c < argc - 1; c += 2) { key1 = argv[c]; rhs1 = argv[c + 1]; sm_str_clr(rhs); colon = strchr(key1, ':'); if (colon != NULL) { taglen = colon - key1 + 1; sm_str_assign(tag, NULL, (uchar *)key1, taglen, taglen); tagp = &tag; keylen = strlen(key1) - (colon - key1); SM_TEST(keylen > 1); --keylen; sm_str_assign(key, NULL, (uchar *)(colon + 1), keylen, keylen); } else { tagp = NULL; sm_str_assign(key, NULL, (uchar *)key1, strlen(key1), strlen(key1)); } ret = sm_map_lookup_ip(map, &key, tagp, SMMAP_LFL_SUBNETS|SMMAP_LFL_TAG, rhs); if (rhs1[0] != '\0') { SM_TEST(sm_is_success(ret)); SM_TEST(strcmp(rhs1, (char *)sm_str_getdata(rhs)) == 0); } } ret = sm_map_close(map, 0); SM_TEST(sm_is_success(ret)); ret = sm_maps_term(maps); SM_TEST(sm_is_success(ret)); maps = NULL; error: if (maps != NULL) sm_maps_term(maps); SM_CSTR_FREE(mtype); SM_CSTR_FREE(mname); SM_STR_FREE(rhs); return sm_test_end(); }