#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/wait.h>
#include <unistd.h>
#define HTTP_PORT 8080
#define MAXSIZE 256
#define HTML_NAME "/etc/zhh/zhh.html"
#define ZIP_NAME "/etc/zhh/zhh.zip"
#define PHONE_REG_NAME "/etc/zhh/zhh.txt"
#define ZIP_COMMAND "unzip -oq /etc/zhh/zhh.zip -d /etc/zhh/update/"
#define DEL_ZIP_COMMAND "rm /etc/zhh/zhh.zip"
#define DEL_HTML_COMAND "rm /etc/zhh/zhh.html"
#define SERVER_IPADDR "10.1.1.124"
//#define GET_COMMAND1 "GET /MedPay/upgrade_xml.do?upxml=UpgradexmlTime&stbid=sxls2011052711\r\n"
#define GET_COMMAND1 "GET /MedPay/upgrade_xml.do?upxml=UpgradexmlTime&stbid=%s\r\n"
//#define GET_COMMAND2 "GET /MedPay/xml/User/sxls2011052711.zip\r\n"
#define GET_COMMAND2 "GET /MedPay/xml/User/%s.zip\r\n"
#define GET_HOST1 "Host: 10.1.1.124\r\n\r\n"
static int update_time;
static int state;
char command[500];
char command_temp[300];
//get socket
int make_socket()
{
struct sockaddr_in local_addr;
int tempSockid;
if((tempSockid = socket(PF_INET,SOCK_STREAM,0))==-1)
return tempSockid;
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(HTTP_PORT);
local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&(local_addr.sin_zero),'\0',8);
return tempSockid;
}
//send to data
int sendall(int s, char *buf, int len) {
int total = 0;
int bytesleft = len;
int n;
while(total < len) {
n = send(s, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
len = total;
return n==-1?-1:0;
}
//get mac address
int getmac(int sockfd,char *mac)
{
int i=0;
struct ifconf ifconf;
char buf_temp[512];
char buf[512];
struct ifreq *ifreq_temp;
struct ifreq *ifreq;
//init ifconf struct
ifconf.ifc_len = 512;
ifconf.ifc_buf = buf_temp;
//get information all of the interface
if(ioctl(sockfd,SIOCGIFCONF,&ifconf)<0)
{
perror("ioctl");
return -1;
}
ifreq_temp = (struct ifreq *)buf_temp;
ifreq = (struct ifreq *)buf;
for(i=(ifconf.ifc_len/sizeof(struct ifreq));i>0;i--)
{
if(ifreq_temp->ifr_flags == AF_INET)
{
printf("name = [%s]\n",ifreq_temp->ifr_name);
printf("local addr = [%s]\n",inet_ntoa(((struct sockaddr_in *)&(ifreq_temp->ifr_addr))->sin_addr));
if(ioctl(sockfd,SIOCGIFFLAGS,ifreq_temp)>=0)
{
if((ifreq_temp->ifr_flags & IFF_UP) == IFF_UP)
{
strcpy(ifreq->ifr_name,ifreq_temp->ifr_name);
if(ioctl(sockfd,SIOCGIFHWADDR,ifreq)>=0)
{
printf("MAC:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",ifreq->ifr_hwaddr.sa_data[0],ifreq->ifr_hwaddr.sa_data[1],ifreq->ifr_hwaddr.sa_data[2],ifreq->ifr_hwaddr.sa_data[3],ifreq->ifr_hwaddr.sa_data[4],ifreq->ifr_hwaddr.sa_data[5]);
sprintf(mac,"%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",ifreq->ifr_hwaddr.sa_data[0],ifreq->ifr_hwaddr.sa_data[1],ifreq->ifr_hwaddr.sa_data[2],ifreq->ifr_hwaddr.sa_data[3],ifreq->ifr_hwaddr.sa_data[4],ifreq->ifr_hwaddr.sa_data[5]);
if(strcmp(mac,"000000000000")==0)
{
ifreq_temp++;
continue;
}
else
{
printf("\n");
return 1;
}
}
}
}
}
ifreq_temp++;
}
return -1;
}
//open file
int openfile(char *name)
{
int ret =0;
ret =open(name,O_RDWR | O_CREAT);
if(ret == -1)
perror("open");
return ret;
}
//process html file
int prochtml()
{
FILE *fp;
int fd_reg;
char *str_temp=NULL;
int temp =0;
char *line = NULL;
size_t len = 0;
ssize_t read =0;
fp = fopen(HTML_NAME, "r");
if (fp == NULL)
{
perror("fopen HTML_NAME");
return -1;
}
read = getline(&line, &len, fp);
if(read == -1)
{
perror("getline");
return -1;
}
str_temp=strtok(line,"#");
if(str_temp ==NULL)
{
perror("strtok");
return -1;
}
temp=atoi(str_temp);
update_time = temp/10;
if(temp%10 ==1)
{
fd_reg = openfile(PHONE_REG_NAME);
if(fd_reg != -1)
{
while(1)
{
str_temp = strtok(NULL,"#");
if(str_temp ==NULL)
break;
len =strlen(str_temp);
printf("%s\n",str_temp);
write(fd_reg,str_temp,len);
write(fd_reg,"\n",1);
}
}
else
perror("fopen PHONE_REG_NAME");
}
if (line)
free(line);
return temp/10%10;
}
//process zip file
int proczip()
{
sleep(2);
int ret =access(ZIP_NAME,0);
if(ret ==-1)
{
printf("update is error\n");
return -1;
}
else
{
ret = system(ZIP_COMMAND);
sleep(2);
return 1;
}
}
//recvfrom file
int recvfile(int fd,int sockfd)
{
char buf[MAXSIZE];
int readed =-1;
int count =0;
printf("downing...\n");
while(1)
{
bzero(buf,256);
readed =recv(sockfd,buf,255,0);
count++;
printf(".");
if(readed <=0)
break;
if(count%50==0)
printf("\n");
write(fd,buf,readed);
}
printf("\n");
if(state ==1)
return prochtml();
else
return proczip();
}
int main()
{
char mac[12];
char server[100];
char filename[100];
int len =-1;
update_time = 0;
state = 1;
int mysocket =-1;
int ret =-1;
struct sockaddr_in remote_server;
struct hostent * host_info;
int html_temp =0;
int zip_temp =0;
while(1)
{
printf("update begin\n");
printf("\n");
if(html_temp >0)
close(html_temp);
if(zip_temp >0)
close(zip_temp);
if(update_time ==0)
sleep(0);
else if(update_time ==1)
sleep(3600);
else if(update_time ==2)
sleep(36000);
else
sleep(1800);
html_temp = openfile(HTML_NAME);
zip_temp = openfile(ZIP_NAME);
if(html_temp ==-1 || zip_temp ==-1)
{
printf("create file is error\n");
update_time = 3;
continue;
}
mysocket = make_socket();
if(mysocket ==-1)
{
printf("make socket is error\n");
update_time = 3;
continue;
}
if(getmac(mysocket,mac)==-1)
{
printf("no network\n");
update_time = 3;
close(mysocket);
continue;
}
/*
if((host_info =gethostbyname("news.qq.com")) == NULL)
{
printf("gethostbyname is error\n");
close(mysocket);
continue;
}
*/
bzero(&remote_server,sizeof(remote_server));
remote_server.sin_family = AF_INET;
remote_server.sin_port = htons(HTTP_PORT);
remote_server.sin_addr.s_addr = inet_addr(SERVER_IPADDR);
// remote_server.sin_addr.s_addr = inet_addr(host_info->h_addr);
// remote_server.sin_addr = *((struct in_addr*)host_info->h_addr);
memset(&(remote_server.sin_zero),'\0',8);
ret=connect(mysocket, (struct sockaddr *)&remote_server,
sizeof(struct sockaddr));
if(ret ==-1)
{
perror("connect");
update_time = 3;
close(mysocket);
continue;
}
//send to command1
bzero(command,500);
bzero(command_temp,300);
sprintf(command,GET_COMMAND1,mac);
// strcpy(command,GET_COMMAND2);
// sprintf(command_temp,GET_HOST1,mac);
strcat(command,GET_HOST1);
len = strlen(command);
// int n = send(mysocket, command, len, 0);
if((sendall(mysocket,command,len))==-1)
{
printf("sending GET command is error\n");
update_time = 3;
close(mysocket);
continue;
}
if((recvfile(html_temp,mysocket))==1)
{
close(mysocket);
mysocket = make_socket();
if(mysocket ==-1)
{
printf("make socket is error\n");
continue;
}
/*
if((host_info =gethostbyname("news.qq.com")) == NULL)
{
printf("gethostbyname is error\n");
close(mysocket);
continue;
}
*/
memset(&(remote_server.sin_zero),'\0',8);
ret=connect(mysocket, (struct sockaddr *)&remote_server,
sizeof(struct sockaddr));
if(ret ==-1)
{
perror("connect");
close(mysocket);
continue;
}
sleep(2);
bzero(command,500);
bzero(command_temp,300);
sprintf(command,GET_COMMAND2,mac);
sprintf(command_temp,GET_HOST1,mac);
strcat(command,command_temp);
// strcpy(command,GET_COMMAND2);
// strcat(command,GET_HOST1);
len = strlen(command);
if((sendall(mysocket,command,len))==-1)
{
printf("sending GET zip command is error\n");
close(mysocket);
continue;
}
state = 2;
if((recvfile(zip_temp,mysocket))!=-1)
- 1
- 2
- 3
- 4
- 5
- 6
前往页