Commit 08f8ef8d183c307771a5223aa68de729be61b81d
1 parent
c9ce1fd2
modifs
Showing
8 changed files
with
81 additions
and
91 deletions
Show diff stats
Network/Makefile
... | ... | @@ -3,7 +3,7 @@ |
3 | 3 | # |
4 | 4 | |
5 | 5 | CC=gcc |
6 | -CFLAGS=-W -Wall -Wextra | |
6 | +CFLAGS=-W -Wall -Wextra -g | |
7 | 7 | CLIBS=ar -rcs |
8 | 8 | LIB=libnet.a |
9 | 9 | LDIR=../libs/ |
... | ... | @@ -18,13 +18,9 @@ all: $(LIB) |
18 | 18 | $(LIB): sender.o sniffer.o tcpserver.o |
19 | 19 | rm -f $@ |
20 | 20 | $(CLIBS) $(LDIR)$@ $^ |
21 | -<<<<<<< HEAD | |
22 | 21 | ranlib $(LDIR)$@ |
23 | 22 | rm -f *.o |
24 | -======= | |
25 | - ranlib $(LDIR)$@ | |
26 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
27 | - | |
23 | + | |
28 | 24 | |
29 | 25 | tcpserver.o: tcpserver.c |
30 | 26 | $(CC) -o $@ -c $^ -I$(IDIR) $(CFLAGS) | ... | ... |
Network/sender.c
... | ... | @@ -9,18 +9,11 @@ |
9 | 9 | #include <errno.h> |
10 | 10 | |
11 | 11 | |
12 | -<<<<<<< HEAD | |
13 | 12 | int initializeSocketUDP(char* service) |
14 | 13 | { |
15 | 14 | int sock_id; |
16 | 15 | struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les requêtes |
17 | -======= | |
18 | 16 | |
19 | -int initializeSocketUDP(char* service) | |
20 | -{ | |
21 | - int sock_id; | |
22 | - struct addrinfo precisions, *resultat=NULL, *origine; // On stocke dans précisions nos besoins pour le socket, dans resultat l'adresse qui respectera les reqêtes | |
23 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
24 | 17 | |
25 | 18 | memset(&precisions, 0, sizeof precisions); |
26 | 19 | precisions.ai_family = AF_UNSPEC; |
... | ... | @@ -67,7 +60,7 @@ int sendUDPBroadcast(char* message, int port) |
67 | 60 | { |
68 | 61 | static struct sockaddr_in addrUDP; |
69 | 62 | addrUDP.sin_family=AF_INET; |
70 | - addrUDP.sin_port=(uint16_t)port; | |
63 | + addrUDP.sin_port=htons(port); | |
71 | 64 | |
72 | 65 | struct in_addr tmpadd={0xffffffff}; |
73 | 66 | addrUDP.sin_addr=tmpadd; |
... | ... | @@ -77,16 +70,10 @@ int sendUDPBroadcast(char* message, int port) |
77 | 70 | int sock_id=initializeSocketUDP( tmp_str ); |
78 | 71 | |
79 | 72 | printf("Initialize via Broadcast %d\n", sock_id); |
80 | -<<<<<<< HEAD | |
81 | 73 | |
82 | - int status; | |
83 | - if((status=sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP))) < 0) { fprintf(stderr, "Erreur sendto %d\n", status); return(-1);} | |
74 | + if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);} | |
84 | 75 | |
85 | -======= | |
86 | 76 | |
87 | - if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0) { fprintf(stderr, "Erreur sendto\n"); return(-1);} | |
88 | - | |
89 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
90 | 77 | close(sock_id); |
91 | 78 | return 0; |
92 | 79 | } |
... | ... | @@ -97,7 +84,7 @@ int sendUDPUnicast(char* address, char* message, int port) |
97 | 84 | { |
98 | 85 | static struct sockaddr_in addrUDP; |
99 | 86 | addrUDP.sin_family=AF_INET; |
100 | - addrUDP.sin_port=(uint16_t)port; | |
87 | + addrUDP.sin_port=htons(port); | |
101 | 88 | |
102 | 89 | inet_aton(address, &(addrUDP.sin_addr)); // Converti l'écriture string de format a.b.c.d en format Internet |
103 | 90 | |
... | ... | @@ -105,27 +92,14 @@ int sendUDPUnicast(char* address, char* message, int port) |
105 | 92 | sprintf(tmp_str, "%d", port); |
106 | 93 | int sock_id=initializeSocketUDP( tmp_str ); |
107 | 94 | printf("Initialize via Unicast %d\n", sock_id); |
108 | -<<<<<<< HEAD | |
109 | 95 | |
110 | 96 | if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto %d\n", errno); return(-1); } |
111 | 97 | |
112 | -======= | |
113 | - | |
114 | - if(sendto(sock_id, message, sizeof(message), 0, (struct sockaddr*) &addrUDP, sizeof(addrUDP)) < 0 ) { fprintf(stderr, "Error sendto\n"); return(-1); } | |
115 | - | |
116 | - close(sock_id); | |
117 | - return 0; | |
118 | -} | |
119 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
120 | - | |
121 | 98 | close(sock_id); |
99 | + | |
122 | 100 | return 0; |
123 | 101 | } |
124 | 102 | |
125 | -<<<<<<< HEAD | |
126 | - | |
127 | -======= | |
128 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
129 | 103 | /* |
130 | 104 | |
131 | 105 | int main(void) | ... | ... |
Network/tcpserver.c
... | ... | @@ -44,8 +44,9 @@ int initialisationServeur(char* service) |
44 | 44 | |
45 | 45 | int val=1; |
46 | 46 | |
47 | - if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR|SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
47 | + if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
48 | 48 | |
49 | + if(setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))<0) { fprintf(stderr, "Erreur setsockopt\n"); return(-1); } | |
49 | 50 | |
50 | 51 | |
51 | 52 | if(bind(sock_fd, resultat->ai_addr, resultat->ai_addrlen)) { fprintf(stderr, "Erreur bind\n"); return(-1); } | ... | ... |
Threads/Makefile
Threads/threadSocket.c
... | ... | @@ -6,50 +6,89 @@ |
6 | 6 | |
7 | 7 | #define BUFF_SIZE 1000 |
8 | 8 | |
9 | -void* reponseConnexion(void* sock) | |
9 | + | |
10 | + | |
11 | + | |
12 | +struct PageWeb | |
10 | 13 | { |
11 | - | |
12 | - printf("Connected\n"); | |
13 | -<<<<<<< HEAD | |
14 | - char buff_rec[BUFF_SIZE]; | |
15 | - char buffer[]="HTTP/1.1 200 OK\r\nServer: Serveur fait maison\r\nContent-Type: text/html; charset=UTF-8\r\nConnection: Keep-alive\r\n\r\nPOST /Page.html HTTP/1.1\r\n\r\n"; | |
14 | + int size; | |
15 | + char* html_contents; | |
16 | +}; | |
16 | 17 | |
17 | 18 | |
18 | - FILE* sockdial_stream = fdopen(*((int*)(sock)), "a+"); | |
19 | - | |
20 | - printf("Sock : %d // Stream : %p \n", *((int*)(sock)), sockdial_stream); | |
21 | - | |
22 | - fgets(buff_rec, BUFF_SIZE, sockdial_stream); | |
23 | - fprintf(sockdial_stream, "%s", buffer); | |
24 | 19 | |
25 | - //while(strcmp(buffer, "EXIT\n")!=0) | |
26 | - //{ | |
27 | - printf("WAIT\n"); | |
28 | - // fgets(buff_rec, BUFF_SIZE, sockdial_stream); | |
20 | + | |
21 | +void freePage(struct PageWeb** page) | |
22 | +{ | |
23 | + if(*page!=NULL) | |
24 | + { | |
25 | + if((*page)->html_contents!=NULL) free((*page)->html_contents); | |
26 | + free(*page); | |
27 | + } | |
28 | +} | |
29 | + | |
30 | + | |
31 | + | |
32 | +void getWeb(char* pageName, struct PageWeb** page) | |
33 | +{ | |
34 | + *page=malloc(sizeof(struct PageWeb)); | |
35 | + | |
36 | + FILE* pageFile=fopen(pageName, "r+"); | |
37 | + | |
38 | + if(pageFile!=NULL) | |
39 | + { | |
40 | + fseek(pageFile, 0L, SEEK_END); | |
41 | + long nbr_char=ftell(pageFile); | |
42 | + fseek(pageFile, 0L, SEEK_SET); | |
43 | + | |
44 | + | |
45 | + (*page)->html_contents=malloc(nbr_char*sizeof(char)); | |
46 | + fread(((*page)->html_contents),1,nbr_char,pageFile); | |
47 | + | |
48 | + (*page)->size=strlen(((*page)->html_contents)); | |
49 | + fclose(pageFile); | |
50 | + } | |
51 | + else | |
52 | + { | |
53 | + char erreur[]="<h1> 404 PAGE NOT FOUND </h1>\r\n\r\n La page que vous cherchez n'a pas été trouvée, sans doute parce que vous êtes nul."; | |
54 | + (*page)->html_contents=malloc(strlen(erreur)*sizeof(char)); | |
55 | + strcpy((*page)->html_contents, erreur); | |
56 | + } | |
29 | 57 | |
30 | - printf("Le message reçu : %s", buff_rec); | |
58 | +} | |
59 | + | |
60 | + | |
31 | 61 | |
32 | - fprintf(sockdial_stream, "Ceci est une réponse du serveur TCP.\n"); | |
33 | 62 | |
34 | - //} | |
35 | -======= | |
63 | +void* reponseConnexion(void* sock) | |
64 | +{ | |
65 | + printf("Connected\n"); // Affiche le fait que qqn soit connecté | |
66 | + | |
67 | + struct PageWeb* page; | |
68 | + | |
69 | + getWeb("../Sioux/Page.html", &page); | |
70 | + | |
71 | + char buff_rec[BUFF_SIZE]; | |
36 | 72 | char buffer[BUFF_SIZE]; |
37 | - | |
73 | + | |
74 | + strcat(buffer,"HTTP/1.1 200 OK\r\nServer: Serveur fait maison\r\nContent-Type: text/html; charset=UTF-8\r\nConnection: Keep-alive\r\n\r\n"); | |
75 | + strcat(buffer, page->html_contents); | |
76 | + | |
77 | + freePage(&page); | |
78 | + | |
38 | 79 | FILE* sockdial_stream = fdopen(*((int*)(sock)), "a+"); |
39 | 80 | |
40 | 81 | printf("Sock : %d // Stream : %p \n", *((int*)(sock)), sockdial_stream); |
41 | - | |
42 | - while(strcmp(buffer, "EXIT\n")!=0) | |
43 | - { | |
44 | - printf("WAIT\n"); | |
45 | - fgets(buffer, BUFF_SIZE, sockdial_stream); | |
46 | 82 | |
47 | - printf("Le message reçu : %s", buffer); | |
83 | + fprintf(sockdial_stream, "%s", buffer); | |
84 | + | |
85 | + printf("WAIT\n"); | |
48 | 86 | |
49 | - fprintf(sockdial_stream, "Ceci est une réponse du serveur TCP.\n"); | |
87 | + fgets(buff_rec, BUFF_SIZE, sockdial_stream); | |
88 | + | |
89 | + printf("Le message reçu : %s", buff_rec); | |
50 | 90 | |
51 | - } | |
52 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
91 | + fprintf(sockdial_stream, "Ceci est une réponse du serveur TCP.\n"); | |
53 | 92 | |
54 | 93 | strcpy(buffer, "START"); |
55 | 94 | printf("Fin de la connexion\n"); |
... | ... | @@ -58,6 +97,8 @@ void* reponseConnexion(void* sock) |
58 | 97 | } |
59 | 98 | |
60 | 99 | |
100 | + | |
101 | + | |
61 | 102 | int lanceThread(void(* fonction) (void *), void* arg, int size) |
62 | 103 | { |
63 | 104 | pthread_t thr_id; | ... | ... |
bin/Makefile
bin/test
No preview for this file type
bin/test.c
1 | 1 | #include <stdlib.h> |
2 | 2 | #include <stdio.h> |
3 | -<<<<<<< HEAD | |
4 | 3 | #include <unistd.h> |
5 | 4 | #include <getopt.h> |
6 | 5 | #include <string.h> |
... | ... | @@ -23,13 +22,6 @@ void handler(int sig) |
23 | 22 | } |
24 | 23 | |
25 | 24 | |
26 | -======= | |
27 | -#include <getopt.h> | |
28 | -#include <string.h> | |
29 | -#include "libnet.h" | |
30 | - | |
31 | - | |
32 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
33 | 25 | void argPortParsing(int argc, char* argv[], char* port) |
34 | 26 | { |
35 | 27 | struct option port_arg={"port", 1, NULL, 'p'}; |
... | ... | @@ -55,7 +47,6 @@ int main(int argc, char* argv[]) |
55 | 47 | char port[10]="80"; |
56 | 48 | argPortParsing(argc, argv, port); |
57 | 49 | |
58 | -<<<<<<< HEAD | |
59 | 50 | action.sa_handler=&handler; |
60 | 51 | sigaction(SIGINT, &action, NULL); |
61 | 52 | |
... | ... | @@ -66,19 +57,6 @@ int main(int argc, char* argv[]) |
66 | 57 | |
67 | 58 | sendUDPBroadcast("BroadWesh", 2020); |
68 | 59 | sendUDPUnicast("192.168.0.37", "Coucou", 2020); |
69 | -======= | |
70 | - | |
71 | - int sock_fd=initialisationServeur(port); | |
72 | - | |
73 | - if( sock_fd==-1 ) { fprintf(stderr, "Initialisation du serveur impossible\n"); return -1; } | |
74 | - | |
75 | - | |
76 | - boucleServeur(sock_fd, (void*)&reponseConnexion); | |
77 | - | |
78 | - | |
79 | - sendUDPBroadcast("BroadWesh", 2020); | |
80 | - | |
81 | ->>>>>>> 52d8a1eaf954b611aaf0861144b440a15f7c517d | |
82 | 60 | return 0; |
83 | 61 | } |
84 | 62 | ... | ... |