Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

separate functions for IP and PORT determination from socket

  • Loading branch information...
commit 8e3362d6d466cd4899a6bd0667de06ac6ed9cb72 1 parent 54b9acf
Robert A authored
Showing with 5 additions and 12 deletions.
  1. +5 −12 duoit.c
View
17 duoit.c
@@ -304,19 +304,18 @@ int get_port(int sockfd) {
struct sockaddr_in6 *sock = (struct sockaddr_in6 *)&addr;
port = ntohs(sock->sin6_port);
}
-
+
+ //sprintf(str_port, "%d", 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) {
+int get_ip(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)) {
@@ -328,19 +327,13 @@ int get_ip_port(usr *user, char *ip, int size) {
//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);
inet_ntop(AF_INET, &sock->sin_addr, ipstr, sizeof ipstr);
} else { //AF_INET6
struct sockaddr_in6 *sock = (struct sockaddr_in6 *)&addr;
- port = ntohs(sock->sin6_port);
inet_ntop(AF_INET6, &sock->sin6_addr, ipstr, sizeof ipstr);
}
strcpy(ip, ipstr);
- strcat(ip, "[");
- sprintf(str_port, "%d", port);
- strcat(ip, str_port);
- strcat(ip, "]");
return 0;
}
@@ -350,11 +343,11 @@ int log_action(usr *user, char *message) {
char ip[INET6_ADDRSTRLEN + 5];
curtime = time(NULL);
- if (get_ip_port(user, ip, sizeof ip)) {
+ if (get_ip(user, ip, sizeof ip)) {
return 1;
}
strftime(log_time, 256, "%H:%M", localtime(&curtime));
- printf("*LOG* %s - %s - %s\n", log_time, ip, message);
+ printf("*LOG* %s - %s[%d] - %s\n", log_time, ip, get_port(user->csocket), message);
return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.