summaryrefslogtreecommitdiff
path: root/src/win32/pipe/pipe.cpp
blob: 69a633f3af7fbe2d94d40ff931c28c69af740728 (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
/* POSIX emulation layer for Windows.
 *
 * (C) 2008-2017 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 */

#include "services.h"
#include "sockets.h"

int pipe(int fds[2])
{
	sockaddrs localhost("127.0.0.1");

	int cfd = socket(AF_INET, SOCK_STREAM, 0), lfd = socket(AF_INET, SOCK_STREAM, 0);
	if (cfd == -1 || lfd == -1)
	{
		anope_close(cfd);
		anope_close(lfd);
		return -1;
	}

	if (bind(lfd, &localhost.sa, localhost.size()) == -1)
	{
		anope_close(cfd);
		anope_close(lfd);
		return -1;
	}

	if (listen(lfd, 1) == -1)
	{
		anope_close(cfd);
		anope_close(lfd);
		return -1;
	}

	sockaddrs lfd_addr;
	socklen_t sz = sizeof(lfd_addr);
	getsockname(lfd, &lfd_addr.sa, &sz);

	if (connect(cfd, &lfd_addr.sa, lfd_addr.size()))
	{
		anope_close(cfd);
		anope_close(lfd);
		return -1;
	}

	int afd = accept(lfd, NULL, NULL);
	anope_close(lfd);
	if (afd == -1)
	{
		anope_close(cfd);
		return -1;
	}

	fds[0] = cfd;
	fds[1] = afd;
	
	return 0;
}