#include "client.h"
int sock;
struct hostent *hp;
struct sockaddr_in addr;
int result;
char CMD[CMD_LEN];
char data[BUFFER_SIZE];
char safeguard = '\0';
FILE* file;
void userName(char* user)
{
memset(CMD, 0, sizeof(CMD));
strcpy(CMD, "user ");
strcat(CMD, user);
strcat(CMD, "\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Sending USER failed!");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void passwd(char* passwd)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pass ");
strcat(CMD, passwd);
strcat(CMD, "\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Recv PASSWORD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void put(char* filename)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
if ((result = write(sock, CMD, strlen(CMD))) < 0)
{
printf("[err] Sending data failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp));
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "type i\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "stor ");
strcat(CMD, filename);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
result = 1;
FILE* file = fopen(filename, "rb");
memset(data, 0, sizeof(data));
while (result = fread(data, sizeof(char), BUFFER_SIZE, file))
{
write(tempSock, data, result);
memset(data, 0, sizeof(data));
}
fclose(file);
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
}
void get(char* filename)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp));
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "type i\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0)
{
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "retr ");
strcat(CMD, filename);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
FILE* file = fopen(filename, "w");
while (result > 0){
memset(data, 0 , sizeof(data));
result = read(tempSock, data, BUFFER_SIZE);
fwrite(data, sizeof(char), result, file);
}
fclose(file);
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
}
void pwd()
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pwd");
strcat(CMD, "\r\n");
if ((result= write(sock, CMD, strlen(CMD))) < 0){
printf("[err] Sending CMD failed!");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void cd(char* cwd)
{
memset(CMD, 0, sizeof(CMD));
char end[CMD_LEN];
strcpy(end, cwd);
strcpy(CMD, "cwd ");
strcat(CMD, end);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s",CMD);
}
void dir(char* path)
{
memset(CMD, 0, sizeof(CMD));
strcat(CMD, "pasv\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0 , sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
int ip1, ip2, ip3, ip4, port1, port2;
char tmp[CMD_LEN];
char *buff = (char *)alloca(CMD_LEN);
memset(buff, 0 , sizeof(buff));
memset(tmp, 0 , sizeof(tmp)); //init
strcpy(tmp, CMD);
buff = strchr(tmp, '(');
sscanf(buff, "(%d,%d,%d,%d,%d,%d", &ip1, &ip2, &ip3, &ip4, &port1, &port2);
int port;
port = port1 * 256 + port2;
int tempSock;
tempSock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in tempAddr;
bzero(&tempAddr, sizeof(struct sockaddr_in));
tempAddr.sin_family = AF_INET;
tempAddr.sin_port = htons(port);
bcopy(hp->h_addr, (char *)&tempAddr.sin_addr, hp->h_length);
int conn;
conn = connect(tempSock, (struct sockaddr*)&tempAddr, sizeof(tempAddr));
if (conn < 0){
perror("[err] Connect failed!\n");
}
result = 1;
memset(CMD, 0 , sizeof(CMD));
strcpy(CMD, "list ");
strcat(CMD, path);
strcat(CMD, "\r\n");
result = write(sock, CMD, strlen(CMD));
if (result < 0){
printf("[err] Sending CMD failed!\n");
}
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN);
printf("-> ");
printf("%s", CMD);
while (result > 0){
memset(data, 0 , sizeof(data));
result = read(tempSock, data, BUFFER_SIZE);
printf("%s", data);
}
close(tempSock);
memset(CMD, 0, sizeof(CMD));
result = read(sock, CMD, CMD_LEN)
评论0