Permalink
Browse files

Anfang einer verketteten Liste der usr-Structs

  • Loading branch information...
1 parent 1aa5445 commit 26765142cca711f0416c6ed1f6bcd3d26340f6f3 Robert A committed Jun 6, 2009
Showing with 59 additions and 31 deletions.
  1. +59 −31 duoit.c
View
90 duoit.c
@@ -13,7 +13,7 @@
#define CPORT "3021" // the port users will be connecting to
#define DPORT "3020" // the default port for data transfering
#define BACKLOG 10 // how many pending connections queue will hold
-#define PATHLENGTH 256
+#define PATHLENGTH 256 //default pathlength for pwd-command - may be dynamically increased later
//struct for command and parameter pointers
typedef struct {
@@ -40,15 +40,15 @@ void *get_in_addr(struct sockaddr *sa){
}
/*
-* Initialize the Server
-*
-* creates socket, binds port and listens to it
-*/
+ * Initialize the Server
+ *
+ * creates socket, binds port and listens to it
+ */
int initialize_server() {
int sock_fd;
struct addrinfo hints, *servinfo, *p;
int yes=1, rv;
-
+
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
@@ -94,33 +94,60 @@ int initialize_server() {
}
printf("server: waiting for connections (port %s) ...\n", CPORT);
-
+
return sock_fd;
}
+/*
+ * initialize head of user-list
+ */
+int ini_user_list(int newfd, usr **user) {
+ *user = calloc(1, sizeof(struct usr));
+ (*user)->csocket = newfd;
+ (*user)->pwd = calloc(1, PATHLENGTH);
+ (*user)->pwd_len = PATHLENGTH;
+ return 0;
+}
+int append_user(){
+}
/*
-* Handle new connection
-*
-* accepts new connections and adds them to the file discriptors
-*/
-void handle_new_connection(int *listener, fd_set *master, int *fdmax) {
+ * Handle new connection
+ *
+ * accepts new connections and adds them to the file discriptors
+ */
+void handle_new_connection(int *listener, fd_set *master, int *fdmax, usr **user) {
socklen_t addrlen;
struct sockaddr_storage remoteaddr;
int newfd; // newly accept()ed socket descriptor
char remoteIP[INET6_ADDRSTRLEN];
-
+
addrlen = sizeof remoteaddr;
newfd = accept(*listener, (struct sockaddr *)&remoteaddr, &addrlen);
-
+
if (newfd == -1) {
perror("accept");
} else {
FD_SET(newfd, master); // add to master set
if (newfd > *fdmax) { // keep track of the max
*fdmax = newfd;
}
-
+
+ //adapt the user list
+ if (!(*user)) {
+ //initialize head of user-list
+ ini_user_list(newfd, user);
+ } else {
+ while (user->next) {
+ user = user->next;
+ }
+ //append user to user-list
+ user->next = calloc(1, sizeof(struct usr));
+ user = user->next;
+ user->csocket = newfd;
+ user->pwd = calloc(1, PATHLENGTH);
+ user->pwd_len = PATHLENGTH;
+ }
send_message(newfd, "220 Welcome to duoit FTP-Server. Service ready.");
inet_ntop(remoteaddr.ss_family, get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN);
printf("new connection from %s on socket %d\n", remoteIP, newfd);
@@ -129,10 +156,10 @@ void handle_new_connection(int *listener, fd_set *master, int *fdmax) {
/*
-* close connection
-*
-* closes connection and removes it from the file discriptors list
-*/
+ * close connection
+ *
+ * closes connection and removes it from the file discriptors list
+ */
void close_connection(int socket, fd_set *master) {
close(socket);
FD_CLR(socket, master);
@@ -171,17 +198,17 @@ int send_message(int socket, char *message) {
* quits the connection with client
*/
int quit_command(instruc *instruction, fd_set *master) {
-
+
send_message(instruction->socket, "221 Goodbye.");
close_connection(instruction->socket, master);
return 0;
}
/*
-* dispatch message
-*
-* TODO
-*/
+ * dispatch message
+ *
+ * TODO
+ */
int dispatch(int socket, char *received, int received_len, fd_set *master) {
instruc instruction;
@@ -248,17 +275,18 @@ int parse_command(char *received, int received_len, instruc *instruction) {
}
/*
-* Main programm
-*
-* accepts new connections, receives new messages and dispatches them
-*/
+ * Main programm
+ *
+ * accepts new connections, receives new messages and dispatches them
+ */
int main(int argc, char *argv[]) {
struct sockaddr_storage client_addr; //client's address information
socklen_t sin_size;
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
+ usr *user;
int i, nbytes;
char buf[256];
@@ -285,9 +313,9 @@ int main(int argc, char *argv[]) {
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == listener) {
- handle_new_connection(&listener, &master, &fdmax);
+ handle_new_connection(&listener, &master, &fdmax, user);
} else {
-
+
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
@@ -300,7 +328,7 @@ int main(int argc, char *argv[]) {
} else {
dispatch(i, buf, nbytes, &master);
}
-
+
}
}
}

0 comments on commit 2676514

Please sign in to comment.