(an IPADDR_ID address means the address
is not resolved yet) */
NCR_Instance data; /* Data for the protocol engine for this source */
- char *name; /* Name of the source, may be NULL */
+ char *name; /* Name of the source as it was specified
+ (may be an IP address) */
int pool_id; /* ID of the pool from which was this source
added or INVALID_POOL */
int tentative; /* Flag indicating there was no valid response
/* Find empty bin & check that we don't have the address already */
if (find_slot2(remote_addr, &slot) != 0) {
return NSR_AlreadyInUse;
+ } else if (!name && !UTI_IsIPReal(&remote_addr->ip_addr)) {
+ /* Name is required for non-real addresses */
+ return NSR_InvalidName;
} else {
if (remote_addr->ip_addr.family != IPADDR_INET4 &&
remote_addr->ip_addr.family != IPADDR_INET6 &&
}
record = get_record(slot);
- record->data = NCR_CreateInstance(remote_addr, type, params, name);
+ assert(!name || !UTI_IsStringIP(name));
+ record->name = Strdup(name ? name : UTI_IPToString(&remote_addr->ip_addr));
+ record->data = NCR_CreateInstance(remote_addr, type, params, record->name);
record->remote_addr = NCR_GetRemoteAddress(record->data);
- record->name = name ? Strdup(name) : NULL;
record->pool_id = pool_id;
record->tentative = 1;
record->conf_id = conf_id;
LOG(severity, "Source %s %s %s (%s)", UTI_IPToString(&old_addr->ip_addr),
replacement ? "replaced with" : "changed to",
- UTI_IPToString(&new_addr->ip_addr), name ? name : "");
+ UTI_IPToString(&new_addr->ip_addr), name);
} else {
LOG(severity, "Source %s (%s) changed port to %d",
- UTI_IPToString(&new_addr->ip_addr), name ? name : "", new_addr->port);
+ UTI_IPToString(&new_addr->ip_addr), name, new_addr->port);
}
return NSR_Success;
NTP_Remote_Address remote_addr;
int i, new_sources, pool_id;
- /* If the name is an IP address, don't bother with full resolving now
- or later when trying to replace the source */
+ /* If the name is an IP address, add the source with the address directly */
if (UTI_StringToIP(name, &remote_addr.ip_addr)) {
remote_addr.port = port;
return NSR_AddSource(&remote_addr, type, params, conf_id);
record->remote_addr = NULL;
NCR_DestroyInstance(record->data);
- if (record->name)
- Free(record->name);
+ Free(record->name);
n_sources--;
}
{
struct UnresolvedSource *us;
- DEBUG_LOG("trying to replace %s", UTI_IPToString(&record->remote_addr->ip_addr));
+ DEBUG_LOG("trying to replace %s (%s)",
+ UTI_IPToString(&record->remote_addr->ip_addr), record->name);
us = MallocNew(struct UnresolvedSource);
us->name = Strdup(record->name);
static struct timespec last_replacement;
struct timespec now;
SourceRecord *record;
+ IPAddr ip_addr;
double diff;
int slot;
record = get_record(slot);
- /* Only sources with a name can be replaced */
- if (!record->name)
+ /* Don't try to replace a source specified by an IP address unless the
+ address changed since the source was added (e.g. by NTS-KE) */
+ if (UTI_StringToIP(record->name, &ip_addr) &&
+ UTI_CompareIPs(&record->remote_addr->ip_addr, &ip_addr, NULL) == 0)
return;
/* Don't resolve names too frequently */
for (i = 0; i < ARR_GetSize(records); i++) {
record = get_record(i);
- if (!record->remote_addr || !record->name)
+ if (!record->remote_addr)
continue;
resolve_source_replacement(record);
char *
NSR_GetName(IPAddr *address)
{
- SourceRecord *record;
int slot;
if (!find_slot(address, &slot))
return NULL;
- record = get_record(slot);
- if (record->name)
- return record->name;
-
- return UTI_IPToString(&record->remote_addr->ip_addr);
+ return get_record(slot)->name;
}
/* ================================================== */