File: Network.cpp

package info (click to toggle)
cigi-ccl 3.3.3a%2Bsvn818-10
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 7,332 kB
  • sloc: cpp: 62,566; makefile: 541; ruby: 400; ansic: 313; sh: 68
file content (325 lines) | stat: -rw-r--r-- 7,999 bytes parent folder | download | duplicates (6)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/** <pre>
 *  The Multi-Purpose Viewer
 *  Copyright (c) 2004 The Boeing Company
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  FILENAME:   Network.cpp
 *  LANGUAGE:   C++
 *  CLASS:      UNCLASSIFIED
 *  PROJECT:    Multi-Purpose Viewer
 *
 *  PROGRAM DESCRIPTION:
 *  This class contains the data and methods necessary to
 *   handle the network interface.
 *
 *  MODIFICATION NOTES:
 *  DATE     NAME                                SCR NUMBER
 *  DESCRIPTION OF CHANGE........................
 *
 *  03/29/2004 Andrew Sampson                       MPV_CR_DR_1
 *  Initial Release.
 *
 *  07/10/2006 Greg Basler                       1.7.2
 *  Corrected a problem with the code that cause a stray
 *  pointer due to the "JUST_IP_ADDRESSES" declaration.
 * </pre>
 *  The Boeing Company
 *  1.7.2
 */


#include "Network.h"

#include <stdio.h>
#include <fcntl.h>
#include <memory.h>

// ================================================
// Network
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Network::Network()
{
	valid = false;
	#ifndef JUST_IP_ADDRESSES
	saddr = NULL;
	#endif
}

// ================================================
// ~Network
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Network::~Network()
{
	closeSocket();
	#ifndef JUST_IP_ADDRESSES
	freeaddrinfo(saddr);
	#endif
}

// ================================================
// openSocket
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
bool Network::openSocket( const char * ip, const int port_send, const int port_rcv )
{
	// Initialize communications.
	InitializeComm(ip, port_send, port_rcv);

	return valid;
}

// ================================================
// openSocket
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
bool Network::openSocket(const int port_rcv )
{
	// Initialize communications.
	InitializeComm(port_rcv);

	return valid;
}


// ================================================
// closeSocket
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
void Network::closeSocket()
{
	if( !valid ) return;


#ifdef WIN32
	// Clean up the sockets.
	closesocket(sndsock);
	closesocket(rcvsock);

   // Clean up WinSock.  Again, this is not needed for non-Windows platforms.
	WSACleanup();
#else

	// Clean up the sockets.
	close(sndsock);
	close(rcvsock);

#endif

	valid = false;
}


// ================================================
// send
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
int Network::send( unsigned char * sendbuff, int sendsize )
{
	if( !valid) return -1;

	#ifdef JUST_IP_ADDRESSES
	if(!saddr.sin_family ) return -1;
	return sendto(sndsock, (const char *)sendbuff, sendsize, 0, (struct sockaddr *)&saddr,
			sizeof(struct sockaddr));
	#else
	if(!saddr) return -1;
	return sendto(sndsock, (const char *)sendbuff, sendsize, 0,
		saddr->ai_addr, saddr->ai_addrlen);
	#endif // JUST_IP_ADDRESSES


}

// ================================================
// recv
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
int Network::recv( unsigned char * rcvbuff, int recvsize )
{
	if( !valid ) return -1;
	return recvfrom(rcvsock, (char *)rcvbuff, recvsize, 0, NULL, 0);
}



// ================================================
// InitializeComm
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
void Network::InitializeComm(const char *ipaddr, const int sndport, const int rcvport)
{
	int status;

#ifdef WIN32
	// Initialize winsock.  If you are using Berkeley sockets on a
	// non-Win32 platform, you can leave out these first two lines.
	WSADATA wsainfo;
	WSAStartup(MAKEWORD(1, 1), &wsainfo);
#endif

	// Open the connection for sending.

#ifdef JUST_IP_ADDRESSES
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(sndport);	// Use whatever port your host listens on
	saddr.sin_addr.s_addr = inet_addr(ipaddr);	// IP address of host

	sndsock = socket(AF_INET, SOCK_DGRAM, 0);
	if( sndsock < 0 ) {
		// socket could not be created
		valid = false;
		return;
	}
#else
	if(saddr)
	{
		freeaddrinfo(saddr);
		saddr = NULL;
	}

	char portstr[6];
	snprintf(portstr, sizeof(portstr), "%u", sndport);
	struct addrinfo hint;
	hint.ai_flags = 0;
	//hint.ai_family = AF_INET | AF_INET6;
	hint.ai_family = AF_UNSPEC;
	hint.ai_socktype = SOCK_DGRAM;
	hint.ai_protocol = IPPROTO_UDP;
	hint.ai_addrlen = 0;
	hint.ai_addr = NULL;
	hint.ai_canonname = NULL;
	hint.ai_next = NULL;
	int result = getaddrinfo(ipaddr, portstr, &hint, &saddr);
	if(result)
	{
		if(saddr)
		{
			freeaddrinfo(saddr);
			saddr = NULL;
		}
		return;
	}
	sndsock = socket(saddr->ai_family, saddr->ai_socktype,
		saddr->ai_protocol);
#endif // JUST_IP_ADDRESSES

	// Open the connection for receiving.
   memset(&raddr,0,sizeof(raddr));
	raddr.sin_family = AF_INET;
	raddr.sin_port = htons(rcvport);	// Use whatever port your host sends to
	raddr.sin_addr.s_addr = htonl(INADDR_ANY);

	rcvsock = socket(AF_INET, SOCK_DGRAM, 0);
	if( rcvsock < 0 ) {
		// socket could not be created
		valid = false;
		return;
	}

	// Make the address reusable.
	int sockparam = 1; /* TRUE; */
	setsockopt(rcvsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&sockparam, sizeof(int));

	// Make the socket non-blocking.
#ifdef WIN32
	// This is how sockets are put in non-blocking mode on Windows.
	unsigned long arg = 1L;
	ioctlsocket(rcvsock, FIONBIO, &arg);
#else
	// This is how sockets are put in non-blocking mode on non-Windows operating systems (and Cygwin).
	fcntl( rcvsock, F_SETFL, O_NONBLOCK );
#endif

	// Bind the socket to the local address.
	status =
		bind(rcvsock, (struct sockaddr *)&raddr, sizeof(raddr));
	if( status != 0 ) {
		// socket could not be bound
		valid = false;
		return;
	}


	// FIXME - more error checking
	valid = true;
}

// ================================================
// InitializeComm
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
void Network::InitializeComm(const int rcvport)
{
	int status;

#ifdef WIN32
	// Initialize winsock.  If you are using Berkeley sockets on a
	// non-Win32 platform, you can leave out these first two lines.
	WSADATA wsainfo;
	WSAStartup(MAKEWORD(1, 1), &wsainfo);
#endif

	sndsock = socket(AF_INET, SOCK_DGRAM, 0);
	if( sndsock < 0 ) {
		// socket could not be created
		valid = false;
		return;
	}

	// Disable sending.
#ifdef JUST_IP_ADDRESSES
	saddr.sin_family = 0;
#else
	if(saddr)
	{
		freeaddrinfo(saddr);
		saddr = NULL;
	}
#endif // JUST_IP_ADDRESSES

	// Open the connection for receiving.
   memset(&raddr,0,sizeof(raddr));
	raddr.sin_family = AF_INET;
	raddr.sin_port = htons(rcvport);	// Use whatever port your host sends to
	raddr.sin_addr.s_addr = htonl(INADDR_ANY);

	rcvsock = socket(AF_INET, SOCK_DGRAM, 0);
	if( rcvsock < 0 ) {
		// socket could not be created
		valid = false;
		return;
	}

	// Make the address reusable.
	int sockparam = 1; /* TRUE; */
	setsockopt(rcvsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&sockparam, sizeof(int));

	// Make the socket non-blocking.
#ifdef WIN32
	// This is how sockets are put in non-blocking mode on Windows.
	unsigned long arg = 1L;
	ioctlsocket(rcvsock, FIONBIO, &arg);
#else
	// This is how sockets are put in non-blocking mode on non-Windows operating systems (and Cygwin).
	fcntl( rcvsock, F_SETFL, O_NONBLOCK );
#endif

	// Bind the socket to the local address.
	status =
		bind(rcvsock, (struct sockaddr *)&raddr, sizeof(raddr));
	if( status != 0 ) {
		// socket could not be bound
		valid = false;
		return;
	}


	// FIXME - more error checking
	valid = true;
}