This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new de87836c7 netpkt: Add parameters to support specific network cards
de87836c7 is described below

commit de87836c7a710507948a339bc8949e541bbc505f
Author: zhangshuai39 <[email protected]>
AuthorDate: Fri Jul 4 11:28:52 2025 +0800

    netpkt: Add parameters to support specific network cards
    
    Supports sending and receiving data packets on specified interfaces.
    
    Signed-off-by: zhangshuai39 <[email protected]>
---
 examples/netpkt/netpkt_main.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)

diff --git a/examples/netpkt/netpkt_main.c b/examples/netpkt/netpkt_main.c
index 5f9aec81f..ab0595be4 100644
--- a/examples/netpkt/netpkt_main.c
+++ b/examples/netpkt/netpkt_main.c
@@ -29,7 +29,10 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <unistd.h>
+#include <netinet/if_ether.h>
+#include <netinet/in.h>
 #include <netpacket/packet.h>
+#include <net/if.h>
 
 /****************************************************************************
  * Private Functions
@@ -39,11 +42,17 @@
  * Name: psock_create
  ****************************************************************************/
 
-static int psock_create(void)
+static int psock_create(const char *ifname)
 {
   int sd;
   struct sockaddr_ll addr;
   int addrlen = sizeof(addr);
+  unsigned int ifindex = 0;
+
+  if (ifname && ifname[0])
+    {
+      ifindex = if_nametoindex(ifname);
+    }
 
   sd = socket(AF_PACKET, SOCK_RAW, 0);
   if (sd < 0)
@@ -55,7 +64,8 @@ static int psock_create(void)
   /* Prepare sockaddr struct */
 
   addr.sll_family = AF_PACKET;
-  addr.sll_ifindex = 0;
+  addr.sll_ifindex = ifindex;
+  addr.sll_protocol = htons(ETH_P_ALL);
   if (bind(sd, (const struct sockaddr *)&addr, addrlen) < 0)
     {
       perror("ERROR: binding socket failed");
@@ -105,6 +115,7 @@ static void netpkt_usage(void)
   printf(" -r     receive\n");
   printf(" -t     transmit\n");
   printf(" -v     verbose\n");
+  printf(" -i <IF> specify interface name (e.g. eth0)\n");
   printf("\n");
 }
 
@@ -116,7 +127,7 @@ static void netpkt_usage(void)
  * Name: netpkt_main
  ****************************************************************************/
 
-int main(int argc, FAR char *argv[])
+int main(int argc, char *argv[])
 {
   int sd;
   int i;
@@ -140,6 +151,9 @@ int main(int argc, FAR char *argv[])
   int do_rxtimes = 3;
   int do_tx = 0;
   int do_txtimes = 3;
+  char ifname[IFNAMSIZ];
+
+  memset(ifname, 0, sizeof(ifname));
 
   if (argc == 1)
     {
@@ -149,7 +163,7 @@ int main(int argc, FAR char *argv[])
 
   /* Parse arguments */
 
-  while ((opt = getopt(argc, argv, "artv")) != -1)
+  while ((opt = getopt(argc, argv, "artvi:")) != -1)
     {
       switch (opt)
         {
@@ -170,13 +184,17 @@ int main(int argc, FAR char *argv[])
             verbose = 1;
             break;
 
+          case 'i':
+            strlcpy(ifname, optarg, sizeof(ifname));
+            break;
+
           default:
             netpkt_usage();
             return -1;
       }
   }
 
-  sd = psock_create();
+  sd = psock_create(ifname);
 
   if (do_tx)
     {

Reply via email to