Skip to content

Commit c1a6d01

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/refactoring-prepare-for-stackcore
2 parents 0153aa2 + 020688c commit c1a6d01

File tree

13 files changed

+473
-11
lines changed

13 files changed

+473
-11
lines changed

deno.lock

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/c/network/.napirc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"language": "c",
3+
"project": {
4+
"include": [
5+
"**/*.c",
6+
"**/*.h"
7+
],
8+
"exclude": [
9+
".git/**",
10+
"**/dist/**",
11+
"**/build/**",
12+
"**/bin/**",
13+
"**/obj/**",
14+
"**/packages/**",
15+
"**/.vs/**",
16+
"**/TestResults/**",
17+
"**/*.user",
18+
"**/*.suo",
19+
"**/.nuget/**",
20+
"**/artifacts/**",
21+
"**/packages/**",
22+
"**/util/**",
23+
"**/test/**",
24+
"**/perf/**",
25+
"**/napi_out/**"
26+
]
27+
},
28+
"outDir": "napi_out",
29+
"metrics": {
30+
"file": {
31+
"maxChar": 100000,
32+
"maxLine": 1000,
33+
"maxDep": 10
34+
},
35+
"symbol": {
36+
"maxChar": 50000,
37+
"maxLine": 500,
38+
"maxDep": 5
39+
}
40+
}
41+
}

examples/c/network/makefile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# --- Macros ---
2+
3+
# compilation
4+
exec = gcc -Wall -Wextra -pedantic -O3 -g3 -fopenmp -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
5+
comp = gcc -c -Wall -Wextra -pedantic -O3 -g3 -fopenmp -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
6+
toexe = -o
7+
toobj = -o
8+
9+
# sources
10+
SRCSC= $(wildcard src/*/*.c)
11+
SRCSH= $(wildcard src/*/*.h)
12+
13+
TSTSC= $(wildcard tst/*.c)
14+
15+
# objects
16+
OBJS= $(wildcard obj/*.o)
17+
OBJBM= $(wildcard src/*/*.o)
18+
19+
20+
# --- Functions ---
21+
22+
all: test main
23+
24+
main: create_exe_main to_obj_src
25+
26+
test: bin_mkdir create_exe_new to_obj
27+
28+
clean: clean_o clean_bin
29+
30+
bin_mkdir:
31+
mkdir -p bin;
32+
33+
obj_mkdir:
34+
mkdir -p obj;
35+
36+
tst_mov:
37+
mv $(foreach exe, $(TSTSC:tst/%.c=%) , $(exe) ) bin
38+
39+
40+
create_exe_new: create_obj
41+
$(foreach test_obj,$(TSTSC:%.c=%.o), $(exec) $(test_obj) $(SRCSC:%.c=%.o) $(toexe) $(test_obj:%.o=%);)
42+
43+
create_exe_main: create_obj_main
44+
$(exec) obj/main.o $(SRCSC:%.c=%.o) $(toexe) app
45+
46+
to_obj: obj_mkdir to_obj_src to_obj_tst
47+
48+
49+
50+
to_obj_src:
51+
mv $(foreach obj,$(SRCSC:%.c=%.o), $(obj) ) obj
52+
53+
to_obj_tst:
54+
mv $(foreach obj,$(TSTSC:%.c=%.o), $(obj) ) obj
55+
56+
57+
create_obj: create_obj_src create_obj_tst
58+
59+
create_obj_src:
60+
$(foreach cfl, $(SRCSC), $(comp) $(cfl) $(toobj) $(cfl:%.c=%).o;)
61+
62+
create_obj_tst:
63+
$(foreach cfl, $(TSTSC), $(comp) $(cfl) $(toobj) $(cfl:%.c=%).o;)
64+
65+
create_obj_main: obj_mkdir create_obj_src
66+
$(comp) src/main.c $(toobj) obj/main.o
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "cartographie.h"
2+
3+
void scanHorizontal(const char* network) {
4+
struct sockaddr_in addr;
5+
int sockfd, ttl = 64, timeout = 1000;
6+
char ip[INET_ADDRSTRLEN];
7+
8+
// Création du socket
9+
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
10+
if (sockfd < 0) {
11+
perror("socket");
12+
exit(EXIT_FAILURE);
13+
}
14+
15+
// Définition de l'option TTL
16+
if (setsockopt(sockfd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
17+
perror("setsockopt");
18+
exit(EXIT_FAILURE);
19+
}
20+
21+
// Définition de l'option de timeout
22+
struct timeval tv;
23+
tv.tv_sec = timeout / 1000;
24+
tv.tv_usec = (timeout % 1000) * 1000;
25+
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
26+
perror("setsockopt");
27+
exit(EXIT_FAILURE);
28+
}
29+
30+
// Scan du réseau
31+
for (int i = 1; i <= 255; i++) {
32+
memset(&addr, 0, sizeof(addr));
33+
addr.sin_family = AF_INET;
34+
35+
// Adresse --> octets
36+
int octet1, octet2, octet3, octet4;
37+
sscanf(network, "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4);
38+
39+
// Dernier octet --> i
40+
octet4 = i;
41+
42+
// Réassemblement de l'adresse
43+
char host[INET_ADDRSTRLEN];
44+
sprintf(host, "%d.%d.%d.%d", octet1, octet2, octet3, octet4);
45+
46+
// Conversion adresse en binaire
47+
inet_pton(AF_INET, host, &(addr.sin_addr));
48+
49+
// Conversion adresse en texte
50+
inet_ntop(AF_INET, &(addr.sin_addr), ip, INET_ADDRSTRLEN);
51+
52+
// Envoi du paquet ICMP
53+
struct icmphdr icmp;
54+
memset(&icmp, 0, sizeof(icmp));
55+
icmp.type = ICMP_ECHO;
56+
icmp.code = 0;
57+
icmp.checksum = htons(~(ICMP_ECHO << 8));
58+
59+
if (sendto(sockfd, &icmp, sizeof(icmp), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
60+
perror("sendto");
61+
continue;
62+
}
63+
64+
// Attente de la réponse
65+
fd_set read_set;
66+
FD_ZERO(&read_set);
67+
FD_SET(sockfd, &read_set);
68+
struct timeval timeout;
69+
timeout.tv_sec = 1;
70+
timeout.tv_usec = 0;
71+
int select_result = select(sockfd + 1, &read_set, NULL, NULL, &timeout);
72+
if (select_result < 0) {
73+
perror("select");
74+
continue;
75+
} else if (select_result == 0) {
76+
printf("Aucune réponse de %s\n", ip);
77+
continue;
78+
}
79+
80+
// Réception de la réponse
81+
char buffer[IP_MAXPACKET];
82+
struct sockaddr_in sender;
83+
socklen_t sender_len = sizeof(sender);
84+
ssize_t packet_len = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&sender, &sender_len);
85+
if (packet_len < 0) {
86+
perror("recvfrom");
87+
continue;
88+
}
89+
90+
// Affichage de l'adresse
91+
printf("L'hôte %s est en ligne\n", ip);
92+
}
93+
94+
// Fermeture socket
95+
close(sockfd);
96+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef CARTOGRAPHIE_H
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include <arpa/inet.h>
8+
#include <sys/socket.h>
9+
#include <sys/time.h>
10+
#include <netinet/in.h>
11+
#include <netinet/ip.h>
12+
#include <netinet/ip_icmp.h>
13+
#include "tnmap.h"
14+
15+
void scanHorizontal(const char* network);
16+
17+
#define CARTOGRAPHIE_H
18+
#endif
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "tnmap.h"
2+
3+
// Vérifie si un hôte est actif
4+
// Retourne un socket si l'hôte est actif, 1 sinon
5+
int tnmap(const char* ip_addr) {
6+
int sock;
7+
struct sockaddr_in server;
8+
int port;
9+
int result[MAX_PORTS] = {0};
10+
11+
// Création du socket
12+
sock = socket(AF_INET, SOCK_STREAM, 0);
13+
if (sock == -1) {
14+
perror("Échec de la création du socket");
15+
return -1;
16+
}
17+
18+
server.sin_addr.s_addr = inet_addr(ip_addr);
19+
server.sin_family = AF_INET;
20+
21+
// Scan des ports
22+
for (port = 1; port <= MAX_PORTS; port++) {
23+
server.sin_port = htons(port);
24+
25+
// Connexion à l'hôte distant
26+
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
27+
result[port - 1] = 0; // Port fermé
28+
} else {
29+
result[port - 1] = 1; // Port ouvert
30+
close(sock);
31+
sock = socket(AF_INET, SOCK_STREAM, 0); // Socket pour le prochain port
32+
if (sock == -1) {
33+
perror("Échec de la création du socket");
34+
return -1;
35+
}
36+
}
37+
}
38+
39+
#pragma omp parallel for
40+
// Affichage des résultats
41+
for (port = 1; port <= MAX_PORTS; port++) {
42+
if (result[port - 1] == 1) {
43+
printf("Le port %d de %s est ouvert\n", port, ip_addr);
44+
}
45+
}
46+
47+
return 1;
48+
}
49+
50+
void scanVertical(const char* ip_addr) {
51+
for (int i = 0; i < 255; i++) {
52+
tnmap(ip_addr);
53+
}
54+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef TNMAP_H
2+
#define TNMAP_H
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <sys/socket.h>
7+
#include <arpa/inet.h>
8+
#include <unistd.h>
9+
#define MAX_PORTS 65535
10+
#endif
11+
12+
int tnmap(const char* ip_addr);
13+
void scanVertical(const char* ip_addr);

examples/c/network/src/main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "cartographie/cartographie.h"
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
int main(int argc, char *argv[]) {
6+
if (argc < 3) {
7+
fprintf(stderr, "Utilisation : %s <mode> <ip>\n", argv[0]);
8+
fprintf(stderr, "Modes :\n");
9+
fprintf(stderr, " -h: Scan horizontal\n");
10+
fprintf(stderr, " -v: Scan vertical\n");
11+
fprintf(stderr, "Option:\n");
12+
fprintf(stderr, " <ip>: IP à scanner verticalemnet or horizontalement (adresse de réseau)\n");
13+
return 1;
14+
}
15+
if (strcmp(argv[1], "-h") == 0) {
16+
scanHorizontal(argv[2]);
17+
} else if (strcmp(argv[1], "-v") == 0) {
18+
scanVertical(argv[2]);
19+
} else {
20+
fprintf(stderr, "Mode invalide\n");
21+
return 1;
22+
}
23+
return 0;
24+
}

0 commit comments

Comments
 (0)