This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Network stack question (how to get IP and MAC of client connecting to a socket)

 

 

I am using the following:

 

ccs 6.1.2  

TIRTOS 2.16.0.08,

compiler 5.2.7 and

XDC 3.31.1333

TM4C1294NCPD

TI network stack

 

I am running a TCP/IP socket server on the Tiva and my question is when a TCP/IP client connects, how can I get the IP address and MAC address of the client when the client connects?

 

Below is code where the accept

 

 

 

/* AF_INET family (IPv4) Socket address data structure. */

struct sockaddr_in {

    UINT16  sin_family;         /* address family */

    UINT16  sin_port;           /* port */

    struct  in_addr sin_addr;

    INT8    sin_zero[8];        /* fixed length address value */

};

 

void connectSocket(SOCKET_INFO *sock)

{

       struct sockaddr_in client_addr;

       int addrlen=sizeof(client_addr);

 

    sock->

  • how can I get the IP address and MAC address of the client when the client connects?

    tcpEcho.c contains some example code which displays the IP address of a client:

        while ((clientfd =
                accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != INVALID_SOCKET) {
    
            System_printf("tcpHandler: Creating thread clientfd = %d  client IP=%s\n", clientfd,
                    inet_ntop (AF_INET, &clientAddr.sin_addr, client_ip_str, sizeof (client_ip_str)));

    I'm not sure how to obtain the MAC address of the client, and if the client is on a different sub-net to the server you would get the MAC address of the intermediate router/gateway rather than that of the client.

    What do you need the MAC address of the connected client for?

  • I actually need the MAC address of the device connecting so I can tell a FPGA the MAC address of the client.  The device that connects to the Tiva is on a local private wired network only.  Normally we need to get the MAC address of the device that connect to the Tiva manually with ifconfig or ipconfig and then use the MAC address to tell the FPGA where to send the data (the MAC address of the client that connected to the Tiva)  but if I can get the MAC address of the client,  when the client connects, we can do everything automatically.

    Doug

  • I actually need the MAC address of the device connecting so I can tell a FPGA the MAC address of the client. 

    From looking at the NDK API Guide found the following functions can be used to get from the IP address of the client to the MAC address of the client:

    1. RtFind() to get the route for an IP address
    2. RtGetLLI() to get the Link Layer Information (LLI) Object associated with the route to the host. This wasn't in the documentation, but found in the <ti/ndk/inc/stack/inc/routeif.h> include file
    3. LLIGetMacAddr() to get the MAC address from the LLI object

    My tcpEcho.c example has been updated to use the above functions. That is using TI-RTOS for TivaC 2.16.1.14 and XDCTools 3.32.0.06_core

    Example output from the program when clients from two PCs on the same subnet connected. The displayed MAC addresses reported match those on the two PCs:

    ss in flash
    Starting the TCP Echo example
    System provider is set to SysMin. Halt the target to view any SysMin contents in ROV.
    Service Status: DHCPC    : Enabled  :          : 000
    Service Status: DHCPC    : Enabled  : Running  : 000
    Network Added: If-1:192.168.0.10
    Service Status: DHCPC    : Enabled  : Running  : 017
    tcpHandler: Creating thread clientfd = 537087020  client IP=192.168.0.118
    client MAC address=ec:b1:d7:3e:5f:f1
    tcpHandler: Connected hostname = MSFT-5-0-ec-b1-d7-3e-5f-f1.lan
    tcpWorker: start clientfd = 0x20034c2c
    tcpWorker stop clientfd=0x20034c2c errno=0 total_rx_bytes=4161536 total_tx_bytes=4161536
    tcpHandler: Creating thread clientfd = 537087788  client IP=192.168.0.22
    client MAC address=00:07:43:15:22:98
    tcpHandler: Connected hostname = DESKTOP-5DD8B6L.lan
    tcpWorker: start clientfd = 0x20034f2c
    tcpWorker stop clientfd=0x20034f2c errno=54 total_rx_bytes=7311360 total_tx_bytes=7311360