update ftpserver
This commit is contained in:
parent
6994f2a93a
commit
ec33ea3f7b
|
@ -22,6 +22,7 @@
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
@ -45,40 +46,29 @@ struct LoginStatus {
|
||||||
|
|
||||||
struct User users[MAX_USERS];
|
struct User users[MAX_USERS];
|
||||||
struct LoginStatus login_list[MAX_CONNECTION];
|
struct LoginStatus login_list[MAX_CONNECTION];
|
||||||
|
void GetFullAddress(char *client_id, char *client_ip, int client_port) {
|
||||||
|
memset(client_id, 0, sizeof(client_id));
|
||||||
|
sprintf(client_id, "%s:%d", client_ip, client_port);
|
||||||
|
}
|
||||||
int CheckPassword(int it){
|
int CheckPassword(int it){
|
||||||
for(int i=0;i<MAX_USERS;i++){
|
for(int i=0;i<MAX_USERS;i++){
|
||||||
if(strcmp(login_list[it]->users[i]->username) == 0 && strcmp(login_list[it]->users[i]->password) == 0)return 1;
|
if(strcmp(login_list[it].username,users[i].username) == 0 && strcmp(login_list[it].password,users[i].password) == 0)return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *GetAddress(struct sockaddr_in addr) {
|
|
||||||
char *address_string = (char *)malloc(INET_ADDRSTRLEN + 10);
|
|
||||||
|
|
||||||
if (address_string == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char ipStr[INET_ADDRSTRLEN];
|
|
||||||
inet_ntop(AF_INET, &(addr.sin_addr), ipStr, sizeof(ipStr));
|
|
||||||
|
|
||||||
sprintf(address_string, "%s:%d", ipStr, ntohs(addr.sin_port));
|
|
||||||
return address_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *GetRespond(char *buf,int it) {
|
char *GetRespond(char *buf,int it) {
|
||||||
char * cmd-malloc(100);
|
char * cmd=malloc(100);
|
||||||
char * data=malloc(100);
|
char * data=malloc(100);
|
||||||
char * respond=malloc(100);
|
char * respond=malloc(100);
|
||||||
sscanf(buf,"%s %s",cmd,data);
|
sscanf(buf,"%s %s",cmd,data);
|
||||||
if(strcmp(cmd,"USER")==0){
|
if(strcmp(cmd,"USER")==0){
|
||||||
login_list[it]->login_status=LOGGED_IN;
|
login_list[it].login_status=LOGED_IN;
|
||||||
strcpy(login_list[it]->username,data);
|
strcpy(login_list[it].username,data);
|
||||||
sprintf(respond,"331 %d login ok, send your password\n",data);
|
sprintf(respond,"331 %s login ok, send your password\n",data);
|
||||||
}
|
}
|
||||||
else if(strcmp(cmd,"PASS")==0){
|
else if(strcmp(cmd,"PASS")==0){
|
||||||
login_list[it]->login_status=LOGGED_IN;
|
login_list[it].login_status=LOGED_IN;
|
||||||
if(CheckPassword(it)){
|
if(CheckPassword(it)){
|
||||||
sprintf(respond,"230 access granted, restrictions apply\n");
|
sprintf(respond,"230 access granted, restrictions apply\n");
|
||||||
}
|
}
|
||||||
|
@ -105,9 +95,12 @@ char *GetRespond(char *buf,int it) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int Init() {
|
int Init() {
|
||||||
memset(login_status,0,sizeof(login_status));
|
memset(login_list,0,sizeof(login_list));
|
||||||
memset(users,0,sizeof(users));
|
memset(users,0,sizeof(users));
|
||||||
|
for (int i = 0;i < MAX_CONNECTION;i++) {
|
||||||
|
login_list[i].addr = (char*)malloc(50);
|
||||||
|
strcpy(login_list[i].addr, "");
|
||||||
|
}
|
||||||
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
printf("Failed to create socket\n");
|
printf("Failed to create socket\n");
|
||||||
|
@ -133,42 +126,46 @@ int Init() {
|
||||||
socklen_t len = sizeof(client_addr);
|
socklen_t len = sizeof(client_addr);
|
||||||
int cmd_fd = accept(fd, (struct sockaddr *)&client_addr, &len);
|
int cmd_fd = accept(fd, (struct sockaddr *)&client_addr, &len);
|
||||||
|
|
||||||
char *client_ip;
|
char client_ip[INET_ADDRSTRLEN];
|
||||||
int client_port;
|
int client_port;
|
||||||
inet_ntop(AF_INET, &(client_addr.sin_addr), client_ip, sizeof(client_ip));
|
inet_ntop(AF_INET, &(client_addr.sin_addr), client_ip, sizeof(client_ip));
|
||||||
client_port = ntohs(client_addr.sin_port);
|
client_port = ntohs(client_addr.sin_port);
|
||||||
printf("Client %s:%d connected.\n", client_ip, client_port);
|
printf("Client %s:%d connected.\n", client_ip, client_port);
|
||||||
|
|
||||||
int it=-1;
|
int it = -1;
|
||||||
for(int i=0;i<MAX_CONNECTION;i++){
|
char client_id[50];
|
||||||
if(strcmp(GetAddress(client_addr),login_list[it]->addr)){
|
for (int i = 0;i < MAX_CONNECTION;i++) {
|
||||||
|
GetFullAddress(client_id, client_ip, client_port);
|
||||||
|
printf("%s\n", client_id);
|
||||||
|
if (strcmp(client_id, login_list[it].addr) == 0) {
|
||||||
it=i;
|
it=i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (it==-1){
|
if (it==-1){
|
||||||
for(int i=0;i<MAX_CONNECTION;i++){
|
for(int i=0;i<MAX_CONNECTION;i++){
|
||||||
if(login_list[i]->login_status==0){
|
if(login_list[i].login_status==0){
|
||||||
it=i;
|
it = i;
|
||||||
|
strcpy(login_list[i].addr, client_id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(it==-1){
|
if(it==-1){
|
||||||
const char limit_exceeded = "530 connection limit exceeded.";
|
const char limit_exceeded[] = "530 connection limit exceeded.";
|
||||||
write(cmd_fd, limit_exceeded, strlen(limit_exceeded));
|
write(cmd_fd, limit_exceeded, strlen(limit_exceeded));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(login_list[it]->login_status == UNLOGED_IN){
|
if(login_list[it].login_status == UNLOGED_IN){
|
||||||
const char welcome = "220 welcome.";
|
const char welcome[] = "220 welcome.";
|
||||||
write(cmd_fd, welcome, strlen(welcome));
|
write(cmd_fd, welcome, strlen(welcome));
|
||||||
}
|
}
|
||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
int len;
|
int size;
|
||||||
len = read(cmd_fd, buf, sizeof(buf));
|
size = read(cmd_fd, buf, sizeof(buf));
|
||||||
|
|
||||||
if (len < 0) {
|
if (size < 0) {
|
||||||
printf("Failed to read or client closed\n");
|
printf("Failed to read or client closed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,9 +177,9 @@ int Init() {
|
||||||
void InsertUser(char * username,char * password)
|
void InsertUser(char * username,char * password)
|
||||||
{
|
{
|
||||||
for(int i=0;i<MAX_USERS;i++){
|
for(int i=0;i<MAX_USERS;i++){
|
||||||
if(strlen(users[i]->username)==0){
|
if(strlen(users[i].username)==0){
|
||||||
strcpy(users[i]->username,username);
|
strcpy(users[i].username,username);
|
||||||
strcpy(users[i]->password,password);
|
strcpy(users[i].password,password);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue