From 80837f042884bf9b6c6562fba01d57f608831229 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Sun, 8 Oct 2017 14:26:42 +0200 Subject: [PATCH] libft changes while cleaning ping.c --- libft/Makefile | 6 ++-- libft/includes/libft.h | 2 +- libft/includes/math.h | 17 +++++----- libft/includes/mytime.h | 20 ++---------- libft/includes/net.h | 27 ++++++++-------- libft/srcs/error/ft_usage.c | 2 +- .../{time/ft_mytime_free.c => net/cksum.c} | 31 ++++++++++++------- libft/srcs/net/create_client.c | 2 +- libft/srcs/time/epoch.c | 13 ++++++-- libft/srcs/time/ft_mytime_get.c | 31 ------------------- libft/srcs/time/ft_time_isrecent.c | 24 -------------- 11 files changed, 61 insertions(+), 114 deletions(-) rename libft/srcs/{time/ft_mytime_free.c => net/cksum.c} (58%) delete mode 100644 libft/srcs/time/ft_mytime_get.c delete mode 100644 libft/srcs/time/ft_time_isrecent.c diff --git a/libft/Makefile b/libft/Makefile index 893f7397..6b54b9eb 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -131,6 +131,7 @@ mem/ft_memdel.c\ mem/ft_memmove.c\ mem/ft_memset.c\ mem/ft_realloc.c\ +net/cksum.c\ net/create_client.c\ net/create_server.c\ net/forge_ip.c\ @@ -203,10 +204,7 @@ str/ft_strsub.c\ str/ft_strtok.c\ str/ft_strtrim.c\ sys/open_new.c\ -time/epoch.c\ -time/ft_mytime_free.c\ -time/ft_mytime_get.c\ -time/ft_time_isrecent.c +time/epoch.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 1b4b5402..47becf5c 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:07:31 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 12:56:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/includes/math.h b/libft/includes/math.h index 4b87935e..a42049e1 100644 --- a/libft/includes/math.h +++ b/libft/includes/math.h @@ -6,20 +6,21 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:41:59 by jhalford #+# #+# */ -/* Updated: 2017/05/16 17:30:35 by ariard ### ########.fr */ +/* Updated: 2017/10/08 12:57:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MATH_H # define MATH_H -# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n') -# define FT_ABS(x) (((x) < 0) ? -(x) : (x)) -# define FT_NEG(x) ((x) < 0) -# define FT_POS(x) ((x) > 0) -# define FT_MIN(a, b) ((a) < (b) ? (a) : (b)) -# define FT_MAX(a, b) ((a) > (b) ? (a) : (b)) -# define FT_DIST(a, b) FT_ABS((a) - (b)) +# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n') +# define FT_ABS(x) (((x) < 0) ? -(x) : (x)) +# define FT_NEG(x) ((x) < 0) +# define FT_POS(x) ((x) > 0) +# define FT_MIN(a, b) ((a) < (b) ? (a) : (b)) +# define FT_MAX(a, b) ((a) > (b) ? (a) : (b)) +# define FT_DIST(a, b) FT_ABS((a) - (b)) +# define FT_PCT(a, t) (t ? 100 * (float)(t - a)/(float)t : 0) char *ft_itoa(int n); char *ft_itoa_base(int nbr, char *base, char *flags); diff --git a/libft/includes/mytime.h b/libft/includes/mytime.h index 524d2973..ce8c9a83 100644 --- a/libft/includes/mytime.h +++ b/libft/includes/mytime.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/25 11:43:12 by jhalford #+# #+# */ -/* Updated: 2017/02/18 13:11:26 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 13:16:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,21 +17,7 @@ # include "libft.h" -struct s_mytime -{ - char *year; - char *month; - char *day; - char *hour; - char *min; - char *sec; -}; - -typedef struct s_mytime t_mytime; - -int ft_time_isrecent(time_t event); -t_mytime *ft_mytime_get(time_t epoch); -void ft_mytime_free(t_mytime **time); -size_t epoch_micro(void); +uint64_t epoch_micro(void); +double time_milli(void); #endif diff --git a/libft/includes/net.h b/libft/includes/net.h index 768ac0cd..b69b7e51 100644 --- a/libft/includes/net.h +++ b/libft/includes/net.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 18:06:12 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:14:42 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 14:36:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,20 +28,21 @@ # include "mytime.h" -int create_server(int port, int backlog, char *protoname); -int create_client(char *addr, int port, char *protoname); -void listener(int domain, int sock, int proto, - void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); +unsigned short cksum(void *b, int len); +int create_server(int port, int backlog, char *protoname); +int create_client(char *addr, int port, char *protoname); +void listener(int domain, int sock, int proto, + void (*handler)(void *buf, int bytes, struct sockaddr_in *addr)); -int net_send(int sock, char *msg, int size); -int net_send_large(int sock, char *msg, int size); -int net_get(int sock, char *msg, int size); -int net_get_fd(int sock, int fd, int size); -int net_get_large(int sock, int fd); +int net_send(int sock, char *msg, int size); +int net_send_large(int sock, char *msg, int size); +int net_get(int sock, char *msg, int size); +int net_get_fd(int sock, int fd, int size); +int net_get_large(int sock, int fd); -int reserve_port(int *port); +int reserve_port(int *port); -void tcphdr_init(struct tcphdr *header); -void iphdr_init(struct ip *header); +void tcphdr_init(struct tcphdr *header); +void iphdr_init(struct ip *header); #endif diff --git a/libft/srcs/error/ft_usage.c b/libft/srcs/error/ft_usage.c index 42add078..0c78fa12 100644 --- a/libft/srcs/error/ft_usage.c +++ b/libft/srcs/error/ft_usage.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/04/02 15:08:56 by jhalford #+# #+# */ -/* Updated: 2017/04/02 20:05:28 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 13:31:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/srcs/time/ft_mytime_free.c b/libft/srcs/net/cksum.c similarity index 58% rename from libft/srcs/time/ft_mytime_free.c rename to libft/srcs/net/cksum.c index c8a89efc..b44820eb 100644 --- a/libft/srcs/time/ft_mytime_free.c +++ b/libft/srcs/net/cksum.c @@ -1,24 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_mytime_free.c :+: :+: :+: */ +/* cksum.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/25 11:45:29 by jhalford #+# #+# */ -/* Updated: 2017/02/18 13:11:33 by jhalford ### ########.fr */ +/* Created: 2017/10/08 12:45:43 by jhalford #+# #+# */ +/* Updated: 2017/10/08 12:48:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "net.h" -void ft_mytime_free(t_mytime **time) +unsigned short cksum(void *b, int len) { - ft_strdel(&(*time)->year); - ft_strdel(&(*time)->month); - ft_strdel(&(*time)->day); - ft_strdel(&(*time)->hour); - ft_strdel(&(*time)->min); - ft_strdel(&(*time)->sec); - ft_memdel((void **)time); + unsigned short *buf; + unsigned int sum; + + buf = b; + sum = 0; + while (len > 1) + { + sum += *((unsigned short*)buf++); + len -= 2; + } + if (len == 1) + sum += *(unsigned char*)buf; + sum = (sum >> 16) + (sum & 0xFFFF); + return (~(sum + (sum >> 16))); } diff --git a/libft/srcs/net/create_client.c b/libft/srcs/net/create_client.c index 3cbd1730..5f5b8585 100644 --- a/libft/srcs/net/create_client.c +++ b/libft/srcs/net/create_client.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 17:59:28 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:02:25 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 13:33:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/srcs/time/epoch.c b/libft/srcs/time/epoch.c index b6d68119..46f1184e 100644 --- a/libft/srcs/time/epoch.c +++ b/libft/srcs/time/epoch.c @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 17:58:42 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:58:43 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 13:15:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "mytime.h" -size_t epoch_micro(void) +uint64_t epoch_micro(void) { struct timeval tv; @@ -20,3 +20,12 @@ size_t epoch_micro(void) return (0); return (tv.tv_sec * 1000000 + tv.tv_usec); } + +double time_milli(void) +{ + struct timeval tv; + + if (gettimeofday(&tv, NULL)) + return (0); + return ((double)((double)tv.tv_sec / 1000. + tv.tv_usec * 1000.)); +} diff --git a/libft/srcs/time/ft_mytime_get.c b/libft/srcs/time/ft_mytime_get.c deleted file mode 100644 index ab8f25b3..00000000 --- a/libft/srcs/time/ft_mytime_get.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_getstrtime.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/25 11:34:56 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:42:19 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -t_mytime *ft_mytime_get(time_t epoch) -{ - char *date; - t_mytime *time; - - time = (t_mytime*)ft_malloc(sizeof(*time)); - date = ctime(&epoch); - date[ft_strlen(date) - 1] = 0; - time->year = ft_isdigit(date[20]) ? - ft_strsub(date, 20, 4) : ft_strsub(date, 24, 5); - time->month = ft_strsub(date, 4, 3); - time->day = ft_strsub(date, 8, 2); - time->hour = ft_strsub(date, 11, 2); - time->min = ft_strsub(date, 14, 2); - time->sec = ft_strsub(date, 17, 2); - return (time); -} diff --git a/libft/srcs/time/ft_time_isrecent.c b/libft/srcs/time/ft_time_isrecent.c deleted file mode 100644 index ec195653..00000000 --- a/libft/srcs/time/ft_time_isrecent.c +++ /dev/null @@ -1,24 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_time_isrecent.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/03 18:01:04 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:44:24 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -int ft_time_isrecent(time_t event) -{ - time_t now; - - now = time(&now); - if (now - event >= 0 && now - event <= 6 * 365 / 12 * 24 * 60 * 60) - return (1); - else - return (0); -}