Skip to content

Commit

Permalink
Revert API change - maintain 9.x compatibility. (apache#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Carroll authored and GitHub Enterprise committed Jun 17, 2021
1 parent 6f63e83 commit 26a7fc9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
10 changes: 4 additions & 6 deletions example/plugins/c-api/protocol/TxnSM.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,6 @@ int
state_dns_lookup(TSCont contp, TSEvent event, TSHostLookupResult host_info)
{
TxnSM *txn_sm = (TxnSM *)TSContDataGet(contp);
struct sockaddr_storage q_server_addr;
struct sockaddr_in *addr;

TSDebug(PLUGIN_NAME, "enter state_dns_lookup");

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

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

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

addr->sin_port = txn_sm->q_server_port;
txn_sm->q_pending_action = TSNetConnect(contp, (struct sockaddr const *)addr);
txn_sm->q_pending_action = TSNetConnect(contp, sa);

return TS_SUCCESS;
}
Expand Down
5 changes: 2 additions & 3 deletions include/ts/ts.h
Original file line number Diff line number Diff line change
Expand Up @@ -1929,10 +1929,9 @@ tsapi TSAction TSHostLookup(TSCont contp, const char *hostname, size_t namelen);
/** Retrieve an address from the host lookup.
*
* @param lookup_result Result handle passed to event callback.
* @param dst Destination for copying the address.
* @return @c TS_SUCCESS if the address was available and copied, @c TS_ERROR otherwise.
* @return A @c sockaddr with the address if successful, a @c nullptr if not.
*/
tsapi TSReturnCode TSHostLookupResultAddrGet(TSHostLookupResult lookup_result, struct sockaddr *dst);
tsapi struct sockaddr const *TSHostLookupResultAddrGet(TSHostLookupResult lookup_result);

/* TODO: Eventually, we might want something like this as well, but it requires
support for building the HostDBInfo struct:
Expand Down
13 changes: 7 additions & 6 deletions plugins/lua/ts_lua_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,14 @@ ts_lua_host_lookup_handler(TSCont contp, TSEvent event, void *edata)
} else if (!edata) {
lua_pushnil(L);
} else {
struct sockaddr_storage addr;
TSHostLookupResult record = (TSHostLookupResult)edata;
TSHostLookupResultAddrGet(record, (struct sockaddr *)&addr);
if (addr.ss_family == AF_INET) {
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&addr)->sin_addr, cip, sizeof(cip));
TSHostLookupResult record = (TSHostLookupResult)edata;
struct sockaddr const *addr = TSHostLookupResultAddrGet(record);
if (addr->sa_family == AF_INET) {
inet_ntop(AF_INET, &((struct sockaddr_in const *)&addr)->sin_addr, cip, sizeof(cip));
} else if (addr->sa_family == AF_INET6) {
inet_ntop(AF_INET6, &((struct sockaddr_in6 const *)&addr)->sin6_addr, cip, sizeof(cip));
} else {
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&addr)->sin6_addr, cip, sizeof(cip));
cip[0] = 0;
}
lua_pushstring(L, cip);
}
Expand Down
49 changes: 37 additions & 12 deletions src/traffic_server/InkAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7436,6 +7436,30 @@ TSNetAcceptNamedProtocol(TSCont contp, const char *protocol)
}

/* DNS Lookups */
/// Context structure for the lookup callback to the plugin.
struct TSResolveInfo {
IpEndpoint addr; ///< Lookup result.
HostDBRecord *record = nullptr; ///< Record for the FQDN.
};

int
TSHostLookupTrampoline(TSCont contp, TSEvent ev, void *data)
{
auto c = reinterpret_cast<INKContInternal *>(contp);
// Set up the local context.
TSResolveInfo ri;
ri.record = static_cast<HostDBRecord *>(data);
if (ri.record) {
ri.record->rr_info()[0].data.ip.toSockAddr(ri.addr);
}
auto *target = reinterpret_cast<INKContInternal *>(c->mdata);
// Deliver the message.
target->handleEvent(ev, &ri);
// Cleanup.
c->destroy();
return TS_SUCCESS;
};

TSAction
TSHostLookup(TSCont contp, const char *hostname, size_t namelen)
{
Expand All @@ -7445,22 +7469,23 @@ TSHostLookup(TSCont contp, const char *hostname, size_t namelen)

FORCE_PLUGIN_SCOPED_MUTEX(contp);

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

TSReturnCode
TSHostLookupResultAddrGet(TSHostLookupResult lookup_result, sockaddr *dst)
sockaddr const *
TSHostLookupResultAddrGet(TSHostLookupResult lookup_result)
{
sdk_assert(sdk_sanity_check_hostlookup_structure(lookup_result) == TS_SUCCESS);
HostDBRecord::Handle record{reinterpret_cast<HostDBRecord *>(lookup_result)};
if (record) {
auto info = record->rr_info();
sdk_assert(info.size() > 0);
info[0].data.ip.toSockAddr(dst);
return TS_SUCCESS;
}
return TS_ERROR;
auto ri{reinterpret_cast<TSResolveInfo *>(lookup_result)};
return ri->addr.isValid() ? &ri->addr.sa : nullptr;
}

/*
Expand Down

0 comments on commit 26a7fc9

Please sign in to comment.