- 论坛徽章:
- 0
|
/*采用ftp协议支持断点续传地址处改为主机地址*/
#include<sys/types.h>;
#include<sys/socket.h>;
#include<fcntl.h>;
#include<sys/stat.h>;
#include<netinet/in.h>;
#include<arpa/inet.h>;
#include<string.h>;
#include<netdb.h>;
#include<unistd.h>;
#include<pwd.h>;
#include<errno.h>;
#include<stdlib.h>;
#include<stdio.h>;
#define ch1 128
#define ch2 64
#define ch3 32
#define ch4 16
#define bl 600 /*一次读的字节数*/
/*建立 socket连接*/
int get_socket_connet(char *ipaddress1,int port)
{ int c;
struct sockaddr_in myaddr1;
char *ip;
if((c=socket(PF_INET,SOCK_STREAM,0))<0)
exit(-1);
myaddr1.sin_family=PF_INET;
myaddr1.sin_port=htons(port);
myaddr1.sin_addr.s_addr=inet_addr(ipaddress1);
bzero(&(myaddr1.sin_zero), ;
if((connect(c,(struct sockaddr *)& myaddr1,sizeof( struct sockaddr)))!=0)
{printf("connect error!" ;
return -1;}
else return c;
}
/*关闭文件描述符*/
void close_fd(int fdnum)
{
close(fdnum);
}
/*计算数据连接的端口号*/
int get_connetport(char *str,int address1_len)
{ int long1,k_len,k2_len,port,portt,porta;
char *point1,*point2,*d,*point3,*point4,pstion[30],bb[10],b1[4],*pp,b2[4],*bd1,*bd2;
if((point1=strchr(str,'('))!=NULL)
printf("%s\n",point1);
point1++;
if((point2=strchr(str,')'))!=NULL)
printf("%s\n",point2);
long1=point2-point1;
printf("%d\n",long1);
point3=strncpy(pstion,point1,long1);
printf("%s\n",point3);
point4=strncpy(bb,point3+(address1_len+1),(long1-address1_len-1));
printf("%s\n",point4);
if((pp=strchr(point4,','))!=NULL)
printf("%s\n",pp);
k2_len=strlen(point4);
k_len=(pp-point4);
bd1=strncpy(b1,point4,k_len);
bd2=strncpy(b2,pp+1,k2_len-k_len-1);
printf("b1 is %s\n",b1);
printf("b2 is %s\n",b2);
port=atol(bd1);
portt=atol(bd2);
porta=(256*port+portt);
printf("%d\n",porta);
return porta;}
/*显示信息*/
void print_buf(int socketf)
{ char printf_buffer[100];
read(socketf,printf_buffer,100);
printf("%s",printf_buffer);
}
/*判断命令反应是否正确*/
int read_buf(int socketf ,char *string)
{ char read_buffer[255];
int test;
test=read(socketf,read_buffer,255);
if(test==-1)
{
printf("read error" ;
exit(-2);
}
if((strncmp(read_buffer,string,3))!=0)
{printf("%s error.",read_buffer);
exit(-3);
}
}
/*发送命令*/
void write_buf(int soketf,char *cmd,char *opt)
{char *comd, combuf[255];
int length;
bzero(combuf,255);
comd=(strcat(combuf,cmd));
comd=(strcat(comd,opt));
length=strlen(comd);
combuf[length]='\r';
combuf[length+1]='\n';
combuf[length+2]='\0';
/* comd=(strcat(comd,"\r\n" );*/
write(soketf,combuf,length+2);
}
char * strcon(char*string1,char*string2,char*string3)
{
char buffer[255], *str1,*str2,*str5;
bzero(buffer,255);
str1=strcat(buffer,string1);
str2=strcat(str1,string2);
str5=strcat(str2,string3);
return str5;
}
int checkuser(char * string)
{ struct passwd *pw;
char *nam;
pw=getpwnam(string);
nam=getenv("LOGNAME" ;
chdir(pw->;pw_dir);
if(strcmp(pw->;pw_name,nam)!=0)
{
printf("\n请使用badmin用户登录\n" ;
exit(-1);}
}
helpd()
{ printf("\n pftp1.0 欢迎使用本软件升级前台 \n" ;
printf("\n 此软件受GPL协议保护,如有不明,请查看GPL协议。\n" ;
}
saybye()
{ printf("\n文件以经传好了.\n" ;
}
/***************************************************************
主程序
****************************************************************/
main(int argc,char *argv[])
{ int socket1,socket2,port,filenm,read_size,read_buffer,size,size1,filelong,address1_len;
union hand
{ unsigned int h3;
char h1[2];
} head;
char buffer[255],buffer1[1500],rh,s1[10],*cd,*nm,*ipaddress1,shell[255],*filen;
struct passwd *userpwd;
long unsigned count1,count2,filecount;
ipaddress1="10.235.**.**";/*定义地址*/
address1_len=strlen(ipaddress1);
cd="/app2/lnpt/temp/install";
nm="install.20020702";
/*struct protoent *protos;
bzero(buffer,255);
bzero(myaddr1,sizeof(myaddr1));
protos=getprotobyname("tcp" ;*/
/*测试输入格式*/
if(argc!=4)
{ if(argc==2)
{ if((strncmp(argv[1],"-h",2))==0)
{helpd();
exit(-1);
}
}
printf("请按如下格式输入:用户名 密码 文件名\n" ;
exit(-1); }
checkuser("badmin");
printf("程序开始运行\n");
/*建立控制连接*/
socket1=get_socket_connet(ipaddress1,21);
print_buf(socket1);
write_buf(socket1,"user ",argv[1]);/*用户名*/
read_buf(socket1,"331");
write_buf(socket1,"pass ",argv[2]);/*口令*/
read_buf(socket1,"230");
write_buf(socket1,"type ","i");/*以二进制*/
read_buf(socket1,"200");
write_buf(socket1,"cwd ",cd);/*进入目录*/
read_buf(socket1,"250");
bzero(buffer,255);
write(socket1,"pasv\r\n",6);/*采用被动模式*/
read(socket1,buffer,255);
port=get_connetport(buffer,address1_len);
/*建立数据连接*/
socket2=get_socket_connet(ipaddress1,port);
write_buf(socket1,"SIZE ",argv[3]);
read(socket1,buffer,255);
if(strncmp(buffer,"213",3)!=0)
printf(" size error!\n");
else printf("%s\n",buffer);
bzero(buffer,255);
write_buf(socket1,"mode ","b");
read_buf(socket1,"200");
/*读取断点*/
chdir(userpwd->;pw_dir);
filelong=100;
filenm=open(argv[3],O_RDWR|O_CREAT|O_EXCL,00777);
if(filenm==-1)
{filenm=open(argv[3],O_RDWR);
if(filenm==-1)
printf("CANONT OPEN IT");
else {
filelong=(lseek(filenm,0,SEEK_END));
sprintf(s1,"%d",filelong);
write_buf(socket1,"rest ",s1);
printf("filelong is %d\n",filelong);
print_buf(socket1);
}
}
/*else write_buf(socket1,"REST ","0"); */
write_buf(socket1,"RETR ",argv[3]);
read_buf(socket1,"150");
/*读写文件*/
for( ;
{
if((read(socket2,buffer1,3))==-1)
{printf("read error.");
break;
}
rh=buffer1[0];
head.h1[0]=buffer1[2];
head.h1[1]=buffer1[1];
/* printf("%u\n",head.h3);*/
if (head.h3==0)
{/*printf("\n head.h3==0");*/
break;
}
if (head.h3==21)
{read_size=read(socket2,buffer1,21);
}
else{
read_size=0;
while(read_buffer!=head.h3)
{
size=bl;
if(size1<bl)
size=size1;
read_size=read(socket2,buffer1,size);
/* printf("read %d byte\n",read_size);*/
if(read_size==-1)
{printf("The file is broken at %u",count1);
exit(-1);
}
write(filenm,buffer1,read_size);
read_buffer+=read_size;
size1=head.h3-read_buffer;
/* printf("write %d byte\n",w);*/
}
bzero(buffer1,1500);
/*printf("write %d byte!\n",read_buffer);*/
printf("*");
read_buffer=0;
if(ch2==rh)
{ printf("break!!");
break;
}
}
}
close_fd(socket1);
close_fd(socket2);
close_fd(filenm);
printf("uid is %d",getuid());
bzero(shell,255);
printf("现在开始解包了");
strcat(shell,"/usr/bin/tar xvf ");
strcat(shell,argv[3]);
system(shell);
printf("现在开始安装文件");
system("./setup");
saybye();
} |
|