anchao commented on code in PR #17284:
URL: https://github.com/apache/nuttx/pull/17284#discussion_r2501447036


##########
libs/libc/netdb/lib_dnsdelserver.c:
##########
@@ -0,0 +1,229 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dnsdelserver.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>

Review Comment:
   ```suggestion
   ```



##########
libs/libc/netdb/lib_dnsdelserver.c:
##########
@@ -0,0 +1,229 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dnsdelserver.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/net/ip.h>
+#include <nuttx/net/dns.h>
+
+#include "netdb/lib_dns.h"
+
+#ifdef CONFIG_NETDB_DNSCLIENT
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int dns_find_nameserver_index(FAR const struct sockaddr *addr,
+                                     socklen_t addrlen)
+{
+  int i;
+  size_t cmplen = 0;
+
+#ifdef CONFIG_NET_IPv4
+  if (addr->sa_family == AF_INET)
+    {
+      cmplen = sizeof(struct sockaddr_in);
+    }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+  if (addr->sa_family == AF_INET6)
+    {
+      cmplen = sizeof(struct sockaddr_in6);
+    }
+#endif
+
+  if (cmplen == 0)
+    {
+      /* Unsupported address family */
+
+      return -ENOSYS;
+    }
+
+  if (addrlen < cmplen)
+    {
+      return -EINVAL;
+    }
+
+  dns_lock();
+
+#ifdef CONFIG_NETDB_RESOLVCONF
+
+  /* Not implemented for CONFIG_NETDB_RESOLVCONF */
+
+  dns_unlock();
+  return -ENOSYS;
+
+#else
+  /* Search for the matching nameserver in the array */
+
+  for (i = 0; i < g_dns_nservers; i++)
+    {
+      if (g_dns_servers[i].addr.sa_family == addr->sa_family)
+        {
+#ifdef CONFIG_NET_IPv4
+          if (addr->sa_family == AF_INET)
+            {
+              FAR struct sockaddr_in *in1 =
+                  (FAR struct sockaddr_in *)&g_dns_servers[i].addr;
+              FAR struct sockaddr_in *in2 =
+                  (FAR struct sockaddr_in *)addr;
+
+              /* Compare only the IP address part, ignore port and padding */
+
+              if (in1->sin_addr.s_addr == in2->sin_addr.s_addr)
+                {
+                  dns_unlock();
+                  return i;
+                }
+            }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+          if (addr->sa_family == AF_INET6)
+            {
+              FAR struct sockaddr_in6 *in6_1 =
+                  (FAR struct sockaddr_in6 *)&g_dns_servers[i].addr;
+              FAR struct sockaddr_in6 *in6_2 =
+                  (FAR struct sockaddr_in6 *)addr;
+
+              /* Compare only the IPv6 address part */
+
+              if (memcmp(&in6_1->sin6_addr, &in6_2->sin6_addr,
+                         sizeof(struct in6_addr)) == 0)

Review Comment:
   net_ipv6addr_cmp



##########
libs/libc/netdb/lib_dnsdelserver.c:
##########
@@ -0,0 +1,229 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dnsdelserver.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/net/ip.h>
+#include <nuttx/net/dns.h>
+
+#include "netdb/lib_dns.h"
+
+#ifdef CONFIG_NETDB_DNSCLIENT
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int dns_find_nameserver_index(FAR const struct sockaddr *addr,
+                                     socklen_t addrlen)
+{
+  int i;
+  size_t cmplen = 0;
+
+#ifdef CONFIG_NET_IPv4
+  if (addr->sa_family == AF_INET)
+    {
+      cmplen = sizeof(struct sockaddr_in);
+    }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+  if (addr->sa_family == AF_INET6)
+    {
+      cmplen = sizeof(struct sockaddr_in6);
+    }
+#endif
+
+  if (cmplen == 0)
+    {
+      /* Unsupported address family */
+
+      return -ENOSYS;
+    }
+
+  if (addrlen < cmplen)
+    {
+      return -EINVAL;
+    }
+
+  dns_lock();
+
+#ifdef CONFIG_NETDB_RESOLVCONF
+
+  /* Not implemented for CONFIG_NETDB_RESOLVCONF */
+
+  dns_unlock();
+  return -ENOSYS;
+
+#else
+  /* Search for the matching nameserver in the array */
+
+  for (i = 0; i < g_dns_nservers; i++)
+    {
+      if (g_dns_servers[i].addr.sa_family == addr->sa_family)
+        {
+#ifdef CONFIG_NET_IPv4
+          if (addr->sa_family == AF_INET)
+            {
+              FAR struct sockaddr_in *in1 =
+                  (FAR struct sockaddr_in *)&g_dns_servers[i].addr;
+              FAR struct sockaddr_in *in2 =
+                  (FAR struct sockaddr_in *)addr;
+
+              /* Compare only the IP address part, ignore port and padding */
+
+              if (in1->sin_addr.s_addr == in2->sin_addr.s_addr)
+                {
+                  dns_unlock();
+                  return i;
+                }
+            }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+          if (addr->sa_family == AF_INET6)
+            {
+              FAR struct sockaddr_in6 *in6_1 =
+                  (FAR struct sockaddr_in6 *)&g_dns_servers[i].addr;
+              FAR struct sockaddr_in6 *in6_2 =
+                  (FAR struct sockaddr_in6 *)addr;
+
+              /* Compare only the IPv6 address part */
+
+              if (memcmp(&in6_1->sin6_addr, &in6_2->sin6_addr,
+                         sizeof(struct in6_addr)) == 0)
+                {
+                  dns_unlock();
+                  return i;
+                }
+            }
+#endif
+        }
+    }
+
+  dns_unlock();
+  return -ENOENT;
+#endif
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: dns_del_nameserver
+ *
+ * Description:
+ *   Remove a DNS server from the list by matching the address
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NETDB_RESOLVCONF
+
+int dns_del_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
+{
+  /* Not implemented for CONFIG_NETDB_RESOLVCONF */
+
+  return -ENOSYS;
+}
+
+int dns_del_nameserver_by_index(int index)
+{
+  /* For resolv.conf mode, removing requires rewriting the file */
+
+  return -ENOSYS;
+}
+
+#else /* !CONFIG_NETDB_RESOLVCONF */
+
+int dns_del_nameserver(FAR const struct sockaddr *addr, socklen_t addrlen)
+{
+  int index;
+
+  DEBUGASSERT(addr != NULL);

Review Comment:
   remove addr check, will crash on line 175



##########
libs/libc/netdb/lib_dnsdelserver.c:
##########
@@ -0,0 +1,229 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_dnsdelserver.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <debug.h>
+
+#include <nuttx/net/ip.h>
+#include <nuttx/net/dns.h>
+
+#include "netdb/lib_dns.h"
+
+#ifdef CONFIG_NETDB_DNSCLIENT
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static int dns_find_nameserver_index(FAR const struct sockaddr *addr,
+                                     socklen_t addrlen)
+{
+  int i;
+  size_t cmplen = 0;
+
+#ifdef CONFIG_NET_IPv4
+  if (addr->sa_family == AF_INET)
+    {
+      cmplen = sizeof(struct sockaddr_in);
+    }
+#endif
+
+#ifdef CONFIG_NET_IPv6
+  if (addr->sa_family == AF_INET6)
+    {
+      cmplen = sizeof(struct sockaddr_in6);
+    }
+#endif
+
+  if (cmplen == 0)
+    {
+      /* Unsupported address family */
+
+      return -ENOSYS;
+    }
+
+  if (addrlen < cmplen)
+    {
+      return -EINVAL;
+    }
+
+  dns_lock();
+
+#ifdef CONFIG_NETDB_RESOLVCONF
+
+  /* Not implemented for CONFIG_NETDB_RESOLVCONF */
+
+  dns_unlock();
+  return -ENOSYS;
+
+#else
+  /* Search for the matching nameserver in the array */
+
+  for (i = 0; i < g_dns_nservers; i++)
+    {
+      if (g_dns_servers[i].addr.sa_family == addr->sa_family)
+        {
+#ifdef CONFIG_NET_IPv4
+          if (addr->sa_family == AF_INET)
+            {
+              FAR struct sockaddr_in *in1 =
+                  (FAR struct sockaddr_in *)&g_dns_servers[i].addr;
+              FAR struct sockaddr_in *in2 =
+                  (FAR struct sockaddr_in *)addr;
+
+              /* Compare only the IP address part, ignore port and padding */
+
+              if (in1->sin_addr.s_addr == in2->sin_addr.s_addr)

Review Comment:
   ```suggestion
                 if (net_ipv4addr_cmp(in1->sin_addr.s_addr, 
in2->sin_addr.s_addr))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to