Skip to content

Commit

Permalink
base
Browse files Browse the repository at this point in the history
  • Loading branch information
selbetar committed Apr 9, 2020
1 parent e4864e9 commit 9a3195c
Show file tree
Hide file tree
Showing 9 changed files with 235 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf

.directory
.clang-format
.projectile
.vscode
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
all: ftp

#The following lines contain the generic build options
CC=gcc
CPPFLAGS=
CFLAGS=-g -Werror-implicit-function-declaration

#List all the .o files here that need to be linked
OBJS=ftp.o network.o


network.o: network.c network.h common.h

ftp.o: ftp.c ftp.h common.h network.h


ftp: $(OBJS)
$(CC) -o ftp $(OBJS) $(CLIBS)

clean:
rm -f *.o
rm -f ftp
7 changes: 7 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __COMMON_H__
#define __COMMON_H__


#define BUF_SIZE 1024

#endif
119 changes: 119 additions & 0 deletions ftp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "ftp.h"
#include "network.h"
#include <strings.h>
unsigned int sfd; // Control Connection fd

int main (int argc, char *argv[])
{
char line[MAX_LINE]; // stores terminal commands
char *command, *parameter;
int c;
// 1 program
// 2 server
// 3 port
if (argc < 2 || argc > 3) {
fprintf (stderr, "Usage: %s <host> <port>\n", argv[0]);
fprintf (stderr, " <host> The address of the ftp server.\n");
fprintf (stderr, " <port> Optional: Specifies the port the client should initiate connection on.\n");
return -1;
}

const char *port = argc == 3 ? argv[2] : DEFAULT_PORT;

sfd = create_connection (argv[1], port);

if (sfd < 0)
exit (EXIT_FAILURE);

char buffer[BUFSIZ];
memset (buffer, 0, BUFSIZ);

if (read_response (buffer) <= 0)
exit (EXIT_FAILURE);

printf ("--> %s", buffer);

while (1) {
// show prompt, but only if input comes from a terminal
if (isatty (STDIN_FILENO))
printf ("> ");

if (!fgets (line, sizeof (line), stdin))
break;

if (!strchr (line, '\n')) {
while ((c = fgetc (stdin)) != EOF && c != '\n')
;
if (c == '\n') {
//printErrorCommmandTooLong(stdout);
fprintf (stdout, "%s", "ok");
continue;
}
}

command = strtok (line, " \t\n\f\r\v");
if (!command)
continue; // ignore blank lines;

// get parameters if provided
parameter = strtok (NULL, "\n\r");

int scode = executeCommand (command, parameter);
}
return 0;
}

int executeCommand (const char *command, const char *parameter)
{
if (strcasecmp (command, "quit") == 0) {
// todo
}
else if (strcasecmp (command, "user") == 0) {
// todo
}
else if (strcasecmp (command, "pass") == 0) {
// todo
}
else if (strcasecmp (command, "get") == 0) {
// todo
}
else if (strcasecmp (command, "features") == 0) {
// todo
}
else if (strcasecmp (command, "cd") == 0) {
// todo
}
else if (strcasecmp (command, "nlist") == 0) {
// todo
}
else if (strcasecmp (command, "pwd") == 0) {
// todo
}
else if (strcasecmp (command, "put") == 0) {
// todo
}
else if (strcasecmp (command, "delete") == 0) {
// todo
}
else {
// todo
}
}

int send_msg (const char *msg)
{
}

int read_response (char *buf)
{
ssize_t length = read (sfd, buf, BUFSIZ);
return length;
}

// pasvr_t getPasvData()
// {
// }

// void flush()
// {
// }
15 changes: 15 additions & 0 deletions ftp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef __FTCLIENT_H__
#define __FTCLIENT_H__

#define DEFAULT_PORT "21"
#define MAX_LINE 256

typedef struct pasv_response {

} pasvr_t;

int read_response (char *buf);
int send_msg(const char *msg);
int executeCommand(const char *command, const char *parameter);

#endif
51 changes: 51 additions & 0 deletions network.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "network.h"
#include "common.h"

int create_connection (const char *host, const char *port)
{
struct addrinfo hints;
struct addrinfo *result, *rp;
int sfd, s, j;
size_t len;
ssize_t nread;
char buf[BUF_SIZE];

/* Obtain address(es) matching host/port */
memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_INET; /* Allow IPv4 */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0; /* Any protocol */

s = getaddrinfo (host, port, &hints, &result);
if (s != 0) {
fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
return -1;
}

/* getaddrinfo() returns a list of address structures.
Try each address until we successfully connect(2).
If socket(2) (or connect(2)) fails, we (close the socket
and) try the next address. */

for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket (rp->ai_family, rp->ai_socktype,
rp->ai_protocol);
if (sfd == -1)
continue;

if (connect (sfd, rp->ai_addr, rp->ai_addrlen) != -1)
break; /* Success */

close (sfd);
}

if (rp == NULL) { /* No address succeeded */
fprintf (stderr, "Could not connect\n");
return -1;
}

freeaddrinfo (result); /* No longer needed */

return sfd;
}
14 changes: 14 additions & 0 deletions network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __NETWORK_H__
#define __NETWORK_H__

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

int create_connection (const char *host, const char *port);

#endif
1 change: 1 addition & 0 deletions printRoutines.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions printRoutines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 9a3195c

Please sign in to comment.