summaryrefslogtreecommitdiffstats
path: root/libktorrent/net/socket.cpp
blob: ff09a34edcb70f57f3ae3b4d297eccd3fbfbad5f (plain)
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
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   joris.guisson@gmail.com                                               *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

#include <tqglobal.h>

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>

#if defined(Q_OS_LINUX) && !defined(__FreeBSD_kernel__)
#include <asm/ioctls.h>
#endif

#ifdef Q_OS_SOLARIS
#include <sys/filio.h>
#endif

#ifndef MSG_NOSIGNAL
#define MSG_NOTQT_SIGNAL 0
#endif

#include <unistd.h>
#include <fcntl.h>

#include <torrent/globals.h>
#include <util/log.h>
#include "socket.h"

using namespace bt;

namespace net
{

	Socket::Socket(int fd) : m_fd(fd),m_state(IDLE)
	{
#if defined(Q_OS_MACX) || defined(Q_OS_DARWIN) || (defined(Q_OS_FREEBSD) && !defined(__DragonFly__) && __FreeBSD_version < 600020)
		int val = 1; 
		if (setsockopt(m_fd,SOL_SOCKET,SO_NOSIGPIPE,&val,sizeof(int)) < 0)
		{
			Out(SYS_CON|LOG_NOTICE) << TQString("Failed to set the NOSIGPIPE option : %1").tqarg(strerror(errno)) << endl;
		}
#endif
		cacheAddress();
	}
	
	Socket::Socket(bool tcp) : m_fd(-1),m_state(IDLE)
	{
		int fd = socket(PF_INET,tcp ? SOCK_STREAM : SOCK_DGRAM,0);
		if (fd < 0)
		{
			Out(SYS_GEN|LOG_IMPORTANT) << TQString("Cannot create socket : %1").tqarg(strerror(errno)) << endl;
		}
		m_fd = fd;
#if defined(Q_OS_MACX) || defined(Q_OS_DARWIN) || (defined(Q_OS_FREEBSD) && !defined(__DragonFly__) && __FreeBSD_version < 600020)
		int val = 1;
		if (setsockopt(m_fd,SOL_SOCKET,SO_NOSIGPIPE,&val,sizeof(int)) < 0)
		{
			Out(SYS_CON|LOG_NOTICE) << TQString("Failed to set the NOSIGPIPE option : %1").tqarg(strerror(errno)) << endl;
		}
#endif	
	}
	
	Socket::~Socket()
	{
		if (m_fd >= 0)
		{
			shutdown(m_fd, SHUT_RDWR);
			::close(m_fd);
		}
	}
	
	void Socket::close()
	{
		if (m_fd >= 0)
		{
			shutdown(m_fd, SHUT_RDWR);
			::close(m_fd);
			m_fd = -1;
			m_state = CLOSED;
		}
	}
	
	void Socket::setNonBlocking()
	{
		fcntl(m_fd, F_SETFL, O_NONBLOCK);
	}
		
	bool Socket::connectTo(const Address & a)
	{
		struct sockaddr_in addr;
		memset(&addr,0,sizeof(struct sockaddr_in));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(a.port());
		addr.sin_addr.s_addr = htonl(a.ip());

		if (::connect(m_fd,(struct sockaddr*)&addr,sizeof(struct sockaddr)) < 0)
		{
			if (errno == EINPROGRESS)
			{
			//	Out(SYS_CON|LOG_DEBUG) << "Socket is connecting" << endl;
				m_state = CONNECTING;
				return false;
			}
			else
			{
				Out(SYS_CON|LOG_NOTICE) << TQString("Cannot connect to host %1:%2 : %3")
					.tqarg(a.toString()).tqarg(a.port()).tqarg(strerror(errno)) << endl;
				return false;
			}
		}
		m_state = CONNECTED;
		cacheAddress();
		return true;
	}
	
	bool Socket::bind(Uint16 port,bool also_listen)
	{
		struct sockaddr_in addr;
		memset(&addr,0,sizeof(struct sockaddr_in));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
	
		if (::bind(m_fd,(struct sockaddr*)&addr,sizeof(struct sockaddr)) < 0)
		{
			Out(SYS_CON|LOG_IMPORTANT) << TQString("Cannot bind to port %1 : %2").tqarg(port).tqarg(strerror(errno)) << endl;
			return false;
		}

		if (also_listen && listen(m_fd,5) < 0)
		{
			Out(SYS_CON|LOG_IMPORTANT) << TQString("Cannot listen to port %1 : %2").tqarg(port).tqarg(strerror(errno)) << endl;
			return false;
		}

		int val = 1;
		if (setsockopt(m_fd,SOL_SOCKET,SO_REUSEADDR,&val,sizeof(int)) < 0)
		{
			Out(SYS_CON|LOG_NOTICE) << TQString("Failed to set the reuseaddr option : %1").tqarg(strerror(errno)) << endl;
		}
		m_state = BOUND;
		return true;
	}
	
	int Socket::send(const bt::Uint8* buf,int len)
	{
		int ret = ::send(m_fd,buf,len,MSG_NOSIGNAL);
		if (ret < 0)
		{
			if (errno != EAGAIN && errno != EWOULDBLOCK)
			{
			//	Out(SYS_CON|LOG_DEBUG) << "Send error : " << TQString(strerror(errno)) << endl;
				close();
			}
			return 0;
		}
		return ret;
	}
	
	int Socket::recv(bt::Uint8* buf,int max_len)
	{
		int ret = ::recv(m_fd,buf,max_len,0);
		if (ret < 0)
		{
			if (errno != EAGAIN && errno != EWOULDBLOCK)
			{
			//	Out(SYS_CON|LOG_DEBUG) << "Receive error : " << TQString(strerror(errno)) << endl;
				close();
			}
			return 0;
		}
		else if (ret == 0)
		{
			// connection closed
			close();
			return 0;
		}
		return ret;
	}
	
	int Socket::sendTo(const bt::Uint8* buf,int len,const Address & a)
	{
		struct sockaddr_in addr;
		memset(&addr,0,sizeof(struct sockaddr_in));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(a.port());
		addr.sin_addr.s_addr = htonl(a.ip());

		int ns = 0;
		while (ns < len)
		{
			int left = len - ns;
			int ret = ::sendto(m_fd,(char*)buf + ns,left,0,(struct sockaddr*)&addr,sizeof(struct sockaddr));
			if (ret < 0)
			{
				Out(SYS_CON|LOG_DEBUG) << "Send error : " << TQString(strerror(errno)) << endl;
				return 0;
			}

			ns += ret;
		}
		return ns;
	}
	
	int Socket::recvFrom(bt::Uint8* buf,int max_len,Address & a)
	{
		struct sockaddr_in addr;
		memset(&addr,0,sizeof(struct sockaddr_in));
		socklen_t sl = sizeof(struct sockaddr);
		
		int ret = ::recvfrom(m_fd,buf,max_len,0,(struct sockaddr*)&addr,&sl);
		if (ret < 0)
		{
			Out(SYS_CON|LOG_DEBUG) << "Receive error : " << TQString(strerror(errno)) << endl;
			return 0;
		}

		a.setPort(ntohs(addr.sin_port));
		a.setIP(ntohl(addr.sin_addr.s_addr));
		return ret;
	}
	
	int Socket::accept(Address & a)
	{
		struct sockaddr_in addr;
		memset(&addr,0,sizeof(struct sockaddr_in));
		socklen_t slen = sizeof(struct sockaddr_in);

		int sfd = ::accept(m_fd,(struct sockaddr*)&addr,&slen);
		if (sfd < 0)
		{
			Out(SYS_CON|LOG_DEBUG) << "Accept error : " << TQString(strerror(errno)) << endl;
			return -1;
		}
		
		a.setPort(ntohs(addr.sin_port));
		a.setIP(ntohl(addr.sin_addr.s_addr));

		Out(SYS_CON|LOG_DEBUG) << "Accepted connection from " << TQString(inet_ntoa(addr.sin_addr)) << endl;
		return sfd;
	}
	
	bool Socket::setTOS(unsigned char type_of_service)
	{
#if defined(Q_OS_MACX) || defined(Q_OS_DARWIN) || (defined(Q_OS_FREEBSD) && __FreeBSD_version < 600020) || defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_BSD4)
		unsigned int c = type_of_service;
#else
		unsigned char c = type_of_service;
#endif
		if (setsockopt(m_fd,IPPROTO_IP,IP_TOS,&c,sizeof(c)) < 0)
		{
			Out(SYS_CON|LOG_NOTICE) << TQString("Failed to set TOS to %1 : %2")
					.tqarg(type_of_service).tqarg(strerror(errno)) << endl;
			return false;
		}
		return true;
	}
	
	Uint32 Socket::bytesAvailable() const
	{
		int ret = 0;
		if (ioctl(m_fd,FIONREAD,&ret) < 0)
			return 0;
		
		return ret;
	}
	
	bool Socket::connectSuccesFull()
	{
		if (m_state != CONNECTING)
			return false;
		
		int err = 0;
		socklen_t len = sizeof(int);
		if (getsockopt(m_fd,SOL_SOCKET,SO_ERROR,&err,&len) < 0)
			return false;
		
		if (err == 0)
		{
			m_state = CONNECTED;
			cacheAddress();
		}
		
		return err == 0;
	}
	
	void Socket::cacheAddress()
	{
		struct sockaddr_in raddr;
		socklen_t slen = sizeof(struct sockaddr_in);
		if (getpeername(m_fd,(struct sockaddr*)&raddr,&slen) == 0)
			addr = Address(inet_ntoa(raddr.sin_addr),ntohs(raddr.sin_port));
	}

	/*
	void Socket::setReadBufferSize(int rbs)
	{
		if (setsockopt(m_fd, SOL_SOCKET, SO_RCVBUF, (char *)&rbs,sizeof(int)) < 0)
		{
			Out(SYS_CON|LOG_DEBUG) << "Failed to set read buffer size " << endl;
		}
	}
	*/
}