主题中讨论的其他器件: CC3100
大家好、
我使用的是 TI RTOS 和 tm4c129encpdt mnicrocontroller。 我有一项要求、即我想使用设备的 IP 地址查找设备的 Mac 地址。 根据该示例、
我在使用 cc3100的 wifi 项目中完成了这项工作。
但是、现在我也有一个以太网项目、并且希望在那里执行同样的操作。 我正在努力,但我无法做到这一点。 我用以太网套接字 API 替换了套接字 API 调用。
请告诉我是否有人可以解决此问题。
附加以太网代码。 (对于 wifi 代码、一个 ccan 访问上述链接、因为整个代码对 cc3100正常工作)。
void Wireless_Build_Arp_Packet(char* buf, unsigned char * src_mac, unsigned char *src_ip,
unsigned char * dest_mac, unsigned char *dest_ip)
{
struct arp_hdr* arp1;
struct arp_ethip* ethip;
struct arp_eth_hdr* ethhdr1;
unsigned char eth_type[2] = {0x08, 0x06}; // for arp
// unsigned char eth_type[2] = {0x80, 0x35}; // for rarp
ethhdr1 = (struct arp_eth_hdr*)buf;
memcpy(ethhdr1->dst_mac, dest_mac, ETH_ADDR_LEN);
memcpy(ethhdr1->src_mac, src_mac, ETH_ADDR_LEN);
memcpy(ethhdr1->type, eth_type, 2);
arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
arp1->ar_hrd = sl_Htons(ARP_HRD_ETH);
arp1->ar_pro = sl_Htons(ARP_PRO_IP);
arp1->ar_hln = ETH_ADDR_LEN;
arp1->ar_pln = IP_ADDR_LEN;
arp1->ar_op = sl_Htons(ARP_HRD_ETH);
ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
memcpy(ethip->ar_sha, src_mac, ETH_ADDR_LEN);
memcpy(ethip->ar_spa, src_ip, IP_ADDR_LEN);
memcpy(ethip->ar_tha, dest_mac, ETH_ADDR_LEN);
memcpy(ethip->ar_tpa, dest_ip, IP_ADDR_LEN);
}
void Wireless_Decode_Arp_Packet(char* buf){
struct arp_hdr* arp1;
struct arp_ethip* ethip;
struct arp_eth_hdr* ethhdr1;
ethhdr1 = (struct arp_eth_hdr*)buf;
// Mac address we were trying to get..
// Need to return this using pointers
logAsHexStr("Dest Mac address------ %s", "", ethhdr1->src_mac,6);
arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
// UART_PRINT("\n\rGot ARP response!\n\r");
// UART_PRINT("\n\rIP addr %i.%i.%i.%i ",ethip->ar_spa[0],ethip->ar_spa[1],ethip->ar_spa[2],ethip->ar_spa[3]);
// UART_PRINT("maps to MAC addr %x:%x:%x:%x:%x:%x!\n\r",ethip->ar_sha[0],ethip->ar_sha[1],ethip->ar_sha[2],ethip->ar_sha[3],ethip->ar_sha[4],ethip->ar_sha[5]);
// struct arp_hdr* arp1;
// struct arp_ethip* ethip;
// struct arp_eth_hdr* ethhdr1;
//
// char d_ipAdd[6] = {0};
// char d_mac[8] = {0};
//
// strncpy(buf+26, d_ipAdd, 4);
//
//
//
// // ethhdr1 = (struct arp_ethip*)buf;
// ethhdr1 = (struct arp_eth_hdr*)buf;
//
//
// strncpy(ethhdr1->src_mac, d_mac, 6);
//
// logAsHexStr("Dest Mac address------ %s", "", ethhdr1->src_mac, 6);
// logStr("Dest IP add -------- %s", "", d_ipAdd);
// logAsHexStr("Dest ip address in hex ------ %s", "", d_ipAdd, 4);
//
////
//
//
////
//// ethip = (struct arp_ethip*)(buf+ ETH_HDR_LEN);
//// arp1 = (struct arp_hdr*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
//
// arp1 = (struct arp_hdr*)(buf+ ETH_HDR_LEN);
// ethip = (struct arp_ethip*)(buf + ARP_HDR_LEN + ETH_HDR_LEN);
// // UART_PRINT("\n\rGot ARP response!\n\r");
// // UART_PRINT("\n\rIP addr %i.%i.%i.%i ",ethip->ar_spa[0],ethip->ar_spa[1],ethip->ar_spa[2],ethip->ar_spa[3]);
// // UART_PRINT("maps to MAC addr %x:%x:%x:%x:%x:%x!\n\r",ethip->ar_sha[0],ethip->ar_sha[1],ethip->ar_sha[2],ethip->ar_sha[3],ethip->ar_sha[4],ethip->ar_sha[5]);
//
}
char buf4[200];
void arp_protocol( unsigned char *s_mac, unsigned char *s_ip, unsigned char *d_mac, unsigned char *d_ip)
{
int fd, ret, len;
uint32_t header = 1;
//SlTimeval_t tTimeout;
// fd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW); // working for wifi
fd = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
Wireless_Build_Arp_Packet(buf4, s_mac, s_ip, d_mac, d_ip);
len = ARP_HDR_LEN + ARP_ETHIP_LEN + ETH_HDR_LEN;
ret = send(fd, buf4, len, 0);
__delay_cycles(5000000000);
logg("recv arp packet....", "");
ret = recv(fd, buf4, 200,0);
logg(" arp done... now decoding....", "");
Wireless_Decode_Arp_Packet(buf4);
close(fd);
}
似乎这不起作用、因为在这一行中-在上面的代码中、
fd =套接字(AF_iNet、SOCK_RAW、IPPROTO_IP);
我们没有合适的宏。 (请参考上述链接)。
谢谢
