272 Zeilen
7,1 KiB
C
272 Zeilen
7,1 KiB
C
/* librairie standard ... */
|
|
#include <stdlib.h>
|
|
/* pour getopt */
|
|
#include <unistd.h>
|
|
/* déclaration des types de base */
|
|
#include <sys/types.h>
|
|
/* constantes relatives aux domaines, types et protocoles */
|
|
#include <sys/socket.h>
|
|
/* constantes et structures propres au domaine UNIX */
|
|
#include <sys/un.h>
|
|
/* constantes et structures propres au domaine INTERNET */
|
|
#include <netinet/in.h>
|
|
/* structures retournées par les fonctions de gestion de la base de
|
|
données du réseau */
|
|
#include <netdb.h>
|
|
/* pour les entrées/sorties */
|
|
#include <stdio.h>
|
|
/* pour la gestion des erreurs */
|
|
#include <errno.h>
|
|
#define BASE_SIZE 10
|
|
|
|
void construire_message(char *message, char motif, int lg);
|
|
void afficher_message(char *message, int lg);
|
|
void print_usage(char* arg0);
|
|
|
|
int main (int argc, char **argv) {
|
|
int c;
|
|
extern char *optarg;
|
|
extern int optind;
|
|
int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */
|
|
int source = -1 ; /* 0=puits, 1=source */
|
|
int udp=0; /* 0=TCP, 1=UDP */
|
|
|
|
while ((c = getopt(argc, argv, "pn:su")) != -1) {
|
|
switch (c) {
|
|
case 'p':
|
|
if (source == 1) {
|
|
print_usage(argv[0]);
|
|
exit(1);
|
|
}
|
|
source = 0;
|
|
break;
|
|
|
|
case 's':
|
|
if (source == 0) {
|
|
print_usage(argv[0]);
|
|
exit(1) ;
|
|
}
|
|
source = 1;
|
|
break;
|
|
|
|
case 'n':
|
|
nb_message = atoi(optarg);
|
|
break;
|
|
|
|
case 'u':
|
|
udp=1;
|
|
break;
|
|
|
|
default:
|
|
print_usage(argv[0]);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
char* nom_machine_distante;
|
|
|
|
// Recuperation du port
|
|
int port=atoi(argv[argc-1]);
|
|
port = htons(port);
|
|
|
|
char* message = malloc(BASE_SIZE * sizeof(char));
|
|
|
|
if (source == -1) {
|
|
print_usage(argv[0]);
|
|
exit(1) ;
|
|
}
|
|
|
|
if (source == 1) {
|
|
//Recuperation du nom logique
|
|
nom_machine_distante=argv[argc-2];
|
|
} else printf("PUITS:");
|
|
|
|
|
|
if (nb_message != -1) {
|
|
if (source == 1)
|
|
printf("nb de tampons à envoyer : %d\n", nb_message);
|
|
else
|
|
printf("nb de tampons à recevoir : %d\n", nb_message);
|
|
} else {
|
|
if (source == 1) {
|
|
nb_message = 10 ;
|
|
printf("nb de tampons à envoyer = 10 par défaut\n");
|
|
} else
|
|
printf("nb de tampons à recevoir = infini\n");
|
|
}
|
|
|
|
if (udp==1){
|
|
// Creation du socket local
|
|
int sock= socket(AF_INET,SOCK_DGRAM,0);
|
|
|
|
if (source==1) {
|
|
|
|
|
|
|
|
// Creation de l'adresse du socket distant
|
|
struct hostent *hp ;
|
|
struct sockaddr_in adr_dest;
|
|
int longueur_adr_dest = sizeof(adr_dest);
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
memset((char *)& adr_dest, 0, sizeof(adr_dest)) ;
|
|
|
|
adr_dest.sin_family=AF_INET;
|
|
adr_dest.sin_port=port;
|
|
|
|
|
|
if ((hp = gethostbyname(nom_machine_distante)) == NULL) {
|
|
printf("erreur gethostbyname\n") ;
|
|
exit(1) ;
|
|
}
|
|
|
|
memcpy( (char*)&(adr_dest.sin_addr.s_addr),
|
|
hp->h_addr,
|
|
hp->h_length ) ;
|
|
|
|
for (int i = 0; i < nb_message; i++) {
|
|
// Construction du message
|
|
construire_message(message, 'a' + (i % 26), BASE_SIZE);
|
|
|
|
// Envoi du message
|
|
|
|
sendto(sock,message,longueur_message,0,(struct sockaddr*)&adr_dest,longueur_adr_dest);
|
|
}
|
|
} else { // UDP & puit
|
|
// Creation de l'adresse du socket distant
|
|
struct sockaddr_in adr_locale;
|
|
int longueur_adr_locale = sizeof(adr_locale);
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
memset((char *)& adr_locale, 0, sizeof(adr_locale)) ;
|
|
|
|
adr_locale.sin_family=AF_INET;
|
|
adr_locale.sin_port=port;
|
|
adr_locale.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
// Bind the local socket to any local address (ie any available interface)
|
|
if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) {
|
|
printf("failed to bind\n");
|
|
exit(1);
|
|
}
|
|
|
|
while (1) {
|
|
// Receive a single message because we are lazy
|
|
recvfrom(sock, message, longueur_message, 0, NULL, NULL);
|
|
|
|
// Afficher notre seule et unique triste message
|
|
afficher_message(message, longueur_message);
|
|
}
|
|
}
|
|
} else { // TCP
|
|
// Creation du socket local
|
|
int sock= socket(AF_INET,SOCK_STREAM,0);
|
|
|
|
if (source==1) {
|
|
// Creation de l'adresse du socket distant
|
|
struct hostent *hp ;
|
|
struct sockaddr_in adr_dest;
|
|
int longueur_adr_dest = sizeof(adr_dest);
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
memset((char *)& adr_dest, 0, sizeof(adr_dest)) ;
|
|
|
|
adr_dest.sin_family=AF_INET;
|
|
adr_dest.sin_port=port;
|
|
|
|
|
|
if ((hp = gethostbyname(nom_machine_distante)) == NULL) {
|
|
printf("erreur gethostbyname\n") ;
|
|
exit(1) ;
|
|
}
|
|
|
|
memcpy( (char*)&(adr_dest.sin_addr.s_addr),
|
|
hp->h_addr,
|
|
hp->h_length ) ;
|
|
|
|
// Demande de connexion
|
|
int succ;
|
|
if ((succ=connect(sock,(struct sockaddr*) &adr_dest,longueur_adr_dest))!=0){
|
|
printf("Echec connect");
|
|
exit(1);
|
|
}
|
|
|
|
// Envoie des messages
|
|
for (int i = 0; i < nb_message; i++) {
|
|
// Construction du message
|
|
construire_message(message, 'a' + (i % 26), BASE_SIZE);
|
|
|
|
// Envoi du message
|
|
write(sock,message,longueur_message);
|
|
}
|
|
|
|
// Close socket to avoid dangling connections
|
|
close(sock);
|
|
} else { // TCP & puit
|
|
// Creation de l'adresse du socket local
|
|
struct sockaddr_in adr_locale;
|
|
socklen_t longueur_adr_locale = sizeof(adr_locale);
|
|
|
|
int longueur_message = BASE_SIZE;
|
|
|
|
memset((char *)& adr_locale, 0, sizeof(adr_locale)) ;
|
|
|
|
adr_locale.sin_family=AF_INET;
|
|
adr_locale.sin_port=port;
|
|
adr_locale.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
// Bind the local socket to any local address (ie any available interface)
|
|
if (bind(sock, (struct sockaddr *) &adr_locale, longueur_adr_locale) == -1) {
|
|
printf("failed to bind\n");
|
|
exit(1);
|
|
}
|
|
|
|
// set linstening queue size
|
|
if (listen(sock, 5) == -1) {
|
|
printf("échec et mat listen\n");
|
|
exit(1);
|
|
}
|
|
|
|
int sock_bis;
|
|
// Accept a single connection on the main thread
|
|
if ((sock_bis = accept( sock, (struct sockaddr *)&adr_locale, &longueur_adr_locale)) == -1){
|
|
printf("échec du accept\n") ;
|
|
exit(1) ;
|
|
}
|
|
|
|
while (read(sock_bis, message, longueur_message) > 0) {
|
|
afficher_message(message, longueur_message);
|
|
}
|
|
close(sock_bis);
|
|
close(sock);
|
|
}
|
|
}
|
|
exit(0);
|
|
}
|
|
|
|
void construire_message(char *message, char motif, int lg) {
|
|
int i;
|
|
for (i=0;i<lg;i++) message[i] = motif;
|
|
}
|
|
|
|
void afficher_message(char *message, int lg) {
|
|
int i;
|
|
printf("message reçu : ");
|
|
for (i=0;i<lg;i++){
|
|
printf("%c", message[i]);
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
|
|
void print_usage(char* arg0) {
|
|
printf("usage: %s [-psu] [host] <port>\n", arg0);
|
|
printf("parameters: host With -s, address of the host to connect to. Required with -s.\n");
|
|
printf(" port Port to connect or bind to. Required.\n");
|
|
printf("options: -p Runs a TCP/UDP sink. Incompatible with -s.\n");
|
|
printf(" -s Runs a TCP/UDP faucet. Incompatible with -p.\n");
|
|
printf(" -u Use UDP instead of TCP.\n");
|
|
}
|