#include"mysock.h"
#include<string.h>
#define DBG(a) printf("%s = %d\n", #a, a)
#define NOOP ((char*)"NOOP\r\n")
struct Mysock_client client;
struct Mysock_server server;
char * host = NULL;
char port[6]= "21";
char username[1024];
char password[1024];
char buf[1024];
char buf1[1024];
bool pasv = true;
char pasv_port[20];
bool set_pasv();
void dologin();
void shell();
void lcd();
void pwd();
void cd();
void get();
void put();
void del();
void dir();
void my_quit();
int main(int argc, char * argv[]){
/* init */
int len, status;
int i;
char cmd[30];
init_mysock_client(&client);
init_mysock_server(&server);
if(argc < 2){
printf("Usage: %s \n", argv[0]);
exit(1);
}
host = argv[1];
if(argc > 2)
strcpy(port, argv[2]);
printf("host : %s port %s\n", argv[1], port);
mysock_client_con(&client, host, port);
printf("connect success\n");
if( (*client.cli_sock.connected)(&(client.cli_sock)) == false) {
printf("Can't connect to server (%s:%p)\n", host, port);
return 1;
}
(*client.cli_sock.sendline)(&(client.cli_sock),NOOP);
while (len = (*client.cli_sock.recvline)(&client.cli_sock, buf), len > 0){
buf[len] = 0;
sscanf(buf, "%d", &status);
if(status != 220) break;
}
dologin();
while (true){
printf("ftp> ");
if(gets(buf) == NULL) my_quit();
sscanf(buf, "%10s", cmd);
for(i = 0; cmd[i]; ++i){
if(cmd[i] <= 'z' && cmd[i] >= 'a')
cmd[i] -= 32;
}
if(cmd[0] == '!'){
shell();
}else if(strcmp(cmd, "LCD") == 0){
lcd();
}else if(strcmp(cmd, "PWD") == 0){
pwd();
}else if(strcmp(cmd, "CD") == 0){
cd();
}else if(strcmp(cmd, "GET") == 0){
get();
}else if(strcmp(cmd, "PUT") == 0){
put();
}else if(strcmp(cmd, "DEL") == 0){
del();
}else if(strcmp(cmd, "DIR") == 0){
dir();
}else if(strcmp(cmd, "my_quit") == 0){
quit();
}else{
printf("Illegal Command. Supported commands are: \n");
printf("lcd, pwd, cd, put, get, del, dir, my_quit.\n");
printf("Command with a prefix \"!\" will run in the shell.\n");
}
}
return 0;
}
void dologin(){
int len;
int status;
while(true){
printf("Username: ");
scanf("%s", username);
getchar();
sprintf(buf, "USER %s\r\n", username);
(*client.cli_sock.sendline)(&client.cli_sock, buf);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
sscanf(buf, "%d", &status);
if(status != 331) continue;
printf("Password: ");
scanf("%s", password);
getchar();
sprintf(buf, "PASS %s\r\n", password);
(*client.cli_sock.sendline)(&client.cli_sock, buf);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
sscanf(buf, "%d", &status);
if(status == 230)break;
}
}
void put(){
char file[1024], *p;
int len;
FILE* fp;
mysock_client sender;
init_mysock_client(&sender);
for (p = buf; *p; p++){
if(*p == ' ') {
p++;
break;
}
}
strcpy(file, p);
fp = fopen(file, "r");
if (fp == NULL){
printf("Can't open file\n");
return;
}
set_pasv();
sprintf(buf1, "STOR %s\r\n", file);
printf("%s", buf1);
(*client.cli_sock.sendline)(&client.cli_sock, buf1);
if(pasv){
(*sender.conn)(&sender, host, pasv_port);
if((*sender.cli_sock.connected)(&sender.cli_sock) == false){
printf("Unable to connect to server (%s:%s)\n", host, pasv_port);
}
(*client.cli_sock.recvline)(&client.cli_sock, buf);
while(len = fread(buf, 1, 1000, fp), len > 0){
(*sender.cli_sock.my_sendmsg)(&sender.cli_sock, buf, len);
if(len < 1000)
break;
}
(*sender.cli_sock.my_quit)(&sender.cli_sock);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
printf("Data transferred.\n");
}else{
printf("Not pasv. Not Completed.\n");
}
fclose(fp);
}
void pwd(){
int i;
(*client.cli_sock.sendline)(&client.cli_sock, (char *)"PWD\r\n");
(*client.cli_sock.recvline)(&client.cli_sock, buf);
}
void cd(){
int len = strlen(buf);
char *p;
if(len == 2) {
pwd();
return;
}
buf[len+1] = 0;
for (p = buf; *p; p++){
if(*p == ' ') break;
}
sprintf(buf1, "CWD%s\r\n", p);
(*client.cli_sock.sendline)(&client.cli_sock, buf1);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
}
void get(){
char *p, file[1024];
int len;
int status;
int datalen;
char f_buf[64];
FILE *fp ;
mysock_client recver;
p = NULL;
init_mysock_client(&recver);
for (p = buf; *p; p++){
if(*p == ' ') {
p++;
strcpy(file, p);
printf("FILE: %s~\n", file);
break;
}
}
set_pasv();
(*client.cli_sock.sendline)(&client.cli_sock, (char*)"TYPE I\r\n");
(*client.cli_sock.recvline)(&client.cli_sock, buf1);
sprintf(buf1, "RETR %s\r\n", file);
(*client.cli_sock.sendline)(&client.cli_sock, buf1);
if(pasv){
(*recver.conn)(&recver, host, pasv_port);
if((*recver.cli_sock.connected)(&recver.cli_sock) == false){
printf("Unable to connect to server (%s:%s)\n", host, pasv_port);
}
(*client.cli_sock.recvline)(&client.cli_sock, buf);
sscanf(buf, "%d", &status);
if(status == 550){
(*recver.cli_sock.my_quit)(&recver.cli_sock);
return;
}
for(p = buf;*p != '('; p++);
sscanf(p, "(%d", &datalen);
printf("Data length: %d\n", datalen);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
fp = fopen(file, "w");
if (fp == NULL){
printf("Can't open file for write\n");
return;
}
while(datalen > 0){
if (datalen >= 64){
len = (*recver.cli_sock.my_recvmsg)(&recver.cli_sock, f_buf, 64);
fwrite(f_buf, 1, 64, fp);
}else{
len = (*recver.cli_sock.my_recvmsg)(&recver.cli_sock, f_buf, datalen);
fwrite(f_buf, 1, datalen, fp);
break;
}
datalen -= 64;
}
fclose(fp);
(*recver.cli_sock.my_quit)(&recver.cli_sock);
printf("Data transferred.\n");
}else{
printf("Not pasv. Not Completed.\n");
}
}
void dir(){
int i;
mysock_client recver;
init_mysock_client(&recver);
set_pasv();
(*client.cli_sock.sendline)(&client.cli_sock, (char*)"LIST\r\n");
if(pasv){
(*recver.conn)(&recver, host, pasv_port);
if((*recver.cli_sock.connected)(&recver.cli_sock) == false){
printf("Unable to connect to server (%s:%s)\n", host, pasv_port);
}
(*client.cli_sock.recvline)(&client.cli_sock, buf);
while(true){
i = (*recver.cli_sock.my_recvmsg)(&recver.cli_sock,buf, 64);
buf[i] = 0;
printf("%s", buf);
if(i < 64) break;
}
(*recver.cli_sock.my_quit)(&recver.cli_sock);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
//printf("Data transferred.\n");
}else{
printf("Not pasv. Not Completed.\n");
}
}
void quit(){
(*client.cli_sock.sendline)(&client.cli_sock, (char*)"my_quit\r\n");
(*client.cli_sock.recvline)(&client.cli_sock, buf);
(*client.cli_sock.my_quit)(&client.cli_sock);
}
bool set_pasv(){
int status;
int i;
(*client.cli_sock.sendline)(&client.cli_sock, (char*)"PASV\r\n");
(*client.cli_sock.recvline)(&client.cli_sock, buf);
sscanf(buf, "%d", &status);
if(status == 227){
pasv = 1;
for(i = 0; buf[i]; ++i){
if(buf[i] == '('){
int a, b;
sscanf(buf+i, "(%*d,%*d,%*d,%*d,%d,%d)", &a, &b);
sprintf(pasv_port, "%d", a * 256 + b);
printf("PASV PORT: %s\n", pasv_port);
break;
}
}
return true;
}else{
pasv = 0;
return false;
}
}
void shell(){
if(buf[1] == 0) return;
system(buf+1);
}
void lcd(){
char *p;
for (p = buf; *p; p++){
if(*p == ' ') break;
}
if(*p == 0) {
system("pwd");
return;
}
p++;
if(chdir(p) == 0){
printf("Changed to %s\n", p);
}else{
printf("Failed to change to %s\n", p);
}
}
void del(){
char buf1[1024];
char *p;
int len = strlen(buf);
if(len == 3) return;
for (p = buf; *p; p++){
if(*p == ' ') break;
}
sprintf(buf1, "DELE%s\r\n", p);
(*client.cli_sock.sendline)(&client.cli_sock, buf1);
(*client.cli_sock.recvline)(&client.cli_sock, buf);
}
|