diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/main/client.c b/main/client.c new file mode 100755 index 0000000..0b7d498 --- /dev/null +++ b/main/client.c @@ -0,0 +1,95 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define BUFSIZE 512 +#define MAXSERVERNAME 100 +int my_work(FILE *, int, struct sockaddr * ,socklen_t); + +int main( int C, char *argv[] ) +{ + int sd, ret; + struct sockaddr_in serveraddress; + if (NULL == argv[1]) + { + printf("Please enter the IP Address of the server\n"); + exit(1); + } + if (NULL== argv[2]) + { + printf("Please enter the Port Number of the server\n"); + exit(1); + } + sd = socket( AF_INET, SOCK_DGRAM, 0 ); + if(0 > sd ) + { + perror( "socket" ); + exit (1); + } + + memset( &serveraddress, 0, sizeof(serveraddress) ); + serveraddress.sin_family = AF_INET; + serveraddress.sin_port = htons(atoi(argv[2]));//PORT NO + serveraddress.sin_addr.s_addr = inet_addr(argv[1]);//ADDRESS + + printf("Client Starting service\n"); + + ret = my_work(stdin,sd ,(struct sockaddr *)&serveraddress, + sizeof(serveraddress)); + if (0 > ret) + { + printf("Client Exiting - Some error occured\n"); + close(sd); + exit(1); + } + close(sd); + exit(0); +} + + + +int my_work(FILE *fp, /*Here to be used as stdin as argument*/ + int sockfd , + struct sockaddr *to ,socklen_t length) /*Connection Socket */ +{ + + char sendbuf[BUFSIZE], recvbuf[BUFSIZE],servername[MAXSERVERNAME]; + char *cptr; + int ret, numbytes, slen; + socklen_t structlen; + struct sockaddr_in serveraddr; + + + while(1){ + printf("Enter Data For the server or press CTRL-D to exit\n"); + /*Reading data from the keyboard*/ + cptr = fgets(sendbuf,BUFSIZE,fp); + if (NULL == cptr) + { + printf("Possible error or end of file\n"); + return 0; + } + slen = strlen (sendbuf); + /*Sending the read data over socket*/ + ret = sendto(sockfd,sendbuf,slen,0,to,length); + if (0 > ret) + { + perror("Error in sending data:\n"); + return -1; + } + printf("Data Sent To Server---------------------\n"); + } + + + + +} + diff --git a/main/encrypt_func.c b/main/encrypt_func.c new file mode 100755 index 0000000..a0cb098 --- /dev/null +++ b/main/encrypt_func.c @@ -0,0 +1,23 @@ +#include +#include +#include + +void encrypt(char * message, int degree) { + + int n = 0, rotateSwap = 0; + int i; + + for(i=0; i='a' && message[i] <='z'){ + n = 'z' - message[i]; + if( degree > n ){ + rotateSwap = degree - n - 1; + message[i] = 'a'; + message[i] += rotateSwap; + } + else{ + message[i] = message[i] + degree; + } + } + } +} diff --git a/main/encrypt_func.h b/main/encrypt_func.h new file mode 100755 index 0000000..b9f385d --- /dev/null +++ b/main/encrypt_func.h @@ -0,0 +1,10 @@ +#include +#include +#include + +#ifndef ENCRYPT_H_INCLUDED +#define ENCRYPT_H_INCLUDED + +void encrypt(char * message, int degree); + +#endif diff --git a/main/main.c b/main/main.c new file mode 100755 index 0000000..8ede420 --- /dev/null +++ b/main/main.c @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "encrypt_func.h" +#include "morse_func.h" + +#define BUFSIZE 512 +#define MYPORT 69 +#define MAXNAME 100 + + +int main(int argc, char *argv[] ) + +{ + int sd, numbytes,bytessent, ret; + struct sockaddr_in + serveraddress,cliaddr; + socklen_t length; + char clientname[MAXNAME],datareceived[BUFSIZE]; + + sd = socket( AF_INET, SOCK_DGRAM, 0 ); + if(0 > sd ) + { + perror( "socket" ); + exit( 1 ); + } + + + memset( &serveraddress, 0, sizeof(serveraddress) ); + memset( &cliaddr, 0, sizeof(cliaddr) ); + serveraddress.sin_family = AF_INET; + serveraddress.sin_port = htons(MYPORT);//PORT NO + serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);//IP ADDRESS + ret=bind(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress)); + if(0 > ret) + { + perror("Bind Fails:"); + exit(1); + } + + printf("UDP Server: Waiting for client data\n"); + length=sizeof(cliaddr); + + + while(1){ + // Chaine de caractere en morse + char * msg_morse = (char *) malloc(sizeof(char)*BUFSIZE); + /*Received a datagram*/ + numbytes = recvfrom(sd,datareceived,BUFSIZE,0, + (struct sockaddr*)&cliaddr, &length); + if (0 > numbytes) + { + perror("Error while receiving:"); + exit(1); + } + /*Printing client's address*/ + printf("Data Received from %s\n", + inet_ntop(AF_INET,&cliaddr.sin_addr, + clientname,sizeof(clientname))); + + /*Sending the Received datagram back*/ + datareceived[numbytes]='\0'; + printf("Server Receives: %s\n",datareceived); + + encrypt(datareceived, 3); + printf("Message encrypted: %s\n", datareceived); + + ascii2morse(datareceived, msg_morse); + printf("MORSE message: %s\n", msg_morse); + + free(msg_morse); + + } + + +} + + diff --git a/main/makefile b/main/makefile new file mode 100755 index 0000000..827b31d --- /dev/null +++ b/main/makefile @@ -0,0 +1,5 @@ +CC = ~/Documents/tp-armadeus/armadeus/buildroot/output/host/usr/bin/arm-linux-gcc + +all: main.c encrypt_func.c morse_func.c + $(CC) -o main.o main.c encrypt_func.c morse_func.c -I. + scp main.o root@172.20.13.45:/home/tp/main.o diff --git a/main/morse_func.c b/main/morse_func.c new file mode 100755 index 0000000..f723a70 --- /dev/null +++ b/main/morse_func.c @@ -0,0 +1,92 @@ +#include "morse_func.h" + +int ascii2morse(char *s, char *outmsg){ + + char *letters[] = { + "1 -1 3 ", // A | a + "3 -1 1 -1 1 -1 1 ", // B | b + "3 -1 1 -1 3 -1 1 ", // C | c + "3 -1 1 -1 1 ", // D | d + "1 ", // E | e + "1 -1 1 -1 3 -1 1 ", // F | f + "3 -1 3 -1 1 ", // G | g + "1 -1 1 -1 1 -1 1 ", // H | h + "1 -1 1 -1 ", // I | i + "1 -1 3 -1 3 -1 3 ", // J | j + "3 -1 1 -1 3 ", // K | k + "1 -1 3 -1 1 -1 1 ", // L | l + "3 -1 3 ", // M | m + "3 -1 1 ", // N | n + "3 -1 3 -1 3 ", // O | o + "1 -1 3 -1 3 -1 1 ", // P | p + "3 -1 3 -1 1 -1 3 ", // Q | q + "1 -1 3 -1 1 ", // R | r + "1 -1 1 -1 1 ", // S | s + "3 ", // T | t + "1 -1 1 -1 3 ", // U | u + "1 -1 1 -1 1 -1 3 ", // V | v + "1 -1 3 -1 3 ", // W | w + "3 -1 1 -1 1 -1 3 ", // X | x + "3 -1 1 -1 3 -1 3 ", // Y | y + "3 -1 3 -1 1 -1 1 " // Z | z + }; + + char *numbers[] = { + "3 -1 3 -1 3 -1 3 -1 3 ", // 0 + "1 -1 3 -1 3 -1 3 -1 3 ", // 1 + "1 -1 1 -1 3 -1 3 -1 3 ", // 2 + "1 -1 1 -1 1 -1 3 -1 3 ", // 3 + "1 -1 1 -1 1 -1 1 -1 3 ", // 4 + "1 -1 1 -1 1 -1 1 -1 1 ", // 5 + "3 -1 1 -1 1 -1 1 -1 1 ", // 6 + "3 -1 3 -1 1 -1 1 -1 1 ", // 7 + "3 -1 3 -1 3 -1 1 -1 1 ", // 8 + "3 -1 3 -1 3 -1 3 -1 3 ", // 9 + }; + + int i, c; + for (i=0;i= 'a')) + strcat(outmsg, letters[c - 'a']); + + else if((c <= 'Z') && (c >= 'A')) + strcat(outmsg, letters[c - 'A']); + + else if((c <= '9') && (c >= '0')) + strcat(outmsg, numbers[c - '0']); + + else if(c == '.') + strcat(outmsg, "1 -1 3 -1 1 -1 3 -1 1 -1 3 "); + + else if(c == ',') + strcat(outmsg, "3 -1 3 -1 1 -1 1 -1 3 -1 3 "); + + else if(c == '?') + strcat(outmsg, "1 -1 1 -1 3 -1 3 -1 1 -1 1 "); + + else if(c == '-') + strcat(outmsg, "3 -1 1 -1 1 -1 1 -1 1 -1 3 "); + + else // caractere non valide + printf("Le caracter %c n'est pas valide \nValeur en ASCII = %d\ni = %d",s[i],s[i],i); + + // espace entre elements + if (s[i+2] == '\0') + break; + if (s[i+1] != ' ') + strcat(outmsg, "-3 "); // entre elements + else{ + strcat(outmsg, "-7 "); // fin d'un mot + i++; + } + } + } + + //fclose(f); + + return EXIT_SUCCESS; +} diff --git a/main/morse_func.h b/main/morse_func.h new file mode 100755 index 0000000..70d0033 --- /dev/null +++ b/main/morse_func.h @@ -0,0 +1,10 @@ +#include +#include +#include + +#ifndef FUNCTIONS_H_INCLUDED +#define FUNCTIONS_H_INCLUDED + +int ascii2morse(char *s, char *outmsg); + +#endif diff --git a/main/morse_func2.c b/main/morse_func2.c new file mode 100755 index 0000000..57a80b5 --- /dev/null +++ b/main/morse_func2.c @@ -0,0 +1,92 @@ +#include "morse_func.h" + +int ascii2morse(char *s, FILE *outfile){ + + char *letters[] = { + "1 -1 3 ", // A | a + "3 -1 1 -1 1 -1 1 ", // B | b + "3 -1 1 -1 3 -1 1 ", // C | c + "3 -1 1 -1 1 ", // D | d + "1 ", // E | e + "1 -1 1 -1 3 -1 1 ", // F | f + "3 -1 3 -1 1 ", // G | g + "1 -1 1 -1 1 -1 1 ", // H | h + "1 -1 1 -1 ", // I | i + "1 -1 3 -1 3 -1 3 ", // J | j + "3 -1 1 -1 3 ", // K | k + "1 -1 3 -1 1 -1 1 ", // L | l + "3 -1 3 ", // M | m + "3 -1 1 ", // N | n + "3 -1 3 -1 3 ", // O | o + "1 -1 3 -1 3 -1 1 ", // P | p + "3 -1 3 -1 1 -1 3 ", // Q | q + "1 -1 3 -1 1 ", // R | r + "1 -1 1 -1 1 ", // S | s + "3 ", // T | t + "1 -1 1 -1 3 ", // U | u + "1 -1 1 -1 1 -1 3 ", // V | v + "1 -1 3 -1 3 ", // W | w + "3 -1 1 -1 1 -1 3 ", // X | x + "3 -1 1 -1 3 -1 3 ", // Y | y + "3 -1 3 -1 1 -1 1 " // Z | z + }; + + char *numbers[] = { + "3 -1 3 -1 3 -1 3 -1 3 ", // 0 + "1 -1 3 -1 3 -1 3 -1 3 ", // 1 + "1 -1 1 -1 3 -1 3 -1 3 ", // 2 + "1 -1 1 -1 1 -1 3 -1 3 ", // 3 + "1 -1 1 -1 1 -1 1 -1 3 ", // 4 + "1 -1 1 -1 1 -1 1 -1 1 ", // 5 + "3 -1 1 -1 1 -1 1 -1 1 ", // 6 + "3 -1 3 -1 1 -1 1 -1 1 ", // 7 + "3 -1 3 -1 3 -1 1 -1 1 ", // 8 + "3 -1 3 -1 3 -1 3 -1 3 ", // 9 + }; + + int i, c; + for (i=0;i= 'a')) + fputs(letters[c - 'a'],outfile); + + else if((c <= 'Z') && (c >= 'A')) + fputs(letters[c - 'A'],outfile); + + else if((c <= '9') && (c >= '0')) + fputs(numbers[c - '0'],outfile); + + else if(c == '.') + fputs("1 -1 3 -1 1 -1 3 -1 1 -1 3 ",outfile); + + else if(c == ',') + fputs("3 -1 3 -1 1 -1 1 -1 3 -1 3 ",outfile); + + else if(c == '?') + fputs("1 -1 1 -1 3 -1 3 -1 1 -1 1 ",outfile); + + else if(c == '-') + fputs("3 -1 1 -1 1 -1 1 -1 1 -1 3 ",outfile); + + else // caractere non valide + printf("Le caracter %c n'est pas valide \nValeur en ASCII = %d\ni = %d",s[i],s[i],i); + + // espace entre elements + if (s[i+2] == '\0') + break; + if (s[i+1] != ' ') + fputs("-3 ",outfile); // entre elements + else{ + fputs("-7 ",outfile); // fin d'un mot + i++; + } + } + } + + //fclose(f); + + return EXIT_SUCCESS; +} diff --git a/sprint/ClientServer_C/Makefile b/sprint/ClientServer_C/Makefile old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/Makefile.in b/sprint/ClientServer_C/Makefile.in old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/README.md b/sprint/ClientServer_C/README.md old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/autom4te.cache/output.0 b/sprint/ClientServer_C/autom4te.cache/output.0 old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/autom4te.cache/requests b/sprint/ClientServer_C/autom4te.cache/requests old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/autom4te.cache/traces.0 b/sprint/ClientServer_C/autom4te.cache/traces.0 old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/autoscan.log b/sprint/ClientServer_C/autoscan.log old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/client.c b/sprint/ClientServer_C/client.c old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/config.log b/sprint/ClientServer_C/config.log old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/configure.ac b/sprint/ClientServer_C/configure.ac old mode 100755 new mode 100644 diff --git a/sprint/ClientServer_C/server.c b/sprint/ClientServer_C/server.c old mode 100755 new mode 100644 diff --git a/sprint/Cryptage/test.c b/sprint/Cryptage/test.c index 6e0ee26..b7b5a3f 100755 --- a/sprint/Cryptage/test.c +++ b/sprint/Cryptage/test.c @@ -24,7 +24,7 @@ void encrypt(char * message, int degree) { int main() { - char * message = (char *) malloc(sizeof(char)*1); + char * message = (char *) malloc(sizeof(char)*100); int degree; printf("Enter message:"); scanf("%s", message); diff --git a/todo.md b/todo.md old mode 100755 new mode 100644 index dda2693..0b04527 --- a/todo.md +++ b/todo.md @@ -5,4 +5,6 @@ - [ ] commenter le code - [ ] bouclage pour qu'on puisse envoyer plusieurs messages -## \ No newline at end of file +## + +ajout d'un script init.d dans `/etc/init.d/` \ No newline at end of file