anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

pipe.cpp (1000B)

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