libft changes while cleaning ping.c

This commit is contained in:
Jack Halford 2017-10-08 14:26:42 +02:00
parent a3f0bb0db8
commit 80837f0428
11 changed files with 61 additions and 114 deletions

View file

@ -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))

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,20 +6,21 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -1,24 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_mytime_free.c :+: :+: :+: */
/* cksum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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.));
}

View file

@ -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);
}

View file

@ -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);
}