]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Clean up and refactor dns_adb_getcookie()
authorAram Sargsyan <aram@isc.org>
Fri, 9 Dec 2022 15:09:12 +0000 (15:09 +0000)
committerAram Sargsyan <aram@isc.org>
Thu, 15 Dec 2022 12:34:26 +0000 (12:34 +0000)
The dns_adb_getcookie() doesn't use the 'adb' parameter, remove it.

Refactor the dns_adb_getcookie() function to just return the size of
the cookie when the caller passes 'NULL' as the 'cookie' argument.

lib/dns/adb.c
lib/dns/include/dns/adb.h
lib/dns/resolver.c

index e656c121cc08dbce85f5db67438f2052c803c70c..7aa11e0a2928a83f141fc4856600e6db2b120cb3 100644 (file)
@@ -3465,21 +3465,26 @@ dns_adb_setcookie(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
 }
 
 size_t
-dns_adb_getcookie(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
-                 unsigned char *cookie, size_t len) {
-       REQUIRE(DNS_ADB_VALID(adb));
+dns_adb_getcookie(dns_adbaddrinfo_t *addr, unsigned char *cookie, size_t len) {
        REQUIRE(DNS_ADBADDRINFO_VALID(addr));
 
        dns_adbentry_t *entry = addr->entry;
 
        LOCK(&entry->lock);
-       if (cookie != NULL && entry->cookie != NULL && len >= entry->cookielen)
-       {
-               memmove(cookie, entry->cookie, entry->cookielen);
-               len = entry->cookielen;
-       } else {
+       if (entry->cookie == NULL) {
                len = 0;
+               goto unlock;
        }
+       if (cookie != NULL) {
+               if (len < entry->cookielen) {
+                       len = 0;
+                       goto unlock;
+               }
+               memmove(cookie, entry->cookie, entry->cookielen);
+       }
+       len = entry->cookielen;
+
+unlock:
        UNLOCK(&entry->lock);
 
        return (len);
index 3a0a51e2d4e89d72cf6fb7579ae7b1f74f35a66a..7cee32f3e409887c5f1e5be31c4a05e3c2bf247f 100644 (file)
@@ -671,19 +671,17 @@ dns_adb_setcookie(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
  */
 
 size_t
-dns_adb_getcookie(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
-                 unsigned char *cookie, size_t len);
+dns_adb_getcookie(dns_adbaddrinfo_t *addr, unsigned char *cookie, size_t len);
 /*
- * Retrieve the saved COOKIE value and store it in 'cookie' which has
- * size 'len'.
+ * If 'cookie' is not NULL, then retrieve the saved COOKIE value and store it
+ * in 'cookie' which has size 'len'.
  *
  * Requires:
- *\li  'adb' is valid.
  *\li  'addr' is valid.
  *
  * Returns:
- *     The size of the cookie or zero if it doesn't fit in the buffer
- *     or it doesn't exist.
+ *     The size of the cookie or zero if it doesn't exist, or when 'cookie' is
+ *      not NULL and it doesn't fit in the buffer.
  */
 
 void
index c78402a4e9ea7214755fadb3bfcea0615b17b228..3db50f2cee21f56d43f6bd949997d7e5f6d228a0 100644 (file)
@@ -2744,8 +2744,8 @@ resquery_send(resquery_t *query) {
                                ednsopts[ednsopt].code = DNS_OPT_COOKIE;
                                ednsopts[ednsopt].length =
                                        (uint16_t)dns_adb_getcookie(
-                                               fctx->adb, query->addrinfo,
-                                               cookie, sizeof(cookie));
+                                               query->addrinfo, cookie,
+                                               sizeof(cookie));
                                if (ednsopts[ednsopt].length != 0) {
                                        ednsopts[ednsopt].value = cookie;
                                        inc_stats(
@@ -7658,9 +7658,8 @@ resquery_response(isc_result_t eresult, isc_region_t *region, void *arg) {
            !query->rmessage->cc_ok && !query->rmessage->cc_bad &&
            (rctx.retryopts & DNS_FETCHOPT_TCP) == 0)
        {
-               unsigned char cookie[COOKIE_BUFFER_SIZE];
-               if (dns_adb_getcookie(fctx->adb, query->addrinfo, cookie,
-                                     sizeof(cookie)) > CLIENT_COOKIE_SIZE)
+               if (dns_adb_getcookie(query->addrinfo, NULL, 0) >
+                   CLIENT_COOKIE_SIZE)
                {
                        if (isc_log_wouldlog(dns_lctx, ISC_LOG_INFO)) {
                                char addrbuf[ISC_SOCKADDR_FORMATSIZE];