Debian Codegraph @

src/libITG/ITGapi.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 #include "../common/ITG.h"
30
31 static int socket_r = 0;
32
33 #ifdef WIN32
34 int InitializeWinsock(WORD wVersionRequested)
35 {
36 WSADATA wsaData;
37 int err;
38
39 err = WSAStartup(wVersionRequested, &wsaData);
40
41
42
43 if (err != 0)
44 return 0;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
59 return 0;
60
61
62 return 1;
63 }
64
65
66 char nameProgram[]="ITGapi";
67 #endif
68
69
70
71 int DITGsend(char *sender, char *message)
72 {
73 #ifdef WIN32
74 if (InitializeWinsock(MAKEWORD(1,1)) != 1) {
75 fprintf(stderr, "WSAStartup() failed");
76 exit(1);
77 }
78 #endif
79
80 struct addrinfo* locale = 0;
81
82 freeaddrinfo(locale);
83
84
85
86 getaddrinfo("0.0.0.0", NULL, NULL, &locale);
87 socket_r = socket(locale->ai_family, SOCK_DGRAM, 0);
88 if (socket_r < 0)
89 printf("error into create a socket - DITGSend function \n");
90
91 #ifdef UNIX
92 int flags;
93 flags = fcntl(socket_r, F_GETFL, 0);
94 fcntl(socket_r, F_SETFL, flags | O_NONBLOCK);
95 #endif
96
97 #ifdef WIN32
98 unsigned long i = 1;
99 ioctlsocket(socket_r, FIONBIO, &i);
100 #endif
101
102
103
104
105
106
107
108
109 struct sockaddr_in Sender;
110 struct hostent *host;
111
112 Sender.sin_family = AF_INET;
113 if (!(host = gethostbyname(sender))) {
114 cerr << endl << "DITGsend: Invalid destination address" << endl;
115 return -1;
116 }
117 memcpy((char *) &Sender.sin_addr, host->h_addr, host->h_length);
118 Sender.sin_port = htons(DEFAULT_PORT_SENDER_MANAGER);
119 if (sendto(socket_r, message, strlen(message), 0, (struct sockaddr *) &Sender, sizeof(Sender)) != (int) strlen(message))
120 return -1;
121 return 0;
122 }
123
124
125 int catchManagerMsg(char **senderIP, char **msg)
126 {
127 struct sockaddr_in Sender;
128 socklen_t SenderSlen = sizeof(Sender);
129 uint16_t length;
130 uint8_t msgtype;
131 char *buffer;
132
133 if (!socket_r)
134 return MNG_NOMSG;
135
136 buffer = (char *) malloc(MAX_FLOW_LINE_SIZE);
137
138 if (recvfrom(socket_r, buffer, MAX_FLOW_LINE_SIZE, 0, (struct sockaddr *) &Sender, &SenderSlen) < 0) {
139 free(buffer);
140 return MNG_NOMSG;
141 }
142 *senderIP = (char *) malloc(30);
143 strncpy(*senderIP, inet_ntoa(Sender.sin_addr), 30);
144 msgtype = *(uint8_t *) buffer;
145 length = *((uint16_t *) &(buffer[1]));
146 memmove(buffer, &buffer[3], length);
147 buffer[length] = '\0';
148 *msg = buffer;
149
150 return msgtype;
151 }