Skip to content

Commit 5e56c8a

Browse files
Revert API change - maintain 9.x compatibility.
1 parent d8b6194 commit 5e56c8a

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

example/plugins/c-api/protocol/TxnSM.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,6 @@ int
477477
state_dns_lookup(TSCont contp, TSEvent event, TSHostLookupResult host_info)
478478
{
479479
TxnSM *txn_sm = (TxnSM *)TSContDataGet(contp);
480-
struct sockaddr_storage q_server_addr;
481-
struct sockaddr_in *addr;
482480

483481
TSDebug(PLUGIN_NAME, "enter state_dns_lookup");
484482

@@ -489,16 +487,16 @@ state_dns_lookup(TSCont contp, TSEvent event, TSHostLookupResult host_info)
489487
txn_sm->q_pending_action = NULL;
490488

491489
/* Get the server IP from data structure TSHostLookupResult. */
492-
TSHostLookupResultAddrGet(host_info, (struct sockaddr *)&q_server_addr);
490+
struct sockaddr const *sa = TSHostLookupResultAddrGet(host_info);
493491

494492
/* Connect to the server using its IP. */
495493
set_handler(txn_sm->q_current_handler, (TxnSMHandler)&state_connect_to_server);
496494
TSAssert(txn_sm->q_pending_action == NULL);
497-
TSAssert(q_server_addr.ss_family == AF_INET); /* NO IPv6 in this plugin */
498-
addr = (struct sockaddr_in *)(&q_server_addr);
495+
TSAssert(sa->sa_family == AF_INET); /* NO IPv6 in this plugin */
496+
struct sockaddr_in *addr = (struct sockaddr_in *)(sa);
499497

500498
addr->sin_port = txn_sm->q_server_port;
501-
txn_sm->q_pending_action = TSNetConnect(contp, (struct sockaddr const *)addr);
499+
txn_sm->q_pending_action = TSNetConnect(contp, sa);
502500

503501
return TS_SUCCESS;
504502
}

include/ts/ts.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,10 +1921,9 @@ tsapi TSAction TSHostLookup(TSCont contp, const char *hostname, size_t namelen);
19211921
/** Retrieve an address from the host lookup.
19221922
*
19231923
* @param lookup_result Result handle passed to event callback.
1924-
* @param dst Destination for copying the address.
1925-
* @return @c TS_SUCCESS if the address was available and copied, @c TS_ERROR otherwise.
1924+
* @return A @c sockaddr with the address if successful, a @c nullptr if not.
19261925
*/
1927-
tsapi TSReturnCode TSHostLookupResultAddrGet(TSHostLookupResult lookup_result, struct sockaddr *dst);
1926+
tsapi struct sockaddr const *TSHostLookupResultAddrGet(TSHostLookupResult lookup_result);
19281927

19291928
/* TODO: Eventually, we might want something like this as well, but it requires
19301929
support for building the HostDBInfo struct:

plugins/lua/ts_lua_misc.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,11 @@ ts_lua_host_lookup_handler(TSCont contp, TSEvent event, void *edata)
476476
} else if (!edata) {
477477
lua_pushnil(L);
478478
} else {
479-
struct sockaddr_storage addr;
480479
TSHostLookupResult record = (TSHostLookupResult)edata;
481-
TSHostLookupResultAddrGet(record, (struct sockaddr *)&addr);
482-
if (addr.ss_family == AF_INET) {
480+
struct sockaddr *addr = TSHostLookupResultAddrGet(record);
481+
if (addr->sa_family == AF_INET) {
483482
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&addr)->sin_addr, cip, sizeof(cip));
484-
} else {
483+
} else if (addr->sa_family == AF_INET6) {
485484
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&addr)->sin6_addr, cip, sizeof(cip));
486485
}
487486
lua_pushstring(L, cip);

src/traffic_server/InkAPI.cc

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7392,6 +7392,30 @@ TSNetAcceptNamedProtocol(TSCont contp, const char *protocol)
73927392
}
73937393

73947394
/* DNS Lookups */
7395+
/// Context structure for the lookup callback to the plugin.
7396+
struct TSResolveInfo {
7397+
IpEndpoint addr; ///< Lookup result.
7398+
HostDBRecord *record = nullptr; ///< Record for the FQDN.
7399+
};
7400+
7401+
int
7402+
TSHostLookupTrampoline(TSCont contp, TSEvent ev, void *data)
7403+
{
7404+
auto c = reinterpret_cast<INKContInternal *>(contp);
7405+
// Set up the local context.
7406+
TSResolveInfo ri;
7407+
ri.record = static_cast<HostDBRecord *>(data);
7408+
if (ri.record) {
7409+
ri.record->rr_info()[0].data.ip.toSockAddr(ri.addr);
7410+
}
7411+
auto *target = reinterpret_cast<INKContInternal *>(c->mdata);
7412+
// Deliver the message.
7413+
target->handleEvent(ev, &ri);
7414+
// Cleanup.
7415+
c->destroy();
7416+
return TS_SUCCESS;
7417+
};
7418+
73957419
TSAction
73967420
TSHostLookup(TSCont contp, const char *hostname, size_t namelen)
73977421
{
@@ -7401,22 +7425,23 @@ TSHostLookup(TSCont contp, const char *hostname, size_t namelen)
74017425

74027426
FORCE_PLUGIN_SCOPED_MUTEX(contp);
74037427

7404-
INKContInternal *i = (INKContInternal *)contp;
7405-
return (TSAction)hostDBProcessor.getbyname_re(i, hostname, namelen);
7428+
// There is no place to store the actual sockaddr to which a pointer should be returned.
7429+
// therefore an intermediate continuation is created to intercept the reply from HostDB.
7430+
// Its handler can create the required sockaddr context on the stack and then forward
7431+
// the event to the plugin continuation. The sockaddr cannot be placed in the HostDB
7432+
// record because that is a shared object.
7433+
auto bouncer = INKContAllocator.alloc();
7434+
bouncer->m_event_func = &TSHostLookupTrampoline;
7435+
bouncer->mdata = contp;
7436+
return (TSAction)hostDBProcessor.getbyname_re(bouncer, hostname, namelen);
74067437
}
74077438

7408-
TSReturnCode
7409-
TSHostLookupResultAddrGet(TSHostLookupResult lookup_result, sockaddr *dst)
7439+
sockaddr const *
7440+
TSHostLookupResultAddrGet(TSHostLookupResult lookup_result)
74107441
{
74117442
sdk_assert(sdk_sanity_check_hostlookup_structure(lookup_result) == TS_SUCCESS);
7412-
HostDBRecord::Handle record{reinterpret_cast<HostDBRecord *>(lookup_result)};
7413-
if (record) {
7414-
auto info = record->rr_info();
7415-
sdk_assert(info.size() > 0);
7416-
info[0].data.ip.toSockAddr(dst);
7417-
return TS_SUCCESS;
7418-
}
7419-
return TS_ERROR;
7443+
auto ri{reinterpret_cast<TSResolveInfo *>(lookup_result)};
7444+
return ri->addr.isValid() ? &ri->addr.sa : nullptr;
74207445
}
74217446

74227447
/*

0 commit comments

Comments
 (0)