Rocking the blogosphere
Apple Online Store

Building tsocks on Mac OS X

I recently told you about a cool piece of software called tsocks which allows non-SOCKS-aware applications to use SOCKS by using LD_PRELOAD to intercept their socket calls and route them through a SOCKS server.

Installing on Debian was simple as the software was written for Linux and there is a Debian package available for it. Installing on FreeBSD is also simple as it turns out that someone did a FreeBSD port for it.

It took some patching to get tsocks (version 1.8 beta 5) to compile on Mac OS X. Here’s how I did it:

wget 'http://ftp1.sourceforge.net/tsocks/tsocks-1.8beta5.tar.gz' && \
tar -xzf tsocks-1.8beta5.tar.gz && cd tsocks-1.8 && \
wget 'http://marc.abramowitz.info/download/tsocks-1.8_macosx.patch' && \
patch < tsocks-1.8_macosx.patch && autoconf && \
./configure && make && sudo make install

Download tsocks-1.8_macosx.patch

Doing this patch was an education for me about Autoconf as well as how Mac OS X dynamic linking works.

The patch address issues such as:

  • Using gcc-4.0 ended up causing link errors for me so I force use of gcc-3.3.
  • Linking dynamic libraries requires special linker options, as does linking object files with multiply defined symbols.
  • Libraries typically have a .dylib extension rather than .so (because OS X uses the proprietary Mach-O object file format instead of ELF).
  • LD_PRELOAD is not supported. Instead I had to use OS X variables - DYLD_INSERT_LIBRARIES and DYLD_FORCE_FLAT_NAMESPACES.
  • The code itself needed a few minor patches in order to compile; the use of typedefs like socklen_t instead of non-portable types like int and the use of the OS X gcc extension __attribute__ ((constructor)) to defined a dynamic library initialization function.
  • I needed to do a bunch of autoconf stuff to support all of this and to make it so that the tsocks shell script gets generated to accomodate all of these differences.

Of course after doing all of this, I found out that there is a tsocks DarwinPort, which builds but doesn’t seem to work because it still tries to use LD_PRELOAD.

Del.icio.us Digg Reddit Technorati

Possibly related posts

Comments

  1. Paul Locker
    February 13th, 2006 | 3:22 pm

    Does this tweaked version for osx still require the tsocks.conf file in /etc?

  2. Marc
    February 13th, 2006 | 5:37 pm

    Yep, it still needs the tsocks.conf file.

    What’s reallya bummer about this is that I’ve been able to get it to work for command-line apps like ssh, but I can’t get it to work with Aqua apps like Firefox, Safari, Thunderbird, etc.

    It seems that these apps crash immediately on startup when DYLD\_FORCE\_FLAT\_NAMESPACES is set.

    Anybody have any ideas of how to get around this?

  3. Mike
    February 26th, 2006 | 2:06 pm

    When i compile on 1.4.5 i get an error at the end of ./configure. i tried to installed wih darwin ports but i cannot access the internet page it requires to download. thanks

  4. Mark
    March 28th, 2006 | 11:11 pm

    It would be most excellent if you would contribute your hard work and tsocks expertise to the DP project’s tsocks port. I think you’d find it would maximize the effects of your hard work. If you are willing to adapt your instructions for the DP port please post what patches would be required to make the tsocks DP port work correctly. There is already a bug filed for the port and it sounds like you know of additional flaws.

    http://bugzilla.opendarwin.org/show_bug.cgi?id=6080

    You could probably even be the maintainer for the port if you wish. If I can help in anyway to assist you in contributing please let me know; I have no specific knowedge of tsocks but I contribute to the project so I could help.

  5. Mark
    March 28th, 2006 | 11:19 pm
  6. Mark
    May 29th, 2006 | 11:03 am

    The DarwinPorts tsocks port is now fixed.

  7. July 29th, 2006 | 10:20 am

    I tried installing the darwin ports version and I keep getting an error: 12:50:53 libtsocks(23015): Error 56 attempting to connect to SOCKS server (Socket is already connected)

    And when I tried to do the patch mentioned in your post on an Intel Mac, it got angry at me. I ran configure and it blew up at me.

    I have successfully installed it on a PPC, just haven’t gotten it to use the SOCKS proxy yet. :/

  8. argelian
    November 13th, 2006 | 9:28 pm

    When i run ./configure i get the following error:

    checking for gcc… gcc-3.3
    checking for C compiler default output file name… configure: error: C compiler cannot create executables

    Does anyone have a clue how I can fix this?

  9. argelian
    November 13th, 2006 | 10:41 pm

    Also, when I successfully installed it from darwinports i got this when running tsocks:

    dyld: could not load inserted library: /opt/local/lib/libtsocks.dylib

    Trace/BPT trap

    I just want to play some World of Warcraft… =(

  10. Scott
    February 9th, 2007 | 1:30 pm

    I installed the 1.8 beta 5.1 version from Darwin Ports. I was able to run Aqua apps like safari from a terminal window with no changes. This is on 10.3, from a zsh shell (although that latter shouldn’t matter).

    Some apps required using ‘open’ (namely Entourage).

    Couple of examples:

    % tsocks /Applications/Safari.app/Contents/MacOS/Safari &

    % tsocks open “/Applications/Microsoft Office 2004/Microsoft Entourage”

  11. Scott
    February 9th, 2007 | 6:08 pm

    Skip that. I was on crack. Hadn’t disconnected the vpn so it was using that connection.

  12. Alfred Perlstein
    March 16th, 2007 | 8:06 pm

    Hey man, thanks for doing this, it works, except for some reason I had to hack it back to using stock gcc and manually set the FLATNAMESPACE environment flag.

    (I’m using 10.4.8)

  13. Ivan
    March 27th, 2007 | 7:14 am

    Intel Mac, the same problem. Cannot create executables

  14. Ivan
    March 27th, 2007 | 7:45 am

    It seems like gcc-3.3 is for PPC only in the XCode distribution.
    /usr/libexec/gcc/darwin/i386 in Midnight Commander :

    ..
    *as
    !default ( -> 4.0 )

    and that’s all! No any cc1, cc1plus, gnat1 etc.

  15. June 1st, 2007 | 10:47 pm

    but I can’t get it to work with Aqua apps like Firefox, Safari, Thunderbird, etc.

    For Apple’s apps, it seems that they’re using its own proxy setting. Check out System Preferences — Network — choose your net access, click Configure — click on Proxies Tab; there’s an option titled “SOCKS Proxy”. There you go.

    Blimey, that’s VERY deeply buried indeed.

  16. plopix
    March 19th, 2008 | 4:33 pm

    has anyone a working tsocks for leopard?

  17. April 25th, 2008 | 2:35 pm

    Most of MacOSX application is static linked, or something like that…

    So, Don’t expect that tsocks’ll work with MacOSX default applications (safari, etc)

  18. September 24th, 2008 | 11:01 am

    Got that working on Leopard. Thanks for posting this.

  19. Laco
    September 27th, 2008 | 8:53 pm

    Can you please post more details how did you make it work? I wanted to compile but cannot install gcc42 from fink.

    Thanks a lot for help.

Leave a reply

Apple Online Store
Apple Online Store