Anope IRC Services  Version 2.0
pipe.cpp
Go to the documentation of this file.
1  /* POSIX emulation layer for Windows.
2  *
3  * Copyright (C) 2008-2014 Anope Team <team@anope.org>
4  *
5  * Please read COPYING and README for further details.
6  */
7 
8 #include "services.h"
9 #include "sockets.h"
10 
11 int pipe(int fds[2])
12 {
13  sockaddrs localhost("127.0.0.1");
14 
15  int cfd = socket(AF_INET, SOCK_STREAM, 0), lfd = socket(AF_INET, SOCK_STREAM, 0);
16  if (cfd == -1 || lfd == -1)
17  {
18  anope_close(cfd);
19  anope_close(lfd);
20  return -1;
21  }
22 
23  if (bind(lfd, &localhost.sa, localhost.size()) == -1)
24  {
25  anope_close(cfd);
26  anope_close(lfd);
27  return -1;
28  }
29 
30  if (listen(lfd, 1) == -1)
31  {
32  anope_close(cfd);
33  anope_close(lfd);
34  return -1;
35  }
36 
37  sockaddrs lfd_addr;
38  socklen_t sz = sizeof(lfd_addr);
39  getsockname(lfd, &lfd_addr.sa, &sz);
40 
41  if (connect(cfd, &lfd_addr.sa, lfd_addr.size()))
42  {
43  anope_close(cfd);
44  anope_close(lfd);
45  return -1;
46  }
47 
48  int afd = accept(lfd, NULL, NULL);
49  anope_close(lfd);
50  if (afd == -1)
51  {
52  anope_close(cfd);
53  return -1;
54  }
55 
56  fds[0] = cfd;
57  fds[1] = afd;
58 
59  return 0;
60 }
61 
size_t size() const
Definition: sockets.cpp:43
sockaddr sa
Definition: sockets.h:30
#define anope_close
Definition: services.h:64
int pipe(int fds[2])
Definition: pipe.cpp:11
#define accept
Definition: socket.h:28