GithubHelp home page GithubHelp logo

isabella232 / inet64_tcp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yandex/inet64_tcp

0.0 0.0 0.0 123 KB

Magic thing to make old Erlang stuff work in IPv6-only networks

License: Other

Erlang 98.97% Makefile 1.03%

inet64_tcp's Introduction

Magic IPv4/IPv6 TCP driver for Erlang

Usage

  • Interactively: inet64_tcp:start()
  • Permanently: add inet64_tcp to your application's run-time dependencies

Demo

Assume you have some IPv4-only and IPv6-only hosts running some old Erlang code which is unaware of inet6 address family.

We will simulate this with couple of entries in /etc/hosts:

# Testing v4-only and v6-only loopback
127.0.0.1   local4
::1         local6

Let's start some dummy server and try to connect to it:

1> Acceptor = fun Acceptor(LSock) -> {ok, S} = gen_tcp:accept(LSock), io:format("Accepted ~p~n", [inet:peername(S)]), gen_tcp:close(S), Acceptor(LSock) end.
#Fun<erl_eval.30.90072148>
2> spawn(fun() -> {ok, L} = gen_tcp:listen(7890, []), Acceptor(L) end).
<0.35.0>
4> gen_tcp:connect("local4", 7890, []).
{ok,#Port<0.719>}
Accepted {ok,{{127,0,0,1},56356}}
6> gen_tcp:connect("local6", 7890, []).
{error,nxdomain}
7> gen_tcp:connect("local6", 7890, [inet6]).
{error,econnrefused}

As you see, using IPv6 requires options on both server and client side.

Let's start a new VM, load inet64_tcp driver and try all the stuff above:

1> inet64_tcp:start().
ok
2> Acceptor = fun Acceptor(LSock) -> {ok, S} = gen_tcp:accept(LSock), io:format("Accepted ~p~n", [inet:peername(S)]), gen_tcp:close(S), Acceptor(LSock) end.
#Fun<erl_eval.30.90072148>
3> spawn(fun() -> {ok, L} = gen_tcp:listen(7890, []), Acceptor(L) end).
<0.40.0>
4> gen_tcp:connect("local4", 7890, []).
{ok,#Port<0.750>}
Accepted {ok,{{0,0,0,0,0,65535,32512,1},56491}}
5> gen_tcp:connect("local6", 7890, []).
{ok,#Port<0.752>}
Accepted {ok,{{0,0,0,0,0,0,0,1},56492}}

As you see, the code started to work with IPv6-only addresses without any modifications.

If you stop the inet64_tcp app, things break again:

25> inet64_tcp:stop().

=INFO REPORT==== 25-Jun-2015::15:40:32 ===
    application: inet64_tcp
    exited: stopped
    type: temporary
ok
26> gen_tcp:connect("local6", 7890, []).
{error,nxdomain}

inet64_tcp's People

Contributors

kevinreddot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.