驅動程式 - VxWorks - 23.09 - C/C++ - MUX Binding



參考資訊:
https://zhuanlan.zhihu.com/p/597577575
https://studies.ac.upc.edu/doctorat/ENGRAP/VxWorks-device-drivers.htm
https://forums.windriver.com/t/vxworks-software-development-kit-sdk/43
https://mail.prz-rzeszow.pl/~ssamolej/vxworks/vxworks_kernel_programmers_guide_6.6.pdf
https://d13321s3lxgewa.cloudfront.net/downloads/wrsdk-vxworks7-docs/2309/README_qemu.html
https://learning.windriver.com/path/vxworks7-essentials-workbench-and-tools/vxworks-kernel-shell
https://cpp.hotexamples.com/site/file?hash=0x097d75ab101fe1fd6f0ba040e22d27e925f02c2a5bb9f311e7f20174c3e077c5&fullName=vxworks5-master/target/src/drv/wdbEndPktDrv.c&project=andy345/vxworks5

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iosLib.h>
#include <muxLib.h>
#include <ipnet_ip4.h>
#include <net/ethernet.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>

#define MYPORT          9999
#define NIC_INTF_NAME   "gei"

static PROTO_COOKIE MuxCookie = NULL;

static STATUS mux_rx_callback(void *pCookie, long type, M_BLK_ID mblk, LL_HDR_INFO *llHdrInfo, void *muxUserArg)
{
    char *pInPkt = NULL;
    char *pData = NULL;
    struct ip *pIpHdr = NULL;
    struct tcphdr *pTcpHdr = NULL;
    struct udphdr *pUdpHdr = NULL;
    int size = llHdrInfo->dataOffset + IPNET_IP_HDR_SIZE + sizeof(struct tcphdr);
    unsigned short sport = 0;
    unsigned short dport = 0;

    if (type != ETHERTYPE_IP) {
        return OK;
    }

    if (!(mblk->mBlkHdr.mFlags & M_PKTHDR) || (mblk->mBlkPktHdr.len < size)) {
        return OK; 
    }

    if (mblk->mBlkHdr.mLen < size) {
        if (netMblkOffsetToBufCopy(mblk, llHdrInfo->dataOffset, pInPkt, size, NULL) == 0) {
            return OK;
        }

        pIpHdr = (struct ip *)pInPkt;
        pTcpHdr = (struct tcphdr *)(pInPkt + IPNET_IP_HDR_SIZE);
        pData = (char *)((unsigned char *)pTcpHdr + sizeof(struct tcphdr));
    }
    else {
        pIpHdr = (struct ip *)(mblk->mBlkHdr.mData + llHdrInfo->dataOffset);
        pTcpHdr = (struct tcphdr *)(mblk->mBlkHdr.mData + llHdrInfo->dataOffset + IPNET_IP_HDR_SIZE);
        pData = (char *)((unsigned char *)pTcpHdr + sizeof(struct tcphdr));
    }

    sport = ntohs(pTcpHdr->th_sport);
    dport = ntohs(pTcpHdr->th_dport);
    if ((pIpHdr->ip_p == IPPROTO_TCP) && (sport == MYPORT)) {
        printf("TCP flags: 0x%x\n", pTcpHdr->th_flags);
        if (pTcpHdr->th_flags & TH_PUSH) {
            printf("TCP data: \'%s\'\n", pData);
        }
    }

    return OK;
}

STATUS myInit(void)
{
    if (muxLibInit() != OK) {
        printf( "Failed to initialize mux subsystem\n");
        return -1;
    }

    if(muxDevExists(NIC_INTF_NAME, 0)) {
        if ((MuxCookie = muxBind(NIC_INTF_NAME, 0, mux_rx_callback, NULL, NULL, NULL, MUX_PROTO_SNARF, "MyMuxTest", NULL)) == NULL) {
            printf("Failed to bind network interface\n ");
        }
    }
    printf("Bind mux interface successfully\n");
    return OK;
}