注意:
1、使用结构体的时候,但一个成员是48字节,但是只用了32字节的时候在发送的字节里面是按照48发送而不是32!
/**
recv broadcast run in the 3518c board
**/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <linux/in.h>
#include <stdlib.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#ifdef SOLARIS
#include <sys/sockio.h>
#endif
#define PORT 18600
#define CMD_HEAD_SIZE 12
#define MAXINTERFACES 16
#define MAX_LENGHT_16 16
#define MAX_LENGHT_32 32
#define MAX_LENGHT_128 128
#define MAX_PAYLOAD_LEN 1400
#define NET_LOOP_NAME "lo"
#define NET_ETHERNET_NAME "eth"
#define NET_WRILESS_NAME_RA "ra"
#define NET_WRILESS_NAME_WLAN "wlan"
//#define JOSEPH_BROADCAST_ARM
// common head of search tool and ipnc
typedef struct commandHead
{
int nEvent;
int nPayloadLen;
int nResved1;
unsigned char ucPayload[MAX_PAYLOAD_LEN];
}commandHead;
// ipnc info
typedef struct ipncInfo
{
char sIpncID[MAX_LENGHT_16];
char mac[MAX_LENGHT_32];
char sIP[MAX_LENGHT_16];
char sVer[MAX_LENGHT_128];
}joseph_ipncInfo;
// event define
// search ipnc req
#define JOSOPH_SEARCH_IPNC_REQ 0x00000001
// search ack from ipnc
// payload: ipncInfo
#define JOSOPH_SEARCH_IPNC_ACK 0x00000002
joseph_ipncInfo joseph_ipnc_info_net_device;
int get_device_mac_ip (argc, argv)
register int argc;
register char *argv[];
{
register int fd, intrface, retn = 0;
struct ifreq buf[MAXINTERFACES];
struct arpreq arp;
struct ifconf ifc;
memset(&joseph_ipnc_info_net_device,0,sizeof(joseph_ipncInfo));
if ((fd = socket (AF_INET, SOCK_DGRAM, 0)) >= 0)
{
ifc.ifc_len = sizeof buf;
ifc.ifc_buf = (caddr_t) buf;
if (!ioctl (fd, SIOCGIFCONF, (char *) &ifc))
{
/*set SIOCGIFCONF to get the info of net interface*/
intrface = ifc.ifc_len / sizeof (struct ifreq);
printf("Interface num is : Intrface = %d \n",intrface);
/*get the ip and port of device depend on the value of interface*/
while (intrface-- > 0)
{
/*get the name of net device*/
printf ("net device %s\n", buf[intrface].ifr_name);
/*Judge card type of net device*/
if (!(ioctl (fd, SIOCGIFFLAGS, (char *) &buf[intrface])))
{
/*judge the misc mode of net device*/
if (buf[intrface].ifr_flags & IFF_PROMISC)
{
puts ("the interface is PROMISC");
retn++;
}
}
/*judge the status of net device*/
if (buf[intrface].ifr_flags & IFF_UP)
{
puts("the interface status is UP");
#ifdef JOSEPH_BROADCAST_ARM
if((strstr(buf[intrface].ifr_name,NET_ETHERNET_NAME)) || ((strstr(buf[
intrface].ifr_name,NET_LOOP_NAME))))
#else
if((strstr(buf[intrface].ifr_name,NET_LOOP_NAME)))
#endif
{
continue;
}
}
else
{
puts("the interface status is DOWN");
break;
}
/*get the ip of current net device*/
if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface])))
{
puts ("IP address is:");
printf("%s",(char *)inet_ntoa((struct in_addr)((struct sockaddr_in*)(&buf
[intrface].ifr_addr))->sin_addr));
sprintf(joseph_ipnc_info_net_device.sIP,"%s",(char *)inet_ntoa((struct
in_addr)((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr));
puts("");
}
/*get the mac of current net device*/
if (!(ioctl (fd, SIOCGIFHWADDR, (char *) &buf[intrface])))
{
puts ("HW address is:");
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);
sprintf(joseph_ipnc_info_net_device.mac,"%02x:%02x:%02x:%02x:%02x:%02x",
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[0],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[1],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[2],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[3],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[4],
(unsigned char)buf[intrface].ifr_hwaddr.sa_data[5]);
puts("");
}
}
}
}
else
perror ("cpm: socket");
close (fd);
return retn;
}
int joseph_fill_broadcas_pack(void)
{
send_true_size = 0;
/* get the info of net device*/
get_device_mac_ip();
strcpy(joseph_ipnc_info_net_device.sIpncID , "C0123456789");
strcpy(joseph_ipnc_info_net_device.sVer , "V2.00.002 Build time 2014-04-29");
printf("The Ip of broadcast server is : %s ,The size is %d\n",
joseph_ipnc_info_net_device.sIP,strlen(joseph_ipnc_info_net_device.sIP));
printf("The Mac of joseph ipnc is : %s ,The size is %d\n",
joseph_ipnc_info_net_device.mac,strlen(joseph_ipnc_info_net_device.mac));
printf("The Id of joseph ipnc is : %s ,The size is %d\n",
joseph_ipnc_info_net_device.sIpncID,strlen(joseph_ipnc_info_net_device.sIpncID
));
printf("The Bersion of joseph ipnc is : %s ,The size is %d\n",
joseph_ipnc_info_net_device.sVer,strlen(joseph_ipnc_info_net_device.sVer));
printf("The listen port is is : %d \n",PORT );
return 0;
}
int main(int argc,char*argv[]){
int ret=-1;
int sock;
/*addr of server*/
struct sockaddr_in server_addr;
/*addr of client*/
struct sockaddr_in from_addr;
commandHead broadcast_ack_buf_recv;
commandHead broadcast_ack_buf_send;
int from_len=sizeof(struct sockaddr_in);
int count=-1;
fd_set readfd;
char buffer[MAX_PAYLOAD_LEN];
struct timeval timeout;
timeout.tv_sec=2;
timeout.tv_usec=0;
sock=socket(AF_INET,SOCK_DGRAM,0);
if(sock<0){
return;
}
memset((void*)&server_addr,0,sizeof(struct sockaddr_in));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr=htons(INADDR_ANY);
server_addr.sin_port=htons(PORT);
/*bind the content of server to socket*/
ret=bind(sock,(struct sockaddr*)&server_addr,sizeof(server_addr));
if(ret<0){
return;
}
joseph_fill_broadcas_pack();
while(1){
printf("=============================Recv Broadcast========================
===========\n");
timeout.tv_sec=2;
timeout.tv_usec=0;
FD_ZERO(&readfd);
FD_SET(sock,&readfd);
ret=select(sock+1,&readfd,NULL,NULL,&timeout);
printf("ret=%d\n",ret);
switch(ret){
case -1:
break;
case 0:
printf("timeout\n");
break;
default:
if(FD_ISSET(sock,&readfd)){
/*from_addr is client info of net*/
count=recvfrom(sock,&broadcast_ack_buf_recv,MAX_PAYLOAD_LEN,0,(struct
sockaddr*)&from_addr,&from_len);
broadcast_ack_buf_recv.nEvent = ntohl(broadcast_ack_buf_recv.nEvent);
if(broadcast_ack_buf_recv.nEvent == JOSOPH_SEARCH_IPNC_REQ){
broadcast_ack_buf_send.nEvent = htonl(JOSOPH_SEARCH_IPNC_ACK);
broadcast_ack_buf_send.nPayloadLen = htonl(sizeof(joseph_ipncInfo));
/*analysis the ip and port of client*/
printf("Client IP is %s\n",(char *)inet_ntoa(from_addr.sin_addr));
printf("Client Send Port:%d\n",ntohs(from_addr.sin_port));
memset(&broadcast_ack_buf_send.ucPayload,0,MAX_PAYLOAD_LEN);
memcpy(&broadcast_ack_buf_send.ucPayload,&joseph_ipnc_info_net_device,
sizeof(joseph_ipncInfo));
count=sendto(sock,&broadcast_ack_buf_send,(CMD_HEAD_SIZE+sizeof(
joseph_ipncInfo)),0,(struct sockaddr*)&from_addr,from_len);
printf("The value of count is %d\n",count);
if(count < 0)
{
printf("The value of count is %d\n",count);
}
}
}
break;
}
}
close(sock);
return;
}