/******************************************************** Resolve a name into an IP address. Use this function if the string is either an IP address, DNS or host name or NetBIOS name. This uses the name switch in the smb.conf to determine the order of name resolution. *********************************************************/ BOOL resolve_name(const char *name, struct in_addr *return_ip, int name_type) { int i; BOOL pure_address = True; pstring name_resolve_list; fstring tok; char *ptr; if (strcmp(name,"0.0.0.0") == 0) { return_ip->s_addr = 0; return True; } if (strcmp(name,"255.255.255.255") == 0) { return_ip->s_addr = 0xFFFFFFFF; return True; } for (i=0; pure_address && name[i]; i++) if (!(isdigit((int)name[i]) || name[i] == '.')) pure_address = False; /* if it's in the form of an IP address then get the lib to interpret it */ if (pure_address) { return_ip->s_addr = inet_addr(name); return True; } pstrcpy(name_resolve_list, lp_name_resolve_order()); if (name_resolve_list == NULL || *name_resolve_list == '\0') pstrcpy(name_resolve_list, "host"); ptr = name_resolve_list; while (next_token(&ptr, tok, LIST_SEP, sizeof(tok))) { if((strequal(tok, "host") || strequal(tok, "hosts"))) { if (name_type == 0x20 && resolve_hosts(name, return_ip)) { return True; } } else if(strequal( tok, "lmhosts")) { if (resolve_lmhosts(name, return_ip, name_type)) { return True; } } else if(strequal( tok, "wins")) { /* don't resolve 1D via WINS */ if (name_type != 0x1D && resolve_wins(name, return_ip, name_type)) { return True; } } else if(strequal( tok, "bcast")) { if (resolve_bcast(name, return_ip, name_type)) { return True; } } else { DEBUG(0,("resolve_name: unknown name switch type %s\n", tok)); } } return False; }
void test_ResolveSingleAddress(void) { const char* HOSTS[1] = {"192.0.2.1"}; const int HOSTCOUNT = COUNTOF(HOSTS); struct addrinfo** actual = NULL; TEST_ASSERT_EQUAL(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC)); //TEST_ASSERT_TRUE(actual != NULL); //TEST_ASSERT_TRUE(CompareAddrinfo(HOSTS[0], AF_INET, 0, **actual)); }
/* * The actual main function. */ int sntp_main ( int argc, char **argv ) { register int c; struct kod_entry *reason = NULL; int optct; /* boolean, u_int quiets gcc4 signed overflow warning */ u_int sync_data_suc; struct addrinfo **bcastaddr = NULL; struct addrinfo **resh = NULL; struct addrinfo *ai; int resc; int kodc; int ow_ret; int bcast = 0; char *hostname; optct = optionProcess(&sntpOptions, argc, argv); argc -= optct; argv += optct; /* Initialize logging system */ init_logging(); if (HAVE_OPT(LOGFILE)) open_logfile(OPT_ARG(LOGFILE)); msyslog(LOG_NOTICE, "Started sntp"); /* IPv6 available? */ if (isc_net_probeipv6() != ISC_R_SUCCESS) { ai_fam_pref = AF_INET; #ifdef DEBUG printf("No ipv6 support available, forcing ipv4\n"); #endif } else { /* Check for options -4 and -6 */ if (HAVE_OPT(IPV4)) ai_fam_pref = AF_INET; else if (HAVE_OPT(IPV6)) ai_fam_pref = AF_INET6; } /* Parse config file if declared TODO */ /* * If there's a specified KOD file init KOD system. If not use * default file. For embedded systems with no writable * filesystem, -K /dev/null can be used to disable KoD storage. */ if (HAVE_OPT(KOD)) kod_init_kod_db(OPT_ARG(KOD)); else kod_init_kod_db("/var/db/ntp-kod"); if (HAVE_OPT(KEYFILE)) auth_init(OPT_ARG(KEYFILE), &keys); #ifdef EXERCISE_KOD_DB add_entry("192.168.169.170", "DENY"); add_entry("192.168.169.171", "DENY"); add_entry("192.168.169.172", "DENY"); add_entry("192.168.169.173", "DENY"); add_entry("192.168.169.174", "DENY"); delete_entry("192.168.169.174", "DENY"); delete_entry("192.168.169.172", "DENY"); delete_entry("192.168.169.170", "DENY"); if ((kodc = search_entry("192.168.169.173", &reason)) == 0) printf("entry for 192.168.169.173 not found but should have been!\n"); else free(reason); #endif /* Considering employing a variable that prevents functions of doing anything until * everything is initialized properly */ resc = resolve_hosts((void *)argv, argc, &resh, ai_fam_pref); if (resc < 1) { printf("Unable to resolve hostname(s)\n"); return -1; } bcast = ENABLED_OPT(BROADCAST); if (bcast) { const char * myargv[2]; myargv[0] = OPT_ARG(BROADCAST); myargv[1] = NULL; bcast = resolve_hosts(myargv, 1, &bcastaddr, ai_fam_pref); } /* Select a certain ntp server according to simple criteria? For now * let's just pay attention to previous KoDs. */ sync_data_suc = FALSE; for (c = 0; c < resc && !sync_data_suc; c++) { ai = resh[c]; do { hostname = addrinfo_to_str(ai); if ((kodc = search_entry(hostname, &reason)) == 0) { if (is_reachable(ai)) { ow_ret = on_wire(ai, bcast ? bcastaddr[0] : NULL); if (0 == ow_ret) sync_data_suc = TRUE; } } else { printf("%d prior KoD%s for %s, skipping.\n", kodc, (kodc > 1) ? "s" : "", hostname); free(reason); } free(hostname); ai = ai->ai_next; } while (NULL != ai); freeaddrinfo(resh[c]); } free(resh); if (!sync_data_suc) return 1; return 0; }