Réseaux TCP/IP |
Il s’agit d’un atelier de programmation réseau autour d’une manette série.
| 6 octets | 6 octets | 2 octets | 48 à 1500 octets |
| Adresse destination | Adresse source | Type | Données |
| 00:1a:a0:c0:17:6a |
| ff:ff:ff:ff:ff:ff |
| 2 octets | 2 octets | 1 octet | 1 octet | 2 octets | 6 octets | 4 octets | 6 octets | 4 octets |
| Type de matériel | Type de protocole | Longueur @ matérielle | Longueur @ protocole | Opération | @ émetteur matérielle | @ émetteur protocole | @ destinataire matérielle | @ destinataire protocole |
| 0x0001 | 0x0800 | 6 | 4 | 0x0001 ou 0x0002 | - | - | - | - |
| 4 bits | 4 bits | 1 octet | 2 octets | 4 octets | 1 octet | 1 octet | 2 octets | 4 octets | 4 octets |
| Version | Longueur entêtes | Services et congestion | Longueur totale | Fragmentation | Durée vie | Protocole | Somme de contrôle | @ IPv4 source | @ IPv4 destination |
| 0x4 | 0x5 | 0x00 | - | 0x00000000 | 0x40 | - | - | - | - |
| 192.168.0.1 |
| 193.48.57.34 |
| fe80::1 |
| 2001:660:4401:6002:215:c5ff:fef3:e3f |
| 192.168.0.0/255.255.255.0 |
| 193.48.57.0/26 |
#include <stdio.h>
#include <stdint.h>
int main(void){
uint32_t sum=0;
uint8_t h,l;
while(scanf("%hhx",&h)==1){
if(scanf("%hhx",&l)!=1) l=0;
sum += ((uint16_t)h<<8)|l;
}
while(sum>>16) sum=(sum&0xffff)+(sum>>16);
printf("%04x\n",(uint16_t)~sum);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#define SIZE_TOTAL 59
#define SIZE_IPHEAD 20
uint8_t ip[SIZE_TOTAL]={
0x45,0x00,0x00,0x3b,0x2c,0xf5,0x40,0x00,0x40,0x11,0xd5,0xf7,0xac,0x1a,0x91,0x4a,
0xc1,0x30,0x39,0x30,0xdd,0x9e,0x00,0x35,0x00,0x27,0x37,0xfe,0x7f,0x4a,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x70,0x72,0x6f,0x78,0x79,0x04,0x70,
0x6c,0x69,0x6c,0x02,0x66,0x72,0x00,0x00,0x1c,0x00,0x01
};
uint16_t checksum(uint8_t *bytes,int size){
uint32_t sum=0;
int i;
for(i=0;i<size;i += 2){
uint8_t h,l;
h=bytes[i];
l=(i<size-1)?bytes[i+1]:0;
sum += ((uint16_t)h<<8)|l;
}
while(sum>>16) sum=(sum&0xffff)+(sum>>16);
return (uint16_t)~sum;
}
int main(void){
uint16_t sum=checksum(ip,SIZE_IPHEAD);
printf("%04x\n",sum);
return 0;
}
| 1 octet | 1 octet | 2 octets |
| Type | Code | Somme de contrôle |
| Type | Code | Description |
| 0 | 0 | Réponse d’écho |
| 3 | 0 | Réseau inaccessible |
| 3 | 1 | Machine inaccessible |
| 3 | 2 | Protocole inaccessible |
| 3 | 3 | Port inaccessible |
| 3 | 4 | Fragmentation impossible |
| 4 | 0 | Limitation du débit de la source |
| 5 | 0 | Modification de route |
| 8 | 0 | Demande d’écho |
| 11 | 0 | Datagramme trop vieux |
| 2 octet | 2 octet | 2 octets | 2 octets |
| Port source | Port destination | Longueur totale | Somme de contrôle |
#include <stdio.h>
#include <stdint.h>
#define SIZE_TOTAL 59
#define SIZE_IPHEAD 20
#define SIZE_IPADDR 4
#define SIZE_PORT 2
uint8_t ip[SIZE_TOTAL]={
0x45,0x00,0x00,0x3b,0x2c,0xf5,0x40,0x00,0x40,0x11,0xd5,0xf7,0xac,0x1a,0x91,0x4a,
0xc1,0x30,0x39,0x30,0xdd,0x9e,0x00,0x35,0x00,0x27,0x19,0x14,0x7f,0x4a,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x70,0x72,0x6f,0x78,0x79,0x04,0x70,
0x6c,0x69,0x6c,0x02,0x66,0x72,0x00,0x00,0x1c,0x00,0x01
};
uint32_t checksum(uint8_t *bytes,int size,uint32_t sum,unsigned char final){
int i;
for(i=0;i<size;i += 2){
uint8_t h,l;
h=bytes[i];
l=(i<size-1)?bytes[i+1]:0;
sum += ((uint16_t)h<<8)|l;
}
if(final){
while(sum>>16) sum=(sum&0xffff)+(sum>>16);
return (~sum)&0xffff;
}
else return sum;
}
int main(void){
uint8_t *udp=ip+SIZE_IPHEAD;
uint8_t *addresses=ip+SIZE_IPHEAD-2*SIZE_IPADDR;
uint8_t protocol[]={0x00,0x11};
uint8_t *udpsize=udp+2*SIZE_PORT;
uint32_t sum=checksum(addresses,2*SIZE_IPADDR,0,0);
sum=checksum(protocol,2,sum,0);
sum=checksum(udpsize,2,sum,0);
sum=checksum(udp,SIZE_TOTAL-SIZE_IPHEAD,sum,1);
printf("%04x\n",sum);
return 0;
}
| Numéro de couche | Nom du nain | Nom de la couche | Explication |
| 1 | Dormeur | Physique | Les connexions physiques sont ennuyeuses |
| 2 | Atchoum | Lien | Un renifleur réseau montre une explosion de paquets |
| 3 | Joyeux | Réseau | Tout le monde est content de la couche réseau |
| 4 | Prof | Transport | Compliqué à comprendre (TCP en particulier) |
| 5 | Simplet | Session | Faudrait l’être pour en réaliser une |
| 6 | Timide | Présentation | Personne n’en a vu |
| 7 | Grincheux | Applications | Un développeur réseau est d’abord rude |
int initialisationServeur(char *service,int connexions){
struct addrinfo precisions,*resultat,*origine;
int statut;
int s;
/* Construction de la structure adresse */
memset(&precisions,0,sizeof precisions);
precisions.ai_family=AF_UNSPEC;
precisions.ai_socktype=SOCK_STREAM;
precisions.ai_flags=AI_PASSIVE;
statut=getaddrinfo(NULL,service,&precisions,&origine);
if(statut<0){ perror("initialisationServeur.getaddrinfo"); exit(EXIT_FAILURE); }
struct addrinfo *p;
for(p=origine,resultat=origine;p!=NULL;p=p->ai_next)
if(p->ai_family==AF_INET6){ resultat=p; break; }
/* Creation d'une socket */
s=socket(resultat->ai_family,resultat->ai_socktype,resultat->ai_protocol);
if(s<0){ perror("initialisationServeur.socket"); exit(EXIT_FAILURE); }
/* Options utiles */
int vrai=1;
if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&vrai,sizeof(vrai))<0){
perror("initialisationServeur.setsockopt (REUSEADDR)");
exit(EXIT_FAILURE);
}
if(setsockopt(s,IPPROTO_TCP,TCP_NODELAY,&vrai,sizeof(vrai))<0){
perror("initialisationServeur.setsockopt (NODELAY)");
exit(EXIT_FAILURE);
}
/* Specification de l'adresse de la socket */
statut=bind(s,resultat->ai_addr,resultat->ai_addrlen);
if(statut<0){ freeaddrinfo(origine); shutdown(s,SHUT_RDWR); return -1; }
/* Liberation de la structure d'informations */
freeaddrinfo(origine);
/* Taille de la queue d'attente */
statut=listen(s,connexions);
if(statut<0){ shutdown(s,SHUT_RDWR); return -2; }
return s;
}
int boucleServeur(int ecoute,int (*traitement)(int))
{
int dialogue;
while(1){
/* Attente d'une connexion */
if((dialogue=accept(ecoute,NULL,NULL))<0) return -1;
/* Passage de la socket de dialogue a la fonction de traitement */
if(traitement(dialogue)<0){ shutdown(ecoute,SHUT_RDWR); return 0;}
}
}
int gestionClient(int s){
/* Obtient une structure de fichier */
FILE *dialogue=fdopen(s,"a+");
if(dialogue==NULL){ perror("gestionClient.fdopen"); exit(EXIT_FAILURE); }
/* Echo */
char ligne[MAX_LIGNE];
while(fgets(ligne,MAX_LIGNE,dialogue)!=NULL)
fprintf(dialogue,"> %s",ligne);
/* Termine la connexion */
fclose(dialogue);
return 0;
}
#define MAX_CONNEXIONS 16
int main(void)
{
int s;
/* Initialisation du serveur */
s=initialisationServeur("4000",MAX_CONNEXIONS);
/* Lancement de la boucle d'ecoute */
boucleServeur(s,gestionClient);
}
int connexionServeur(char *hote,char *service){
struct addrinfo precisions,*resultat,*origine;
int statut;
int s;
/* Creation de l'adresse de socket */
memset(&precisions,0,sizeof precisions);
precisions.ai_family=AF_UNSPEC;
precisions.ai_socktype=SOCK_STREAM;
statut=getaddrinfo(hote,service,&precisions,&origine);
if(statut<0){ perror("connexionServeur.getaddrinfo"); exit(EXIT_FAILURE); }
struct addrinfo *p;
for(p=origine,resultat=origine;p!=NULL;p=p->ai_next)
if(p->ai_family==AF_INET6){ resultat=p; break; }
/* Creation d'une socket */
s=socket(resultat->ai_family,resultat->ai_socktype,resultat->ai_protocol);
if(s<0){ perror("connexionServeur.socket"); exit(EXIT_FAILURE); }
/* Connection de la socket a l'hote */
if(connect(s,resultat->ai_addr,resultat->ai_addrlen)<0)
{ freeaddrinfo(origine); shutdown(s,SHUT_RDWR); return -1; }
/* Liberation de la structure d'informations */
freeaddrinfo(origine);
return s;
}
int main(void)
{
int s;
/* Connection au serveur */
s=connexionServeur(machine,"4000");
if(s<0){ fprintf(stderr,"Erreur de connexion au serveur\n"); exit(EXIT_FAILURE); }
/* Boucle de communication avec le serveur */
struct pollfd descripteurs[2];
descripteurs[0].fd=s;
descripteurs[0].events=POLLIN;
descripteurs[1].fd=0;
descripteurs[1].events=POLLIN;
while(1){
char tampon[MAX_TAMPON];
int nb=poll(descripteurs,2,-1);
if(nb<0){ perror("main.poll"); exit(EXIT_FAILURE); }
if((descripteurs[0].revents&POLLIN)!=0){
int taille=read(s,tampon,MAX_TAMPON);
if(taille<=0) break;
write(1,tampon,taille);
}
if((descripteurs[1].revents&POLLIN)!=0){
int taille=read(0,tampon,MAX_TAMPON);
if(taille<=0) break;
write(s,tampon,taille);
}
}
/* On termine la connexion */
shutdown(s,SHUT_RDWR);
return 0;
}
int initialisationSocketUDP(char *service){
struct addrinfo precisions,*resultat,*origine;
int statut;
int s;
/* Construction de la structure adresse */
memset(&precisions,0,sizeof precisions);
precisions.ai_family=AF_UNSPEC;
precisions.ai_socktype=SOCK_DGRAM;
precisions.ai_flags=AI_PASSIVE;
statut=getaddrinfo(NULL,service,&precisions,&origine);
if(statut<0){ perror("initialisationSocketUDP.getaddrinfo"); exit(EXIT_FAILURE); }
struct addrinfo *p;
for(p=origine,resultat=origine;p!=NULL;p=p->ai_next)
if(p->ai_family==AF_INET6){ resultat=p; break; }
/* Creation d'une socket */
s=socket(resultat->ai_family,resultat->ai_socktype,resultat->ai_protocol);
if(s<0){ perror("initialisationSocketUDP.socket"); exit(EXIT_FAILURE); }
/* Options utiles */
int vrai=1;
if(setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&vrai,sizeof(vrai))<0){
perror("initialisationServeurUDPgenerique.setsockopt (REUSEADDR)");
exit(EXIT_FAILURE);
}
if(setsockopt(s,SOL_SOCKET,SO_BROADCAST,&vrai,sizeof vrai)<0){
perror("initialisationSocketUDP.setsockopt (BROADCAST)");
exit(EXIT_FAILURE);
}
/* Specification de l'adresse de la socket */
statut=bind(s,resultat->ai_addr,resultat->ai_addrlen);
if(statut<0) { freeaddrinfo(origine); shutdown(s,SHUT_RDWR); return -1; }
/* Liberation de la structure d'informations */
freeaddrinfo(origine);
return s;
}
int boucleServeurUDP(int s,int (*traitement)(unsigned char *,int,char *)){
while(1){
struct sockaddr_storage adresse;
socklen_t taille=sizeof(adresse);
unsigned char message[MAX_UDP_MESSAGE];
int nboctets=recvfrom(s,message,MAX_UDP_MESSAGE,0,(struct sockaddr *)&adresse,&taille);
if(nboctets<0) return -1;
char nom[MAX_NOM];
nom[0]='\0';
getnameinfo((struct sockaddr *)&adresse,taille,nom,MAX_NOM,NULL,0,0);
if(traitement(message,nboctets,nom)<0) break;
}
return 0;
}
int main(void){
int s=initialisationSocketUDP("4242");
boucleServeurUDP(s,votreFonctionUDP);
close(s);
return 0;
}
void *creationAddresseUDP(char *hote,char *service){
struct addrinfo precisions,*resultat,*origine;
int statut;
/* Creation de l'adresse de socket */
memset(&precisions,0,sizeof precisions);
precisions.ai_family=AF_UNSPEC;
precisions.ai_socktype=SOCK_DGRAM;
statut=getaddrinfo(hote,service,&precisions,&origine);
if(statut<0){ perror("creationAddressUDP.getaddrinfo"); exit(EXIT_FAILURE); }
struct addrinfo *p;
for(p=origine,resultat=origine;p!=NULL;p=p->ai_next)
if(p->ai_family==AF_INET6){ resultat=p; break; }
/* Copie de la structure adresse */
struct sockaddr_storage *adresse=malloc(sizeof(struct sockaddr_storage));
if(adresse==NULL){ perror("creationAddressUDP.malloc"); exit(EXIT_FAILURE); }
memcpy(adresse,resultat->ai_addr,resultat->ai_addrlen);
/* Liberation de la structure d'informations */
freeaddrinfo(origine);
return adresse;
}
void messageUDP(int s,void *adresse,unsigned char *message,int taille){
int nboctets=sendto(s,message,taille,0,(struct sockaddr *)adresse,sizeof(struct sockaddr_storage));
if(nboctets<0){ perror("messageUDP.sento"); exit(EXIT_FAILURE); }
}
int main(int argc,char *argv[]){
if(argc!=3){
fprintf(stderr,"Syntaxe : %s <serveur> <message>\n",argv[0]);
exit(EXIT_FAILURE);
}
char *hote=argv[1];
char *message=argv[2];
char *service="4242";
int s=initialisationSocketUDP("0");
void *cible=creationAddresseUDP(hote,service);
int i;
for(i=0;i<10;i++)
messageUDP(s,cible,(unsigned char *)&i,sizeof i);
free(cible);
close(s);
return 0;
}
$ nc www.polytech-lille.fr 80 GET / HTTP/1.1 Host: pevele.escaut.net Accept: */* User-Agent: WebParTelnet From: Xavier.Redon@polytech-lille.fr If-Modified-Since: Sat, 10 Jan 2003 19:43:31 GMT HTTP/1.1 200 OK Date: Tue, 11 Mar 2003 20:44:29 GMT Server: Apache/1.3.27 (Unix) Debian GNU/Linux PHP/4.2.3 X-Powered-By: PHP/4.2.3 Transfer-Encoding: chunked Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML> <HEAD> <TITLE>Polytech'Lille > Ecole polytechnique universitaire de Lille, ...
$ host -t MX polytech-lille.fr polytech-lille.fr mail is handled by 50 peronne.escaut.net. polytech-lille.fr mail is handled by 100 douaisis.escaut.net. $ nc -C peronne.escaut.net 25 220 <serveur SMTP> ESMTP Sendmail ... HELO <domaine du client SMTP> 250 <serveur SMTP> Hello pleased to meet you MAIL FROM: <Zorro@monterey.org> 250 Zorro@monterey.org... Sender ok RCPT TO: <Xavier.Redon@polytech-lille.fr> 250 Recipient ok DATA 354 Enter mail, end with "." on a line by itself Subject: Z comme zozo Je tiens a vous dire que j'ai prevenu l'ISO de votre manque de respect envers le modele OSI. Zorro . 250 PAA27560 Message accepted for delivery QUIT 221 <serveur SMTP> closing connection $
$ nc pop.polytech-lille.fr 110 +OK <Serveur POP3> POP3 3.3(28) w/IMAP client USER rex +OK User name accepted, password please PASS <shut!!> +OK Mailbox rex open, 1 messages LIST +OK Mailbox scan listing follows 1 2234 . TOP 1 1 +OK Top of message follows Return-Path: <Zorro@monterey.org> . DELE 1 +OK Message deleted QUIT +OK Sayonara $
$ nc pop.polytech-lille.fr 143
* OK pevele Cyrus IMAP4 v1.5.14 server ready
tag1 login rex <shut!!>
tag1 OK User logged in
tag2 list "" *
* LIST () "." "INBOX"
* LIST () "." "INBOX.Drafts"
* LIST () "." "INBOX.Sent"
* LIST () "." "INBOX.Trash"
tag2 OK Completed
tag3 select inbox
* FLAGS (\Answered \Flagged \Draft \Deleted \Seen)
* 241 EXISTS
* 0 RECENT
* OK [UNSEEN 180]
tag3 OK [READ-WRITE] Completed
tag4 fetch 10 body[text]
* 10 FETCH (BODY[TEXT] {125}
Salut,
...
A+,
tag4 OK Completed
tag5 logout
* BYE LOGOUT received
tag5 OK Completed
$
Ce document a été traduit de LATEX par HEVEA