libft changes while cleaning ping.c
This commit is contained in:
parent
a3f0bb0db8
commit
80837f0428
11 changed files with 61 additions and 114 deletions
|
|
@ -131,6 +131,7 @@ mem/ft_memdel.c\
|
||||||
mem/ft_memmove.c\
|
mem/ft_memmove.c\
|
||||||
mem/ft_memset.c\
|
mem/ft_memset.c\
|
||||||
mem/ft_realloc.c\
|
mem/ft_realloc.c\
|
||||||
|
net/cksum.c\
|
||||||
net/create_client.c\
|
net/create_client.c\
|
||||||
net/create_server.c\
|
net/create_server.c\
|
||||||
net/forge_ip.c\
|
net/forge_ip.c\
|
||||||
|
|
@ -203,10 +204,7 @@ str/ft_strsub.c\
|
||||||
str/ft_strtok.c\
|
str/ft_strtok.c\
|
||||||
str/ft_strtrim.c\
|
str/ft_strtrim.c\
|
||||||
sys/open_new.c\
|
sys/open_new.c\
|
||||||
time/epoch.c\
|
time/epoch.c
|
||||||
time/ft_mytime_free.c\
|
|
||||||
time/ft_mytime_get.c\
|
|
||||||
time/ft_time_isrecent.c
|
|
||||||
|
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/07 13:49:04 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,21 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/20 15:41:59 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
|
#ifndef MATH_H
|
||||||
# define MATH_H
|
# define MATH_H
|
||||||
|
|
||||||
# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n')
|
# define FT_WS(x) (x == ' ' || x == '\t' || x == '\n')
|
||||||
# define FT_ABS(x) (((x) < 0) ? -(x) : (x))
|
# define FT_ABS(x) (((x) < 0) ? -(x) : (x))
|
||||||
# define FT_NEG(x) ((x) < 0)
|
# define FT_NEG(x) ((x) < 0)
|
||||||
# define FT_POS(x) ((x) > 0)
|
# define FT_POS(x) ((x) > 0)
|
||||||
# define FT_MIN(a, b) ((a) < (b) ? (a) : (b))
|
# define FT_MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||||
# define FT_MAX(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_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(int n);
|
||||||
char *ft_itoa_base(int nbr, char *base, char *flags);
|
char *ft_itoa_base(int nbr, char *base, char *flags);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/25 11:43:12 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"
|
# include "libft.h"
|
||||||
|
|
||||||
struct s_mytime
|
uint64_t epoch_micro(void);
|
||||||
{
|
double time_milli(void);
|
||||||
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);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/10/07 18:06:12 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"
|
# include "mytime.h"
|
||||||
|
|
||||||
int create_server(int port, int backlog, char *protoname);
|
unsigned short cksum(void *b, int len);
|
||||||
int create_client(char *addr, int port, char *protoname);
|
int create_server(int port, int backlog, char *protoname);
|
||||||
void listener(int domain, int sock, int proto,
|
int create_client(char *addr, int port, char *protoname);
|
||||||
void (*handler)(void *buf, int bytes, struct sockaddr_in *addr));
|
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(int sock, char *msg, int size);
|
||||||
int net_send_large(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(int sock, char *msg, int size);
|
||||||
int net_get_fd(int sock, int fd, int size);
|
int net_get_fd(int sock, int fd, int size);
|
||||||
int net_get_large(int sock, int fd);
|
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 tcphdr_init(struct tcphdr *header);
|
||||||
void iphdr_init(struct ip *header);
|
void iphdr_init(struct ip *header);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/04/02 15:08:56 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,31 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_mytime_free.c :+: :+: :+: */
|
/* cksum.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/25 11:45:29 by jhalford #+# #+# */
|
/* Created: 2017/10/08 12:45:43 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/02/18 13:11:33 by jhalford ### ########.fr */
|
/* 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);
|
unsigned short *buf;
|
||||||
ft_strdel(&(*time)->month);
|
unsigned int sum;
|
||||||
ft_strdel(&(*time)->day);
|
|
||||||
ft_strdel(&(*time)->hour);
|
buf = b;
|
||||||
ft_strdel(&(*time)->min);
|
sum = 0;
|
||||||
ft_strdel(&(*time)->sec);
|
while (len > 1)
|
||||||
ft_memdel((void **)time);
|
{
|
||||||
|
sum += *((unsigned short*)buf++);
|
||||||
|
len -= 2;
|
||||||
|
}
|
||||||
|
if (len == 1)
|
||||||
|
sum += *(unsigned char*)buf;
|
||||||
|
sum = (sum >> 16) + (sum & 0xFFFF);
|
||||||
|
return (~(sum + (sum >> 16)));
|
||||||
}
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/10/07 17:59:28 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/10/07 17:58:42 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"
|
#include "mytime.h"
|
||||||
|
|
||||||
size_t epoch_micro(void)
|
uint64_t epoch_micro(void)
|
||||||
{
|
{
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
|
|
||||||
|
|
@ -20,3 +20,12 @@ size_t epoch_micro(void)
|
||||||
return (0);
|
return (0);
|
||||||
return (tv.tv_sec * 1000000 + tv.tv_usec);
|
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.));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_getstrtime.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_time_isrecent.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue