[
  {
    "path": "AUTHORS",
    "content": "original code\nN3E7CR34TUR3.\nhttp://proxychains.sourceforge.net\nnetcreature@users.sourceforge.net\n\nmain.c, remote-dns, thread safety, bugfixes, build system, cleanups\nrofl0r.\nhttps://github.com/rofl0r/proxychains\n\nlocalnet, bugfixes, macos x support, bsd support, packaging\nhaad.\nhttps://github.com/haad/proxychains\n\nlocalnet-port, bugfixes\njianing yang.\nhttps://github.com/jianingy/proxychains\n"
  },
  {
    "path": "COPYING",
    "content": "\t\t    GNU GENERAL PUBLIC LICENSE\n\t\t       Version 2, June 1991\n\n Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n                          675 Mass Ave, Cambridge, MA 02139, USA\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n\t\t\t    Preamble\n\n  The licenses for most software are designed to take away your\nfreedom to share and change it.  By contrast, the GNU General Public\nLicense is intended to guarantee your freedom to share and change free\nsoftware--to make sure the software is free for all its users.  This\nGeneral Public License applies to most of the Free Software\nFoundation's software and to any other program whose authors commit to\nusing it.  (Some other Free Software Foundation software is covered by\nthe GNU Library General Public License instead.)  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthis service if you wish), that you receive source code or can get it\nif you want it, that you can change the software or use pieces of it\nin new free programs; and that you know you can do these things.\n\n  To protect your rights, we need to make restrictions that forbid\nanyone to deny you these rights or to ask you to surrender the rights.\nThese restrictions translate to certain responsibilities for you if you\ndistribute copies of the software, or if you modify it.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must give the recipients all the rights that\nyou have.  You must make sure that they, too, receive or can get the\nsource code.  And you must show them these terms so they know their\nrights.\n\n  We protect your rights with two steps: (1) copyright the software, and\n(2) offer you this license which gives you legal permission to copy,\ndistribute and/or modify the software.\n\n  Also, for each author's protection and ours, we want to make certain\nthat everyone understands that there is no warranty for this free\nsoftware.  If the software is modified by someone else and passed on, we\nwant its recipients to know that what they have is not the original, so\nthat any problems introduced by others will not reflect on the original\nauthors' reputations.\n\n  Finally, any free program is threatened constantly by software\npatents.  We wish to avoid the danger that redistributors of a free\nprogram will individually obtain patent licenses, in effect making the\nprogram proprietary.  To prevent this, we have made it clear that any\npatent must be licensed for everyone's free use or not licensed at all.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\f\n\t\t    GNU GENERAL PUBLIC LICENSE\n   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\n  0. This License applies to any program or other work which contains\na notice placed by the copyright holder saying it may be distributed\nunder the terms of this General Public License.  The \"Program\", below,\nrefers to any such program or work, and a \"work based on the Program\"\nmeans either the Program or any derivative work under copyright law:\nthat is to say, a work containing the Program or a portion of it,\neither verbatim or with modifications and/or translated into another\nlanguage.  (Hereinafter, translation is included without limitation in\nthe term \"modification\".)  Each licensee is addressed as \"you\".\n\nActivities other than copying, distribution and modification are not\ncovered by this License; they are outside its scope.  The act of\nrunning the Program is not restricted, and the output from the Program\nis covered only if its contents constitute a work based on the\nProgram (independent of having been made by running the Program).\nWhether that is true depends on what the Program does.\n\n  1. You may copy and distribute verbatim copies of the Program's\nsource code as you receive it, in any medium, provided that you\nconspicuously and appropriately publish on each copy an appropriate\ncopyright notice and disclaimer of warranty; keep intact all the\nnotices that refer to this License and to the absence of any warranty;\nand give any other recipients of the Program a copy of this License\nalong with the Program.\n\nYou may charge a fee for the physical act of transferring a copy, and\nyou may at your option offer warranty protection in exchange for a fee.\n\n  2. You may modify your copy or copies of the Program or any portion\nof it, thus forming a work based on the Program, and copy and\ndistribute such modifications or work under the terms of Section 1\nabove, provided that you also meet all of these conditions:\n\n    a) You must cause the modified files to carry prominent notices\n    stating that you changed the files and the date of any change.\n\n    b) You must cause any work that you distribute or publish, that in\n    whole or in part contains or is derived from the Program or any\n    part thereof, to be licensed as a whole at no charge to all third\n    parties under the terms of this License.\n\n    c) If the modified program normally reads commands interactively\n    when run, you must cause it, when started running for such\n    interactive use in the most ordinary way, to print or display an\n    announcement including an appropriate copyright notice and a\n    notice that there is no warranty (or else, saying that you provide\n    a warranty) and that users may redistribute the program under\n    these conditions, and telling the user how to view a copy of this\n    License.  (Exception: if the Program itself is interactive but\n    does not normally print such an announcement, your work based on\n    the Program is not required to print an announcement.)\n\f\nThese requirements apply to the modified work as a whole.  If\nidentifiable sections of that work are not derived from the Program,\nand can be reasonably considered independent and separate works in\nthemselves, then this License, and its terms, do not apply to those\nsections when you distribute them as separate works.  But when you\ndistribute the same sections as part of a whole which is a work based\non the Program, the distribution of the whole must be on the terms of\nthis License, whose permissions for other licensees extend to the\nentire whole, and thus to each and every part regardless of who wrote it.\n\nThus, it is not the intent of this section to claim rights or contest\nyour rights to work written entirely by you; rather, the intent is to\nexercise the right to control the distribution of derivative or\ncollective works based on the Program.\n\nIn addition, mere aggregation of another work not based on the Program\nwith the Program (or with a work based on the Program) on a volume of\na storage or distribution medium does not bring the other work under\nthe scope of this License.\n\n  3. You may copy and distribute the Program (or a work based on it,\nunder Section 2) in object code or executable form under the terms of\nSections 1 and 2 above provided that you also do one of the following:\n\n    a) Accompany it with the complete corresponding machine-readable\n    source code, which must be distributed under the terms of Sections\n    1 and 2 above on a medium customarily used for software interchange; or,\n\n    b) Accompany it with a written offer, valid for at least three\n    years, to give any third party, for a charge no more than your\n    cost of physically performing source distribution, a complete\n    machine-readable copy of the corresponding source code, to be\n    distributed under the terms of Sections 1 and 2 above on a medium\n    customarily used for software interchange; or,\n\n    c) Accompany it with the information you received as to the offer\n    to distribute corresponding source code.  (This alternative is\n    allowed only for noncommercial distribution and only if you\n    received the program in object code or executable form with such\n    an offer, in accord with Subsection b above.)\n\nThe source code for a work means the preferred form of the work for\nmaking modifications to it.  For an executable work, complete source\ncode means all the source code for all modules it contains, plus any\nassociated interface definition files, plus the scripts used to\ncontrol compilation and installation of the executable.  However, as a\nspecial exception, the source code distributed need not include\nanything that is normally distributed (in either source or binary\nform) with the major components (compiler, kernel, and so on) of the\noperating system on which the executable runs, unless that component\nitself accompanies the executable.\n\nIf distribution of executable or object code is made by offering\naccess to copy from a designated place, then offering equivalent\naccess to copy the source code from the same place counts as\ndistribution of the source code, even though third parties are not\ncompelled to copy the source along with the object code.\n\f\n  4. You may not copy, modify, sublicense, or distribute the Program\nexcept as expressly provided under this License.  Any attempt\notherwise to copy, modify, sublicense or distribute the Program is\nvoid, and will automatically terminate your rights under this License.\nHowever, parties who have received copies, or rights, from you under\nthis License will not have their licenses terminated so long as such\nparties remain in full compliance.\n\n  5. You are not required to accept this License, since you have not\nsigned it.  However, nothing else grants you permission to modify or\ndistribute the Program or its derivative works.  These actions are\nprohibited by law if you do not accept this License.  Therefore, by\nmodifying or distributing the Program (or any work based on the\nProgram), you indicate your acceptance of this License to do so, and\nall its terms and conditions for copying, distributing or modifying\nthe Program or works based on it.\n\n  6. Each time you redistribute the Program (or any work based on the\nProgram), the recipient automatically receives a license from the\noriginal licensor to copy, distribute or modify the Program subject to\nthese terms and conditions.  You may not impose any further\nrestrictions on the recipients' exercise of the rights granted herein.\nYou are not responsible for enforcing compliance by third parties to\nthis License.\n\n  7. If, as a consequence of a court judgment or allegation of patent\ninfringement or for any other reason (not limited to patent issues),\nconditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot\ndistribute so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you\nmay not distribute the Program at all.  For example, if a patent\nlicense would not permit royalty-free redistribution of the Program by\nall those who receive copies directly or indirectly through you, then\nthe only way you could satisfy both it and this License would be to\nrefrain entirely from distribution of the Program.\n\nIf any portion of this section is held invalid or unenforceable under\nany particular circumstance, the balance of the section is intended to\napply and the section as a whole is intended to apply in other\ncircumstances.\n\nIt is not the purpose of this section to induce you to infringe any\npatents or other property right claims or to contest validity of any\nsuch claims; this section has the sole purpose of protecting the\nintegrity of the free software distribution system, which is\nimplemented by public license practices.  Many people have made\ngenerous contributions to the wide range of software distributed\nthrough that system in reliance on consistent application of that\nsystem; it is up to the author/donor to decide if he or she is willing\nto distribute software through any other system and a licensee cannot\nimpose that choice.\n\nThis section is intended to make thoroughly clear what is believed to\nbe a consequence of the rest of this License.\n\f\n  8. If the distribution and/or use of the Program is restricted in\ncertain countries either by patents or by copyrighted interfaces, the\noriginal copyright holder who places the Program under this License\nmay add an explicit geographical distribution limitation excluding\nthose countries, so that distribution is permitted only in or among\ncountries not thus excluded.  In such case, this License incorporates\nthe limitation as if written in the body of this License.\n\n  9. The Free Software Foundation may publish revised and/or new versions\nof the General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\nEach version is given a distinguishing version number.  If the Program\nspecifies a version number of this License which applies to it and \"any\nlater version\", you have the option of following the terms and conditions\neither of that version or of any later version published by the Free\nSoftware Foundation.  If the Program does not specify a version number of\nthis License, you may choose any version ever published by the Free Software\nFoundation.\n\n  10. If you wish to incorporate parts of the Program into other free\nprograms whose distribution conditions are different, write to the author\nto ask for permission.  For software which is copyrighted by the Free\nSoftware Foundation, write to the Free Software Foundation; we sometimes\nmake exceptions for this.  Our decision will be guided by the two goals\nof preserving the free status of all derivatives of our free software and\nof promoting the sharing and reuse of software generally.\n\n\t\t\t    NO WARRANTY\n\n  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\nFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\nOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\nPROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\nOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\nTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\nPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\nREPAIR OR CORRECTION.\n\n  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\nREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\nINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\nOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\nTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\nYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\nPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\nPOSSIBILITY OF SUCH DAMAGES.\n\n\t\t     END OF TERMS AND CONDITIONS\n"
  },
  {
    "path": "ChangeLog",
    "content": "ProxyChains version history (public releases)\n====================\n\nver 4.2\n\t* Introduce BSD support make proxychains available on (NetBSD, FreeBSD ,OpenBSD and DragonFlyBSD)\n\t* Update pkgsrc-wip proxychains package to 4.2.0 release and make easy to install and use\n\t  proxychains\n-------------------------------------------------------------------------\n\nver 4.1\n       * Mac OS X support, create install script.\n       * thread-safe fixes by @rofl0r\n\n-------------------------------------------------------------------------\n\nver 4.0\n\nchanged:\n       * Add a lot of fixes\n       * localnet-patch\n       * remote-dns\n       * thread-safe\n-------------------------------------------------------------------------\n\nver 3.1\n\nchanged:\n\t* dns resolver script fix\n\t* prototypes in core.h\n\n-------------------------------------------------------------------------\nver 3.0\n\nadded:\n\t* new feature - DNS from behind proxy\n\t* proxyresolv - stand alone command\n\t* proxychains.conf - new option to enable/disable DNS support\n\nchanged:\n\t* bugfixes in core lib\n\t* fixed strict chain\n\t* fixed random chain\n\t* output text\n\t* autotools fix \n\n-------------------------------------------------------------------------\nver 2.1\n\t* bugfuxes\n\n-------------------------------------------------------------------------\nver 2.0\n\t* major core rewrite\n\t* new config options\n\n-------------------------------------------------------------------------\n\nver 1.8.2\n\t* minor bugfixes\n\t* improved compilation on FreeBSD & OpenBSD sysems.\n\t* improved compilation on Sun Solaris systems .\n\t* cross platform (UNIX) issues\n\n-------------------------------------------------------------------------\n\nver 1.8.0\n\nadded:\n\t* Socks5 protocol\n\t* Socks4 protocol\n\t* HTTP proxy auth basic\n\t* Socks4 user auth\n\t* Socks5 user/pass auth\n\t* more chain options (random, strict, dynamic )\n\t* configurable timeout for TCP connect.\n\t* configurable timeout for TCP read.\n\t* INSTALL file (explains how to install properly)\n\nchanged:\n\t* configuration file entries (proxychains.conf)\n\t* configuration file lookup\n\n-------------------------------------------------------------------------\n\nver 0.0.1\n\t* TCP calls interception\n\t* HTTP CONNECT proxy protocol.\n"
  },
  {
    "path": "Makefile",
    "content": "#\n# Makefile for proxychains (requires GNU make), stolen from musl\n#\n# Use config.mak to override any of the following variables.\n# Do not make changes here.\n#\n\nprefix = /usr/local\nincludedir = $(prefix)/include\nlibdir = $(prefix)/lib\nconfdir = $(prefix)/etc\ndatadir = $(prefix)/share\n\nexec_prefix = $(prefix)\nbindir = $(exec_prefix)/bin\nzshcompdir = $(datadir)/zsh/site-functions\n\nSRCS = $(sort $(wildcard src/*.c))\nOBJS = $(SRCS:.c=.o)\nLOBJS = src/core.o src/common.o src/libproxychains.o\nDEPS = $(OBJS:.o=.d)\n\nCCFLAGS  = -MD -Wall -O2 -g -std=c99 -D_GNU_SOURCE -pipe -DTHREAD_SAFE -Werror\nLDFLAGS = -shared -fPIC\nINC     =\nPIC     = -fPIC\nAR      = $(CROSS_COMPILE)ar\nRANLIB  = $(CROSS_COMPILE)ranlib\n\nLD_SET_SONAME = -Wl\nINSTALL_FLAGS = -D -m\n\n-include config.mak\n\nSHARED_LIBS = $(LDSO_PATHNAME)\nALL_LIBS = $(SHARED_LIBS)\nPXCHAINS = proxychains4\nALL_TOOLS = $(PXCHAINS)\n\nCCFLAGS+=$(USER_CFLAGS) $(OS_CFLAGS)\nLDFLAGS+=$(USER_LDFLAGS) $(OS_LDFLAGS)\nCXXFLAGS+=$(CCFLAGS) $(USER_CFLAGS) $(OS_CFLAGS)\nCFLAGS_MAIN=-DLIB_DIR=\\\"$(libdir)\\\" -DINSTALL_PREFIX=\\\"$(prefix)\\\" -DDLL_NAME=\\\"$(LDSO_PATHNAME)\\\" -DSYSCONFDIR=\\\"$(confdir)\\\"\n\nUNAME_S := $(shell uname -s)\nifeq ($(UNAME_S),Linux)\n\tLDSO_PATHNAME = libproxychains.$(LDSO_SUFFIX)\n    LDSO_SUFFIX = so.4\nendif\nifeq ($(UNAME_S),Darwin)\n\tLDSO_PATHNAME = libproxychains4.$(LDSO_SUFFIX)\n    LDSO_SUFFIX = dylib\nendif\n\n\nall: $(ALL_LIBS) $(ALL_TOOLS)\n\n#install: $(ALL_LIBS:lib/%=$(DESTDIR)$(libdir)/%) $(DESTDIR)$(LDSO_PATHNAME)\ninstall: install-exec install-config install-zsh-completion\n\ninstall-exec:\n\tinstall -d $(DESTDIR)/$(bindir) $(DESTDIR)/$(libdir) $(DESTDIR)/$(confdir) $(DESTDIR)/$(includedir)\n\tinstall $(INSTALL_FLAGS) 755 $(ALL_TOOLS) src/proxychains src/proxyresolv $(DESTDIR)/$(bindir)/\n\tinstall $(INSTALL_FLAGS) 644 $(ALL_LIBS) $(DESTDIR)/$(libdir)/\n\ninstall-config:\n\tinstall -d $(DESTDIR)/$(confdir)\n\tinstall $(INSTALL_FLAGS) 644 src/proxychains.conf $(DESTDIR)/$(confdir)/\n\ninstall-zsh-completion:\n\tinstall -d $(DESTDIR)/$(zshcompdir)\n\tinstall $(INSTALL_FLAGS) 644 completions/zsh/_proxychains4 -t $(DESTDIR)/$(zshcompdir)\n\nclean:\n\trm -f $(ALL_LIBS)\n\trm -f $(ALL_TOOLS)\n\trm -f $(OBJS)\n\trm -f $(DEPS)\n\n%.o: %.c\n\t$(CC) $(CCFLAGS) $(CFLAGS_MAIN) $(INC) $(PIC) -c -o $@ $<\n\n$(LDSO_PATHNAME): $(LOBJS)\n\t$(CC) $(LDFLAGS) $(LD_SET_SONAME)$(LDSO_PATHNAME) -o $@ $(LOBJS)\n\n$(ALL_TOOLS): $(OBJS)\n\t$(CC) src/main.o src/common.o -o $(PXCHAINS)\n\n.PHONY: all clean install install-exec install-config install-zsh-completion\n\n-include $(DEPS)\n"
  },
  {
    "path": "README.md",
    "content": "# ProxyChains ver. 4.3.0 README\n\n![build-badge](https://github.com/haad/proxychains/actions/workflows/buildci.yml/badge.svg)\n\n\nProxyChains is a UNIX program, that hooks network-related libc functions\nin dynamically linked programs via a preloaded DLL and redirects the\nconnections through SOCKS4a/5 or HTTP proxies.\n\nWARNING: this program works only on dynamically linked programs.\nalso both proxychains and the program to call must use\nthe same dynamic linker (i.e. same libc)\n\n## Known limitations of the current version\n\nwhen a process forks, does a DNS lookup in the child, and then uses\nthe ip in the parent, the corresponding ip mapping will not be found,\nthis is because the fork can't write back into the parents mapping table.\nIRSSI shows this behavior, so you have to pass the resolved ip address\nto it. (you can use the proxyresolv script (requires \"dig\") to do so)\n\nthis means that you can't currently use tor onion urls for irssi.\nto solve this issue, an external data store (file, pipe, ...) has to\nmanage the dns <-> ip mapping. of course there has to be proper locking.\nshm_open, mkstemp, are possible candidates for a file based approach,\nthe other option is to spawn some kind of server process that manages the\nmap lookups. since connect() etc are hooked, this must not be a TCP server.\n\nI am reluctant on doing this change, because the described behavior\nseems pretty idiotic (doing a fork only for a DNS lookup), and irssi\nis currently the only known affected program.\n\n## Installation\n\n### Using release version\n\n*Proxychains-4.3.0* are available with pkgsrc to everyone using it on _Linux_,\n_NetBSD_, _FreeBSD_, _OpenBSD_, _DragonFlyBSD_ or _Mac OS X_. You just need to install\npkgsrc-wip repository and run\n  make install\nin a wip/proxychains directory.\n\nYou can find out more about pkgsrc on link:http://www.pkgsrc.org[pkgsrc] and about pkgsrc-wip on\nlink:https://pkgsrc.org/wip[Pkgsrc-wip homepage]\n\n### Installing on Mac OS X with homebrew\n\nYou can install current proxychains on Mac OS X with an homebrew. You have to\ndownload unofficial link:https://gist.github.com/3792521[homebrew formula] from\nto your BREW_HOME by default /usr/local/Library/Formula/ and run\n\n```\n$ brew install proxychains\n```\n\n### Running Current Source code version\n\n```\n# needs a working C compiler, preferably gcc\n./configure\nmake\nsudo make install\n```\n\n## Changelog\n\n*Version (4.x)* removes the dnsresolver script which required a dynamically\nlinked \"dig\" binary to be present with remote DNS lookup.\nthis speeds up any operation involving DNS, as the old script had to use TCP.\nadditionally it allows to use .onion urls when used with TOR.\nalso it removed the broken autoconf build system with a simple Makefile.\nthere's a ./configure script though for convenience.\nit also adds support for a config file passed via command line switches/\nenvironment variables.\n\n*Version (3.x)* introduces support for DNS resolving through proxy\nit supports SOCKS4, SOCKS5 and HTTP CONNECT proxy servers.\n\n* Auth-types\n ** socks - \"user/pass\",\n ** http - \"basic\"\n\n## When to use it\n\n* When the only way to get \"outside\" from your LAN is through proxy server.\n* To get out from behind restrictive firewall which filters outgoing ports.\n* To use two (or more) proxies in chain:\n\n```\n   like: your_host <--> proxy1 <--> proxy2 <--> target_host\n```\n\n* To \"proxify\" some program with no proxy support built-in (like telnet)\n* Access intranet from outside via proxy.\n* To use DNS behind proxy.\n\n### Some cool features\n\n* This program can mix different proxy types in the same chain\n\n```\n  like: your_host <-->socks5 <--> http <--> socks4 <--> target_host\n```\n\n* Different chaining options supported\n  random order from the list ( user defined length of chain ).\n  exact order  (as they appear in the list )\n  dynamic order (smart exclude dead proxies from chain)\n* You can use it with any TCP client application, even network scanners\n  yes, yes - you can make portscan via proxy (or chained proxies)\n  for example with Nmap scanner by fyodor (www.insecure.org/nmap).\n\n```\n  proxychains nmap -sT -PO -p 80 -iR  (find some webservers through proxy)\n```\n\n* You can use it with servers, like squid, sendmail, or whatever.\n* DNS resolving through proxy.\n\n## Configuration\n\nproxychains looks for configuration in the following order:\n\n* SOCKS5 proxy host ip and port in environment variable ${PROXYCHAINS_SOCKS5_HOST} ${PROXYCHAINS_SOCKS5_PORT}\n  (if ${PROXYCHAINS_SOCKS5_PORT} is set, no further configuration will be searched. if ${PROXYCHAINS_SOCKS5_HOST} isn't set, host ip will become \"127.0.0.1\")\n* file listed in environment variable ${PROXYCHAINS_CONF_FILE} or\n  provided as a -f argument to proxychains script or binary.\n* ./proxychains.conf\n* $(HOME)/.proxychains/proxychains.conf\n* /etc/proxychains.conf\n\nsee more in */etc/proxychains.conf*\n\n### Usage Example\n\n```\n$ proxychains4 telnet targethost.com\n```\n\nin this example it will run telnet through proxy(or chained proxies)\nspecified by *proxychains.conf*\n\n### Usage Example\n\n```\n$ proxychains4 -f /etc/proxychains-other.conf targethost2.com\n```\n\nin this example it will use different configuration file then *proxychains.conf*\nto connect to targethost2.com host.\n\n### Usage Example\n\n```\n$ proxyresolv targethost.com\n```\n\nin this example it will resolve targethost.com through proxy(or chained proxies)\nspecified by *proxychains.conf*\n\n### Usage Example:\n\n```\n$ ssh -fN -D 4321 some.example.com\n$ PROXYCHAINS_SOCKS5_HOST=127.0.0.1 PROXYCHAINS_SOCKS5_PORT=4321 proxychains zsh\n```\n\nin this example, it will run a shell with all traffic proxied through\nOpenSSH's \"dynamic proxy\" (SOCKS5 proxy) on localhost port 4321.\n\n### Usage Example:\n\n```\n$ export PROXY_DNS_SERVER=8.8.8.8\n$ proxychains4 telnet targethost.com\n```\n\nin this example, it will telnet to targethost.com using the 8.8.8.8\nnameserver supplied by the user through the PROXY_DNS_SERVER\n"
  },
  {
    "path": "TODO",
    "content": "ProxyChains ver 4.0 TODO\n===================\n\n- hooks for reentrant dns functions, i.e. gethostbyaddr_r\n- shadowsocks support #133\n- proxy change according to attempts #156\n\n\n\n\n\n"
  },
  {
    "path": "completions/zsh/_proxychains4",
    "content": "#compdef proxychains4\n\n_arguments -s -S '1: :{_command_names -e}' '*:: :_normal'\n"
  },
  {
    "path": "configure",
    "content": "#!/bin/sh\n\nprefix=/usr/local\ndevelflg=0\n\nusage() {\n\techo \"supported arguments\"\n\techo \"--prefix=/path \t\tdefault: $prefix\"\n\techo \"--exec_prefix=/path\tdefault: $prefix/bin\"\n\techo \"--bindir=/path\t\tdefault: $prefix/bin\"\n\techo \"--libdir=/path\t\tdefault: $prefix/lib\"\n\techo \"--includedir=/path\tdefault: $prefix/include\"\n\techo \"--sysconfdir=/path\tdefault: $prefix/etc\"\n\techo \"--devel\t\t\t\tdefault:no (set development mode)\"\n\techo \"--help : show this text\"\n\texit 1\n}\n\nspliteq() {\n\targ=$1\n\techo \"${arg#*=}\"\n\t#alternatives echo \"$arg\" | cut -d= -f2-\n\t# or echo \"$arg\" | sed 's/[^=]*=//'\n}\n\nparsearg() {\n\tcase \"$1\" in\n\t--prefix=*) prefix=`spliteq $1`;;\n\t--exec_prefix=*) exec_prefix=`spliteq $1`;;\n\t--bindir=*) bindir=`spliteq $1`;;\n\t--libdir=*) libdir=`spliteq $1`;;\n\t--includedir=*) includedir=`spliteq $1`;;\n\t--sysconfdir=*) sysconfdir=`spliteq $1`;;\n\t--devel) develflg=1;;\n\t--help) usage;;\n\tesac\n}\n\nismac() {\n\tuname -s | grep Darwin\n}\n\nisbsd() {\n        uname -s | grep BSD\n}\n\nislinux() {\n        uname -s | grep Linux\n}\n\nwhile true ; do\n\tcase $1 in\n\t-*) parsearg \"$1\"; shift;;\n\t*) break ;;\n\tesac\ndone\n\nif [ -z \"$exec_prefix\" ] ; then\n\texec_prefix=$prefix\nfi\n\nif [ -z \"$libdir\" ] ; then\n\tlibdir=$prefix/lib\nfi\n\nif [ -z \"$includedir\" ] ; then\n\tincludedir=$prefix/include\nfi\n\nif [ -z \"$sysconfdir\" ] ; then\n\tsysconfdir=$prefix/etc\nfi\n\nif [ -z \"$bindir\" ] ; then\n\tbindir=$exec_prefix/bin\nfi\n\nif [ -z \"$CC\" ] ; then\n\tCC=cc\nfi\n\nif [ $develflg -eq 1 ]; then\n\tCFLAGS=\"-Wextra -Wunused -Wuninitialized -Wconversion -fno-common -g -O0 -DDEBUG\"\nfi\n\necho CC?=$CC>config.mak\n[ -z \"$CPPFLAGS\" ] || echo CPPFLAGS?=$CPPFLAGS>>config.mak\n[ -z \"$CFLAGS\" ] || echo USER_CFLAGS?=$CFLAGS>>config.mak\n[ -z \"$LDFLAGS\" ] || echo USER_LDFLAGS?=$LDFLAGS>>config.mak\n\necho prefix=$prefix>>config.mak\necho exec_prefix=$exec_prefix>>config.mak\necho bindir=$bindir>>config.mak\necho libdir=$libdir>>config.mak\necho includedir=$includedir>>config.mak\necho sysconfdir=$sysconfdir>>config.mak\n\nif ismac ; then\n\tarch=`uname -m`\n\n\techo LDSO_SUFFIX=dylib>>config.mak\n\techo OS_CFLAGS+=-DIS_MAC=1 -arch $arch >>config.mak\n\techo OS_LDFLAGS+=-arch $arch -lpthread -ldl -Wl,>>config.mak\n\techo LD_SET_SONAME=-Wl,-install_name,>>config.mak\n\techo LDSO_SUFFIX=dylib>>config.mak\n\techo INSTALL_FLAGS=-m>>config.mak\nfi\n\nif islinux ; then\n    echo OS_LDFLAGS=-pthread -ldl -Wl,--no-as-needed>>config.mak\n    echo LD_SET_SONAME=-Wl,-soname= >>config.mak\nfi\n\nif isbsd ; then\n    #echo OS_LDFLAGS=\n    #echo OS_CFLAGS\n    echo INSTALL_FLAGS=-m>>config.mak\n    echo LD_SET_SONAME=-Wl,-rpath,$libdir -install_name,>>config.mak\nfi\n\necho done, now run make \\&\\& make install\n\n\n"
  },
  {
    "path": "dist/proxychains.conf",
    "content": "strict_chain\n#proxy_dns\n#remote_dns_subnet 224\ntcp_read_time_out 15000\ntcp_connect_time_out 8000\n\nlocalnet 127.0.0.0/255.0.0.0\n\n\n[ProxyList]\nhttp 162.243.184.252 8585\n#socks4 184.170.245.148 4145\n#raw 162.243.184.252 8585\n"
  },
  {
    "path": "proxychains.lsm",
    "content": "Begin3\nTitle:          ProxyChains\nVersion:        4.3\nEntered-date:\nDescription:\nKeywords:\nAuthor:          <N37CR347UR3>\nMaintained-by:   <haad@netbsd.org>\nPrimary-site:\nHome-page:      https://github.com/haad/proxychains\nOriginal-site:\nPlatforms:       Linux and other Unices\nCopying-policy: GNU Public License\nEnd\n"
  },
  {
    "path": "src/common.c",
    "content": "#include \"common.h\"\n\n#include <stdlib.h>\n#include <unistd.h>\n#include <stdio.h>\n\nstatic int check_path(char *path) {\n\tif(!path)\n\t\treturn 0;\n\treturn access(path, R_OK) != -1;\n}\n\nchar *get_config_path(char* default_path, char* pbuf, size_t bufsize) {\n\tchar buf[512];\n\t// top priority: user defined path\n\tchar *path = default_path;\n\tif(check_path(path))\n\t\treturn path;\n\tif (!pbuf)\n\t\tgoto err;\n\n\t// priority 1: env var PROXYCHAINS_CONF_FILE\n\tpath = getenv(PROXYCHAINS_CONF_FILE_ENV_VAR);\n\tif(check_path(path))\n\t\treturn path;\n\n\t// priority 2; proxychains conf in actual dir\n\tpath = getcwd(buf, sizeof(buf));\n\tsnprintf(pbuf, bufsize, \"%s/%s\", path, PROXYCHAINS_CONF_FILE);\n\tpath = pbuf;\n\tif(check_path(path))\n\t\treturn path;\n\n\t// priority 3; $HOME/.proxychains/proxychains.conf\n\tpath = getenv(\"HOME\");\n\tsnprintf(pbuf, bufsize, \"%s/.proxychains/%s\", path, PROXYCHAINS_CONF_FILE);\n\tpath = pbuf;\n\tif(check_path(path))\n\t\treturn path;\n\n\t// priority 4: $SYSCONFDIR/proxychains.conf\n\tpath = SYSCONFDIR \"/\" PROXYCHAINS_CONF_FILE;\n\tif(check_path(path))\n\t\treturn path;\n\n\t// priority 5: /etc/proxychains.conf\n\tpath = \"/etc/\" PROXYCHAINS_CONF_FILE;\n\tif(check_path(path))\n\t\treturn path;\n\nerr:\n\tperror(\"couldn't find configuration file\");\n\texit(1);\n}\n"
  },
  {
    "path": "src/common.h",
    "content": "#define PROXYCHAINS_CONF_FILE_ENV_VAR \"PROXYCHAINS_CONF_FILE\"\n#define PROXYCHAINS_QUIET_MODE_ENV_VAR \"PROXYCHAINS_QUIET_MODE\"\n#define PROXYCHAINS_CONF_FILE \"proxychains.conf\"\n#define PROXYCHAINS_SOCKS5_PORT_ENV_VAR \"PROXYCHAINS_SOCKS5_PORT\"\n#define PROXYCHAINS_SOCKS5_HOST_ENV_VAR \"PROXYCHAINS_SOCKS5_HOST\"\n#define PROXYCHAINS_DNS_ENV_VAR \"PROXYCHAINS_DNS\"\n#define LOG_PREFIX \"[proxychains] \"\n\n#define PROXYCHAINS_VERSION_MAJOR 4\n#define PROXYCHAINS_VERSION_MINOR 3\n#define PROXYCHAINS_VERSION_BUGFIX 0\n\n#include <stddef.h>\n\nchar *get_config_path(char* default_path, char* pbuf, size_t bufsize);\n"
  },
  {
    "path": "src/core.c",
    "content": "/***************************************************************************\n                          core.c  -  description\n                             -------------------\n    begin                : Tue May 14 2002\n    copyright            :  netcreature (C) 2002\n    email                : netcreature@users.sourceforge.net\n ***************************************************************************\n *     GPL *\n ***************************************************************************\n *                                                                         *\n *   This program is free software; you can redistribute it and/or modify  *\n *   it under the terms of the GNU General Public License as published by  *\n *   the Free Software Foundation; either version 2 of the License, or     *\n *   (at your option) any later version.                                   *\n *                                                                         *\n ***************************************************************************/\n#include <sys/types.h>\n#include <sys/cdefs.h>\n\n#include <stdio.h>\n#include <unistd.h>\n#include <stdlib.h>\n#include <string.h>\n#include <errno.h>\n#include <netdb.h>\n\n#include <sys/utsname.h>\n#include <netinet/in.h>\n#include <arpa/inet.h>\n#include <sys/socket.h>\n#include <sys/poll.h>\n#include <sys/wait.h>\n#include <fcntl.h>\n#include <time.h>\n#include <sys/time.h>\n#include <stdarg.h>\n#include <assert.h>\n\n#ifdef THREAD_SAFE\npthread_mutex_t internal_ips_lock;\n\n#ifdef __APPLE__\npthread_mutex_t internal_getsrvbyname_lock;\n#endif /* __APPLE__ */\n\n#endif /* THREAD_SAFE */\n\n#include \"core.h\"\n#include \"common.h\"\n\nextern int tcp_read_time_out;\nextern int tcp_connect_time_out;\nextern int proxychains_quiet_mode;\nextern unsigned int remote_dns_subnet;\n\ninternal_ip_lookup_table internal_ips = { 0, 0, NULL };\n\nuint32_t dalias_hash(char *s0) {\n\tunsigned char *s = (void *) s0;\n\tuint_fast32_t h = 0;\n\twhile(*s) {\n\t\th = 16 * h + *s++;\n\t\th ^= h >> 24 & 0xf0;\n\t}\n\treturn h & 0xfffffff;\n}\n\nuint32_t index_from_internal_ip(ip_type internalip) {\n\tip_type tmp = internalip;\n\tuint32_t ret;\n\tret = (uint32_t) (tmp.octet[3] + (tmp.octet[2] << 8) + (tmp.octet[1] << 16));\n\tret -= 1;\n\treturn ret;\n}\n\nchar *string_from_internal_ip(ip_type internalip) {\n\tchar *res = NULL;\n\tuint32_t index = index_from_internal_ip(internalip);\n\tMUTEX_LOCK(&internal_ips_lock);\n\tif(index < internal_ips.counter)\n\t\tres = internal_ips.list[index]->string;\n\tMUTEX_UNLOCK(&internal_ips_lock);\n\treturn res;\n}\n\nin_addr_t make_internal_ip(uint32_t index) {\n\tip_type ret;\n\tindex++;\t\t// so we can start at .0.0.1\n\tif(index > 0xFFFFFF)\n\t\treturn (in_addr_t) - 1;\n\tret.octet[0] = remote_dns_subnet & 0xFF;\n\tret.octet[1] = (index & 0xFF0000) >> 16;\n\tret.octet[2] = (index & 0xFF00) >> 8;\n\tret.octet[3] = index & 0xFF;\n\treturn (in_addr_t) ret.as_int;\n}\n\nstatic const char base64[] = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\nstatic int poll_retry(struct pollfd *fds, nfds_t nfsd, int timeout)\n{\n\tint ret;\n\tint time_remain = timeout;\n\tint time_elapsed = 0;\n\n\tstruct timeval start_time;\n\tstruct timeval tv;\n\n\tgettimeofday(&start_time, NULL);\n\n\tdo {\n\t\t//printf(\"Retry %d\\n\", time_remain);\n\t\tret = poll(fds, nfsd, time_remain);\n\t\tgettimeofday(&tv, NULL);\n\t\ttime_elapsed = ((int)(tv.tv_sec - start_time.tv_sec) * 1000 + (int)(tv.tv_usec - start_time.tv_usec) / 1000);\n\t\t//printf(\"Time elapsed %d\\n\", time_elapsed);\n\t\ttime_remain = timeout - time_elapsed;\n\t} while(ret == -1 && errno == EINTR && time_remain > 0);\n\n\t//if (ret == -1)\n\t//printf(\"Return %d %d %s\\n\", ret, errno, strerror(errno));\n\treturn ret;\n}\n\n\nstatic void encode_base_64(char *src, char *dest, int max_len) {\n\tint n, i;\n\tsize_t l;\n\n\tl = strlen(src);\n\tmax_len = (max_len - 1) / 4;\n\tfor(i = 0; i < max_len; i++, src += 3, l -= 3) {\n\t\tswitch (l) {\n\t\t\tcase 0:\n\t\t\t\tbreak;\n\t\t\tcase 1:\n\t\t\t\tn = src[0] << 16;\n\t\t\t\t*dest++ = base64[(n >> 18) & 077];\n\t\t\t\t*dest++ = base64[(n >> 12) & 077];\n\t\t\t\t*dest++ = '=';\n\t\t\t\t*dest++ = '=';\n\t\t\t\tbreak;\n\t\t\tcase 2:\n\t\t\t\tn = src[0] << 16 | src[1] << 8;\n\t\t\t\t*dest++ = base64[(n >> 18) & 077];\n\t\t\t\t*dest++ = base64[(n >> 12) & 077];\n\t\t\t\t*dest++ = base64[(n >> 6) & 077];\n\t\t\t\t*dest++ = '=';\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\tn = src[0] << 16 | src[1] << 8 | src[2];\n\t\t\t\t*dest++ = base64[(n >> 18) & 077];\n\t\t\t\t*dest++ = base64[(n >> 12) & 077];\n\t\t\t\t*dest++ = base64[(n >> 6) & 077];\n\t\t\t\t*dest++ = base64[n & 077];\n\t\t}\n\t\tif(l < 3)\n\t\t\tbreak;\n\t}\n\t*dest++ = 0;\n}\n\nvoid proxychains_write_log(char *str, ...) {\n\tchar buff[1024*20];\n\tva_list arglist;\n\tif(!proxychains_quiet_mode) {\n\t\tva_start(arglist, str);\n\t\tvsnprintf(buff, sizeof(buff), str, arglist);\n\t\tva_end(arglist);\n\t\tfprintf(stderr, \"%s\", buff);\n\t\tfflush(stderr);\n\t}\n}\n\nstatic size_t write_n_bytes(int fd, char *buff, size_t size) {\n\tsize_t i = 0;\n\tsize_t wrote = 0;\n\tfor(;;) {\n\t\ti = (size_t) write(fd, &buff[wrote], size - wrote);\n\t\tif(i <= 0)\n\t\t\treturn i;\n\t\twrote += i;\n\t\tif(wrote == size)\n\t\t\treturn wrote;\n\t}\n}\n\nstatic size_t read_n_bytes(int fd, char *buff, size_t size) {\n\tint ready;\n\tsize_t i;\n\tstruct pollfd pfd[1];\n\n\tpfd[0].fd = fd;\n\tpfd[0].events = POLLIN;\n\tfor(i = 0; i < size; i++) {\n\t\tpfd[0].revents = 0;\n\t\tready = poll_retry(pfd, 1, tcp_read_time_out);\n\t\tif(ready != 1 || !(pfd[0].revents & POLLIN) || 1 != read(fd, &buff[i], 1))\n\t\t\treturn 0;\n\t}\n\treturn size;\n}\n\nstatic int timed_connect(int sock, const struct sockaddr *addr, socklen_t len) {\n\tint ret, value;\n\tsocklen_t value_len;\n\tstruct pollfd pfd[1];\n\n\tpfd[0].fd = sock;\n\tpfd[0].events = POLLOUT;\n\tfcntl(sock, F_SETFL, O_NONBLOCK);\n\tret = true_connect(sock, addr, len);\n\tPDEBUG(\"\\nconnect ret=%d\\n\", ret);\n\n\tif(ret == -1 && errno == EINPROGRESS) {\n\t\tret = poll_retry(pfd, 1, tcp_connect_time_out);\n\t\tPDEBUG(\"\\npoll ret=%d\\n\", ret);\n\t\tif(ret == 1) {\n\t\t\tvalue_len = sizeof(socklen_t);\n\t\t\tgetsockopt(sock, SOL_SOCKET, SO_ERROR, &value, &value_len);\n\t\t\tPDEBUG(\"\\nvalue=%d\\n\", value);\n\t\t\tif(!value)\n\t\t\t\tret = 0;\n\t\t\telse\n\t\t\t\tret = -1;\n\t\t} else {\n\t\t\tret = -1;\n\t\t}\n\t} else {\n#ifdef DEBUG\n\t\tif(ret == -1)\n\t\t\tperror(\"true_connect\");\n#endif\n\t\tif(ret != 0)\n\t\t\tret = -1;\n\t}\n\n\tfcntl(sock, F_SETFL, !O_NONBLOCK);\n\treturn ret;\n}\n\n\n#define INVALID_INDEX 0xFFFFFFFFU\n#define HTTP_AUTH_MAX ((0xFF * 2) + 1 + 1)\n\nstatic int tunnel_to(int sock, ip_type ip, unsigned short port, proxy_type pt, char *user, char *pass) {\n\tchar *dns_name = NULL;\n\tsize_t dns_len = 0;\n\n\tPDEBUG(\"tunnel_to()\\n\");\n\n\t// we use ip addresses with 224.* to lookup their dns name in our table, to allow remote DNS resolution\n\t// the range 224-255.* is reserved, and it won't go outside (unless the app does some other stuff with\n\t// the results returned from gethostbyname et al.)\n\t// the hardcoded number 224 can now be changed using the config option remote_dns_subnet to i.e. 127\n\tif(ip.octet[0] == remote_dns_subnet) {\n\t\tdns_name = string_from_internal_ip(ip);\n\t\tif(!dns_name)\n\t\t\tgoto err;\n\t\tdns_len = strlen(dns_name);\n\t\tif(!dns_len)\n\t\t\tgoto err;\n\t}\n\n\tPDEBUG(\"host dns %s\\n\", dns_name ? dns_name : \"<NULL>\");\n\n\tsize_t ulen = strlen(user);\n\tsize_t passlen = strlen(pass);\n\n\tif(ulen > 0xFF || passlen > 0xFF || dns_len > 0xFF) {\n\t\tproxychains_write_log(LOG_PREFIX \"error: maximum size of 255 for user/pass or domain name!\\n\");\n\t\tgoto err;\n\t}\n\n\tsize_t len;\n\tunsigned char buff[BUFF_SIZE];\n\tchar ip_buf[16];\n\t//memset (buff, 0, sizeof(buff));\n\n\tswitch (pt) {\n\t\tcase RAW_TYPE: {\n\t\t\treturn SUCCESS;\n\t\t}\n\t\tbreak;\n\t\tcase HTTP_TYPE:{\n\t\t\t\tif(!dns_len) {\n\t\t\t\t\tinet_ntop(AF_INET, &ip.octet[0], ip_buf, sizeof(ip_buf));\n\t\t\t\t\tdns_name = ip_buf;\n\t\t\t\t}\n\n\t\t\t\tsnprintf((char *) buff, sizeof(buff), \"CONNECT %s:%d HTTP/1.0\\r\\n\", dns_name,\n\t\t\t\t\t ntohs(port));\n\n\t\t\t\tif(user[0]) {\n\t\t\t\t\t// 2 * 0xff: username and pass, plus 1 for ':' and 1 for zero terminator.\n\t\t\t\t\tchar src[HTTP_AUTH_MAX];\n\t\t\t\t\tchar dst[(4 * HTTP_AUTH_MAX)];\n\n\t\t\t\t\tmemcpy(src, user, ulen);\n\t\t\t\t\tmemcpy(src + ulen, \":\", 1);\n\t\t\t\t\tmemcpy(src + ulen + 1, pass, passlen);\n\t\t\t\t\tsrc[ulen + 1 + passlen] = 0;\n\n\t\t\t\t\tencode_base_64(src, dst, sizeof(dst));\n\t\t\t\t\tstrcat((char *) buff, \"Proxy-Authorization: Basic \");\n\t\t\t\t\tstrcat((char *) buff, dst);\n\t\t\t\t\tstrcat((char *) buff, \"\\r\\n\\r\\n\");\n\t\t\t\t} else\n\t\t\t\t\tstrcat((char *) buff, \"\\r\\n\");\n\n\t\t\t\tlen = strlen((char *) buff);\n\n\t\t\t\tif(len != (size_t) send(sock, buff, len, 0))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tlen = 0;\n\t\t\t\t// read header byte by byte.\n\t\t\t\twhile(len < BUFF_SIZE) {\n\t\t\t\t\tif(1 == read_n_bytes(sock, (char *) (buff + len), 1))\n\t\t\t\t\t\tlen++;\n\t\t\t\t\telse\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\tif(len > 4 &&\n\t\t\t\t\t   buff[len - 1] == '\\n' &&\n\t\t\t\t\t   buff[len - 2] == '\\r' && buff[len - 3] == '\\n' && buff[len - 4] == '\\r')\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\n\t\t\t\t// if not ok (200) or response greather than BUFF_SIZE return BLOCKED;\n\t\t\t\tif(len == BUFF_SIZE || !(buff[9] == '2' && buff[10] == '0' && buff[11] == '0'))\n\t\t\t\t\treturn BLOCKED;\n\n\t\t\t\treturn SUCCESS;\n\t\t\t}\n\t\t\tbreak;\n\n\t\tcase SOCKS4_TYPE:{\n\t\t\t\tbuff[0] = 4;\t// socks version\n\t\t\t\tbuff[1] = 1;\t// connect command\n\t\t\t\tmemcpy(&buff[2], &port, 2);\t// dest port\n\t\t\t\tif(dns_len) {\n\t\t\t\t\tip.octet[0] = 0;\n\t\t\t\t\tip.octet[1] = 0;\n\t\t\t\t\tip.octet[2] = 0;\n\t\t\t\t\tip.octet[3] = 1;\n\t\t\t\t}\n\t\t\t\tmemcpy(&buff[4], &ip, 4);\t// dest host\n\t\t\t\tlen = ulen + 1;\t// username\n\t\t\t\tif(len > 1)\n\t\t\t\t\tmemcpy(&buff[8], user, len);\n\t\t\t\telse {\n\t\t\t\t\tbuff[8] = 0;\n\t\t\t\t}\n\n\t\t\t\t// do socksv4a dns resolution on the server\n\t\t\t\tif(dns_len) {\n\t\t\t\t\tmemcpy(&buff[8 + len], dns_name, dns_len + 1);\n\t\t\t\t\tlen += dns_len + 1;\n\t\t\t\t}\n\n\t\t\t\tif((len + 8) != write_n_bytes(sock, (char *) buff, (8 + len)))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tif(8 != read_n_bytes(sock, (char *) buff, 8))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tif(buff[0] != 0 || buff[1] != 90)\n\t\t\t\t\treturn BLOCKED;\n\n\t\t\t\treturn SUCCESS;\n\t\t\t}\n\t\t\tbreak;\n\t\tcase SOCKS5_TYPE:{\n\t\t\t\tif(user) {\n\t\t\t\t\tbuff[0] = 5;\t//version\n\t\t\t\t\tbuff[1] = 2;\t//nomber of methods\n\t\t\t\t\tbuff[2] = 0;\t// no auth method\n\t\t\t\t\tbuff[3] = 2;\t/// auth method -> username / password\n\t\t\t\t\tif(4 != write_n_bytes(sock, (char *) buff, 4))\n\t\t\t\t\t\tgoto err;\n\t\t\t\t} else {\n\t\t\t\t\tbuff[0] = 5;\t//version\n\t\t\t\t\tbuff[1] = 1;\t//nomber of methods\n\t\t\t\t\tbuff[2] = 0;\t// no auth method\n\t\t\t\t\tif(3 != write_n_bytes(sock, (char *) buff, 3))\n\t\t\t\t\t\tgoto err;\n\t\t\t\t}\n\n\t\t\t\tif(2 != read_n_bytes(sock, (char *) buff, 2))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tif(buff[0] != 5 || (buff[1] != 0 && buff[1] != 2)) {\n\t\t\t\t\tif(buff[0] == 5 && buff[1] == 0xFF)\n\t\t\t\t\t\treturn BLOCKED;\n\t\t\t\t\telse\n\t\t\t\t\t\tgoto err;\n\t\t\t\t}\n\n\t\t\t\tif(buff[1] == 2) {\n\t\t\t\t\t// authentication\n\t\t\t\t\tchar in[2];\n\t\t\t\t\tchar out[515];\n\t\t\t\t\tchar *cur = out;\n\t\t\t\t\tint c;\n\t\t\t\t\t*cur++ = 1;\t// version\n\t\t\t\t\tc = ulen & 0xFF;\n\t\t\t\t\t*cur++ = (char)c;\n\t\t\t\t\tmemcpy(cur, user, (size_t)c);\n\t\t\t\t\tcur += c;\n\t\t\t\t\tc = passlen & 0xFF;\n\t\t\t\t\t*cur++ = (char)c;\n\t\t\t\t\tmemcpy(cur, pass, (size_t)c);\n\t\t\t\t\tcur += c;\n\n\t\t\t\t\tif((size_t)(cur - out) != write_n_bytes(sock, out, (size_t) (cur - out)))\n\t\t\t\t\t\tgoto err;\n\n\n\t\t\t\t\tif(2 != read_n_bytes(sock, in, 2))\n\t\t\t\t\t\tgoto err;\n\t\t\t\t\tif(in[0] != 1 || in[1] != 0) {\n\t\t\t\t\t\tif(in[0] != 1)\n\t\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\telse\n\t\t\t\t\t\t\treturn BLOCKED;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tsize_t buff_iter = 0;\n\t\t\t\tbuff[buff_iter++] = 5;\t// version\n\t\t\t\tbuff[buff_iter++] = 1;\t// connect\n\t\t\t\tbuff[buff_iter++] = 0;\t// reserved\n\n\t\t\t\tif(!dns_len) {\n\t\t\t\t\tbuff[buff_iter++] = 1;\t// ip v4\n\t\t\t\t\tmemcpy(buff + buff_iter, &ip, 4);\t// dest host\n\t\t\t\t\tbuff_iter += 4;\n\t\t\t\t} else {\n\t\t\t\t\tbuff[buff_iter++] = 3;\t//dns\n\t\t\t\t\tbuff[buff_iter++] = dns_len & 0xFF;\n\t\t\t\t\tmemcpy(buff + buff_iter, dns_name, dns_len);\n\t\t\t\t\tbuff_iter += dns_len;\n\t\t\t\t}\n\n\t\t\t\tmemcpy(buff + buff_iter, &port, 2);\t// dest port\n\t\t\t\tbuff_iter += 2;\n\n\n\t\t\t\tif(buff_iter != write_n_bytes(sock, (char *) buff, buff_iter))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tif(4 != read_n_bytes(sock, (char *) buff, 4))\n\t\t\t\t\tgoto err;\n\n\t\t\t\tif(buff[0] != 5 || buff[1] != 0)\n\t\t\t\t\tgoto err;\n\n\t\t\t\tswitch (buff[3]) {\n\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\tlen = 4;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\tlen = 16;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 3:\n\t\t\t\t\t\tlen = 0;\n\t\t\t\t\t\tif(1 != read_n_bytes(sock, (char *) &len, 1))\n\t\t\t\t\t\t\tgoto err;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tgoto err;\n\t\t\t\t}\n\n\t\t\t\tif(len + 2 != read_n_bytes(sock, (char *) buff, len + 2))\n\t\t\t\t\tgoto err;\n\n\t\t\t\treturn SUCCESS;\n\t\t\t}\n\t\t\tbreak;\n\t}\n\n\terr:\n\treturn SOCKET_ERROR;\n}\n\n#define TP \" ... \"\n#define DT \"Dynamic chain\"\n#define ST \"Strict chain\"\n#define RT \"Random chain\"\n\nstatic int start_chain(int *fd, proxy_data * pd, char *begin_mark) {\n\tstruct sockaddr_in addr;\n\tchar ip_buf[16];\n\n\t*fd = socket(PF_INET, SOCK_STREAM, 0);\n\tif(*fd == -1)\n\t\tgoto error;\n\n\tinet_ntop(AF_INET, &pd->ip.octet[0], ip_buf, sizeof(ip_buf));\n\tproxychains_write_log(LOG_PREFIX \"%s \" TP \" %s:%d \",\n\t\t\t      begin_mark, ip_buf, htons(pd->port));\n\tpd->ps = PLAY_STATE;\n\tmemset(&addr, 0, sizeof(addr));\n\taddr.sin_family = AF_INET;\n\taddr.sin_addr.s_addr = (in_addr_t) pd->ip.as_int;\n\taddr.sin_port = pd->port;\n\tif(timed_connect(*fd, (struct sockaddr *) &addr, sizeof(addr))) {\n\t\tpd->ps = DOWN_STATE;\n\t\tgoto error1;\n\t}\n\tpd->ps = BUSY_STATE;\n\treturn SUCCESS;\n\terror1:\n\tproxychains_write_log(TP \" timeout\\n\");\n\terror:\n\tif(*fd != -1)\n\t\tclose(*fd);\n\treturn SOCKET_ERROR;\n}\n\nunsigned int get_rand_int(unsigned int range){\n    static FILE *fp;\n    unsigned int randval;\n    if (!fp) {\n        fp = fopen(\"/dev/urandom\", \"r\");\n    }\n    if(fread(&randval, sizeof(randval), 1, fp)) {\n\treturn (randval % range);\n    } else {\n\tsrand((unsigned int)time(NULL));\n\treturn ((unsigned int)rand() % range);\n    }\n}\n\nstatic proxy_data *select_proxy(select_type how, proxy_data * pd, unsigned int proxy_count, unsigned int *offset) {\n\tunsigned int i = 0, k = 0;\n\n\tif(*offset >= proxy_count)\n\t\treturn NULL;\n\tswitch (how) {\n\t\tcase RANDOMLY:\n\n\t\t\tdo {\n\t\t\t\tk++;\n\t\t\t\ti = 0 + get_rand_int(proxy_count);\n\t\t\t} while(pd[i].ps != PLAY_STATE && k < proxy_count * 100);\n\t\t\tbreak;\n\t\tcase FIFOLY:\n\t\t\tfor(i = *offset; i < proxy_count; i++) {\n\t\t\t\tif(pd[i].ps == PLAY_STATE) {\n\t\t\t\t\t*offset = i;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tdefault:\n\t\t\tbreak;\n\t}\n\tif(i >= proxy_count)\n\t\ti = 0;\n\treturn (pd[i].ps == PLAY_STATE) ? &pd[i] : NULL;\n}\n\n\nstatic void release_all(proxy_data * pd, unsigned int proxy_count) {\n\tunsigned int i;\n\tfor(i = 0; i < proxy_count; i++)\n\t\tpd[i].ps = PLAY_STATE;\n\treturn;\n}\n\nstatic void release_busy(proxy_data * pd, unsigned int proxy_count) {\n\tunsigned int i;\n\tfor(i = 0; i < proxy_count; i++)\n\t\tif(pd[i].ps == BUSY_STATE)\n\t\t\tpd[i].ps = PLAY_STATE;\n\treturn;\n}\n\nstatic unsigned int calc_alive(proxy_data * pd, unsigned int proxy_count) {\n\tunsigned int i, alive_count = 0;\n\trelease_busy(pd, proxy_count);\n\tfor(i = 0; i < proxy_count; i++)\n\t\tif(pd[i].ps == PLAY_STATE)\n\t\t\talive_count++;\n\treturn alive_count;\n}\n\nstatic int chain_step(int ns, proxy_data * pfrom, proxy_data * pto) {\n\tint retcode = -1;\n\tchar *hostname;\n\tchar ip_buf[16];\n\n\tPDEBUG(\"chain_step()\\n\");\n\n\tif(pto->ip.octet[0] == remote_dns_subnet) {\n\t\thostname = string_from_internal_ip(pto->ip);\n\t\tif(!hostname)\n\t\t\tgoto usenumericip;\n\t} else {\n\tusenumericip:\n\t\tinet_ntop(AF_INET, &pto->ip.octet[0], ip_buf, sizeof(ip_buf));\n\t\thostname = ip_buf;\n\t}\n\n\tproxychains_write_log(TP \" %s:%d \", hostname, htons(pto->port));\n\tretcode = tunnel_to(ns, pto->ip, pto->port, pfrom->pt, pfrom->user, pfrom->pass);\n\tswitch (retcode) {\n\t\tcase SUCCESS:\n\t\t\tpto->ps = BUSY_STATE;\n\t\t\tbreak;\n\t\tcase BLOCKED:\n\t\t\tpto->ps = BLOCKED_STATE;\n\t\t\tproxychains_write_log(\"<--denied\\n\");\n\t\t\tclose(ns);\n\t\t\tbreak;\n\t\tcase SOCKET_ERROR:\n\t\t\tpto->ps = DOWN_STATE;\n\t\t\tproxychains_write_log(\"<--socket error or timeout!\\n\");\n\t\t\tclose(ns);\n\t\t\tbreak;\n\t}\n\treturn retcode;\n}\n\nint connect_proxy_chain(int sock, ip_type target_ip,\n\t\t\tunsigned short target_port, proxy_data * pd,\n\t\t\tunsigned int proxy_count, chain_type ct, unsigned int max_chain) {\n\tproxy_data p4;\n\tproxy_data *p1, *p2, *p3;\n\tint ns = -1;\n\tunsigned int offset = 0;\n\tunsigned int alive_count = 0;\n\tunsigned int curr_len = 0;\n\n\tp3 = &p4;\n\n\tPDEBUG(\"connect_proxy_chain\\n\");\n\n\tagain:\n\n\tswitch (ct) {\n\t\tcase DYNAMIC_TYPE:\n\t\t\tcalc_alive(pd, proxy_count);\n\t\t\toffset = 0;\n\t\t\tdo {\n\t\t\t\tif(!(p1 = select_proxy(FIFOLY, pd, proxy_count, &offset)))\n\t\t\t\t\tgoto error_more;\n\t\t\t} while(SUCCESS != start_chain(&ns, p1, DT) && offset < proxy_count);\n\t\t\tfor(;;) {\n\t\t\t\tp2 = select_proxy(FIFOLY, pd, proxy_count, &offset);\n\t\t\t\tif(!p2)\n\t\t\t\t\tbreak;\n\t\t\t\tif(SUCCESS != chain_step(ns, p1, p2)) {\n\t\t\t\t\tPDEBUG(\"GOTO AGAIN 1\\n\");\n\t\t\t\t\tgoto again;\n\t\t\t\t}\n\t\t\t\tp1 = p2;\n\t\t\t}\n\t\t\t//proxychains_write_log(TP);\n\t\t\tp3->ip = target_ip;\n\t\t\tp3->port = target_port;\n\t\t\tif(SUCCESS != chain_step(ns, p1, p3))\n\t\t\t\tgoto error;\n\t\t\tbreak;\n\n\t\tcase STRICT_TYPE:\n\t\t\tcalc_alive(pd, proxy_count);\n\t\t\toffset = 0;\n\t\t\tif(!(p1 = select_proxy(FIFOLY, pd, proxy_count, &offset))) {\n\t\t\t\tPDEBUG(\"select_proxy failed\\n\");\n\t\t\t\tgoto error_strict;\n\t\t\t}\n\t\t\tif(SUCCESS != start_chain(&ns, p1, ST)) {\n\t\t\t\tPDEBUG(\"start_chain failed\\n\");\n\t\t\t\tgoto error_strict;\n\t\t\t}\n\t\t\twhile(offset < proxy_count) {\n\t\t\t\tif(!(p2 = select_proxy(FIFOLY, pd, proxy_count, &offset)))\n\t\t\t\t\tbreak;\n\t\t\t\tif(SUCCESS != chain_step(ns, p1, p2)) {\n\t\t\t\t\tPDEBUG(\"chain_step failed\\n\");\n\t\t\t\t\tgoto error_strict;\n\t\t\t\t}\n\t\t\t\tp1 = p2;\n\t\t\t}\n\t\t\t//proxychains_write_log(TP);\n\t\t\tp3->ip = target_ip;\n\t\t\tp3->port = target_port;\n\t\t\tif(SUCCESS != chain_step(ns, p1, p3))\n\t\t\t\tgoto error;\n\t\t\tbreak;\n\n\t\tcase RANDOM_TYPE:\n\t\t\talive_count = calc_alive(pd, proxy_count);\n\t\t\tif(alive_count < max_chain)\n\t\t\t\tgoto error_more;\n\t\t\tcurr_len = offset = 0;\n\t\t\tdo {\n\t\t\t\tif(!(p1 = select_proxy(RANDOMLY, pd, proxy_count, &offset)))\n\t\t\t\t\tgoto error_more;\n\t\t\t} while(SUCCESS != start_chain(&ns, p1, RT) && offset < max_chain);\n\t\t\twhile(++curr_len < max_chain) {\n\t\t\t\tif(!(p2 = select_proxy(RANDOMLY, pd, proxy_count, &offset)))\n\t\t\t\t\tgoto error_more;\n\t\t\t\tif(SUCCESS != chain_step(ns, p1, p2)) {\n\t\t\t\t\tPDEBUG(\"GOTO AGAIN 2\\n\");\n\t\t\t\t\tgoto again;\n\t\t\t\t}\n\t\t\t\tp1 = p2;\n\t\t\t}\n\t\t\t//proxychains_write_log(TP);\n\t\t\tp3->ip = target_ip;\n\t\t\tp3->port = target_port;\n\t\t\tif(SUCCESS != chain_step(ns, p1, p3))\n\t\t\t\tgoto error;\n\t}\n\n\tproxychains_write_log(TP \" OK\\n\");\n\tdup2(ns, sock);\n\tclose(ns);\n\treturn 0;\n\terror:\n\tif(ns != -1)\n\t\tclose(ns);\n\terrno = ECONNREFUSED;\t// for nmap ;)\n\treturn -1;\n\n\terror_more:\n\tproxychains_write_log(\"\\n!!!need more proxies!!!\\n\");\n\terror_strict:\n\tPDEBUG(\"error\\n\");\n\n\trelease_all(pd, proxy_count);\n\tif(ns != -1)\n\t\tclose(ns);\n\terrno = ETIMEDOUT;\n\treturn -1;\n}\n\nstatic const ip_type local_host = { {127, 0, 0, 1} };\n\nchar hostname[256]; // default maximum length of hostname in linux\nstruct hostent *proxy_gethostbyname(const char *name, struct gethostbyname_data* data) {\n\tchar buff[256];\n\tuint32_t i, hash;\n\t// yep, new_mem never gets freed. once you passed a fake ip to the client, you can't \"retreat\" it\n\tvoid *new_mem;\n\tsize_t l;\n\tstruct hostent *hp;\n\n\tdata->resolved_addr_p[0] = (char *) &data->resolved_addr;\n\tdata->resolved_addr_p[1] = NULL;\n\n\tdata->hostent_space.h_addr_list = data->resolved_addr_p;\n\n\tdata->resolved_addr = 0;\n\n\tgethostname(buff, sizeof(buff));\n\n\tif(!strcmp(buff, name)) {\n\t\tdata->resolved_addr = inet_addr(buff);\n\t\tif(data->resolved_addr == (in_addr_t) (-1))\n\t\t\tdata->resolved_addr = (in_addr_t) (local_host.as_int);\n\n\t\tsnprintf(hostname,sizeof hostname, \"%s\", name);\n\t    data->hostent_space.h_name = hostname;\n\t    data->hostent_space.h_length = sizeof (in_addr_t);\n\t    data->hostent_space.h_addrtype = AF_INET;\n\n\t\treturn &data->hostent_space;\n\t}\n\n\tmemset(buff, 0, sizeof(buff));\n\n\twhile((hp = gethostent()))\n\t\tif(!strcmp(hp->h_name, name))\n\t\t\treturn hp;\n\n\thash = dalias_hash((char *) name);\n\n\tMUTEX_LOCK(&internal_ips_lock);\n\n\t// see if we already have this dns entry saved.\n\tif(internal_ips.counter) {\n\t\tfor(i = 0; i < internal_ips.counter; i++) {\n\t\t\tif(internal_ips.list[i]->hash == hash && !strcmp(name, internal_ips.list[i]->string)) {\n\t\t\t\tdata->resolved_addr = make_internal_ip(i);\n\t\t\t\tPDEBUG(\"got cached ip for %s\\n\", name);\n\t\t\t\tgoto have_ip;\n\t\t\t}\n\t\t}\n\t}\n\n\t// grow list if needed.\n\tif(internal_ips.capa < internal_ips.counter + 1) {\n\t\tPDEBUG(\"realloc\\n\");\n\t\tnew_mem = realloc(internal_ips.list, (internal_ips.capa + 16) * sizeof(void *));\n\t\tif(new_mem) {\n\t\t\tinternal_ips.capa += 16;\n\t\t\tinternal_ips.list = new_mem;\n\t\t} else {\n\toom:\n\t\t\tproxychains_write_log(\"out of mem\\n\");\n\t\t\tgoto err_plus_unlock;\n\t\t}\n\t}\n\n\tdata->resolved_addr = make_internal_ip(internal_ips.counter);\n\tif(data->resolved_addr == (in_addr_t) - 1)\n\t\tgoto err_plus_unlock;\n\n\tl = strlen(name);\n\tnew_mem = malloc(sizeof(string_hash_tuple) + l + 1);\n\tif(!new_mem)\n\t\tgoto oom;\n\n\tPDEBUG(\"creating new entry %d for ip of %s\\n\", (int) internal_ips.counter, name);\n\n\tinternal_ips.list[internal_ips.counter] = new_mem;\n\tinternal_ips.list[internal_ips.counter]->hash = hash;\n\n\tinternal_ips.list[internal_ips.counter]->string = (char *) new_mem + sizeof(string_hash_tuple);\n\n\tmemcpy(internal_ips.list[internal_ips.counter]->string, name, l + 1);\n\n\tinternal_ips.counter += 1;\n\n\thave_ip:\n\n\tMUTEX_UNLOCK(&internal_ips_lock);\n\n\tstrncpy(data->addr_name, name, sizeof(data->addr_name) - 1);\n\n\tdata->hostent_space.h_name = data->addr_name;\n\tdata->hostent_space.h_length = sizeof(in_addr_t);\n\tdata->hostent_space.h_addrtype = AF_INET;\n\n\treturn &data->hostent_space;\n\n\terr_plus_unlock:\n\tMUTEX_UNLOCK(&internal_ips_lock);\n\treturn NULL;\n}\n\nstruct addrinfo_data {\n\tstruct addrinfo addrinfo_space;\n\tstruct sockaddr sockaddr_space;\n\tchar addr_name[256];\n};\n\nvoid proxy_freeaddrinfo(struct addrinfo *res) {\n\tfree(res);\n}\n\nvoid proxy_getservbyname(const char * service, struct servent *se_buf,\n\tchar * buf, size_t buf_len, struct servent **se_result)\n{\n\n#ifdef __linux__\n\tgetservbyname_r(service, NULL, se_buf, buf, buf_len, se_result);\n#endif\n\n#ifdef __APPLE__\n\tstruct servent *se;\n\n\tMUTEX_LOCK(&internal_getsrvbyname_lock);\n\tif(service) {\n\t\tse = getservbyname(service, NULL);\n\n\t\tif ( se != NULL ) {\n\t\t\tmemcpy(se_buf, se, buf_len);\n\t\t\t*se_result = se_buf;\n\t\t} else {\n\t\t\t*se_result = NULL;\n\t\t}\n\t}\n\tMUTEX_UNLOCK(&internal_getsrvbyname_lock);\n#endif /* __APPLE__ */\n}\n\nint proxy_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {\n\tstruct gethostbyname_data ghdata;\n\tstruct addrinfo_data *space;\n\tstruct servent *se = NULL;\n\tstruct hostent *hp = NULL;\n\tstruct servent se_buf;\n\tstruct addrinfo *p;\n\tchar buf[1024];\n\tint port;\n\n//      printf(\"proxy_getaddrinfo node %s service %s\\n\",node,service);\n\tspace = calloc(1, sizeof(struct addrinfo_data));\n\tif(!space)\n\t\treturn 1;\n\n\tif(node && !inet_aton(node, &((struct sockaddr_in *) &space->sockaddr_space)->sin_addr)) {\n\t\thp = proxy_gethostbyname(node, &ghdata);\n\t\tif(hp) {\n\t\t\tmemcpy(&((struct sockaddr_in *) &space->sockaddr_space)->sin_addr,\n\t\t\t       *(hp->h_addr_list), sizeof(in_addr_t));\n\t\t} else {\n\t\t\tfree(space);\n\t\t\treturn 1;\n\t\t}\n\t}\n\tif(service)\n\t\tproxy_getservbyname(service, &se_buf, buf, sizeof(buf), &se);\n\n\tport = se ? se->s_port : htons((uint16_t)atoi(service ? service : \"0\"));\n\t((struct sockaddr_in *) &space->sockaddr_space)->sin_port = (in_port_t)port;\n\n\t*res = p = &space->addrinfo_space;\n\tassert((size_t)p == (size_t) space);\n\n\tp->ai_addr = &space->sockaddr_space;\n\tif(node)\n\t\tstrncpy(space->addr_name, node, sizeof(space->addr_name) - 1);\n\tp->ai_canonname = space->addr_name;\n\tp->ai_next = NULL;\n\tp->ai_family = space->sockaddr_space.sa_family = AF_INET;\n\tp->ai_addrlen = sizeof(space->sockaddr_space);\n\n\tif(hints) {\n\t\tp->ai_socktype = hints->ai_socktype;\n\t\tp->ai_flags = hints->ai_flags;\n\t\tp->ai_protocol = hints->ai_protocol;\n\t} else {\n#ifdef BSD\n\t\tp->ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);\n#else\n\t\tp->ai_flags = (AI_ADDRCONFIG);\n#endif\n\t}\n\n\treturn 0;\n}\n"
  },
  {
    "path": "src/core.h",
    "content": "/***************************************************************************\n                          core.h  -  description\n                             -------------------\n    begin                : Tue May 14 2002\n    copyright          :  netcreature (C) 2002\n    email                 : netcreature@users.sourceforge.net\n ***************************************************************************\n ***************************************************************************\n *                                                                         *\n *   This program is free software; you can redistribute it and/or modify  *\n *   it under the terms of the GNU General Public License as published by  *\n *   the Free Software Foundation; either version 2 of the License, or     *\n *   (at your option) any later version.                                   *\n *                                                                         *\n ***************************************************************************/\n\n#include <stdint.h>\n\n#ifndef __CORE_HEADER\n#define __CORE_HEADER\n#define BUFF_SIZE 8*1024  // used to read responses from proxies.\n#define     MAX_LOCALNET 64\n#define     MAX_DNAT 64\n\ntypedef union {\n\tunsigned char octet[4];\n\tuint32_t as_int;\n} ip_type;\n\ntypedef struct {\n\tuint32_t hash;\n\tchar* string;\n} string_hash_tuple;\n\ntypedef struct {\n\tuint32_t counter;\n\tuint32_t capa;\n\tstring_hash_tuple** list;\n} internal_ip_lookup_table;\n\nextern internal_ip_lookup_table internal_ips;\n\n#ifdef THREAD_SAFE\n#include <pthread.h>\n\nextern pthread_mutex_t internal_ips_lock;\nextern pthread_mutex_t internal_getsrvbyname_lock;\n\n# define MUTEX_LOCK(x) pthread_mutex_lock(x)\n# define MUTEX_UNLOCK(x) pthread_mutex_unlock(x)\n# define MUTEX_INIT(x,y) pthread_mutex_init(x, y)\n#else\n# define MUTEX_LOCK(x)\n# define MUTEX_UNLOCK(x)\n# define MUTEX_INIT(x,y)\n#endif\n\n/*error codes*/\ntypedef enum {\n\tSUCCESS=0,\n\tMEMORY_FAIL,        // malloc failed\n\tSOCKET_ERROR,  // look errno for more\n\tCHAIN_DOWN,    // no proxy in chain responds to tcp\n\tCHAIN_EMPTY,   //  if proxy_count = 0\n\tBLOCKED  //  target's port blocked on last proxy in the chain\n} ERR_CODE;\n\ntypedef enum {\n\tHTTP_TYPE,\n\tRAW_TYPE,\n\tSOCKS4_TYPE,\n\tSOCKS5_TYPE\n} proxy_type;\n\ntypedef enum {\n\tDYNAMIC_TYPE,\n\tSTRICT_TYPE,\n\tRANDOM_TYPE}\nchain_type;\n\ntypedef enum {\n\tPLAY_STATE,\n\tDOWN_STATE,\n\tBLOCKED_STATE,\n\tBUSY_STATE\n} proxy_state;\n\ntypedef enum {\n\tRANDOMLY,\n\tFIFOLY\n} select_type;\n\ntypedef struct {\n\tstruct in_addr in_addr, netmask;\n\tunsigned short port;\n} localaddr_arg;\n\ntypedef struct {\n\tstruct in_addr orig_dst, new_dst;\n\tunsigned short orig_port, new_port;\n} dnat_arg;\n\ntypedef struct {\n\tip_type ip;\n\tunsigned short port;\n\tproxy_type pt;\n\tproxy_state ps;\n\tchar user[256];\n\tchar pass[256];\n} proxy_data;\n\nint connect_proxy_chain (int, ip_type, unsigned short, proxy_data *, unsigned int,\n    chain_type, unsigned int);\n\nvoid proxychains_write_log(char *, ...);\n\ntypedef int (*connect_t)(int, const struct sockaddr *, socklen_t);\ntypedef struct hostent* (*gethostbyname_t)(const char *);\ntypedef int (*freeaddrinfo_t)(struct addrinfo *);\n\n#if (defined __linux__) || (defined __APPLE__)\ntypedef struct hostent *(*gethostbyaddr_t) (const void *, socklen_t, int);\n#else\ntypedef struct hostent *(*gethostbyaddr_t) (const char *, socklen_t, int);\n#endif\n\ntypedef int (*getaddrinfo_t)(const char *, const char *, const struct addrinfo *,\n\t\t\t     struct addrinfo **);\ntypedef int (*getnameinfo_t) (const struct sockaddr *, socklen_t, char *,\n\t\t\t      socklen_t, char *, socklen_t, int);\n\n\nextern connect_t true_connect;\nextern gethostbyname_t true_gethostbyname;\nextern getaddrinfo_t true_getaddrinfo;\nextern freeaddrinfo_t true_freeaddrinfo;\nextern getnameinfo_t true_getnameinfo;\nextern gethostbyaddr_t true_gethostbyaddr;\n\nstruct gethostbyname_data {\n\tstruct hostent hostent_space;\n\tin_addr_t resolved_addr;\n\tchar *resolved_addr_p[2];\n\tchar addr_name[1024 * 8];\n};\n\nstruct hostent* proxy_gethostbyname(const char *, struct gethostbyname_data *);\nvoid proxy_getservbyname(const char *, struct servent *, char *, size_t, struct servent **);\nint proxy_getaddrinfo(const char *, const char *, const struct addrinfo *, struct addrinfo **);\nvoid proxy_freeaddrinfo(struct addrinfo *);\n\n#ifdef DEBUG\n# define PDEBUG(fmt, args...) do { fprintf(stderr,\"DEBUG:\"fmt, ## args); fflush(stderr); } while(0)\n#else\n# define PDEBUG(fmt, args...) do {} while (0)\n#endif\n\n#endif\n"
  },
  {
    "path": "src/libproxychains.c",
    "content": "/***************************************************************************\n                          libproxychains.c  -  description\n                             -------------------\n    begin                : Tue May 14 2002\n    copyright          :  netcreature (C) 2002\n    email                 : netcreature@users.sourceforge.net\n ***************************************************************************/\n /*     GPL */\n/***************************************************************************\n *                                                                         *\n *   This program is free software; you can redistribute it and/or modify  *\n *   it under the terms of the GNU General Public License as published by  *\n *   the Free Software Foundation; either version 2 of the License, or     *\n *   (at your option) any later version.                                   *\n *                                                                         *\n ***************************************************************************/\n\n#undef _GNU_SOURCE\n#define _GNU_SOURCE\n\n#include <sys/types.h>\n#include <sys/param.h>\n\n#include <dlfcn.h>\n#include <errno.h>\n#include <fcntl.h>\n#include <netdb.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n\n#include <netinet/in.h>\n#include <arpa/inet.h>\n#include <sys/socket.h>\n\n#include \"core.h\"\n#include \"common.h\"\n\n#define     satosin(x)      ((struct sockaddr_in *) &(x))\n#define     SOCKADDR(x)     (satosin(x)->sin_addr.s_addr)\n#define     SOCKADDR_2(x)     (satosin(x)->sin_addr)\n#define     SOCKPORT(x)     (satosin(x)->sin_port)\n#define     SOCKFAMILY(x)     (satosin(x)->sin_family)\n#define     MAX_CHAIN 512\n\nconnect_t true_connect;\ngethostbyname_t true_gethostbyname;\ngetaddrinfo_t true_getaddrinfo;\nfreeaddrinfo_t true_freeaddrinfo;\ngetnameinfo_t true_getnameinfo;\ngethostbyaddr_t true_gethostbyaddr;\n\nint tcp_read_time_out;\nint tcp_connect_time_out;\nint proxychains_got_chain_data = 0;\nint proxychains_quiet_mode = 0;\nint proxychains_resolver = 0;\n\nunsigned int proxychains_proxy_count = 0;\nunsigned int proxychains_max_chain = 1;\nunsigned int remote_dns_subnet = 224;\n\nlocaladdr_arg localnet_addr[MAX_LOCALNET];\nchain_type proxychains_ct;\nproxy_data proxychains_pd[MAX_CHAIN];\n\nsize_t num_localnet_addr = 0;\ndnat_arg dnats[MAX_DNAT];\nsize_t num_dnats = 0;\n\n#ifdef THREAD_SAFE\npthread_once_t init_once = PTHREAD_ONCE_INIT;\n#endif\nstatic int init_l = 0;\n\nstatic void load_default_settings(chain_type *ct);\nstatic inline void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);\nstatic void simple_socks5_env(proxy_data * pd, unsigned int *proxy_count, chain_type * ct);\n\nstatic void* load_sym(char* symname, void* proxyfunc) {\n\n\tvoid *funcptr = dlsym(RTLD_NEXT, symname);\n\tif(!funcptr) {\n\t\tfprintf(stderr, \"Cannot load symbol '%s' %s\\n\", symname, dlerror());\n\t\texit(1);\n\t} else {\n\t\tPDEBUG(\"loaded symbol '%s'\" \" real addr %p  wrapped addr %p\\n\", symname, funcptr, proxyfunc);\n\t}\n\tif(funcptr == proxyfunc) {\n\t\tPDEBUG(\"circular reference detected, aborting!\\n\");\n\t\tabort();\n\t}\n\treturn funcptr;\n}\n\n#define INIT() init_lib_wrapper(__FUNCTION__)\n\n#define SETUP_SYM(X) do { true_ ## X = load_sym( # X, X ); } while(0)\n\nstatic void do_init(void) {\n\tMUTEX_INIT(&internal_ips_lock, NULL);\n\n#ifdef __APPLE__\n\tMUTEX_INIT(&internal_getsrvbyname_lock, NULL);\n#endif\n\n\t/* check for simple SOCKS5 proxy setup */\n\tsimple_socks5_env(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);\n\n\t/* read the config file */\n\tget_chain_data(proxychains_pd, &proxychains_proxy_count, &proxychains_ct);\n\n\tproxychains_write_log(LOG_PREFIX \"DLL init\\n\");\n\tSETUP_SYM(connect);\n\tSETUP_SYM(gethostbyname);\n\tSETUP_SYM(getaddrinfo);\n\tSETUP_SYM(freeaddrinfo);\n\tSETUP_SYM(gethostbyaddr);\n\tSETUP_SYM(getnameinfo);\n\tinit_l = 1;\n}\n\nstatic void init_lib_wrapper(const char* caller) {\n#ifndef DEBUG\n\t(void) caller;\n#endif\n#ifndef THREAD_SAFE\n\tif(init_l) return;\n\tPDEBUG(\"%s called from %s\\n\", __FUNCTION__,  caller);\n\tdo_init();\n#else\n\tif(!init_l) PDEBUG(\"%s called from %s\\n\", __FUNCTION__,  caller);\n\tpthread_once(&init_once, do_init);\n#endif\n}\n\n/* if we use gcc >= 3, we can instruct the dynamic loader\n * to call init_lib at link time. otherwise it gets loaded\n * lazily, which has the disadvantage that there's a potential\n * race condition if 2 threads call it before init_l is set\n * and PTHREAD support was disabled */\n#if __GNUC__ > 2\n__attribute__((constructor))\nstatic void gcc_init(void) {\n\tINIT();\n}\n#endif\n\nFILE *\nopen_config_file() {\n\tchar home_conf[MAXPATHLEN], prefix_conf[MAXPATHLEN];\n\tFILE *file;\n\n\tsnprintf(home_conf, 256, \"%s/.proxychains/proxychains.conf\", getenv(\"HOME\"));\n\tsnprintf(prefix_conf, 256, \"%s/etc/proxychains.conf\", INSTALL_PREFIX);\n\n\tif(!(file = fopen(\"./proxychains.conf\", \"r\"))) {\n\t\tif(!(file = fopen(home_conf, \"r\"))) {\n\t\t\tif(!(file = fopen(prefix_conf, \"r\"))) {\n\t\t\t\tif(!(file = fopen(\"/etc/proxychains.conf\", \"r\"))) {\n\t\t\t\t\tperror(\"Can't locate proxychains.conf\");\n\t\t\t\t\texit(1);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn file;\n}\n\n/* default settings common to get_chain_data and simple_socks5_env */\nstatic void load_default_settings(chain_type *ct) {\n\tchar *env;\n\n\ttcp_read_time_out = 4 * 1000;\n\ttcp_connect_time_out = 10 * 1000;\n\t*ct = DYNAMIC_TYPE;\n\n\tenv = getenv(PROXYCHAINS_QUIET_MODE_ENV_VAR);\n\tif(env && *env == '1')\n\t\tproxychains_quiet_mode = 1;\n}\n\n/* get configuration from config file */\nstatic void get_chain_data(proxy_data * pd, unsigned int *proxy_count, chain_type * ct) {\n\tunsigned int count = 0;\n\tint port_n = 0, list = 0;\n\tchar buff[1024], type[1024], host[1024], user[1024];\n\tchar *env;\n\tchar local_in_addr_port[32];\n\tchar local_in_addr[32], local_in_port[32], local_netmask[32];\n\tchar dnat_orig_addr_port[32], dnat_new_addr_port[32];\n\tchar dnat_orig_addr[32], dnat_orig_port[32], dnat_new_addr[32], dnat_new_port[32];\n\tFILE *file = NULL;\n\n\tif(proxychains_got_chain_data)\n\t\treturn;\n\n\tload_default_settings(ct);\n\n\tenv = get_config_path(getenv(PROXYCHAINS_CONF_FILE_ENV_VAR), buff, sizeof(buff));\n\tfile = fopen(env, \"r\");\n\n\twhile(fgets(buff, sizeof(buff), file)) {\n\t\tif(buff[0] != '\\n' && buff[strspn(buff, \" \")] != '#') {\n\t\t\t/* proxylist has to come last */\n\t\t\tif(list) {\n\t\t\t\tif(count >= MAX_CHAIN)\n\t\t\t\t\tbreak;\n\n\t\t\t\tmemset(&pd[count], 0, sizeof(proxy_data));\n\n\t\t\t\tpd[count].ps = PLAY_STATE;\n\t\t\t\tport_n = 0;\n\n\t\t\t\tsscanf(buff, \"%s %s %d %s %s\", type, host, &port_n, pd[count].user, pd[count].pass);\n\n\t\t\t\tpd[count].ip.as_int = (uint32_t) inet_addr(host);\n\t\t\t\tpd[count].port = htons((unsigned short) port_n);\n\n\t\t\t\tif(!strcmp(type, \"http\")) {\n\t\t\t\t\tpd[count].pt = HTTP_TYPE;\n\t\t\t\t} else if(!strcmp(type, \"raw\")) {\n\t\t\t\t\tpd[count].pt = RAW_TYPE;\n\t\t\t\t} else if(!strcmp(type, \"socks4\")) {\n\t\t\t\t\tpd[count].pt = SOCKS4_TYPE;\n\t\t\t\t} else if(!strcmp(type, \"socks5\")) {\n\t\t\t\t\tpd[count].pt = SOCKS5_TYPE;\n\t\t\t\t} else\n\t\t\t\t\tcontinue;\n\n\t\t\t\tif(pd[count].ip.as_int && port_n && pd[count].ip.as_int != (uint32_t) - 1)\n\t\t\t\t\tcount++;\n\t\t\t} else {\n\t\t\t\tif(strstr(buff, \"[ProxyList]\")) {\n\t\t\t\t\tlist = 1;\n\t\t\t\t} else if(strstr(buff, \"random_chain\")) {\n\t\t\t\t\t*ct = RANDOM_TYPE;\n\t\t\t\t} else if(strstr(buff, \"strict_chain\")) {\n\t\t\t\t\t*ct = STRICT_TYPE;\n\t\t\t\t} else if(strstr(buff, \"dynamic_chain\")) {\n\t\t\t\t\t*ct = DYNAMIC_TYPE;\n\t\t\t\t} else if(strstr(buff, \"tcp_read_time_out\")) {\n\t\t\t\t\tsscanf(buff, \"%s %d\", user, &tcp_read_time_out);\n\t\t\t\t} else if(strstr(buff, \"tcp_connect_time_out\")) {\n\t\t\t\t\tsscanf(buff, \"%s %d\", user, &tcp_connect_time_out);\n\t\t\t\t} else if(strstr(buff, \"remote_dns_subnet\")) {\n\t\t\t\t\tsscanf(buff, \"%s %d\", user, &remote_dns_subnet);\n\t\t\t\t\tif(remote_dns_subnet >= 256) {\n\t\t\t\t\t\tfprintf(stderr,\n\t\t\t\t\t\t\t\"remote_dns_subnet: invalid value. requires a number between 0 and 255.\\n\");\n\t\t\t\t\t\texit(1);\n\t\t\t\t\t}\n\t\t\t\t} else if(strstr(buff, \"localnet\")) {\n\t\t\t\t\tif(sscanf(buff, \"%s %21[^/]/%15s\", user, local_in_addr_port, local_netmask) < 3) {\n\t\t\t\t\t\tfprintf(stderr, \"localnet format error\");\n\t\t\t\t\t\texit(1);\n\t\t\t\t\t}\n\t\t\t\t\t/* clean previously used buffer */\n\t\t\t\t\tmemset(local_in_port, 0, sizeof(local_in_port) / sizeof(local_in_port[0]));\n\n\t\t\t\t\tif(sscanf(local_in_addr_port, \"%15[^:]:%5s\", local_in_addr, local_in_port) < 2) {\n\t\t\t\t\t\tPDEBUG(\"added localnet: netaddr=%s, netmask=%s\\n\",\n\t\t\t\t\t\t       local_in_addr, local_netmask);\n\t\t\t\t\t} else {\n\t\t\t\t\t\tPDEBUG(\"added localnet: netaddr=%s, port=%s, netmask=%s\\n\",\n\t\t\t\t\t\t       local_in_addr, local_in_port, local_netmask);\n\t\t\t\t\t}\n\t\t\t\t\tif(num_localnet_addr < MAX_LOCALNET) {\n\t\t\t\t\t\tint error;\n\t\t\t\t\t\terror =\n\t\t\t\t\t\t    inet_pton(AF_INET, local_in_addr,\n\t\t\t\t\t\t\t      &localnet_addr[num_localnet_addr].in_addr);\n\t\t\t\t\t\tif(error <= 0) {\n\t\t\t\t\t\t\tfprintf(stderr, \"localnet address error\\n\");\n\t\t\t\t\t\t\texit(1);\n\t\t\t\t\t\t}\n\t\t\t\t\t\terror =\n\t\t\t\t\t\t    inet_pton(AF_INET, local_netmask,\n\t\t\t\t\t\t\t      &localnet_addr[num_localnet_addr].netmask);\n\t\t\t\t\t\tif(error <= 0) {\n\t\t\t\t\t\t\tfprintf(stderr, \"localnet netmask error\\n\");\n\t\t\t\t\t\t\texit(1);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif(local_in_port[0]) {\n\t\t\t\t\t\t\tlocalnet_addr[num_localnet_addr].port =\n\t\t\t\t\t\t\t    (unsigned short) atoi(local_in_port);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlocalnet_addr[num_localnet_addr].port = 0;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t++num_localnet_addr;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfprintf(stderr, \"# of localnet exceed %d.\\n\", MAX_LOCALNET);\n\t\t\t\t\t}\n\t\t\t\t} else if(strstr(buff, \"chain_len\")) {\n\t\t\t\t\tchar *pc;\n\t\t\t\t\tint len;\n\t\t\t\t\tpc = strchr(buff, '=');\n\t\t\t\t\tlen = atoi(++pc);\n\t\t\t\t\tproxychains_max_chain = (unsigned int) (len ? len : 1);\n\t\t\t\t} else if(strstr(buff, \"quiet_mode\")) {\n\t\t\t\t\tproxychains_quiet_mode = 1;\n\t\t\t\t} else if(strstr(buff, \"proxy_dns\")) {\n\t\t\t\t\tproxychains_resolver = 1;\n\t\t\t\t} else if(strstr(buff, \"dnat\")) {\n\t\t\t\t\tif(sscanf(buff, \"%s %21[^ ] %21s\\n\", user, dnat_orig_addr_port, dnat_new_addr_port) < 3) {\n\t\t\t\t\t\tfprintf(stderr, \"dnat format error\");\n\t\t\t\t\t\texit(1);\n\t\t\t\t\t}\n\t\t\t\t\t/* clean previously used buffer */\n\t\t\t\t\tmemset(dnat_orig_port, 0, sizeof(dnat_orig_port) / sizeof(dnat_orig_port[0]));\n\t\t\t\t\tmemset(dnat_new_port, 0, sizeof(dnat_new_port) / sizeof(dnat_new_port[0]));\n\n\t\t\t\t\t(void)sscanf(dnat_orig_addr_port, \"%15[^:]:%5s\", dnat_orig_addr, dnat_orig_port);\n\t\t\t\t\t(void)sscanf(dnat_new_addr_port, \"%15[^:]:%5s\", dnat_new_addr, dnat_new_port);\n\n\t\t\t\t\tif(num_dnats < MAX_DNAT) {\n\t\t\t\t\t\tint error;\n\t\t\t\t\t\terror =\n\t\t\t\t\t\t    inet_pton(AF_INET, dnat_orig_addr,\n\t\t\t\t\t\t\t      &dnats[num_dnats].orig_dst);\n\t\t\t\t\t\tif(error <= 0) {\n\t\t\t\t\t\t\tfprintf(stderr, \"dnat original destination address error\\n\");\n\t\t\t\t\t\t\texit(1);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\terror =\n\t\t\t\t\t\t    inet_pton(AF_INET, dnat_new_addr,\n\t\t\t\t\t\t\t      &dnats[num_dnats].new_dst);\n\t\t\t\t\t\tif(error <= 0) {\n\t\t\t\t\t\t\tfprintf(stderr, \"dnat effective destination address error\\n\");\n\t\t\t\t\t\t\texit(1);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(dnat_orig_port[0]) {\n\t\t\t\t\t\t\tdnats[num_dnats].orig_port =\n\t\t\t\t\t\t\t    (unsigned short) atoi(dnat_orig_port);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdnats[num_dnats].orig_port = 0;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif(dnat_new_port[0]) {\n\t\t\t\t\t\t\tdnats[num_dnats].new_port =\n\t\t\t\t\t\t\t    (unsigned short) atoi(dnat_new_port);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tdnats[num_dnats].new_port = 0;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tPDEBUG(\"added dnat: orig-dst=%s orig-port=%d new-dst=%s new-port=%d\\n\", dnat_orig_addr, dnats[num_dnats].orig_port, dnat_new_addr, dnats[num_dnats].new_port);\n\t\t\t\t\t\t++num_dnats;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfprintf(stderr, \"# of dnat exceed %d.\\n\", MAX_DNAT);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfclose(file);\n\t*proxy_count = count;\n\tproxychains_got_chain_data = 1;\n}\n\nstatic void simple_socks5_env(proxy_data *pd, unsigned int *proxy_count, chain_type *ct) {\n\tchar *port_string;\n        char *host_string;\n\n\tif(proxychains_got_chain_data)\n\t\treturn;\n\n\tload_default_settings(ct);\n\n\tport_string = getenv(PROXYCHAINS_SOCKS5_PORT_ENV_VAR);\n\n\tif(!port_string)\n\t\treturn;\n        \n        host_string = getenv(PROXYCHAINS_SOCKS5_HOST_ENV_VAR);\n        \n        if(!host_string)\n            host_string = \"127.0.0.1\";\n\n        memset(pd, 0, sizeof(proxy_data));\n\n\tpd[0].ps = PLAY_STATE;\n\tpd[0].ip.as_int = (uint32_t) inet_addr(host_string);\n\tpd[0].port = htons((unsigned short) strtol(port_string, NULL, 0));\n\tpd[0].pt = SOCKS5_TYPE;\n\tproxychains_max_chain = 1;\n\n\tif(getenv(PROXYCHAINS_DNS_ENV_VAR))\n\t\tproxychains_resolver = 1;\n\n\t*proxy_count = 1;\n\tproxychains_got_chain_data = 1;\n}\n\n/*******  HOOK FUNCTIONS  *******/\n\nint connect(int sock, const struct sockaddr *addr, socklen_t len) {\n\tint socktype = 0, flags = 0, ret = 0;\n\tsocklen_t optlen = 0;\n\tip_type dest_ip;\n#ifdef DEBUG\n\tchar str[256];\n#endif\n\tstruct in_addr *p_addr_in;\n\tstruct sockaddr_in new_addr;\n\tdnat_arg *dnat = NULL;\n\tunsigned short port;\n\tsize_t i;\n\tint remote_dns_connect = 0;\n\n\tINIT();\n\toptlen = sizeof(socktype);\n\tgetsockopt(sock, SOL_SOCKET, SO_TYPE, &socktype, &optlen);\n\tif(!(SOCKFAMILY(*addr) == AF_INET && socktype == SOCK_STREAM))\n\t\treturn true_connect(sock, addr, len);\n\n\tp_addr_in = &((struct sockaddr_in *) addr)->sin_addr;\n\tport = ntohs(((struct sockaddr_in *) addr)->sin_port);\n\n#ifdef DEBUG\n//      PDEBUG(\"localnet: %s; \", inet_ntop(AF_INET,&in_addr_localnet, str, sizeof(str)));\n//      PDEBUG(\"netmask: %s; \" , inet_ntop(AF_INET, &in_addr_netmask, str, sizeof(str)));\n\tPDEBUG(\"target: %s\\n\", inet_ntop(AF_INET, p_addr_in, str, sizeof(str)));\n\tPDEBUG(\"port: %d\\n\", port);\n#endif\n\n\t// check if connect called from proxydns\n        remote_dns_connect = (ntohl(p_addr_in->s_addr) >> 24 == remote_dns_subnet);\n\n\t// more specific first\n\tfor(i = 0; i < num_dnats && !remote_dns_connect && !dnat; i++)\n\t\tif(dnats[i].orig_dst.s_addr == p_addr_in->s_addr)\n\t\t\tif(dnats[i].orig_port && (dnats[i].orig_port == port))\n\t\t\t\tdnat = &dnats[i];\n\n\tfor(i = 0; i < num_dnats && !remote_dns_connect && !dnat; i++)\n\t\tif(dnats[i].orig_dst.s_addr == p_addr_in->s_addr)\n\t\t\tif(!dnats[i].orig_port)\n\t\t\t\tdnat = &dnats[i];\n\n\tif (dnat) {\n\t\tif (dnat->new_port)\n\t\t\tnew_addr.sin_port = htons(dnat->new_port);\n\t\telse\n\t\t\tnew_addr.sin_port = htons(port);\n\t\tnew_addr.sin_addr = dnat->new_dst;\n\n\t\taddr = (struct sockaddr *)&new_addr;\n\t\tp_addr_in = &((struct sockaddr_in *) addr)->sin_addr;\n\t\tport = ntohs(((struct sockaddr_in *) addr)->sin_port);\n\t}\n\n\tfor(i = 0; i < num_localnet_addr && !remote_dns_connect; i++) {\n\t\tif((localnet_addr[i].in_addr.s_addr & localnet_addr[i].netmask.s_addr)\n\t\t   == (p_addr_in->s_addr & localnet_addr[i].netmask.s_addr)) {\n\t\t\tif(!localnet_addr[i].port || localnet_addr[i].port == port) {\n\t\t\t\tPDEBUG(\"accessing localnet using true_connect\\n\");\n\t\t\t\treturn true_connect(sock, addr, len);\n\t\t\t}\n\t\t}\n\t}\n\n\tflags = fcntl(sock, F_GETFL, 0);\n\tif(flags & O_NONBLOCK)\n\t\tfcntl(sock, F_SETFL, !O_NONBLOCK);\n\n\tdest_ip.as_int = SOCKADDR(*addr);\n\n\tret = connect_proxy_chain(sock,\n\t\t\t\t  dest_ip,\n\t\t\t\t  SOCKPORT(*addr),\n\t\t\t\t  proxychains_pd, proxychains_proxy_count, proxychains_ct, proxychains_max_chain);\n\n\tfcntl(sock, F_SETFL, flags);\n\tif(ret != SUCCESS)\n\t\terrno = ECONNREFUSED;\n\treturn ret;\n}\n\nstatic struct gethostbyname_data ghbndata;\nstruct hostent *gethostbyname(const char *name) {\n\tINIT();\n\n\tPDEBUG(\"gethostbyname: %s\\n\", name);\n\n\tif(proxychains_resolver)\n\t\treturn proxy_gethostbyname(name, &ghbndata);\n\telse\n\t\treturn true_gethostbyname(name);\n\n\treturn NULL;\n}\n\nint getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res) {\n\tint ret = 0;\n\n\tINIT();\n\n\tPDEBUG(\"getaddrinfo: %s %s\\n\", node, service);\n\n\tif(proxychains_resolver)\n\t\tret = proxy_getaddrinfo(node, service, hints, res);\n\telse\n\t\tret = true_getaddrinfo(node, service, hints, res);\n\n\treturn ret;\n}\n\nvoid freeaddrinfo(struct addrinfo *res) {\n\tINIT();\n\n\tPDEBUG(\"freeaddrinfo %p \\n\", res);\n\n\tif(!proxychains_resolver)\n\t\ttrue_freeaddrinfo(res);\n\telse\n\t\tproxy_freeaddrinfo(res);\n\treturn;\n}\n\n// work around a buggy prototype in GLIBC. according to the bugtracker it has been fixed in git at 02 May 2011.\n// 2.14 came out in June 2011 so that should be the first fixed version\n#if defined(__GLIBC__) && (__GLIBC__ < 3) && (__GLIBC_MINOR__ < 14)\nint getnameinfo(const struct sockaddr *sa,\n\t\tsocklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, unsigned int flags)\n#else\nint getnameinfo(const struct sockaddr *sa,\n\t\tsocklen_t salen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags)\n#endif\n{\n\tchar ip_buf[16];\n\tint ret = 0;\n\n\tINIT();\n\n\tPDEBUG(\"getnameinfo: %s %s\\n\", host, serv);\n\n\tif(!proxychains_resolver) {\n\t\tret = true_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);\n\t} else {\n\t\tif(hostlen) {\n\t\t\tinet_ntop(AF_INET, (unsigned char*) &(SOCKADDR_2(*sa)), ip_buf, sizeof(ip_buf));\n\t\t\tstrncpy(host, ip_buf, hostlen);\n\t\t}\n\t\tif(servlen)\n\t\t\tsnprintf(serv, servlen, \"%d\", ntohs(SOCKPORT(*sa)));\n\t}\n\treturn ret;\n}\n\n#if (defined __linux__) || (defined __APPLE__)\nstruct hostent *gethostbyaddr(const void *addr, socklen_t len, int type) {\n#else\nstruct hostent *gethostbyaddr(const char *addr, socklen_t len, int type) {\n#endif\n\tstatic char buf[16];\n\tstatic char ipv4[4];\n\tstatic char *list[2];\n\tstatic struct hostent he;\n\n\tINIT();\n\n\tPDEBUG(\"TODO: proper gethostbyaddr hook\\n\");\n\n\tif(!proxychains_resolver)\n\t\treturn true_gethostbyaddr(addr, len, type);\n\telse {\n\n\t\tPDEBUG(\"len %u\\n\", len);\n\t\tif(len != 4)\n\t\t\treturn NULL;\n\t\the.h_name = buf;\n\t\tmemcpy(ipv4, addr, 4);\n\t\tlist[0] = ipv4;\n\t\tlist[1] = NULL;\n\t\the.h_addr_list = list;\n\t\the.h_addrtype = AF_INET;\n\t\the.h_aliases = NULL;\n\t\the.h_length = 4;\n\t\tinet_ntop(AF_INET, addr, buf, sizeof(buf));\n\t\treturn &he;\n\t}\n\treturn NULL;\n}\n"
  },
  {
    "path": "src/main.c",
    "content": "/*   (C) 2011, 2012 rofl0r\n *                                                                         *\n *   This program is free software; you can redistribute it and/or modify  *\n *   it under the terms of the GNU General Public License as published by  *\n *   the Free Software Foundation; either version 2 of the License, or     *\n *   (at your option) any later version.                                   *\n *                                                                         *\n ***************************************************************************/\n\n#undef _POSIX_C_SOURCE\n#define _POSIX_C_SOURCE 200809L\n#undef _XOPEN_SOURCE\n#define _XOPEN_SOURCE 700\n\n#include <sys/types.h>\n#include <sys/param.h>\n\n#include <errno.h>\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n\n#include <sys/wait.h>\n\n#include \"common.h\"\n\nstatic int usage(char **argv) {\n\tprintf( \"\\nProxychains4: %d.%d.%d\\n\"\n\t\t\t\"Usage:\\t%s -q -f config_file program_name [arguments]\\n\"\n\t\t\t\"\\t-v print version and exit\\n\"\n\t       \t\"\\t-q makes proxychains quiet - this overrides the config setting\\n\"\n\t       \t\"\\t-f allows to manually specify a configfile to use\\n\"\n\t       \t\"\\n\\tfor example : proxychains telnet somehost.com\\n\"\n\t       \t\"\\n\\tMore help in README file\\n\\n\", PROXYCHAINS_VERSION_MAJOR,\n\t       \t\tPROXYCHAINS_VERSION_MINOR, PROXYCHAINS_VERSION_BUGFIX, argv[0]);\n\treturn EXIT_FAILURE;\n}\n\nstatic const char *dll_name = DLL_NAME;\n\nstatic char own_dir[256];\nstatic const char *dll_dirs[] = {\n\t//\".\",\n\town_dir,\n\tLIB_DIR,\n\t\"/lib\",\n\t\"/usr/lib\",\n\t\"/usr/local/lib\",\n\t\"/lib64\",\n\tNULL\n};\n\nstatic void set_own_dir(const char *argv0) {\n\tsize_t l = strlen(argv0);\n\twhile(l && argv0[l - 1] != '/')\n\t\tl--;\n\tif(l == 0)\n\t\t//memcpy(own_dir, \".\", 2);\n\t\tmemcpy(own_dir, \"/dev/null/\", 2);\n\telse {\n\t\tmemcpy(own_dir, argv0, l - 1);\n\t\town_dir[l] = 0;\n\t}\n}\n\n#define MAX_COMMANDLINE_FLAGS 2\n\nint main(int argc, char *argv[]) {\n\tchar *path = NULL;\n\tchar buf[PATH_MAX];\n\tchar pbuf[PATH_MAX];\n\tint start_argv = 1;\n\tint quiet = 0;\n\tsize_t i;\n\n\tconst char *prefix = NULL;\n\n\tfor(i = 0; i < MAX_COMMANDLINE_FLAGS; i++) {\n\t\tif(start_argv < argc && argv[start_argv][0] == '-') {\n\t\t\tif(argv[start_argv][1] == 'q') {\n\t\t\t\tquiet = 1;\n\t\t\t\tstart_argv++;\n\t\t\t} else if(argv[start_argv][1] == 'v') {\n\t\t\t\tprintf(\"Proxychains4 version: %d.%d.%d\\n\", PROXYCHAINS_VERSION_MAJOR, PROXYCHAINS_VERSION_MINOR, PROXYCHAINS_VERSION_BUGFIX);\n\t\t\t\texit(EXIT_SUCCESS);\n\n\t\t\t} else if(argv[start_argv][1] == 'f') {\n\n\t\t\t\tif(start_argv + 1 < argc)\n\t\t\t\t\tpath = argv[start_argv + 1];\n\t\t\t\telse\n\t\t\t\t\treturn usage(argv);\n\n\t\t\t\tstart_argv += 2;\n\t\t\t}\n\t\t} else\n\t\t\tbreak;\n\t}\n\n\tif(start_argv >= argc)\n\t\treturn usage(argv);\n\n\t/* check if path of config file has not been passed via command line */\n\tpath = get_config_path(path, pbuf, sizeof(pbuf));\n\tif(!quiet)\n\t\tfprintf(stderr, LOG_PREFIX \"config file found: %s\\n\", path);\n\n\t/* Set PROXYCHAINS_CONF_FILE to get proxychains lib to use new config file. */\n\tsetenv(PROXYCHAINS_CONF_FILE_ENV_VAR, path, 1);\n\n\tif(quiet)\n\t\tsetenv(PROXYCHAINS_QUIET_MODE_ENV_VAR, \"1\", 1);\n\n\t// search DLL\n\tset_own_dir(argv[0]);\n\n\ti = 0;\n\twhile(dll_dirs[i]) {\n\t\tsnprintf(buf, sizeof(buf), \"%s/%s\", dll_dirs[i], dll_name);\n\t\tif(access(buf, R_OK) != -1) {\n\t\t\tprefix = dll_dirs[i];\n\t\t\tbreak;\n\t\t}\n\t\ti++;\n\t}\n\n\tif(!prefix) {\n\t\tfprintf(stderr, \"couldn't locate %s\\n\", dll_name);\n\t\treturn EXIT_FAILURE;\n\t}\n\n\tif(!quiet)\n\t\tfprintf(stderr, LOG_PREFIX \"preloading %s/%s\\n\", prefix, dll_name);\n\n#ifndef IS_MAC\n\tsnprintf(buf, sizeof(buf), \"%s/%s\", prefix, dll_name);\n\tsetenv(\"LD_PRELOAD\", buf, 1);\n#else\n\tsnprintf(buf, sizeof(buf), \"%s/%s\", prefix, dll_name);\n\tsetenv(\"DYLD_INSERT_LIBRARIES\", buf, 1);\n\tsetenv(\"DYLD_FORCE_FLAT_NAMESPACE\", \"1\", 1);\n#endif\n\texecvp(argv[start_argv], &argv[start_argv]);\n\tperror(\"proxychains can't load process....\");\n\n\treturn EXIT_FAILURE;\n}\n"
  },
  {
    "path": "src/proxychains",
    "content": "#!/bin/sh\necho \"ProxyChains-4.0 (https://github.com/haad/proxychains)\"\n\nusage() {\n\n\techo \"\tusage:\"\n\techo \"\t$0 [hqv] [-f config-file] <prog> [args]\"\n\techo \"\t\t-q make proxychains quiet\"\n\texit\n}\n\nfind_proxy_conf() {\n\n\tshow_proxy_env\n\tif [ -f \"/usr/local/etc/proxychains.conf\" ]; then\n\t\texport PROXYCHAINS_CONF_FILE=\"/usr/local/etc/proxychains.conf\"\n\telse \n\t\tif [ -f \"/etc/proxychains.conf\" ];then\n\t\t\tPROXYCHAINS_CONF_FILE=\"/etc/proxychains.conf\"\n\t\tfi\n\tfi\n}\n\n#\n# XXX We should not overide possible user settings here. Just add libproxychains to it.\n# \nsetup_proxy_env() {\n\n\tif [ -z \"$PROXYCHAINS_CONF_FILE\" ]; then\n\t\tfind_proxy_conf\n\tfi\n\n\tif [ $(uname -s) = \"Darwin\" ]; then\n\t\texport DYLD_FORCE_FLAT_NAMESPACE=\n\t\texport DYLD_INSERT_LIBRARIES=libproxychains4.dylib\n\telse\n\t\texport LD_PRELOAD=libproxychains.so.4\n\tfi\n}\n\ndisable_proxy_env() {\n\n\tif [ $(uname -s) = \"Darwin\" ]; then\n\t\tunset DYLD_FORCE_FLAT_NAMESPACE\n\t\texport DYLD_INSERT_LIBRARIES=$(echo $DYLD_INSERT_LIBRARIES | sed -e 's/libproxychains4.dylib//g')\n\t\tif [ -z $DYLD_INSERT_LIBRARIES ]; then\n\t\t\tunset DYLD_INSERT_LIBRARIES\n\t\tfi\n\telse\n\t\texport LD_PRELOAD=$(echo $DYLD_INSERT_LIBRARIES | sed -e 's/libproxychains.so.4//g')\n\t\tif [ -z $LD_PRELOAD ]; then\n\t\t\tunset LD_PRELOAD\n\t\tfi\n\tfi\n}\n\nshow_proxy_env() {\n\n\techo \"Proxified environment setup\"\n\techo \"PROXYCHAINS_CONF_FILE = $PROXYCHAINS_CONF_FILE\"\n\t\n\tif [ $(uname -s) = \"Darwin\" ]; then\n\t\techo \"DYLD_FORCE_FLAT_NAMESPACE = $DYLD_FORCE_FLAT_NAMESPACE\"\n\t\techo \"DYLD_INSERT_LIBRARIES = $DYLD_INSERT_LIBRARIES\"\n\telse\n\t\techo \"LD_PRELOAD = $LD_PRELOAD\"\n\tfi\n}\n\nif [ $# = 0 ] ; then\n\tusage\nfi\n\ncase \"$1\" in\n\t-h)\n\t\tusage\n\t\t;;\n\t-f)\n\t\texport PROXYCHAINS_CONF_FILE=$2;\n\t\tshift;\n\t\tshift;\n\t\t;;\n\t-q)\n\t\texport PROXYCHAINS_QUIET_MODE=1;\n\t\tshift;\n\t\t;;\n\tshow)\n\t\tshow_proxy_env\n\t\treturn\n\t\t;;\nesac\n\ncase \"$1\" in\n\ton)\n\t\tsetup_proxy_env\n\t\tshow_proxy_env\n\t\t;;\n\toff)\n\t\tdisable_proxy_env\n\t\tshow_proxy_env\n\t\t;;\n\t*)\n\t\tsetup_proxy_env\n\t\texec \"$@\"\n\t\t;;\nesac\n"
  },
  {
    "path": "src/proxychains.conf",
    "content": "# proxychains.conf  VER 4\n#\n#        HTTP, SOCKS4, SOCKS5 tunneling proxifier with DNS.\n#\t\n\n# The option below identifies how the ProxyList is treated.\n# only one option should be uncommented at time,\n# otherwise the last appearing option will be accepted\n#\n#dynamic_chain\n#\n# Dynamic - Each connection will be done via chained proxies\n# all proxies chained in the order as they appear in the list\n# at least one proxy must be online to play in chain\n# (dead proxies are skipped)\n# otherwise EINTR is returned to the app\n#\nstrict_chain\n#\n# Strict - Each connection will be done via chained proxies\n# all proxies chained in the order as they appear in the list\n# all proxies must be online to play in chain\n# otherwise EINTR is returned to the app\n#\n#random_chain\n#\n# Random - Each connection will be done via random proxy\n# (or proxy chain, see  chain_len) from the list.\n# this option is good to test your IDS :)\n\n# Make sense only if random_chain\n#chain_len = 2\n\n# Quiet mode (no output from library)\n#quiet_mode\n\n# Proxy DNS requests - no leak for DNS data\nproxy_dns \n\n# set the class A subnet number to usefor use of the internal remote DNS mapping\n# we use the reserved 224.x.x.x range by default,\n# if the proxified app does a DNS request, we will return an IP from that range.\n# on further accesses to this ip we will send the saved DNS name to the proxy.\n# in case some control-freak app checks the returned ip, and denies to \n# connect, you can use another subnet, e.g. 10.x.x.x or 127.x.x.x.\n# of course you should make sure that the proxified app does not need\n# *real* access to this subnet. \n# i.e. dont use the same subnet then in the localnet section\n#remote_dns_subnet 127 \n#remote_dns_subnet 10\nremote_dns_subnet 224\n\n# Some timeouts in milliseconds\ntcp_read_time_out 15000\ntcp_connect_time_out 8000\n\n# By default enable localnet for loopback address ranges\n# RFC5735 Loopback address range\nlocalnet 127.0.0.0/255.0.0.0\n# RFC1918 Private Address Ranges\n# localnet 10.0.0.0/255.0.0.0\n# localnet 172.16.0.0/255.240.0.0\n# localnet 192.168.0.0/255.255.0.0\n\n\n# Example for localnet exclusion\n## Exclude connections to 192.168.1.0/24 with port 80\n# localnet 192.168.1.0:80/255.255.255.0\n\n## Exclude connections to 192.168.100.0/24\n# localnet 192.168.100.0/255.255.255.0\n\n## Exclude connections to ANYwhere with port 80\n# localnet 0.0.0.0:80/0.0.0.0\n\n\n### Examples for dnat\n## Trying to proxy connections to destinations which are dnatted,\n## will result in proxying connections to the new given destinations.\n## Whenever I connect to 1.1.1.1 on port 1234 actually connect to 1.1.1.2 on port 443\n# dnat 1.1.1.1:1234  1.1.1.2:443\n\n## Whenever I connect to 1.1.1.1 on port 443 actually connect to 1.1.1.2 on port 443\n## (no need to write :443 again)\n# dnat 1.1.1.2:443  1.1.1.2\n\n## No matter what port I connect to on 1.1.1.1 port actually connect to 1.1.1.2 on port 443\n# dnat 1.1.1.1  1.1.1.2:443\n\n## Always, instead of connecting to 1.1.1.1, connect to 1.1.1.2\n# dnat 1.1.1.1  1.1.1.2\n\n\n# ProxyList format\n#       type  host  port [user pass]\n#       (values separated by 'tab' or 'blank')\n#\n#\n#        Examples:\n#\n#            \tsocks5\t192.168.67.78\t1080\tlamer\tsecret\n#\t\thttp\t192.168.89.3\t8080\tjustu\thidden\n#\t \tsocks4\t192.168.1.49\t1080\n#\t        http\t192.168.39.93\t8080\t\n#\t\t\n#\n#       proxy types: http, socks4, socks5, raw\n#        * raw: The traffic is simply forwarded to the proxy without modification.\n#        ( auth types supported: \"basic\"-http  \"user/pass\"-socks )\n#\n[ProxyList]\n# add proxy here ...\n# meanwhile\n# defaults set to \"tor\"\nsocks4 \t127.0.0.1 9050\n\n"
  },
  {
    "path": "src/proxyresolv",
    "content": "#!/bin/sh\n# This script is called by proxychains to resolve DNS names\n\n# DNS server used to resolve names\n\nif [ -z \"$PROXY_DNS_SERVER\" ] ; then\n    DNS_SERVER=208.67.222.222   # OpenDNS\nelse\n    DNS_SERVER=$PROXY_DNS_SERVER\nfi\n\nif [ $# = 0 ] ; then\n\techo \"\tusage:\"\n\techo \"\t\tproxyresolv <hostname> \"\n\texit\nfi\n\nawk 'BEGIN{ARGC=0} {for(i=2;i<=NF;i++){if($i==ARGV[1] && $1!~\":\"){print $1;found=1}} } END{exit found?0:1}' \"$1\" </etc/hosts\nif [ \"$?\" -eq \"0\" ]; then\n        exit\nfi\n\nexport LD_PRELOAD=libproxychains.so\ndrill $1 @$DNS_SERVER -t | awk '/A.+[0-9]+\\.[0-9]+\\.[0-9]/{print $5;}'\n"
  },
  {
    "path": "tests/test_getaddrinfo.c",
    "content": "#include <stdio.h>\n#include <stdlib.h>\n#include <netdb.h>\n#include <netinet/in.h>\n#include <sys/socket.h>\n \n#ifndef   NI_MAXHOST\n#define   NI_MAXHOST 1025\n#endif\n \nint main(void) {\n\tstruct addrinfo *result;\n\tstruct addrinfo *res;\n\tint error;\n\t\n\t/* resolve the domain name into a list of addresses */\n\terror = getaddrinfo(\"www.example.com\", NULL, NULL, &result);\n\tif (error != 0)\n\t{   \n\t\tfprintf(stderr, \"error in getaddrinfo: %s\\n\", gai_strerror(error));\n\t\treturn EXIT_FAILURE;\n\t}   \n\t\n\t/* loop over all returned results and do inverse lookup */\n\tfor (res = result; res != NULL; res = res->ai_next)\n\t{   \n\t\tchar hostname[NI_MAXHOST] = \"\";\n\t\n\t\terror = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0); \n\t\tif (error != 0)\n\t\t{\n\t\tfprintf(stderr, \"error in getnameinfo: %s\\n\", gai_strerror(error));\n\t\tcontinue;\n\t\t}\n\t\tif (*hostname != '\\0')\n\t\tprintf(\"hostname: %s\\n\", hostname);\n\t}   \n\t\n\tfreeaddrinfo(result);\n\treturn EXIT_SUCCESS;\n}\n"
  }
]