#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include "ATA_Update.h"
#define MY_SOCK_PORT 2468
int socketId;
BOOL isSetServerAddr;
struct sockaddr sa;
int main(int argc, char *argv[])
{
isSetServerAddr = FALSE;
socketId = socket(PF_PACKET, SOCK_PACKET, htons(0x0003));
if (socketId < 0)
{
perror ("can not get SOCK_PACKET socket");
return 0;
}
//struct sockaddr sa;
memset(&sa, 0, sizeof(struct sockaddr));
sa.sa_family = PF_PACKET;
strcpy(sa.sa_data, "eth0");
if (bind(socketId, &sa, sizeof(sa)) < 0)
{
perror("bind ");
return 0;
}
// int value = 1;
// setsockopt(socketId, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value));
printf("send data test...\n");
while (1)
{
SendData(CMD_BROADCAST, NO_NEED_ACK);
sleep(5);
}
/*
printf("send broad cast pkg...\n");
BlockWaitForUpdate();
printf("get the server addr, after BlockWaitForUpdate\n");
printf("begin to recv data......\n");
RecvData(socketId);
printf("recv data over.........\n");
*/
return 0;
}
BOOL SendData(UINT8 cmdType, UINT8 ackType)
{
int i = 0;
int writeNum = 0;
printf("the cmd is: %d || ack type is : %d \n", cmdType, ackType);
UINT8 sendBuffer[100];
memset(sendBuffer, 0 , 100);
for (i = 0; i < 100; i++)
{
sendBuffer[i] = 16;
}
//00-16-76-A8-6E-AA
sendBuffer[0] = 0x00;
sendBuffer[1] = 0x16;
sendBuffer[2] = 0x76;
sendBuffer[3] = 0xA8;
sendBuffer[4] = 0x6E;
sendBuffer[5] = 0xAA;
//50:78:4C:70:92:EF
sendBuffer[6] = 0x50;
sendBuffer[7] = 0x78;
sendBuffer[8] = 0x4c;
sendBuffer[9]= 0x70;
sendBuffer[10] = 0x92;
sendBuffer[11] = 0xEF;
/*
for (i = 6; i < 12; i++)
{
sendBuffer[i] = 0x00;
}
*/
sendBuffer[12] = 0x00;
sendBuffer[13] = 0x00;
sendBuffer[14] = cmdType;
sendBuffer[15] = ackType;
memcpy(sendBuffer + 14, "ATA_UPDATE", 10);
for (i = 0; i < 100; i++)
{
printf(" %02x", sendBuffer[i]);
}
writeNum = sendto(socketId, sendBuffer, 100, 0, &sa, sizeof sa);
printf(" == write %d \n", writeNum);
if (writeNum == -1)
perror("sendto");
return TRUE;
}
/*
BOOL BlockWaitForUpdate()
{
fd_set readFd;
struct timeval tv;
int ret;
SendData(CMD_BROADCAST, NO_NEED_ACK);
while(1)
{
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&readFd);
FD_SET(socketId, &readFd);
ret = select(socketId + 1, &readFd, NULL, NULL, &tv);
switch(ret)
{
case -1:
break;
case 0:
printf("resend data\n");
SendData(CMD_BROADCAST, NO_NEED_ACK);
break;
default:
if (FD_ISSET(socketId, &readFd))
{
printf("here come pkg\n");
if (isRightPkg(socketId, CMD_UPDATE))
{
return TRUE;
}
}
}
}
}
//这里还可以进行一些字符内容的设定,增加安全性
BOOL isRightData(char *recvBuff, UINT8 cmdType)
{
if (recvBuff == NULL)
return FALSE;
if (recvBuff[0] == cmdType)
return TRUE;
return FALSE;
}
BOOL isRightPkg(int sockfd, UINT8 cmdType)
{
int recvNum;
char recvBuff[1024];
struct sockaddr_in addr;
int addrLen = sizeof(struct sockaddr_in);
memset(&addr, 0, sizeof(addr));
recvNum = recvfrom(sockfd, recvBuff, sizeof(recvBuff),
0, (struct sockaddr*)&addr, &addrLen);
if (recvNum < 0)
{
return FALSE;
}
recvBuff[recvNum] = 0;
printf("receive from [%s] : %s\n", inet_ntoa(addr.sin_addr), recvBuff);
if (!isSetServerAddr)
{
if ((cmdType == CMD_UPDATE)
&&( isRightData(recvBuff, CMD_UPDATE)))
{
isSetServerAddr = TRUE;
serverAddress = addr;
printf("address is set: %s\n", inet_ntoa(serverAddress.sin_addr));
SendData(CMD_ACK, ACK_FOR_UPDATE);
printf("\n\n---after here begin to data transfer........\n");
return TRUE;
}
else
return FALSE;
}
else
{
if ((addr.sin_addr.s_addr == serverAddress.sin_addr.s_addr)
&& (isRightData(recvBuff, cmdType)))
{
return TRUE;
}
}
return FALSE;
}
void RecvData(int sockfd)
{
int recvNum;
char recvBuff[1024];
struct sockaddr_in addr;
int addrLen = sizeof(struct sockaddr_in);
memset(&addr, 0, sizeof(addr));
while (1)
{
recvNum = recvfrom(sockfd, recvBuff, sizeof(recvBuff),
0, (struct sockaddr*)&addr, &addrLen);
if (recvNum < 0)
{
printf("error with recv data\n");
continue;
}
recvBuff[recvNum] = 0;
PrintPkgAndAddr(PKG_RECV, addr, recvBuff, recvNum);
//here process the pkg
//PutDataInPkg();
//CheckRecvPkg();
//send ack back
if (recvBuff[0] == CMD_DATA)
{
SendData(CMD_ACK, ACK_FOR_DATA);
}
else
{
}
}
}
void PrintPkgAndAddr(PKG_TYPE type, struct sockaddr_in addr, char *buff, int len)
{
int i;
if (type == PKG_SEND)
{
printf("\nsend [%d bytes] to [%s] : ", len, inet_ntoa(addr.sin_addr));
}
else if (type == PKG_RECV)
{
printf("\nrecv [%d bytes] from [%s] : ", len, inet_ntoa(addr.sin_addr));
}
for (i = 0; i < len; i++)
{
printf(" %02x ", (unsigned char)buff[i]);
}
printf("\n");
}
void PutDataInPkg(char *buff)
{
}
BOOL CheckRecvPkg(APP_PKG *pkg)
{
}
*/
posted on 2007-11-26 19:18
吴剑 阅读(255)
评论(0) 编辑 收藏 引用