Permalink
Browse files

logging improved (look & security)

  • Loading branch information...
1 parent e1201d7 commit 807461ffc92f57b6d39fe3d77384448b0872795a Robert A committed Jun 7, 2009
Showing with 23 additions and 7 deletions.
  1. +23 −7 duoit.c
View
@@ -205,7 +205,7 @@ int format_command(char *command, int size) {
/*
* send messages to socket
*
- * TODO: prüfen was gesendet wurd, send_all
+ * TODO: prüfen was gesendet wurde, send_all
*/
int send_message(int socket, char *message) {
char reply[strlen(message) + 2];
@@ -217,6 +217,7 @@ int send_message(int socket, char *message) {
/*
* quits the connection with client
+ * TODO: usr struct freigeben
*/
int quit_command(instruc *instruction, usr *user, fd_set *master) {
@@ -225,13 +226,21 @@ int quit_command(instruc *instruction, usr *user, fd_set *master) {
return 0;
}
-void get_ip_port(usr *user, char *ip) {
+/*
+ * fills the ip-string with the formatted IP[PORT] of the corresponding user
+ */
+int get_ip_port(usr *user, char *ip, int size) {
socklen_t len;
struct sockaddr_storage addr;
char ipstr[INET6_ADDRSTRLEN + 5];
char str_port[4];
int port;
+ //check whether buffer is big enough
+ if(size < (INET6_ADDRSTRLEN + 5)) {
+ return 1;
+ }
+
len = sizeof(struct sockaddr_storage);
getpeername(user->csocket, (struct sockaddr*)&addr, &len);
//deal with both IPv4 and IPv6
@@ -246,20 +255,25 @@ void get_ip_port(usr *user, char *ip) {
}
strcpy(ip, ipstr);
- strcat(ip, ":");
+ strcat(ip, "[");
sprintf(str_port, "%d", port);
strcat(ip, str_port);
+ strcat(ip, "]");
+ return 0;
}
-void log_action(usr *user, char *message) {
+int log_action(usr *user, char *message) {
char log_time[256];
time_t curtime;
char ip[INET6_ADDRSTRLEN + 5];
curtime = time(NULL);
- get_ip_port(user, ip);
+ if (get_ip_port(user, ip, sizeof ip)) {
+ return 1;
+ }
strftime(log_time, 256, "%H:%M", localtime(&curtime));
- printf("%s - %s - %s\n", log_time, ip, message);
+ printf("*LOG* %s - %s - %s\n", log_time, ip, message);
+ return 0;
}
@@ -386,7 +400,9 @@ int main(int argc, char *argv[]) {
close_connection(i, &master);
} else {
find_user(i, user, &current);
- log_action(current, "Zauberei!\n");
+ if (log_action(current, "Zauberei!")) {
+ printf("*ERR* logging error\n");
+ }
dispatch(current, buf, nbytes, &master);
}

0 comments on commit 807461f

Please sign in to comment.