File: comsocket.cpp

package info (click to toggle)
musicbrainz 1.0.1.final-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,168 kB
  • ctags: 2,610
  • sloc: ansic: 16,052; sh: 8,658; cpp: 6,283; perl: 327; makefile: 112
file content (395 lines) | stat: -rw-r--r-- 9,283 bytes parent folder | download
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*_________________________________________________________________________

  MusicBrainz -- The Internet music metadatabase

  Portions Copyright (C) 2000 Relatable

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   
   This library 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
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  $Id: comsocket.cpp,v 1.11 2001/09/12 21:43:19 robert Exp $
____________________________________________________________________________*/
/***************************************************************************
                          comsocket.cpp  -  description
                             -------------------
    begin                : Thu Mar 23 2000
    copyright            : (C) 2000 by Relatable, LLC
    programed by         : Sean Ward
    email                : sward@relatable.com
 ***************************************************************************/

#include "config.h"

#include "comsocket.h"
#ifndef WIN32

#include <netinet/tcp.h>
#include <errno.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <net/if.h>
#include <sys/ioctl.h>
#endif

#define mb_socklen_t ACCEPT_ARG3

#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif

/* FreeBSD uses IPPROTO_TCP */
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif

#if defined(__QNX__) || defined (__BEOS__)
struct pollfd
{
   int            fd;
   unsigned short events;
   unsigned short revents;
};

#define POLLIN   1
#define POLLOUT  4
#define POLLPRI  2
#define POLLERR  8
#define POLLHUP  16
#define POLLNVAL 32

static int poll(struct pollfd *fds, unsigned int nfds, int timeout)
{
    struct timeval tv;
    fd_set rset, wset, xset;
    struct pollfd *f;
    int ready;
    int maxfd = 0;

    FD_ZERO(&rset);
    FD_ZERO(&wset);
    FD_ZERO(&xset);

    for (f = fds; f < &fds[nfds]; ++f)
       if (f->fd >= 0)
       {
           if (f->events & POLLIN)
              FD_SET(f->fd, &rset);
           if (f->events & POLLOUT)
              FD_SET(f->fd, &wset);
           if (f->events & POLLPRI)
              FD_SET(f->fd, &xset);
           if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
              maxfd = f->fd;
       }

    tv.tv_sec = timeout / 1000;
    tv.tv_usec = (timeout % 1000) * 1000;

    ready = select(maxfd + 1, &rset, &wset, &xset, timeout == -1 ? NULL : &tv);

    if (ready > 0)
       for (f = fds; f < &fds[nfds]; ++f)
       {
           f->revents = 0;
           if (f->fd >= 0)
           {
               if (FD_ISSET(f->fd, &rset))
                   f->revents |= POLLIN;
               if (FD_ISSET(f->fd, &wset))
                   f->revents |= POLLOUT;
               if (FD_ISSET(f->fd, &xset))
                   f->revents |= POLLPRI;
           }
       }

    return ready;
}
#else
#include <sys/poll.h>
#endif

MBCOMSocket::MBCOMSocket(int nSocket, int nSockType)
{
	m_nSocket = nSocket;
	m_bConnected = false;
	if (m_nSocket != INVALID_SOCKET) m_bConnected = true;
	m_nSockType = nSockType;
}

MBCOMSocket::~MBCOMSocket()
{
	if (IsConnected()) Disconnect();
}

/** Connects a socket to pIP, on nPort, of type nType. */
int MBCOMSocket::Connect(const char* pIP, int nPort, int nType, bool
	bBroadcast)
{
	if (this->IsConnected()) this->Disconnect();
	
	sockaddr_in addr;
		
	hostent* pServer;
	int nErr = 0;
	m_nSockType = nType;
	m_nSocket = socket(AF_INET, nType, 0);
	if (m_nSocket < 0) return m_nSocket;
		
	pServer = gethostbyname(pIP);
	if (pServer == NULL)
	{
		close(m_nSocket);
		m_nSocket = -1;
		return -1;
	}
	memset((char*)&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	memcpy((char*)&(addr.sin_addr.s_addr), (char*)(pServer->h_addr), pServer->h_length);
	addr.sin_port = htons(nPort);

	int nflag = 1;
	if (nType == SOCK_STREAM)
	{
		nErr = setsockopt(m_nSocket, SOL_TCP, TCP_NODELAY, &nflag, sizeof(int));
	}
	if (bBroadcast)
	{
		nErr = setsockopt(m_nSocket, SOL_SOCKET, SO_BROADCAST,
			&nflag, sizeof(int));
	}
	
	nErr = connect(m_nSocket, (sockaddr*)&addr, sizeof(sockaddr_in));
	if (nErr != 0)
	{
		
		close(m_nSocket);
		m_nSocket = -1;
		return -1;
	}
		
	m_bConnected = true;
	return 0;
}

/** Disconnects the current socket */
int MBCOMSocket::Disconnect()
{
	int nErr = 0;
	if (!IsConnected()) return -1;
	if (m_nSockType == SOCK_STREAM)
	{
		nErr = shutdown(m_nSocket, SHUT_RDWR);
	}
	
	nErr = close(m_nSocket);
	m_nSocket = -1;
	m_bConnected = false;
	return (nErr != -1);
}

/** Checks if there is a current open connection */
bool MBCOMSocket::IsConnected()
{
	return m_bConnected;
}

/** Reads from a socket, into pbuffer, up to a max of nLen byte, and writes how many were actually written to nBytesWritten. */
int MBCOMSocket::Read(char* pBuffer, int nLen, int* nBytesWritten)
{
	if (!IsConnected()) return -1;	// no connection
	int nErr = 0;
	nErr = recv(m_nSocket, (void*)pBuffer, nLen, 0);
	if ((nErr != -1) && (nBytesWritten != NULL))
	{
		*nBytesWritten = nErr;
	}
	return ((nErr != -1) - 1);
}

/** Writes to a socket, from buffer pBuffer, up to nLen bytes, and returns the number of written bytes in pnBytesWritten. */
int MBCOMSocket::Write(const char* pBuffer, int nLen, int* pnBytesWritten)
{
	if (!IsConnected()) return -1; // no connection
	int nErr = 0;
	bool bRepeat = true;
	while (bRepeat)
	{
		nErr = send(m_nSocket, (void*)pBuffer, nLen, 0);
		bRepeat = false;
		if ((nErr == -1) && (errno == EINTR))
		{
			bRepeat = true;
		}
	}
	if ((nErr != -1) && (pnBytesWritten != NULL))
	{
		*pnBytesWritten = nErr;
	}
	return ((nErr != -1) - 1);
}
/** Sets TCPNODELAY to nFlag */
int MBCOMSocket::SetNoDelay(int nFlag)
{
	if (!IsConnected()) return -1;
	int nErr = 0;
	if (m_nSockType == SOCK_STREAM)
	{
		nErr = setsockopt(m_nSocket, SOL_TCP, TCP_NODELAY, &nFlag, sizeof(int));
	}
  return nErr;
}

/** Reads in a non blocking fashion (ie, selects and polls) for nTimeout seconds */
int MBCOMSocket::NBRead(char* pBuffer, int nLen, int* nBytesWritten, int nTimeout)
{
	struct pollfd pfd;
	pfd.fd = m_nSocket;
	pfd.events = POLLIN;
	
	int retval;
	
	retval = poll(&pfd, 1, nTimeout*1000);
	if (retval > 0)
	{
		return this->Read(pBuffer, nLen, nBytesWritten);
	}
	else
	{
		return -1;
	}
}

int MBCOMSocket::SetNonBlocking(bool bType)
{
	int nRes = 0;
	int flags = 0;
	flags = fcntl(m_nSocket, F_GETFL, 0);
	if (bType)
	{
		nRes = fcntl(m_nSocket, F_SETFL, flags | O_NONBLOCK);
	}
	else
	{
		flags &= ~O_NONBLOCK;
		nRes = fcntl(m_nSocket, F_SETFL, flags);
	}
	return nRes;
}

int MBCOMSocket::NBConnect(const char* pIP, int nPort, int nType, int nTimeout)
{
	if (this->IsConnected()) this->Disconnect();
	
	sockaddr_in addr;
	hostent* pServer;
	int nErr = 0;
	m_nSockType = nType;
	m_nSocket = socket(AF_INET, nType, 0);
	if (m_nSocket < 0) return m_nSocket;
		
	pServer = gethostbyname(pIP);
	if (pServer == NULL)
	{
		close(m_nSocket);
		m_nSocket = -1;
		return -1;
	}
	memset((char*)&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	memcpy((char *)&(addr.sin_addr.s_addr), (char*)(pServer->h_addr), pServer->h_length);
	addr.sin_port = htons(nPort);

	int nflag = 1;
	if (nType == SOCK_STREAM)
	{
		nErr = setsockopt(m_nSocket, SOL_TCP, TCP_NODELAY, &nflag, sizeof(int));
	}
	nErr = this->SetNonBlocking(true);

	nErr = connect(m_nSocket, (sockaddr*)&addr, sizeof(sockaddr_in));
	
	if (nErr == 0)
	{ // connected immediately
		m_bConnected = true;
		this->SetNonBlocking(false);
		return 1;
	}
	else
	{
		if (errno != EINPROGRESS)
		{
			close(m_nSocket);
			m_nSocket = -1;
			return -1;
		}
		fd_set rset, wset;
		FD_ZERO(&rset);
		FD_SET(m_nSocket, &rset);
		wset = rset;
		struct timeval tval;
		tval.tv_sec = nTimeout;
		tval.tv_usec = 0;
		
		if (( nErr = select(m_nSocket + 1, &rset, &wset, NULL,
			nTimeout ? &tval : NULL) ) == 0) 
		{
			errno = ETIMEDOUT;
			close(m_nSocket);
			m_nSocket = -1;
			return -1;
		}
		if (FD_ISSET(m_nSocket, &rset) || FD_ISSET(m_nSocket,
&wset))
		{
			int error = 0;
                        mb_socklen_t len = sizeof(error);

		if (getsockopt(m_nSocket, SOL_SOCKET, SO_ERROR, &error,
&len) < 0)
		{	
		errno = ETIMEDOUT;
			close(m_nSocket);
			m_nSocket = -1;
			return -1;
		}
		}
	}
		
	m_bConnected = true;
	this->SetNonBlocking(false);
	return 1;
}

/** Sets multicast packets to only go through the NIC labeled pNIC */
int MBCOMSocket::SetMCastInterface(const char* pNIC)
{
#ifndef __linux__
//#warning WARNING COMSocket::SetMCastInterface is NOT IMPLEMENTED
    return -1;
#else
	struct ip_mreqn mReq;
	memset(&mReq, 0, sizeof(ip_mreq));
	int nErr = -1;
	if (m_nSockType == SOCK_DGRAM)
	{
		mReq.imr_ifindex = if_nametoindex(pNIC);
		nErr = setsockopt(m_nSocket, SOL_IP, IP_MULTICAST_IF,
				&mReq, sizeof(ip_mreqn));
	}
	return ((nErr != -1) - 1);
#endif
}