summaryrefslogtreecommitdiff
path: root/protocols/Gadu-Gadu/src/libgadu/win32.cpp
blob: 55da0d8e0df7bc6d540386b9315d30452e1972f2 (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
////////////////////////////////////////////////////////////////////////////////
// Gadu-Gadu Plugin for Miranda IM
//
// Copyright (c) 2003-2009 Adam Strzelecki <ono+miranda@java.pl>
// Copyright (c) 2009-2010 Bartosz Bia³ek
//
// 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
////////////////////////////////////////////////////////////////////////////////

#ifdef _WIN32
#include "win32.h"

int sockpipe(SOCKET filedes[2])
{
	SOCKET sock;
	struct sockaddr_in sin;
	int len = sizeof(sin);

	filedes[0] = filedes[1] = INVALID_SOCKET;

	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		return -1;

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(0);
	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

	if (bind(sock, (SOCKADDR *)&sin, len) == SOCKET_ERROR ||
		listen(sock, 1) == SOCKET_ERROR ||
		getsockname(sock, (SOCKADDR *)&sin, &len) == SOCKET_ERROR) {
		closesocket(sock);
		return -1;
	}

	if ((filedes[1] = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET ||
		connect(filedes[1], (SOCKADDR *)&sin, len) == SOCKET_ERROR) {
		closesocket(sock);
		return -1;
	}

	if ((filedes[0] = accept(sock, (SOCKADDR *)&sin, &len)) == INVALID_SOCKET) {
		closesocket(filedes[1]);
		filedes[1] = INVALID_SOCKET;
		closesocket(sock);
		return -1;
	}

	closesocket(sock);
	return 0;
}

#endif /* _WIN32 */