Skip to content

Commit fea8566

Browse files
authored
Add port in HostDBRecord (#12856)
* Add port in HostDBRecord * Address comments from Copiot * Fix typo
1 parent 981f449 commit fea8566

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

include/iocore/hostdb/HostDBProcessor.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <chrono>
2727
#include <atomic>
28+
#include <cstdint>
2829
#include <utility>
2930

3031
#include "tscore/HashFNV.h"
@@ -312,11 +313,13 @@ class HostDBRecord : public RefCountObj
312313
* @param query_name Name of the query for the record.
313314
* @param rr_count Number of info instances.
314315
* @param srv_name_size Storage for SRV names, if any.
316+
* @param port Port Number, if any.
317+
*
315318
* @return An instance sufficient to hold the specified data.
316319
*
317320
* The query name will stored and initialized, and the info instances initialized.
318321
*/
319-
static self_type *alloc(swoc::TextView query_name, unsigned rr_count, size_t srv_name_size = 0);
322+
static self_type *alloc(swoc::TextView query_name, unsigned rr_count, size_t srv_name_size = 0, in_port_t port = 0);
320323

321324
/// Type of data stored in this record.
322325
HostDBType record_type = HostDBType::UNSPEC;
@@ -390,6 +393,11 @@ class HostDBRecord : public RefCountObj
390393
*/
391394
swoc::TextView name_view() const;
392395

396+
/**
397+
@return Port Number
398+
*/
399+
in_port_t port() const;
400+
393401
/// Get the array of info instances.
394402
swoc::MemSpan<HostDBInfo> rr_info();
395403

@@ -519,6 +527,10 @@ class HostDBRecord : public RefCountObj
519527
unsigned failed_p : 1; ///< DNS error.
520528
} f;
521529
} flags{0};
530+
531+
private:
532+
/// Port Number if hash key includes it.
533+
in_port_t _port = 0;
522534
};
523535

524536
struct HostDBCache;
@@ -733,6 +745,12 @@ HostDBRecord::name_view() const
733745
return {this->name(), swoc::TextView::npos};
734746
}
735747

748+
inline in_port_t
749+
HostDBRecord::port() const
750+
{
751+
return _port;
752+
}
753+
736754
inline ts_time
737755
HostDBRecord::expiry_time() const
738756
{

src/iocore/hostdb/HostDB.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ probe_ip(HostDBHash const &hash)
434434
Dbg(dbg_ctl_hostdb, "DNS %.*s", int(hash.host_name.size()), hash.host_name.data());
435435
IpAddr tip;
436436
if (0 == tip.load(hash.host_name)) {
437-
result = HostDBRecord::Handle{HostDBRecord::alloc(hash.host_name, 1)};
437+
result = HostDBRecord::Handle{HostDBRecord::alloc(hash.host_name, 1, 0, hash.port)};
438438
result->af_family = tip.family();
439439
auto &info = result->rr_info()[0];
440440
info.assign(tip);
@@ -919,7 +919,7 @@ HostDBContinuation::dnsEvent(int event, HostEnt *e)
919919

920920
// In the event that the lookup failed (SOA response-- for example) we want to use hash.host_name, since it'll be ""
921921
TextView query_name = (failed || !hash.host_name.empty()) ? hash.host_name : TextView{e->ent.h_name, strlen(e->ent.h_name)};
922-
HostDBRecord::Handle r{HostDBRecord::alloc(query_name, valid_records, failed ? 0 : e->srv_hosts.srv_hosts_length)};
922+
HostDBRecord::Handle r{HostDBRecord::alloc(query_name, valid_records, failed ? 0 : e->srv_hosts.srv_hosts_length, hash.port)};
923923
r->key = hash.hash.fold(); // always set the key
924924
r->af_family = af;
925925
r->flags.f.failed_p = failed;
@@ -1542,7 +1542,7 @@ HostDBRecord::free()
15421542
}
15431543

15441544
HostDBRecord *
1545-
HostDBRecord::alloc(TextView query_name, unsigned int rr_count, size_t srv_name_size)
1545+
HostDBRecord::alloc(TextView query_name, unsigned int rr_count, size_t srv_name_size, in_port_t port)
15461546
{
15471547
const swoc::Scalar<8, ssize_t> qn_size = round_up(query_name.size() + 1);
15481548
const swoc::Scalar<8, ssize_t> r_size = round_up(sizeof(self_type) + qn_size + rr_count * sizeof(HostDBInfo) + srv_name_size);
@@ -1554,6 +1554,7 @@ HostDBRecord::alloc(TextView query_name, unsigned int rr_count, size_t srv_name_
15541554
new (self) self_type();
15551555
self->_iobuffer_index = iobuffer_index;
15561556
self->_record_size = r_size;
1557+
self->_port = port;
15571558

15581559
Dbg(dbg_ctl_hostdb, "allocating %ld bytes for %.*s with %d RR records at [%p]", r_size.value(), int(query_name.size()),
15591560
query_name.data(), rr_count, self);

src/iocore/hostdb/test_HostFile.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ HostDBHash::~HostDBHash() {}
168168
#include "swoc/Scalar.h"
169169

170170
HostDBRecord *
171-
HostDBRecord::alloc(swoc::TextView query_name, unsigned int rr_count, size_t srv_name_size)
171+
HostDBRecord::alloc(swoc::TextView query_name, unsigned int rr_count, size_t srv_name_size, in_port_t port)
172172
{
173173
const swoc::Scalar<8, ssize_t> qn_size = swoc::round_up(query_name.size() + 1);
174174
const swoc::Scalar<8, ssize_t> r_size =
@@ -179,6 +179,7 @@ HostDBRecord::alloc(swoc::TextView query_name, unsigned int rr_count, size_t srv
179179
new (self) self_type();
180180
self->_iobuffer_index = 0;
181181
self->_record_size = r_size;
182+
self->_port = port;
182183

183184
Dbg(dbg_ctl_hostdb, "allocating %ld bytes for %.*s with %d RR records at [%p]", r_size.value(), int(query_name.size()),
184185
query_name.data(), rr_count, self);

src/mgmt/rpc/handlers/hostdb/HostDB.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ template <> struct convert<HostDBCache> {
8787
partition.copy(partition_entries);
8888
}
8989

90+
if (partition_entries.empty()) {
91+
continue;
92+
}
93+
9094
Node partition_node;
9195
partition_node["id"] = i;
9296

@@ -116,10 +120,12 @@ template <> struct convert<HostDBRecord> {
116120
{
117121
Node metadata;
118122
metadata["name"] = record.name();
123+
metadata["port"] = record.port();
119124
metadata["type"] = str(record.record_type);
120-
metadata["af_familiy"] = str(record.af_family);
125+
metadata["af_family"] = str(record.af_family);
121126
metadata["failed"] = record.is_failed();
122127
metadata["ip_timestamp"] = record.ip_timestamp.time_since_epoch().count();
128+
metadata["hash_key"] = record.key;
123129

124130
Node node;
125131
node["metadata"] = metadata;

0 commit comments

Comments
 (0)