This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retour.c
161 lines (132 loc) · 4.7 KB
/
retour.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "retour.h"
#include "raler.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#define IPv4 4
#define IPv6 6
#define IPv4LEN 32
#define IPv6LEN 128
#define MAXLEN 1024
#define CHK(v) do{if((v)==-1)raler(1,#v);} while(0)
void init_retour(const int n, struct retour *ret)
{
ret->n = n;
ret->nlib = INIT_LIB;
ret->datagrammes = malloc(INIT_LIB * sizeof(char *));
if (ret->datagrammes == NULL) raler(0, "Malloc");
for (int i=0; i<INIT_LIB; ++i)
{
ret->datagrammes[i] = calloc(MAXLEN, sizeof(char));
if (ret->datagrammes[i] == NULL) raler(0, "calloc");
}
ret->taille_dg = malloc(INIT_LIB * sizeof(int));
if (ret->taille_dg == NULL) raler(0, "malloc");
memset(ret->taille_dg, 0, INIT_LIB*sizeof(int));
ret->type = malloc(INIT_LIB * sizeof(uint8_t));
if (ret->type == NULL) raler(0, "Malloc");
// type à -1 signifie qu'il n'y a rien à utiliser
memset(ret->type, -1, INIT_LIB * sizeof(uint8_t));
ret->port = calloc(INIT_LIB, sizeof(uint16_t));
if (ret->port == NULL) raler(0, "Malloc");
ret->Ip = malloc(INIT_LIB * sizeof(char *));
if (ret->Ip == NULL) raler(0, "Malloc");
for (int i=0; i<INIT_LIB; ++i)
{
ret->Ip[i] = calloc(IP_S, sizeof(char));
if (ret->Ip[i] == NULL) raler(0, "Malloc");
}
}
void free_retour(struct retour *ret)
{
for (int i = 0; i < ret->nlib; ++i)
free(ret->datagrammes[i]);
free(ret->datagrammes);
free(ret->taille_dg);
free(ret->type);
free(ret->port);
for (int i = 0; i < ret->nlib; ++i)
free(ret->Ip[i]);
free(ret->Ip);
}
int recherche_librairie(const char* addr,const uint16_t port,const uint8_t type,
struct retour *ret)
{
int ind = 0;
int ind_libre, is_libre = 0;
// on recherche si on a déjà commencé un dg pour la librairie
for (; ind<ret->nlib; ++ind)
{
if ((strncmp(addr, ret->Ip[ind], IP_S) == 0) &&
port == ret->port[ind])
{
break;
}
if ((ret->port[ind] == 0) && (is_libre == 0))
{
is_libre = 1;
ind_libre = ind;
}
}
if (ind < ret->nlib) // on a trouvé la librairie
{
return ind;
}
else
{
if (is_libre == 1) // une place est libre
{
memcpy(ret->Ip[ind_libre], addr, IP_S);
ret->port[ind_libre] = port;
ret->type[ind_libre] = type;
return ind_libre;
}
else // sinon on rajoute une case
{
ret->nlib += 1;
ret->datagrammes = realloc(ret->datagrammes,
ret->nlib * sizeof(char *));
if (ret->datagrammes == NULL) raler(0, "realloc");
ret->datagrammes[ret->nlib-1] = calloc(MAXLEN, sizeof(char));
if (ret->datagrammes[ret->nlib-1] == NULL) raler(0, "calloc");
ret->taille_dg = realloc(ret->taille_dg, ret->nlib*sizeof(int));
if (ret->taille_dg == NULL) raler(0, "realloc");
ret->taille_dg[ret->nlib-1] = 0;
ret->type = realloc(ret->type, ret->nlib * sizeof(uint8_t));
if (ret->type == NULL) raler(0, "realloc");
ret->type[ret->nlib-1] = type;
ret->port = realloc(ret->port, ret->nlib * sizeof(uint16_t));
if (ret->port == NULL) raler(0, "realloc");
ret->port[ret->nlib-1] = port;
ret->Ip = realloc(ret->Ip, ret->nlib * sizeof(char *));
if (ret->Ip == NULL) raler(0, "realloc");
ret->Ip[ret->nlib-1] = malloc(IP_S * sizeof(char));
memcpy(ret->Ip[ret->nlib-1], addr, IP_S);
return ret->nlib - 1;
} // if is_libre
} // if ind < nlib
} // fonction rechercher librairie
void ajouter_livre(char *titre, const int ind_lib, struct retour *ret)
{
if (ret->taille_dg[ind_lib] == 0) // on n'a pas encore commencé à écrire
{ // le dg
*(uint16_t *) ret->datagrammes[ind_lib] = htons(1);
strncpy(&ret->datagrammes[ind_lib][2], titre, TITRE_S);
ret->taille_dg[ind_lib] = 2 + TITRE_S;
}
else
{
int old_ind = ret->taille_dg[ind_lib];
int new_len = ret->taille_dg[ind_lib] + TITRE_S;
if (new_len > MAXLEN)
raler(0, "Trop de livre à commander à la librairie");
uint16_t old_nb_liv = ntohs(*(uint16_t *) ret->datagrammes[ind_lib]);
*(uint16_t *) ret->datagrammes[ind_lib] = htons(old_nb_liv + 1);
strncpy(&ret->datagrammes[ind_lib][old_ind], titre, TITRE_S);
ret->taille_dg[ind_lib] += new_len;
}
}