Beispiel #1
0
void command_put(int sfd_client, struct packet* chp, char* filename)
{
	FILE* f = fopen(filename, "rb");	// Yo!
	if(!f)
	{
		fprintf(stderr, "File could not be opened for reading. Aborting...\n");
		return;
	}
	clear_packet(chp);
	chp->type = REQU;
	chp->comid = PUT;
	strcpy(chp->buffer, filename);
	send_packet(sfd_client, chp);
	recv_packet(sfd_client, chp);
	//printpacket(chp, HP);
	if(chp->type == INFO && chp->comid == PUT && strlen(chp->buffer))
	{
		printf("\t%s\n", chp->buffer);
		chp->type = DATA;
		send_file(sfd_client, chp, f);
		fclose(f);
	}
	else
		fprintf(stderr, "Error sending file.\n");
	send_EOT(sfd_client, chp);
}
// the opration of DIR command
void command_dir(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
    int x;
    shp->type = DATA;
    DIR* d = opendir(lpwd);
    if(!d)
    {
        er("opendir()", (int) d);
        sprintf(shp->buffer, "ER: could not get the dir info!\n");
        data = htonp(shp);
        if( (x = send(sfd_server_data, data, size_packet,0)) != size_packet)
            er("send()",x);
    }
    else
    {
        sprintf(shp->buffer, "OK: get the dir info.\n");
        data = htonp(shp);
        if(( x = send(sfd_server_data, data, size_packet, 0)) != size_packet)
            er("send()",x);
    }
    struct dirent* e;

    while(e = readdir(d))
    {
        sprintf(shp->buffer, "%s\t%s", e->d_type == 4 ? "DIR:" : e->d_type == 8 ? "FILE:" : "UNDEF:" , e->d_name);
        data = htonp(shp);
        //if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
        if(( x = send(sfd_server_data, data, size_packet, 0)) != size_packet)    
            er("send()", x);
    }
    send_EOT(shp, data, sfd_server_data);
    //send_EOT(shp, data, sfd_client);
}
// the operation of GET file command
void command_get(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
    int x;
    FILE* f = fopen(shp->buffer, "rb");
    shp->type = INFO;
    shp->comid = GET;
    strcpy(shp->buffer, f ? "OK: File found; processing..." : "ER: Error opening file.");
    printpacket(shp, HP);
    data = htonp(shp);
    if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
        er("send()", x);
    if(f)
    {
        shp->type = DATA;
        //send_file(shp, data, sfd_client, f);
        send_file(shp, data, sfd_server_data, f);
        fclose(f);
    }
    send_EOT(shp, data, sfd_server_data);
    //send_EOT(shp, data, sfd_client);
}
// the operation of PRT command
void command_prt(struct packet* shp, struct packet* data, int sfd_client, char* lpwd)
{
    if(DEBUG)
        printf("Starting prt...\n");
    int x;
    int dataportnum = atoi(shp->buffer);
    shp->type = INFO;
    shp->comid = PRT;
    strcpy(shp->buffer, dataportnum ? "OK: Port Gotten; processing..." : "ER: Error getting port num.");
 
    printpacket(shp, HP);

    data = htonp(shp);
    if(( x = send(sfd_client, data, size_packet, 0)) != size_packet)
        er("send()", x);

    if(DEBUG)
        printf("OK, waiting for connection\n");

    if(dataportnum)
    {
        struct sockaddr_in skin_server_data;
        int x;
        size_t size_sockaddr = sizeof(struct sockaddr), size_packet = sizeof(struct packet);

        if(( x = sfd_server_data = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            er("socket()", x);

        memset((char*) & skin_server_data, 0, sizeof(struct sockaddr_in));
        skin_server_data.sin_family = AF_INET;
        skin_server_data.sin_addr.s_addr = inet_addr(IPCLIENT);
        skin_server_data.sin_port = htons(dataportnum);

        if(( x = connect(sfd_server_data, (struct sockaddr*) &skin_server_data, size_sockaddr)) < 0)
            er("connect()", x);
        printf(ID "Data channel build. Communicating with client @ %s:%d...\n\n", IPCLIENT, dataportnum);
    }
    send_EOT(shp, data, sfd_client);    
}
Beispiel #5
0
void* serve_client(void* info)
{
int sfd_client, connection_id, x;
struct packet* data = (struct packet*) malloc(size_packet);
struct packet* shp;
char lpwd[LENBUFFER];
struct client_info* ci = (struct client_info*) info;
sfd_client = ci->sfd;
connection_id = ci->cid;

while(1)
{
if((x = recv(sfd_client, data, size_packet, 0)) == 0)
{
fprintf(stderr, "client closed/terminated. closing connection.\n");
break;
}

shp = ntohp(data);

if(shp->type == TERM)
break;

if(shp->conid == -1)
shp->conid = connection_id;

if(shp->type == REQU)
{
switch(shp->comid)
{
case PWD:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_pwd(shp, data, sfd_client, lpwd);
break;
case CD:
if((x = chdir(shp->buffer)) == -1)
fprintf(stderr, "Wrong path.\n");
command_cd(shp, data, sfd_client, x == -1 ? "fail" : "success");
break;
case MKDIR:
command_mkdir(shp, data, sfd_client);
break;
case LS:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_ls(shp, data, sfd_client, lpwd);
break;
case GET:
command_get(shp, data, sfd_client);
break;
case PUT:
command_put(shp, data, sfd_client);
break;
case RGET:
if(!getcwd(lpwd, sizeof lpwd))
er("getcwd()", 0);
command_rget(shp, data, sfd_client);
send_EOT(shp, data, sfd_client);
if((x = chdir(lpwd)) == -1)
fprintf(stderr, "Wrong path.\n");
default:
// print error
break;
}
/*
//send info and then proceed to complete the request
shp->type = INFO;
strcpy(path, shp->buffer);
sprintf(shp->buffer, "File found. Processing...");
data = htonp(shp);
if((x = send(sfd_client, data, size_packet, 0)) != size_packet)
er("send()", x);
send_file(path, sfd_client, shp);
send_TERM(sfd_client, shp);
*/
}
else
{
//show error, send TERM and break
fprintf(stderr, "packet incomprihensible. closing connection.\n");
send_TERM(shp, data, sfd_client);
break;
}
}

close(sfd_client);
fflush(stdout);
}
Beispiel #6
0
void* serve_client(void* info)
{
	int sfd_client, connection_id, x;
	struct packet shp;
	char lpwd[LENBUFFER];
	struct client_info* ci = (struct client_info*) info;
	sfd_client = ci->sfd;
	connection_id = ci->cid;
	
	while(1)
	{
		if(recv_packet_ret(sfd_client, &shp) < 0)
		{
			fprintf(stderr, "client ID(%d) closed/terminated. closing connection.\n", connection_id);
			break;
		}

		if(shp.type == TERM)
			break;

		if(shp.conid == -1)
			shp.conid = connection_id;
		
		if(shp.type == REQU)
		{
			switch(shp.comid)
			{
				case PWD:
					if(!getcwd(lpwd, sizeof lpwd))
						er("getcwd()", 0);
					command_pwd(sfd_client, &shp, lpwd);
					break;
				case CD:
					if((x = chdir(shp.buffer)) == -1)
						fprintf(stderr, "Wrong path.\n");
					command_cd(sfd_client, &shp, x == -1 ? "fail" : "success");
					break;
				case MKDIR:
					command_mkdir(sfd_client, &shp);
					break;
				case LS:
					if(!getcwd(lpwd, sizeof lpwd))
						er("getcwd()", 0);
					command_ls(sfd_client, &shp, lpwd);
					break;
				case GET:
					command_get(sfd_client, &shp);
					break;
				case PUT:
					command_put(sfd_client, &shp);
					break;
				case RGET:
					if(!getcwd(lpwd, sizeof lpwd))
						er("getcwd()", 0);
					command_rget(sfd_client, &shp);
					send_EOT(sfd_client, &shp);
					if((x = chdir(lpwd)) == -1)
						fprintf(stderr, "Wrong path.\n");
					break;
				default:
					// print error
					break;
			}
		}
		else
		{
			//show error, send TERM and break
			fprintf(stderr, "packet incomprihensible. closing connection.\n");
			send_TERM(sfd_client, &shp);
			break;
		}
	}

	close(sfd_client);
	fflush(stdout);
	return NULL;
}