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


##########
net/icmp/icmp_reply.c:
##########
@@ -119,20 +119,75 @@ void icmp_reply(FAR struct net_driver_s *dev, int type, 
int code)
   if (datalen > ICMP_MAXMSGLEN - ipicmplen)
     {
       datalen = ICMP_MAXMSGLEN - ipicmplen;
+      iob_trimtail(dev->d_iob, dev->d_iob->io_pktlen - datalen);
     }
 
-  dev->d_len = ipicmplen + datalen;
+  /* Save the original datagram */
+
+  if (CONFIG_IOB_BUFSIZE >= datalen + ipicmplen +
+                           CONFIG_NET_LL_GUARDSIZE)
+    {
+      /* Reuse current iob */
+
+      memmove((FAR char *)ipv4 + ipicmplen, ipv4, datalen);
+
+      /* Skip icmp header from iob */
+
+      iob_update_pktlen(dev->d_iob, datalen + ipicmplen);
+    }
+  else
+    {
+      FAR struct iob_s *iob;
+
+      /* Save the original datagram to iob chain */
+
+      iob = dev->d_iob;
+      dev->d_iob = NULL;
+
+      /* Re-prepare device buffer */
 
-  /* Copy fields from original packet */
+      if (netdev_iob_prepare(dev, false, 0) != OK)
+        {
+          dev->d_len = 0;
+          dev->d_iob = iob;
+          netdev_iob_release(dev);
+          return;
+        }
 
-  memmove(icmp + 1, ipv4, datalen);
+      /* Copy ipv4 header to device buffer */
+
+      if (iob_trycopyin(dev->d_iob, (FAR void *)ipv4,
+                        IPv4_HDRLEN, 0, false) != IPv4_HDRLEN)
+        {
+          dev->d_len = 0;
+          netdev_iob_release(dev);
+          iob_free_chain(iob);
+          return;
+        }
+
+      /* Skip icmp header from iob */
+
+      iob_update_pktlen(dev->d_iob, dev->d_iob->io_pktlen +
+                                    sizeof(struct icmp_hdr_s));
+
+      /* Concat new icmp packet before original datagram */
+
+      iob_concat(dev->d_iob, iob);
+
+      /* IPv4 header to new iob */
+
+      ipv4 = IPBUF(0);
+    }
+
+  dev->d_len = ipicmplen + datalen;
 
   ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_ICMP,
                     &dev->d_ipaddr, (FAR in_addr_t *)ipv4->srcipaddr,
                     IP_TTL_DEFAULT, NULL);
 
   /* Initialize the ICMP header */
 
+  icmp = (FAR struct icmp_hdr_s *)(ipv4 + 1);
   icmp->type        = type;

Review Comment:
   Done



-- 
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: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to