Projet de C/Réseau Création d'une boite aux lettres basée sur les Protocoles TCP et UDP.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bal.h 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. //--------------------------PROG C ET RESEAUX-----------------------
  2. // Nom : Quintana
  3. // Prénom : Béranger
  4. // Grp : 3AE E2
  5. //------------------------------------------------------------------
  6. #ifndef __tsock__
  7. //Variable qui donne la longueur max d'un message
  8. int maxsize=9999;
  9. //Définitions des types
  10. typedef struct BAL
  11. {
  12. int num ;
  13. int nb;
  14. struct LETTRE * l_first ;
  15. struct LETTRE * l_last ;
  16. struct LETTRE * l_current ;
  17. struct BAL * suiv ;
  18. }BAL ;
  19. typedef struct LETTRE
  20. {
  21. int num ;
  22. int lg;
  23. char *message ;
  24. struct LETTRE * suiv ;
  25. }LETTRE ;
  26. typedef struct LISTE_BAL
  27. {
  28. struct BAL * first ;
  29. struct BAL * last ;
  30. struct BAL * current ;
  31. int nb;
  32. }LISTE_BAL;
  33. //---------------------------------------------------
  34. //---------Déclaration des fonctions-----------------
  35. //---------------------------------------------------
  36. LISTE_BAL* init_BAL();
  37. void add_BAL(int num , LISTE_BAL* liste);
  38. BAL* find_BAL(LISTE_BAL*liste, int num);
  39. int find_BALR(LISTE_BAL*liste, int num);
  40. void add_LETTRE (int n, int lg, BAL* bal , char* mess);
  41. void printBAL(BAL* bal,int lg);
  42. void empty(BAL*bal);
  43. void EBAL(int port, char* dest, int nb_message, int lg_msg, int nBAL);
  44. void SBAL(int port, char* dest);
  45. void RBAL(int port, char* dest, int nBAL);
  46. //---------------------------------------------------
  47. //--------------------GESTION BAL--------------------
  48. //---------------------------------------------------
  49. LISTE_BAL* init_BAL()
  50. {
  51. LISTE_BAL* liste =(LISTE_BAL*)malloc(sizeof(struct LISTE_BAL));
  52. liste->first = NULL;
  53. liste->last=NULL;
  54. liste->current = NULL;
  55. liste->nb=0;
  56. return liste ;
  57. }
  58. //------------------------------------------------
  59. //----Afficher le contenu d'une Liste de BAL------
  60. //6-----------------------------------------------
  61. void printLISTE(struct LISTE_BAL* liste)
  62. {
  63. printf(" __________________________________________\n");
  64. printf(" Check général des BAL de notre liste :\n\n");
  65. printf(" %d BAL dans notre liste \n\n",liste->nb);
  66. liste->current=liste->first;
  67. while (liste->current!=NULL)
  68. {
  69. printf(" BAL n°%d : %d Lettres \n",liste->current->num,liste->current->nb);
  70. liste->current=liste->current->suiv;
  71. }
  72. printf(" __________________________________________\n\n");
  73. }
  74. //------------------------------------------------
  75. //--------Afficher le contenu d'une BAL-----------
  76. //------------------------------------------------
  77. void printBAL(BAL* bal,int lg)
  78. {
  79. printf("Contenu de la BAL n°%d qui contient %d lettres \n",bal->num,bal->nb) ;
  80. bal->l_current=bal->l_first;
  81. printf("\n");
  82. int n=1;
  83. while(bal->l_current!=NULL)
  84. {
  85. printf("BAL n°%d | %d Lettres, lettre n°%d : [",bal->num,bal->nb,n);
  86. afficher_message(bal->l_current->message,lg);
  87. bal->l_current=bal->l_current->suiv;
  88. n++;
  89. }
  90. printf("\n\n");
  91. }
  92. //------------------------------------------------
  93. //----------------Ajouter une BAL-----------------
  94. //------------------------------------------------
  95. void add_BAL(int n, LISTE_BAL * liste)
  96. {
  97. BAL *nouv =malloc(sizeof(struct BAL));
  98. nouv->num=n;
  99. nouv->nb=0;
  100. nouv->l_first=NULL;
  101. nouv->l_last=NULL;
  102. nouv->l_current=NULL;
  103. nouv->suiv=NULL;
  104. if (liste->first == NULL)
  105. {
  106. liste->first = nouv ;
  107. liste->last = nouv ;
  108. }
  109. else
  110. {
  111. liste->last->suiv= nouv ;
  112. liste->last=nouv ;
  113. }
  114. liste->nb++;
  115. }
  116. //------------------------------------------------
  117. //----Retourne une BAL en fonction de son num-----
  118. //6-----------------------------------------------
  119. BAL* find_BAL(LISTE_BAL*liste, int num)
  120. {
  121. BAL* bal=malloc(sizeof(struct BAL));
  122. liste->current=liste->first;
  123. if (liste->first==NULL)
  124. {
  125. add_BAL(num,liste);
  126. bal=liste->first;
  127. }
  128. else
  129. {
  130. liste->current=liste->first;
  131. if (liste->first==liste->last)
  132. {
  133. if (liste->first->num==num)
  134. bal=liste->current;
  135. else
  136. {
  137. add_BAL(num,liste);
  138. bal=liste->last;
  139. }
  140. }
  141. else if (liste->first->num==num)
  142. bal=liste->first;
  143. else
  144. {
  145. int var=0;
  146. while(var==0)
  147. {
  148. if (liste->current->suiv==NULL)
  149. var=-1;
  150. else
  151. {
  152. liste->current=liste->current->suiv;
  153. if (liste->current->num==num)
  154. var=1;
  155. if (liste->current==NULL)
  156. var=-1;
  157. }
  158. }
  159. if (var==1)
  160. bal=liste->current;
  161. else
  162. {
  163. add_BAL(num,liste);
  164. bal=liste->last;
  165. }
  166. }
  167. }
  168. return bal;
  169. }
  170. //--------------------------------------------------------------------------------------------
  171. //----------------------------------------findBALR--------------------------------------------
  172. //-------Retourne -1 si BAL inexistante ou BAL Vide, lg 1ère lettre si BAL existante----------
  173. //--------------------------------------------------------------------------------------------
  174. int find_BALR(LISTE_BAL*liste, int num)
  175. {
  176. int ret;
  177. if (liste->first==NULL)
  178. {
  179. ret=-1;
  180. }
  181. else
  182. {
  183. liste->current=liste->first;
  184. if (liste->current==liste->last)
  185. {
  186. if (liste->current->num==num)
  187. {
  188. if (liste->current->l_first==NULL)
  189. ret=-1;
  190. else
  191. ret=liste->current->l_first->lg;
  192. }
  193. else
  194. ret=-1;
  195. }
  196. else if (liste->first->num==num)
  197. {
  198. if (liste->current->l_first==NULL)
  199. ret=-1;
  200. else
  201. ret=liste->current->l_first->lg;
  202. }
  203. else
  204. {
  205. int var=0;
  206. while(var==0)
  207. {
  208. if (liste->current->suiv==NULL)
  209. var=-1;
  210. else
  211. {
  212. liste->current=liste->current->suiv;
  213. if (liste->current->num==num)
  214. var=1;
  215. if (liste->current==NULL)
  216. var=-1;
  217. }
  218. }
  219. if (var==1)
  220. {
  221. if (liste->current->l_first==NULL)
  222. ret=-1;
  223. else
  224. ret=liste->current->l_first->lg;
  225. }
  226. else
  227. ret=-1;
  228. }
  229. }
  230. return ret;
  231. }
  232. //-----------------------------------------------------------------
  233. //----------------Ajouter une lettre en fin de BAL-----------------
  234. //-----------------------------------------------------------------
  235. void add_LETTRE (int n, int lg, BAL* bal , char* mess)
  236. {
  237. bal->nb=(bal->nb)+1;
  238. LETTRE* nouv;
  239. nouv=(LETTRE*)malloc(sizeof(LETTRE));
  240. nouv->num=n+1;
  241. nouv->lg=lg;
  242. nouv->suiv=NULL;
  243. if (bal->l_first==NULL)
  244. {
  245. bal->l_first=nouv;
  246. bal->l_last=nouv;
  247. bal->l_current=nouv;
  248. }
  249. else
  250. {
  251. bal->l_last->suiv=nouv;
  252. bal->l_last=bal->l_last->suiv;
  253. }
  254. nouv->message=malloc(lg* sizeof(char));
  255. for (int i=0 ; i<lg ; i++)
  256. nouv->message[i] = mess[i];
  257. }
  258. //-------------------------------------------------------------------------------------
  259. //----------------Détruit une liste de BAL en fin d'utilisation de BAL-----------------
  260. //6------------------------------------------------------------------------------------
  261. void empty(BAL*bal)
  262. {
  263. bal->l_current=bal->l_first;
  264. while(bal->l_current!=NULL)
  265. {
  266. bal->l_current=bal->l_current->suiv;
  267. free(bal->l_first);
  268. bal->l_first=bal->l_current;
  269. (bal->nb)--;
  270. }
  271. }
  272. void EBAL(int port, char* dest, int nb_message, int lg_msg, int nBAL)
  273. {
  274. //Déclarations
  275. int sock;
  276. struct sockaddr_in addr_distant ;
  277. int lg_addr_distant=sizeof(addr_distant);
  278. struct hostent *hp;
  279. char motif;
  280. char * message=malloc(lg_msg*sizeof(char));
  281. int envoi=-1;
  282. int lg_pdu=50;
  283. int lg_recv;
  284. char*pdu=malloc(lg_pdu*sizeof(char));
  285. //---------------------------------------
  286. //--------Etablissement connexion--------
  287. //---------------------------------------
  288. printf(" SOURCE : Emission de lettres pour la BAL n°%d\n",nBAL);
  289. printf("____________________________________________________________________\n\n");
  290. sprintf(pdu,"0 %d %d %d",nBAL, nb_message,lg_msg);
  291. //Création socket
  292. if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
  293. {
  294. printf("Erreur à l'ouverture du Socket Stream");
  295. exit(1);
  296. }
  297. //Construction adresse socket distant
  298. memset((char*)&addr_distant,0,sizeof(addr_distant));
  299. addr_distant.sin_family=AF_INET; //Internet
  300. addr_distant.sin_port=port; //Numéro de Port
  301. //Affectation IP
  302. if((hp=gethostbyname(dest))==NULL)
  303. {
  304. printf("Erreur de requête IP.\n");
  305. exit(1);
  306. }
  307. memcpy((char*)&(addr_distant.sin_addr.s_addr), hp->h_addr , hp->h_length);
  308. //Demande de connexion
  309. if (connect(sock,(struct sockaddr *)&addr_distant,sizeof(addr_distant))==-1)
  310. {
  311. printf("Erreur lors de la connexion, en attente de la tentative suivante \n");
  312. exit(1);
  313. }
  314. //-----------------------------------------
  315. //----------------Envoi PDU----------------
  316. //-----------------------------------------
  317. if ((envoi=write(sock,pdu,lg_pdu))==-1)
  318. {
  319. printf("Echec de l'envoi du PDU Emetteur (fonction write en défaut)\n");
  320. exit(1);
  321. }
  322. //-----------------------------------------
  323. //----------TRANSFERT DE DONNEES-----------
  324. //-----------------------------------------
  325. for (int i=1; i<=nb_message;i++)
  326. {
  327. printf("SOURCE : lettre n°%d (%d) [", i,lg_msg);
  328. //Création du message
  329. construire_message2(message,motif,lg_msg,i);
  330. printbuffer2(nBAL,message);
  331. afficher_message(message,lg_msg);
  332. //Envoi du message
  333. if ((envoi=write(sock,message,(lg_msg)/*,0,(struct sockaddr*)&addr_distant,lg_addr_distant)*/))==-1)
  334. {
  335. printf("Echec de l'envoi du message (fonction write en défaut)\n");
  336. exit(1);
  337. }
  338. }
  339. //Fermeture connexion
  340. if(shutdown(sock,2)==-1)
  341. {
  342. printf("Erreur à la fermeture de la connexion TCP \n");
  343. exit(1);
  344. }
  345. //Fermeture Socket
  346. if (close(sock)==-1)
  347. {
  348. printf("Echec de la fermeture du socket distant");
  349. exit(1);
  350. }
  351. free(message);
  352. free(pdu);
  353. printf("Envoi effectué avec succès\n");
  354. }
  355. void SBAL(int port, char*dest)
  356. {
  357. //Déclarations
  358. int sock , sock2; //sock bis local orienté échanges
  359. struct sockaddr* addr_distant;
  360. struct sockaddr_in addr_local;
  361. int lg_addr_distant=sizeof(addr_distant);
  362. int lg_addr_local=sizeof(addr_local);
  363. struct hostent *hp;
  364. char motif;
  365. char *message;
  366. int lg_recv=-1;
  367. int lg_sent=-1;
  368. int lg_pdu=50;
  369. int type=-1;
  370. int nb;
  371. int lg;
  372. int n=1;
  373. int nBAL;
  374. BAL*bal=malloc(sizeof(struct BAL));
  375. char *pdu;//=malloc(sizeof(char));
  376. LISTE_BAL* liste;
  377. //----------------------------------
  378. //------------Connexion ------------
  379. //----------------------------------
  380. //Création socket local
  381. if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
  382. {
  383. printf("Echec de la création d'un socket local\n");
  384. exit(1);
  385. }
  386. //Construction adresse socket local | Affectation port et domaine
  387. memset((char*)&addr_local, 0 , sizeof(addr_local));
  388. addr_local.sin_family=AF_INET;
  389. addr_local.sin_addr.s_addr=INADDR_ANY;
  390. addr_local.sin_port=port;
  391. //Bind
  392. if (bind(sock,(struct sockaddr *)&addr_local, lg_addr_local)==-1)
  393. {
  394. printf("Echec du bind.\n");
  395. exit(1);
  396. }
  397. //Check connexions entrantes
  398. if (listen(sock,100)<0)
  399. {
  400. printf("Trop de connexions en attentes, échec de la demande\n");
  401. exit(1);
  402. }
  403. liste=init_BAL();
  404. while (1)
  405. {
  406. if ((sock2=accept(sock,(struct sockaddr*)&addr_distant,&lg_addr_distant))==-1)
  407. {
  408. printf("Refus de connexion par le serveur\n");
  409. exit(1);
  410. }
  411. pdu=malloc(50*sizeof(char));
  412. if((lg_pdu=read(sock2,pdu, lg_pdu))<0)
  413. {
  414. printf("Echec de lecture du PDU entrant\n");
  415. exit(1);
  416. }
  417. sscanf(pdu, "%d %d %d %d", &type, &nBAL, &nb, &lg);
  418. //GESTION EMETTEUR
  419. if (atoi(pdu)==0)
  420. {
  421. printf(" ||||||| Réception des lettres pour la BAL n°%d |||||\n\n",nBAL);
  422. message=malloc(lg*sizeof(char));
  423. int n=0;
  424. sscanf(pdu, "%d %d %d %d", &type, &nBAL, &nb, &lg);
  425. bal=find_BAL(liste,nBAL);
  426. while (n!=nb)
  427. {
  428. message = malloc(lg* sizeof(char));
  429. if ((lg_recv = read(sock2, message, lg)) == -1)
  430. {
  431. printf("Erreur de lecture\n");
  432. exit(1);
  433. }
  434. if (lg_recv>0)
  435. {
  436. add_LETTRE(n,lg, bal,message);
  437. }
  438. n++;
  439. }
  440. printBAL(bal,lg);
  441. }
  442. //GESTION RECEPTEUR
  443. else if (atoi(pdu)==1)
  444. {
  445. sscanf(pdu, "%d %d", &type, &nBAL);
  446. printf(" ||||||| Restitution des lettres de la BAL n°%d |||||||\n\n",nBAL);
  447. lg=find_BALR(liste,nBAL);
  448. if (lg==-1) // Gestion du cas ou la BAL est vide, on envoie un PDU qui sera analysé par le récepteur.
  449. {
  450. printf(" BAL inexistante, PDU=0 pour informer le récepteur\n\n");
  451. sprintf(pdu,"%d %d",lg,nb);
  452. //printf ("PDU à envoyer : %d\n",lg);
  453. int lg_sent=-1;
  454. nb=1;
  455. if ((lg_sent=write(sock2,pdu,lg_pdu))==-1) /*,0,(struct sockaddr*)&addr_distant,lg_addr_distant)*/
  456. {
  457. printf("Echec de l'envoi du PDU (fonction write en défaut)\n");
  458. exit(1);
  459. }
  460. }
  461. else
  462. {
  463. bal=find_BAL(liste,nBAL);
  464. bal->l_current=bal->l_first;
  465. while(bal->l_current!=NULL)
  466. {
  467. lg=bal->l_current->lg;
  468. nb=bal->nb;
  469. sprintf(pdu,"%d %d",lg,nb);
  470. if ((lg_sent=write(sock2,pdu,lg_pdu))==-1) /*,0,(struct sockaddr*)&addr_distant,lg_addr_distant)*/
  471. {
  472. printf("Echec de l'envoi du PDU Emetteur (fonction write en défaut)\n");
  473. exit(1);
  474. }
  475. message=malloc(lg*sizeof(char));
  476. message=bal->l_current->message;
  477. if ((lg_sent=write(sock2,message,lg))==-1)
  478. {
  479. printf("Erreur lors de l'envoi du message n°%d\n",n);
  480. exit(1);
  481. }
  482. printf("BAL n°%d : Restitution de la lettre n°%d (%d) [",nBAL,n,lg);
  483. afficher_message(message,lg);
  484. bal->l_current=bal->l_current->suiv;
  485. n++;
  486. }
  487. empty(bal);
  488. if ((shutdown(sock2 , 2))==-1)
  489. {
  490. printf("Erreur à la fermeture de la connexion : shutdown\n");
  491. exit(1);
  492. }
  493. }
  494. }
  495. else
  496. {
  497. printf("PDU non reconnu, on quitte par sécurité\n");
  498. exit(1);
  499. }
  500. printLISTE(liste);
  501. free(pdu);
  502. free(message);
  503. }
  504. }
  505. void RBAL(int port, char* dest, int nBAL)
  506. {
  507. //Déclarations
  508. int sock;
  509. struct sockaddr_in addr_distant;
  510. int lg_addr_distant = sizeof(addr_distant);
  511. struct hostent *hp;
  512. char *message; //Penser au free en fin de programme pour libérer l'espace mémoire
  513. int envoi = -1;
  514. int lg_pdu=50;
  515. int lg_recv=-1;
  516. int lg;
  517. int nb;
  518. char *pdu = malloc(lg_pdu*sizeof(char));
  519. //---------------------------------------
  520. //--------Etablissement connexion--------
  521. //---------------------------------------
  522. sprintf(pdu,"1 %d",nBAL);
  523. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  524. {
  525. printf("Erreur à l'ouverture du Socket Stream");
  526. exit(1);
  527. }
  528. //Construction adresse socket distant
  529. memset((char *) &addr_distant, 0, sizeof(addr_distant));
  530. addr_distant.sin_family = AF_INET; //Internet
  531. addr_distant.sin_port = port; //Numéro de Port
  532. //Affectation IP
  533. if ((hp = gethostbyname(dest)) == NULL) {
  534. printf("Erreur de requête IP.\n");
  535. exit(1);
  536. }
  537. memcpy((char *) &(addr_distant.sin_addr.s_addr), hp->h_addr, hp->h_length);
  538. //Demande de connexion
  539. if (connect(sock, (struct sockaddr *) &addr_distant, sizeof(addr_distant)) == -1) {
  540. printf("Erreur lors de la connexion, en attente de la tentative suivante \n");
  541. exit(1);
  542. }
  543. //-----------------------------------------
  544. //----------------Envoi PDU----------------
  545. //-----------------------------------------
  546. if ((envoi = write(sock, pdu, lg_pdu)) == -1) /*,0,(struct sockaddr*)&addr_distant,lg_addr_distant)*/
  547. {
  548. printf("Echec de l'envoi du PDU Emetteur (fonction write en défaut)\n");
  549. exit(1);
  550. }
  551. char*lgmsg=malloc(maxsize* sizeof(char));
  552. nb=10;
  553. int n=1;
  554. lg_recv=1;
  555. printf(" PUITS : Réception du contenu de la BAL n°%d\n",nBAL);
  556. printf("____________________________________________________________________\n\n");
  557. while(n<=nb)
  558. {
  559. if ((lg_recv=read(sock,lgmsg,lg_pdu))==-1)
  560. {
  561. printf("Erreur à la réception du PDU de longueur de message\n");
  562. exit(1);
  563. }
  564. sscanf(lgmsg,"%d %d", &lg , &nb);
  565. if (lg==-1)
  566. {
  567. printf(" ATTENTION : Pas de courrier à récupérer dans la BAL n°%d\n\n",nBAL);
  568. exit(0);
  569. }
  570. message=malloc(lg*sizeof(char));
  571. if ((lg_recv=read(sock,message,lg))==-1)
  572. {
  573. printf("Erreur à la réception du message\n");
  574. exit(1);
  575. }
  576. printf("PUITS : Réception de la lettre n°%d : [",n);
  577. afficher_message(message,lg);
  578. n++;
  579. }
  580. printf("Fermeture de la Connexion\n");
  581. //Ciao le socket
  582. if(close(sock)==-1)
  583. {
  584. printf("Impossible de fermer le socket");
  585. exit(1);
  586. }
  587. }
  588. #endif