Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions C/serveur_TCP/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CC := gcc
CFLAGS := -Wall -Wextra -Werror -g -I include
SOURCE_DIR = src
OBJ_DIR := obj
BIN_DIR := .

SERVER_EXE := $(BIN_DIR)/server
SERVER_SRC := $(wildcard $(SOURCE_DIR)/server/*.c)
SERVER_OBJ := $(patsubst $(SOURCE_DIR)/server/%.c, \
$(OBJ_DIR)/server/%.o, $(SERVER_SRC))

CLIENT_EXE := $(BIN_DIR)/client
CLIENT_SRC := $(wildcard $(SOURCE_DIR)/client/*.c)
CLIENT_OBJ := $(patsubst $(SOURCE_DIR)/client/%.c, \
$(OBJ_DIR)/client/%.o, $(CLIENT_SRC))

all: $(SERVER_EXE) $(CLIENT_EXE)

# $^ represent all prerequists and $@ represent the target
$(SERVER_EXE): $(SERVER_OBJ)
$(CC) $^ -o $@

$(OBJ_DIR)/server/%.o: $(SOURCE_DIR)/server/%.c
$(CC) $(CFLAGS) -c $< -o $@

$(CLIENT_EXE): $(CLIENT_OBJ)
$(CC) $^ -o $@

$(OBJ_DIR)/client/%.o: $(SOURCE_DIR)/client/%.c
$(CC) $(CFLAGS) -c $< -o $@

clean:
@-rm $(SERVER_EXE) $(OBJ_DIR)/server/* 2> /dev/null
@-rm $(CLIENT_EXE) $(OBJ_DIR)/client/* 2> /dev/null
@echo "clean done !"
12 changes: 12 additions & 0 deletions C/serveur_TCP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Server_TCP
A simple TCP server written in C

You can compile server and client binaries with make and launch server in a terminal and client in another

PS: Launch server before client

You can see the readme in the src/server directory a tried to explain how it work as best as I was able too

If I made a mistake in my code or in my explanation please let me know.

I'm sorry too for my bad english I also done as best as I was able to
8 changes: 8 additions & 0 deletions C/serveur_TCP/include/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef GLOBAL_H
#define GLOBAL_H

#define IP INADDR_LOOPBACK
#define PORT 3000
#define SIZE 1024

#endif
9 changes: 9 additions & 0 deletions C/serveur_TCP/include/socket_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef SOCKET_CLIENT_H
#define SOCKET_CLIENT_H

struct sockaddr_in init_socket(int *fd);
struct sockaddr_in init_addr(void);
void connect_socket(int fd, struct sockaddr_in *addr);
void manage_socket(int fd);

#endif
8 changes: 8 additions & 0 deletions C/serveur_TCP/include/socket_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef SOCKET_SERVER_H
#define SOCKET_SERVER_H

void init_socket(int *fd);
void bind_s(int *fd, struct sockaddr_in *addr);
void manage_socket(int clientfd, struct sockaddr_in *client);

#endif
25 changes: 25 additions & 0 deletions C/serveur_TCP/src/client/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include "socket_client.h"
#include "global.h"

int main(void)
{
int fd;
struct sockaddr_in addr;

while (1)
{
addr = init_socket(&fd);
connect_socket(fd, &addr);
manage_socket(fd);
printf("Do you want to try another connection ? (y/n): ");
if (fgetc(stdin) == 'n')
break ;
fgetc(stdin);
}
}
63 changes: 63 additions & 0 deletions C/serveur_TCP/src/client/socket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include "socket_client.h"
#include "global.h"

struct sockaddr_in init_socket(int *fd)
{
*fd = socket(AF_INET, SOCK_STREAM, 0);
if (*fd == -1)
{
printf("Error in creation of the socket.\n");
exit(EXIT_FAILURE);
}
return (init_addr());
}

struct sockaddr_in init_addr(void)
{
struct sockaddr_in addr;

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(IP);
return (addr);
}

void connect_socket(int fd, struct sockaddr_in *addr)
{
if (connect(fd, (struct sockaddr *)addr, sizeof(*addr)) < 0)
{
printf("Error in connection to the socket.\n");
exit(EXIT_FAILURE);
}
system("clear");
printf("You are connected to the server %s !\n",\
inet_ntoa(addr->sin_addr));
}

void manage_socket(int fd)
{
char serveur[SIZE];
char message[SIZE];

while (1)
{
memset(serveur, 0, SIZE);
memset(message, 0, SIZE);
read(fd, serveur, SIZE);
printf("Serveur: %s\n", serveur);
printf("Message or \"quit\" command: ");
fgets(message, SIZE, stdin);
if (strcmp(message, "quit\n") == 0)
break ;
write(fd, message, sizeof(message));
}
printf("The connection has been closed.\n");
close(fd);
}
69 changes: 69 additions & 0 deletions C/serveur_TCP/src/server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Description

This directory correspond of all sources files making the TCP server

# How it work

In Unix world all is considered like a file, a real file, you'r hard drive (sda), special files (like /dev/null), all is a file. (for more precisions you can read [this post.](https://dev.to/eteimz/everything-is-a-file-explained-g2a)
So a socket too is like a file and we can interact with it like any other file, using [File descriptors.](https://en.wikipedia.org/wiki/File_descriptor)

## Init socket

The first step is to create the file descriptor for the socket (note that we use a pointer because it's his identifier and we want to keep it for read/write/close)
```
*fd = socket(\ // socket is the function in C to create the file descriptor
AF_INET,\ // AF_INET represent the address family (here IPv4)
SOCK_STREAM,\ // SOCK_STREAM represent the type (TCP or UDP here it's TCP)
0); // represent the protocol 0 is used because only protocol is possible with SOCK_STREAM
```
now fd contain a file descriptor to the socket, that's how the socket will be identified by the kernel for our operations.

We also create the address where the server will listen
```
struct sockaddr_in addr;

addr.sin_family = AF_INET; // We specify the address family, the same of the socket
addr.sin_port = htons(PORT); // We use htons to set the bytes of the port in the good order
addr.sin_addr.s_addr = htonl(IP); // htonl is the same as htons but return a long instead of a short
```
see [big_endian](https://en.wikipedia.org/wiki/Endianness) to understand "the good order" of htons

## Bind the socket
Now that we have our socket and our address we want to bind the socket and the address it's going to assign the address to the socket
```
bind(*fd,\ // our file descriptor
(struct sockaddr *)addr,\ // we cast our addr to (struct sockaddr *) because this is what the function want
sizeof(*addr)) // we give the size in byte of our address
```

## Listen
Here we have a file descriptor that point to a socket binded with our local address, all is done.
So we can start at listening incoming connections (this is the job of the kernel)
```
listen(*fd\ // our file descriptor
, 5) // the maximum connection in the queue
```

# Comunnicate in the socket

## Accept connection
Now we can just accept all connections incoming in loop
```
while (1)
{
memset(&client, 0, len); // we fill all the client's structure with 0 before accept a new one

clientfd = accept(\ // the accept function return a new file descriptor that point sepcifically to the connection with our client
fd,\ // our socket's file descriptor
(struct sockaddr *)&client,\ client will be filled with all client's info like his IP, etc
&len); // the len of our client's variable (sizeof(client))

manage_socket(clientfd, &client); // once we accepted the connection we can enter in a loop to discuss with the client
}
```

## Manage socket
This is a simple function where the server and the client talk each in turn, we read/write in the socket with the file descriptor like in a regular file.


Thanks to read this if I made a mistake please let me know
31 changes: 31 additions & 0 deletions C/serveur_TCP/src/server/server.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include "socket_server.h"
#include "global.h"

int main(void)
{
int fd;
int clientfd;
socklen_t len;
struct sockaddr_in client;

init_socket(&fd);
len = sizeof(client);
while (1)
{
memset(&client, 0, len);
clientfd = accept(fd, (struct sockaddr *)&client, &len);
if (clientfd == -1)
printf("Accept failed !\n");
manage_socket(clientfd, &client);
printf("Wait another connection ? (y/n) ");
if (fgetc(stdin) == 'n')
break ;
fgetc(stdin);
}
}
64 changes: 64 additions & 0 deletions C/serveur_TCP/src/server/socket.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "socket_server.h"
#include "global.h"

void init_socket(int *fd)
{
struct sockaddr_in addr;

addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = htonl(IP);
*fd = socket(AF_INET, SOCK_STREAM, 0);
bind_s(fd, &addr);
}

void bind_s(int *fd, struct sockaddr_in *addr)
{
if (bind(*fd, (struct sockaddr *)addr, sizeof(*addr)) == -1)
{
printf("Error in bind_s function during bind set-up\n");
printf("errno code: %d\n", errno);
printf("errno text: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("Socket succesfully binded.\n");
if (listen(*fd, 5) == -1)
{
printf("Error un bind_s function during listen set-up\n");
exit(EXIT_FAILURE);
}
printf("Socket is listening.\n");
}

void manage_socket(int clientfd, struct sockaddr_in *client)
{
char buffer[SIZE];
char response[SIZE];

system("clear");
printf("---New Socket Created !---\n");
printf("You are in communication with %s\n",\
inet_ntoa(client->sin_addr));
write(clientfd, "Hey, I listen you !\n", 20);
while (1)
{
memset(buffer, 0, SIZE);
memset(response, 0, SIZE);
if (read(clientfd, buffer, SIZE) <= 0)
break ;
printf("Client: %s\n", buffer);
printf("Response: ");
fgets(response, SIZE, stdin);
write(clientfd, response, strlen(response));
}
printf("The client closed the session.\n");
close(clientfd);
}
4 changes: 4 additions & 0 deletions Python/entropy_calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Entropy calculator

This is a little script writted a time ago by my self for calculate the entropy of a password.
There is several different method to do it so this script is based on [this article.](https://proton.me/blog/what-is-password-entropy)
44 changes: 44 additions & 0 deletions Python/entropy_calculator/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from math import log2

def calculate_entropy_string(password: str, range_password=0) -> int:
if not range_password:
# calculate range_password of a given password before calculate entropy
VALUES = {'lower': 26, 'upper': 26, 'int': 10, 'special' : 32}
check = {'lower': True, 'upper': True, 'int': True, 'special': True}


password_set = set(password)
for char in password_set:
if char.isalpha():
if char.islower() and check['lower']:
range_password += VALUES['lower']
check['lower'] = False
if char.isupper() and check['upper']:
range_password += VALUES['upper']
check['upper'] = False

elif char.isnumeric():
if check['int']:
range_password += VALUES['int']
check['int'] = False

elif check['special']:
range_password += VALUES['special']
check['special'] = False

# expression for calculate entropy
return round(len(password) * log2(range_password), 2)


def calculate_entropy_diceware(password: str):
ENTROPY_BY_WORD = 12.92
lst = password.split()
return len(lst) * ENTROPY_BY_WORD

if __name__ == '__main__':
pwd = input('Enter you\'r password: ')
if len(pwd.split()) > 2:
entropy = calculate_entropy_diceware(pwd)
else:
entropy = calculate_entropy_string(pwd)
print(f"The password \"{pwd}\" have an entropy of {entropy}")