Skip to content

Commit a1a634c

Browse files
committed
{182782396} Look up peer's short hostname
Signed-off-by: Rivers Zhang <hzhang320@bloomberg.net>
1 parent 6dfc8c8 commit a1a634c

File tree

6 files changed

+27
-30
lines changed

6 files changed

+27
-30
lines changed

bdb/bdb_net.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "printformats.h"
4242
#include "crc32c.h"
4343
#include <timer_util.h>
44+
#include <hostname_support.h>
4445

4546
#undef UDP_DEBUG
4647
#undef UDP_TRACE
@@ -200,33 +201,23 @@ void comdb2_early_ack(DB_ENV *dbenv, DB_LSN permlsn, uint32_t commit_gen, uint32
200201
char *print_addr(struct sockaddr_in *addr, char *buf)
201202
{
202203
buf[0] = '\0';
204+
short port = ntohs(addr->sin_port);
203205
if (addr == NULL) {
204206
return buf;
205207
}
206208
if(addr->sin_addr.s_addr == htonl(INADDR_ANY)) {
207-
sprintf(buf, "[0.0.0.0 0.0.0.0:%d ]", ntohs(addr->sin_port));
209+
sprintf(buf, "[0.0.0.0 0.0.0.0:%d ]", port);
208210
return buf;
209211
}
210212
char ip[32] = {0};
211-
char name[256] = {0};
212-
char service[256] = {0};
213+
const char *name;
213214
char errbuf[256] = {0};
214-
socklen_t len;
215215

216-
len = sizeof(*addr);
217-
int rc = getnameinfo((struct sockaddr *)addr, len, name, sizeof(name),
218-
service, sizeof(service), 0);
219-
if (rc) {
220-
#pragma GCC diagnostic push
221-
#pragma GCC diagnostic ignored "-Wunused-result"
222-
strerror_r(errno, errbuf, sizeof(errbuf));
223-
#pragma GCC diagnostic pop
224-
sprintf(buf, "%s:getnameinfo errbuf=%s", __func__, errbuf);
216+
if ((name = get_cached_hostname_by_addr(addr)) == NULL)
225217
return buf;
226-
}
227218

228219
if (inet_ntop(addr->sin_family, &addr->sin_addr.s_addr, ip, sizeof(ip))) {
229-
sprintf(buf, "[%s %s:%s] ", name, ip, service);
220+
sprintf(buf, "[%s %s:%hd] ", name, ip, port);
230221
} else {
231222
#pragma GCC diagnostic push
232223
#pragma GCC diagnostic ignored "-Wunused-result"

db/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ static char *legacy_options[] = {
460460
"disable_sql_table_replacement 1",
461461
"endianize_locklist 0",
462462
"track_db_open 0",
463-
"clear_ufid_on_db_close 0"
463+
"clear_ufid_on_db_close 0",
464+
"get_peer_fqdn 0"
464465
};
465466
// clang-format on
466467

db/db_tunables.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ extern int gbl_new_connection_grace_ms;
626626
extern int gbl_accept_headroom;
627627
extern int gbl_db_track_open;
628628
extern int gbl_clear_ufid_on_db_close;
629+
extern int gbl_get_peer_fqdn;
629630

630631
int parse_int64(const char *value, int64_t *num);
631632

db/db_tunables.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,4 +2614,6 @@ REGISTER_TUNABLE("track_db_open", "Track berkdb open DB handles", TUNABLE_INTEGE
26142614
NULL, NULL, NULL);
26152615
REGISTER_TUNABLE("clear_ufid_on_db_close", "Clear ufid hash on db->close", TUNABLE_INTEGER, &gbl_clear_ufid_on_db_close,
26162616
INTERNAL, NULL, NULL, NULL, NULL);
2617+
REGISTER_TUNABLE("get_peer_fqdn", "When set, use peer's FQDN", TUNABLE_BOOLEAN, &gbl_get_peer_fqdn, INTERNAL, NULL,
2618+
NULL, NULL, NULL);
26172619
#endif /* _DB_TUNABLES_H */

net/net.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "thread_util.h"
7474
#include <timer_util.h>
7575
#include <comdb2_atomic.h>
76+
#include <hostname_support.h>
7677

7778
#ifdef UDP_DEBUG
7879
static int curr_udp_cnt = 0;
@@ -2524,23 +2525,19 @@ static int get_subnet_incomming_syn(host_node_type *host_node_ptr)
25242525
return 0;
25252526
}
25262527

2527-
char host[NI_MAXHOST], service[NI_MAXSERV];
25282528
/* get our host name for local _into_ address of connection */
2529-
int s = getnameinfo((struct sockaddr *)&lcl_addr_inet, lcl_len, host,
2530-
NI_MAXHOST, service, NI_MAXSERV, 0);
2531-
2532-
if (s != 0) {
2533-
logmsg(LOGMSG_WARN, "Incoming connection into unknown (%s:%u): %s\n",
2534-
inet_ntoa(lcl_addr_inet.sin_addr),
2535-
(unsigned)ntohs(lcl_addr_inet.sin_port), gai_strerror(s));
2529+
const char *host = get_cached_hostname_by_addr(&lcl_addr_inet);
2530+
if (host == NULL) {
2531+
logmsg(LOGMSG_WARN, "Incoming connection into unknown (%s:%u)\n", inet_ntoa(lcl_addr_inet.sin_addr),
2532+
(unsigned)ntohs(lcl_addr_inet.sin_port));
25362533
return 0;
25372534
}
25382535

25392536
/* extract the suffix of subnet ex. '_n3' in name node1_n3 */
25402537
int myh_len = strlen(host_node_ptr->netinfo_ptr->myhostname);
25412538
if (strncmp(host_node_ptr->netinfo_ptr->myhostname, host, myh_len) == 0) {
25422539
assert(myh_len <= sizeof(host));
2543-
char *subnet = &host[myh_len];
2540+
const char *subnet = &host[myh_len];
25442541
if (subnet[0])
25452542
strncpy0(host_node_ptr->subnet, subnet, HOSTNAME_LEN);
25462543
}

util/hostname_support.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <arpa/inet.h>
18+
#include <errno.h>
1819
#include <netdb.h>
1920
#include <stdio.h>
2021
#include <stdlib.h>
@@ -23,19 +24,23 @@
2324
#include <sys/types.h>
2425

2526
#include <hostname_support.h>
26-
#include <errno.h>
27+
#include <logmsg.h>
2728

2829
#ifdef CDB2API_TEST
2930
#include <cdb2api_ssl_test.h>
3031
#endif
3132

33+
int gbl_get_peer_fqdn = 1;
3234
static int get_hostname_by_addr(struct sockaddr_in *addr, char *host, socklen_t hostlen)
3335
{
36+
int rc;
37+
int flags = gbl_get_peer_fqdn ? 0 : NI_NOFQDN;
3438
socklen_t addrlen = sizeof(*addr);
35-
if (getnameinfo((struct sockaddr *)addr, addrlen, host, hostlen, NULL, 0, 0)) {
36-
if (!inet_ntop(addr->sin_family, &addr->sin_addr, host, hostlen)) {
37-
return -1;
38-
}
39+
if ((rc = getnameinfo((struct sockaddr *)addr, addrlen, host, hostlen, NULL, 0, flags)) != 0) {
40+
#ifndef DISABLE_HOSTADDR_CACHE
41+
logmsg(LOGMSG_WARN, "getnameinfo: %s\n", gai_strerror(rc));
42+
#endif
43+
return -1;
3944
}
4045
return 0;
4146
}

0 commit comments

Comments
 (0)