Debian Codegraph @

src/ITGLog/channel.cpp

raw
1 /*
2 * Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG)
3 *
4 * Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti,
5 * Stefano Avallone, Antonio Pescape' (PI)
6 * COMICS (COMputer for Interaction and CommunicationS) Group
7 * Department of Electrical Engineering and Information Technologies
8 * University of Napoli "Federico II".
9 * email : [email protected], [email protected], [email protected],
10 * [email protected], [email protected]
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * For commercial use please refer to D-ITG Professional.
26 */
27
28
29
30 #include "../common/ITG.h"
31 #include "../common/debug.h"
32 #include "../common/thread.h"
33 #include "ITGLog.h"
34 #include "channel.h"
35
36
37
38 void infosntoh(struct info *infos, unsigned int dim) {
39 unsigned int count = 0;
40 long int txTimeSec;
41 long int txTimeUsec;
42 long int rxTimeSec;
43 long int rxTimeUsec;
44 while (count < dim) {
45 infos[count].flowId = ntohl(infos[count].flowId);
46 infos[count].seqNum = ntohl(infos[count].seqNum);
47 infos[count].srcPort = ntohl(infos[count].srcPort);
48 infos[count].destPort = ntohl(infos[count].destPort);
49 infos[count].size = ntohl(infos[count].size);
50 txTimeSec = ntohl(infos[count].txTime1);
51 txTimeUsec = ntohl(infos[count].txTime2);
52 rxTimeSec = ntohl(infos[count].rxTime1);
53 rxTimeUsec = ntohl(infos[count].rxTime2);
54 infos[count].txTime1 = txTimeSec / 3600;
55 infos[count].txTime2 = (txTimeSec % 3600) / 60;
56 infos[count].txTime3 = txTimeSec % 60 + txTimeUsec / 1000000.0;
57
58 infos[count].rxTime1 = rxTimeSec / 3600;
59 infos[count].rxTime2 = (rxTimeSec % 3600) / 60;
60 infos[count].rxTime3 = rxTimeSec % 60 + rxTimeUsec / 1000000.0;
61 count++;
62 }
63 }
64
65
66 void logPacketTCP(int newSockSignal, ofstream *out)
67 {
68
69 int port;
70
71
72 int newSockLog;
73
74
75 struct sockaddr_in sockAddress;
76
77
78 struct sockaddr_in srcAddress;
79
80
81
82
83 int logSock, hold, hold2, dim2;
84
85
86 struct info *infos = (struct info *) malloc(logbuffer_size * sizeof(info));
87 char *infosOffset;
88
89
90 socklen_t sinLen = sizeof(srcAddress);
91
92
93
94
95 fd_set activeSet;
96
97 int dim_infos;
98
99 #ifdef DEBUG
100
101 int numRecvdPkt=0;
102
103
104 int numTrip=0;
105 #endif
106
107
108
109
110 sockAddress.sin_family = AF_INET;
111
112 sockAddress.sin_addr.s_addr = htonl(INADDR_ANY);
113
114
115
116 createDataChannel(sockAddress,newSockSignal,logSock,port,"TCP");
117
118
119 if (listen(logSock, 5) < 0)
120 reportErrorAndExit("logPacketTCP","listen","Error into listen on logSock");
121
122
123
124 newSockLog = accept(logSock, (struct sockaddr *) &srcAddress, &sinLen);
125 PRINTD(1,"logPacketTCP: newSockLog TCP : %d \n",newSockLog);
126 if (newSockLog < 0)
127 reportErrorAndExit("logPacketTCP","accept","Error into accept on logSock");
128
129
130 while (1) {
131
132 FD_ZERO(&activeSet);
133
134 FD_SET((unsigned int)newSockSignal, &activeSet);
135
136 FD_SET((unsigned int)newSockLog, &activeSet);
137
138
139 if (select(FD_SETSIZE, &activeSet, NULL, NULL, 0) < 0)
140 reportErrorAndExit("logPacketTCP","select",
141 "Invalid file descriptor or operation interrupted by a signal - Close first Receiver");
142
143 if (FD_ISSET(newSockSignal, &activeSet)) {
144
145 break;
146
147 } else if (FD_ISSET(newSockLog, &activeSet)) {
148
149
150 dim2 = logbuffer_size * sizeof(struct info);
151 hold = 0;
152 infosOffset = (char *)infos;
153 do {
154 hold2 = recv(newSockLog, (char *)infosOffset, dim2, 0);
155 hold += hold2;
156 dim2 -= hold2;
157 infosOffset += hold2;
158 } while (((hold % sizeof(struct info)) != 0) && (hold2 >= 0));
159
160 if (hold < 0)
161
162 printf("** WARNING ** Data lost - Close First Receiver!\n");
163 else {
164
165 dim_infos = hold / sizeof(struct info);
166 infosntoh(infos, dim_infos);
167
168 #ifdef DEBUG
169 numRecvdPkt = numRecvdPkt + hold / sizeof(struct info);
170 numTrip++;
171 #endif
172
173 if (!(*out).write((char *) infos, hold))
174 printf("** WARNING ** Can't write data!\n");
175 }
176 }
177 }
178
179 #ifdef DEBUG
180
181 char hostName[50];
182
183 char hostIP[20];
184 int rit1 = getnameinfo((sockaddr*)&senderLog,sizeof(senderLog),hostName, INET_ADDRSTRLEN, NULL, 0,
185 NI_NOFQDN);
186 int rit2 = getnameinfo((sockaddr*)&senderLog,sizeof(senderLog),hostIP, INET_ADDRSTRLEN, NULL, 0,
187 NI_NUMERICHOST);
188 if ((rit1 == 0) & (rit2 == 0))
189 printf("Data transmission ended on TCP channel from %s(%s)\n",hostName,hostIP);
190 else if ((rit1 != 0) & (rit2 == 0))
191 printf("Data transmission ended on TCP channel from %s\n",hostIP);
192 else
193 #endif
194 printf("Data transmission ended on TCP channel!\n");
195 fflush(stdout);
196
197
198 free(infos);
199 if (closeSock(logSock) < 0)
200 reportErrorAndExit("logPacketTCP","closeSock","Cannot close logSock");
201 if (closeSock(newSockLog) < 0)
202 reportErrorAndExit("logPacketTCP","closeSock","Cannot close newLogSock");
203 PRINTD(1,"logPacketTCP: Number of received packets : %d \n",numRecvdPkt);
204 PRINTD(1,"logPacketTCP: Number of received infos : %d \n",numTrip);
205 }
206
207
208
209 void logPacketUDP(int newSockSignal, ofstream * out)
210 {
211
212 int port;
213
214
215 struct sockaddr_in sockAddress;
216
217
218
219
220 int logSock, hold;
221
222
223 struct info *infos = (struct info *) malloc(logbuffer_size * sizeof(info));
224
225
226
227
228 fd_set activeSet;
229
230 #ifdef DEBUG
231
232 int numRecvdPkt=0;
233
234
235 int numTrip=0;
236 #endif
237
238
239
240
241 sockAddress.sin_family = AF_INET;
242
243 sockAddress.sin_addr.s_addr = htonl(INADDR_ANY);
244
245
246
247 createDataChannel(sockAddress,newSockSignal,logSock,port,"UDP");
248
249
250
251 while (1) {
252
253 FD_ZERO(&activeSet);
254
255 FD_SET((unsigned int)newSockSignal, &activeSet);
256
257 FD_SET((unsigned int)logSock, &activeSet);
258
259
260
261 if (select(FD_SETSIZE, &activeSet, NULL, NULL, 0) < 0)
262 reportErrorAndExit("logPacketUDP","select",
263 "Invalid file descriptor or operation interrupted by a signal - Close first Receiver");
264
265 if (FD_ISSET(newSockSignal, &activeSet)) {
266
267 break;
268
269 } else if (FD_ISSET(logSock, &activeSet)) {
270
271 hold = recv(logSock, (char *) infos, logbuffer_size * sizeof(struct info), 0);
272
273 int dim_infos = hold / sizeof(struct info);
274 infosntoh(infos, dim_infos);
275
276 #ifdef DEBUG
277 numRecvdPkt = numRecvdPkt + hold / sizeof(struct info);
278 numTrip++;
279 #endif
280
281 if (hold < 0)
282 printf("** WARNING ** Data lost - Close First Receiver!\n");
283
284 if (!(*out).write((char *) infos, hold))
285 printf("** WARNING ** Can't write data!\n");
286 }
287 }
288
289 #ifdef DEBUG
290
291 char hostName[50];
292
293 char hostIP[20];
294 int rit1 = getnameinfo((sockaddr*)&senderLog,sizeof(senderLog),hostName, INET_ADDRSTRLEN, NULL, 0,
295 NI_NOFQDN);
296 int rit2 = getnameinfo((sockaddr*)&senderLog,sizeof(senderLog),hostIP, INET_ADDRSTRLEN, NULL, 0,
297 NI_NUMERICHOST);
298 if ((rit1 == 0) & (rit2 == 0))
299 printf("Data transmission ended on UDP channel from %s(%s)\n",hostName,hostIP);
300 else if ((rit1 != 0) & (rit2 == 0))
301 printf("Data transmission ended on UDP channel from %s\n",hostIP);
302 else
303 #endif
304 printf("Data transmission ended on UDP channel\n");
305 fflush(stdout);
306
307
308 free(infos);
309 if (closeSock(logSock) < 0)
310 reportErrorAndExit("logPacketUDP","closeSock","Cannot close logSock");
311 PRINTD(1,"logPacketUDP: Number of received packets : %d \n",numRecvdPkt);
312 PRINTD(1,"logPacketUDP: Number of received infos : %d \n",numTrip);
313 }
314
315
316
317 void createDataChannel(sockaddr_in sockAddress,int newSockSignal,int &logSock,int &port,
318 const char* protocolName)
319 {
320
321 char msg[100];
322
323
324 int size;
325 unsigned int net_port;
326
327 if (strcmp(protocolName, "TCP") == 0)
328 {
329
330 logSock = socket(PF_INET, SOCK_STREAM, 0);
331 PRINTD(1,"createDataChannel: logSock TCP : %d \n", logSock);
332 if (logSock < 0)
333 reportErrorAndExit("createDataChannel","socket","Cannot create STREAM socket");
334 }
335 else
336 {
337
338 logSock = socket(PF_INET, SOCK_DGRAM, 0);
339 PRINTD(1,"createDataChannel: logSock UDP : %d \n", logSock);
340 if (logSock < 0)
341 reportErrorAndExit("createdataChannel","socket","Cannot create DATAGRAM socket");
342 }
343
344
345 port = findPortFree(logSock);
346 PRINTD(1,"createDataChannel: Port : %d \n", port);
347 if (port == 1 )
348 reportErrorAndExit("createDataChannel","findPortFree","Cannot find a port free");
349 net_port = htonl(port);
350
351 memcpy(msg, &net_port, sizeof(int));
352
353 size = sendto(newSockSignal, (const char *) &msg, sizeof(int), 0,
354 (struct sockaddr *) &senderLog, sizeof(senderLog));
355 PRINTD(1,"createDataChannel: Size bytes sent: %d \n", size);
356 if (size < 0)
357 reportErrorAndExit("createDataChannel","sendto","Cannot send port on newSockSignal");
358 printf("Receiving data on port: %d \n", port);
359 fflush(stdout);
360 }