-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
417 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#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"); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
void encrypt(char * message, int degree) { | ||
|
||
int n = 0, rotateSwap = 0; | ||
int i; | ||
|
||
for(i=0; i<strlen(message); i++){ | ||
if(message[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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#ifndef ENCRYPT_H_INCLUDED | ||
#define ENCRYPT_H_INCLUDED | ||
|
||
void encrypt(char * message, int degree); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
#include <sys/wait.h> | ||
#include <fcntl.h> | ||
#include <signal.h> | ||
#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); | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 [email protected]:/home/tp/main.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<strlen(s);i++){ | ||
|
||
c = s[i]; | ||
if (c != ' '){ | ||
// selectionner le caractere | ||
if((c <= 'z') && (c >= '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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#ifndef FUNCTIONS_H_INCLUDED | ||
#define FUNCTIONS_H_INCLUDED | ||
|
||
int ascii2morse(char *s, char *outmsg); | ||
|
||
#endif |
Oops, something went wrong.