Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

datenport und passive-flag in user Struktur verwaltung

  • Loading branch information...
commit 46a009248fdc085d44eb52094468dcfcdfc9b0ca 1 parent 48890ea
Robert A authored
Showing with 32 additions and 1 deletion.
  1. +32 −1 duoit.c
View
33 duoit.c
@@ -26,6 +26,8 @@ typedef struct {
typedef struct usr {
int csocket;
int dsocket;
+ int dport;
+ int passv;
char *pwd;
int pwd_len;
struct usr *next;
@@ -104,6 +106,9 @@ int initialize_server() {
int ini_user_list(int newfd, usr **user) {
*user = calloc(1, sizeof(struct usr));
(*user)->csocket = newfd;
+ (*user)->dsocket = 0;
+ (*user)->dport = get_port(newfd);
+ (*user)->passv = 0;
(*user)->pwd = calloc(1, PATHLENGTH);
(*user)->pwd_len = PATHLENGTH;
(*user)->next = NULL;
@@ -122,6 +127,9 @@ int append_user(int newfd, usr *user){
user->next = calloc(1, sizeof(struct usr));
user = user->next;
user->csocket = newfd;
+ user->dsocket = 0;
+ user->dport = get_port(newfd);
+ user->passv = 0;
user->pwd = calloc(1, PATHLENGTH);
user->pwd_len = PATHLENGTH;
user->next = NULL;
@@ -129,7 +137,7 @@ int append_user(int newfd, usr *user){
}
/*
- * find user by socket number
+ * find user by csocket number
*/
int find_user(int socket, usr *head, usr **result) {
while (head) {
@@ -272,6 +280,29 @@ int quit_command(instruc *instruction, usr *user, fd_set *master) {
}
/*
+ * find the PORT of the user's socket
+ */
+
+int get_port(usr *user) {
+ socklen_t len;
+ struct sockaddr_storage addr;
+ int port;
+
+ len = sizeof(struct sockaddr_storage);
+ getpeername(user->csocket, (struct sockaddr*)&addr, &len);
+ //deal with both IPv4 and IPv6
+ if (addr.ss_family == AF_INET) {
+ struct sockaddr_in *sock = (struct sockaddr_in *)&addr;
+ port = ntohs(sock->sin_port);
+ } else { //AF_INET6
+ struct sockaddr_in6 *sock = (struct sockaddr_in6 *)&addr;
+ port = ntohs(sock->sin6_port);
+ }
+
+ return port;
+}
+
+/*
* fills the ip-string with the formatted IP[PORT] of the corresponding user
*/
int get_ip_port(usr *user, char *ip, int size) {
Please sign in to comment.
Something went wrong with that request. Please try again.