refactor for docker ubuntu, no more libft

This commit is contained in:
Jack Halford 2017-11-27 18:15:35 +00:00
parent dff0d5f79e
commit ee26baea8a
8 changed files with 254 additions and 51 deletions

View file

@ -15,6 +15,7 @@ SHELL := bash
NAME = ft_ping
CC = gcc
RM = rm -rf 2>&-
W_FLAGS = -Wall -Wextra -Werror
D_FLAGS =
FLAGS = $(W_FLAGS) $(D_FLAGS)
@ -22,16 +23,16 @@ FLAGS = $(W_FLAGS) $(D_FLAGS)
LEN_NAME = `printf "%s" $(NAME) |wc -c`
DELTA = $$(echo "$$(tput cols)-31-$(LEN_NAME)"|bc)
LIBFT_DIR = libft/
LIBFT_LIB = $(LIBFT_DIR)libft.a
LIBFT_INC = $(LIBFT_DIR)includes/
SRC_DIR = srcs/
INC_DIR = includes/
SRC_DIR = ./
INC_DIR = ./
OBJ_DIR = objs/
SRC_BASE = \
ping.c\
create_client.c\
epoch.c\
cksum.c\
rs.c\
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
@ -39,60 +40,29 @@ NB = $(words $(SRC_BASE))
INDEX = 0
all :
@make -C $(LIBFT_DIR)
@make -j $(NAME)
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ)
@$(CC) $(OBJS) -o $@ \
$(NAME): $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ)
$(CC) $(OBJS) -o $@ \
-I $(INC_DIR) \
-I $(LIBFT_INC) \
$(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) \
$(CLIENT_OBJ) $(FLAGS) \
-lm
@printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n"
@sudo setcap cap_net_raw+ep $@
@printf "\r\033[38;5;117m✓ SETCAP $@ \033[0m\033[K\n"
$(LIBFT_LIB):
@make -C $(LIBFT_DIR)
# sudo setcap cap_net_raw+ep $@ 2>&-
$(OBJ_DIR) :
@mkdir -p $(OBJ_DIR)
$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
@$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB)))))
@$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB)))))
@$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB) - 1))))
@$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}"))
@printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) ft_p $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')"
@$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\
-I $(INC_DIR)\
-I $(LIBFT_INC)
@$(eval INDEX=$(shell echo $$(($(INDEX)+1))))
$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@ -I $(INC_DIR)
clean: cleanlib
@if [ -e $(OBJ_DIR) ]; then \
rm -rf $(OBJ_DIR); \
printf "\r\033[38;5;202m✗ clean $(NAME) \033[0m\033[K\n"; \
fi;
cleanlib:
@make -C $(LIBFT_DIR) clean
fclean: clean fcleanlib
@for file in $(NAME); do \
if [ -e $$file ]; then \
rm -f $$file ; \
printf "\r\033[38;5;196m✗ fclean $$file\033[0m\033[K\n"; \
fi; \
done;
fcleanlib: cleanlib
@make -C $(LIBFT_DIR) fclean
clean:
$(RM) $(OBJ_DIR)
re: fclean all
relib: fcleanlib $(LIBFT_LIB)
fclean: clean
$(RM) $(NAME)
.PHONY : fclean clean re relib cleanlib fcleanlib
.PHONY : fclean clean re
-include $(OBJS:.o=.d)

31
ping/cksum.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cksum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/08 12:45:43 by jhalford #+# #+# */
/* Updated: 2017/10/08 12:48:41 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ping.h"
unsigned short cksum(void *b, int len)
{
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)));
}

59
ping/create_client.c Normal file
View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:59:28 by jhalford #+# #+# */
/* Updated: 2017/10/08 13:33:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ping.h"
int create_client(char *addr, int port, char *protoname)
{
int sock;
struct protoent *proto;
struct sockaddr_in sin;
if (!(proto = getprotobyname(protoname)))
return (-1);
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = inet_addr(addr);
if (connect(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0)
return (-1);
return (sock);
}
void listener(int domain, int sock, int proto,
void (*handler)(void *buf, int bytes, struct sockaddr *addr))
{
int sd;
struct sockaddr addr;
unsigned char buf[1024];
int bytes;
socklen_t len;
len = sizeof(addr);
sd = socket(domain, sock, proto);
if (sd < 0)
{
perror("listener socket");
exit(0);
}
while (1)
{
bzero(buf, sizeof(buf));
bytes = recvfrom(sd, buf, sizeof(buf), 0,
&addr, &len);
if (bytes > 0 && handler)
handler(buf, bytes, &addr);
else
perror("recvfrom");
}
exit(0);
}

31
ping/epoch.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* epoch.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:58:42 by jhalford #+# #+# */
/* Updated: 2017/10/08 13:15:50 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ping.h"
uint64_t epoch_micro(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL))
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)(tv.tv_sec * 1000. + (double)tv.tv_usec / 1000.));
}

View file

@ -103,7 +103,7 @@ void ping(int signo)
hdr->icmp_seq = ++g_ping.pkt_sent;
msg = (double*)(pkt + sizeof(struct icmp));
epoch = time_milli();
ft_memcpy(msg, (void*)&epoch, sizeof(double));
memcpy(msg, (void*)&epoch, sizeof(double));
hdr->icmp_cksum = cksum(&pkt, sizeof(pkt));
sendto(g_ping.sock, &pkt, sizeof(pkt), 0, g_ping.sa->ai_addr,
sizeof(struct sockaddr));
@ -130,7 +130,7 @@ int main(int ac, char **av)
{
if (ac != 2)
{
ft_usage("%s <addr>\n", av[0]);
dprintf(2, "%s <addr>\n", av[0]);
exit(1);
}
resolve_host(av[1], &g_ping);

View file

@ -13,18 +13,27 @@
#ifndef FT_PING_H
# define FT_PING_H
# include "libft.h"
# include "rs.h"
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# include <sys/types.h>
# include <fcntl.h>
# include <errno.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <resolv.h>
# include <netdb.h>
# include <sys/socket.h>
# include <arpa/inet.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/ip_icmp.h>
# include <sys/wait.h>
# define FT_PCT(a, t) (t ? 100 * (float)(t - a)/(float)t : 0)
typedef struct s_ping t_ping;
struct s_ping
@ -47,4 +56,9 @@ struct s_ping
extern t_ping g_ping;
unsigned short cksum(void *b, int len);
double time_milli(void);
void listener(int domain, int sock, int proto,
void (*handler)(void *buf, int bytes, struct sockaddr *addr));
#endif

61
ping/rs.c Normal file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:57:54 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:19:47 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "rs.h"
#include <math.h>
double sqrt(double x);
void rs_init(t_rs *rs)
{
bzero(rs, sizeof(t_rs));
rs->count = 0;
rs->min = DBL_MAX;
rs->max = -DBL_MAX;
}
void rs_push(t_rs *rs, double n)
{
double delta;
rs->count++;
n < rs->min ? rs->min = n : (0);
n > rs->max ? rs->max = n : (0);
if (rs->count == 1)
{
rs->avg = n;
rs->m = 0;
}
else
{
delta = n - rs->avg;
rs->avg += delta / rs->count;
rs->m += delta * (n - rs->avg);
}
}
void rs_final(t_rs *rs)
{
if (rs->count == 0)
{
rs->min = 0;
rs->max = 0;
}
if (rs->count < 2)
{
rs->var = 0;
rs->stdev = 0;
return ;
}
rs->var = rs->m / (rs->count - 1);
rs->stdev = sqrt(rs->var);
}

37
ping/rs.h Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rs.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:05:30 by jhalford #+# #+# */
/* Updated: 2017/10/08 15:59:58 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_RS_H
# define LIBFT_RS_H
# include <strings.h>
# include <float.h>
# include <dlfcn.h>
# include <math.h>
typedef struct s_rs t_rs;
struct s_rs {
int count;
double min;
double max;
double avg;
double m;
double stdev;
double var;
};
void rs_init(t_rs *rs);
void rs_push(t_rs *rs, double n);
void rs_final(t_rs *rs);
#endif