README.md

# Yggdrasil - simple Erlang client library for Erlang applications #

__Version:__ 1.0.4


# Listeners #
A listener is a set of processes whose role is to listen on a port for new connections. It manages a pool of acceptor processes, each of them indefinitely accepting connections. When it does, it starts a new process executing the protocol handler code. All the socket programming is abstracted through the use of transport handlers.

The listener takes care of supervising all the acceptor and connection processes, allowing developers to focus on building their application.

# STARTING A LISTENER #

A listener can be started by calling the yggdrasil_app:yggdrasil_connect/0 function. Before doing so however, you must ensure that the json file  is  given a proper path [Here to test you can use test.json in src directory].(It takes the Yggdrasil tunnel to connect to the server).

```
yggdrasil_app:yggdrasil_connect().
listening on port 2000
{{2020,8,8},{16,41,18}} Yggdrasil Server Started.
ok

```

You can then connect to it using telnet and see the echo server reply everything you send to it. Then when you're done testing, you can use the "exit" escape to the telnet command line to exit.

# Connecting to the example listener with telnet #

```

$ telnet 203:18f0:c3bb:cd12:66d2:bb08:ce8d:9fde 2000
Trying 203:18f0:c3bb:cd12:66d2:bb08:ce8d:9fde...
Connected to 203:18f0:c3bb:cd12:66d2:bb08:ce8d:9fde.
Escape character is '^]'.
Welcome! This is a Erlang Yggdrasil server developed under Barrel Db Labs...

```

# Installation #

Download the sources from our GitLab repository

To run the application simply run 'rebar3 compile'.

compile, is required to fetch dependencies and compile all applications.


# Basic usage #
The basic usage of yggdrasil is:

# Start Yggdrasil #

To start in the console run:

```erlang

$ rebar3 shell

```

# Simple request #
Do a simple request that will return a client state

Once a valid request is received, the response stanza in JSON is returned.

# getPeers #

Returns one or more records containing information about active peer sessions. The first record typically refers to the current node.


For each IPv6 address:

1>box_pub_key (string) contains the EncryptionPublicKey of the remote node
2>bytes_sent (uint64) contains the number of bytes sent to that peer
3>bytes_recvd (uint64) contains the number of bytes received from that peer
4>endpoint (string) contains the connected IPv4/IPv6 address and port of the peering
5>port (uint8) contains the local switch port number for that peer
6>uptime (float64) contains the number of seconds since the peer connection was established

```

1> yggdrasil_app:peers().  
#{<<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"bytes_recvd">> => 1246171,<<"bytes_sent">> => 453311,
        <<"endpoint">> =>
            <<"tcp://[2001:1af8:4700:a119:7::1]:35239">>,
        <<"port">> => 2,<<"proto">> => <<"tcp">>,
        <<"uptime">> => 5874.782067068},
  <<"203:18f0:c3bb:cd12:66d2:bb08:ce8d:9fde">> =>
      #{<<"box_pub_key">> =>
            <<"64735dcf73c1afa1fde9a8b238077b2dcc474adf42cfa55b5ccfa90ddb1ce011">>,
        <<"bytes_recvd">> => 5092019,<<"bytes_sent">> =>


```

# getSwitchPeers #

Returns zero or more records containing information about switch peers.

```
#{<<"1">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"bytes_recvd">> => 966536,<<"bytes_sent">> => 736652,
        <<"coords">> => <<"[1 79 1]">>,
        <<"endpoint">> => <<"85.17.15.221">>,
        <<"ip">> => <<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">>,
        <<"port">> => 1,<<"proto">> => <<"tcp">>},
  <<"2">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"bytes_recvd">> => 1449809,<<"bytes_sent">> => 502526,
        <<"coords">> => <<"[1 79 1]">>,
        <<"endpoint">> => <<"2001:1af8:4700:a119:7::1">>,
        <<"ip">> => <<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">>,
        <<"port">> => 2,<<"proto">> => <<"tcp">>},
  <<"3">> =>
      #{<<"box_pub_key">> =>
            <<"15bfab4e09218e033bd7feb290e9d4991dbe99f064e606f1718c27568dbb5d71">>,
        <<"bytes_recvd">> => 1016,<<"bytes_sent">> => 0,
        <<"coords">> => <<"[1 16 1]">>,
        <<"endpoint">> => <<"212.129.52.193">>,
        <<"ip">> => <<"204:4738:37c6:d295:1b34:2722:62dd:e8de">>,
        <<"port">> => 3,<<"proto">> => <<"tls">>}}


```

   
# gettunnelrouting #               

```
20> yggdrasil_app:tunnelrouting().
false

```

# gettuntap      #                   

Returns exactly one record containing information about the current node’s TUN/TAP adapter.

```
21> yggdrasil_app:tuntap().       
#{<<"mtu">> => 65535}

``` 

# getallowedencryptionpublickeys  #     

Returns zero or more strings containing the allowed box public keys.

```
22> yggdrasil_app:publickeys().
[]

```

If zero strings are returned then it is implied that all connections are permitted.



# getdht #                         

Returns known nodes in the DHT.

box_pub_key (string) contains the EncryptionPublicKey of the remote node
coords (string) contains the coordinates of the node on the spanning tree
last_seen (uint32) contains the number of seconds since the DHT record was last updated

```
23> yggdrasil_app:dht().       
#{<<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"coords">> => <<"[1 79 1]">>,
        <<"last_seen">> => 16.468516697},
  <<"202:ff64:8c81:e959:9a90:979d:1859:8b0c">> =>
      #{<<"box_pub_key">> =>
            <<"8016d5909c8c5d3da913b29ed2334210296dfa4c0fa09af55df182d6b9171e17">>,
        <<"coords">> => <<"[1 79 1 83]">>,
        <<"last_seen">> => 16.422435899},
  <<"203:1613:6687:55b5:b884:b0f0:4eec:4252">> => ........

  ```



 
# getself #                        

Returns exactly one record containing information about the current Yggdrasil node.

```
24> yggdrasil_app:self().
#{<<"203:18f0:c3bb:cd12:66d2:bb08:ce8d:9fde">> =>
      #{<<"box_pub_key">> =>
            <<"64735dcf73c1afa1fde9a8b238077b2dcc474adf42cfa55b5ccfa90ddb1ce011">>,
        <<"build_name">> => <<"yggdrasil">>,
        <<"build_version">> => <<"0.3.14">>,
        <<"coords">> => <<"[1 79 1 144]">>,
        <<"subnet">> => <<"303:18f0:c3bb:cd12::/64">>}}

```


# getsourcesubnets #              

```
29> yggdrasil_app:subnets().            
null

```
                      
                
# getmulticastinterfaces  #
Returns zero or more strings containing the enabled multicast peering interfaces.

If zero strings are returned then it is implied that multicast peering is not allowed on any interface.

```

28> yggdrasil_app:multicastinterfaces().
[<<"br-cb669f0878fc">>,<<"br-dd3c49ad303f">>,
 <<"br-fea73654c37e">>,<<"eno1">>,<<"wlo1">>,<<"virbr0">>,
 <<"docker0">>]

```
                                 
# getsessions #                      

Returns zero or more records containing information about open sessions between the current Yggdrasil node and other nodes. Open sessions indicate that traffic has been exchanged with the remote node recently.

```
27> yggdrasil_app:getsessions().
#{}

```

# getswitchpeers #                       

Returns zero or more records containing information about switch peers.

```
25> yggdrasil_app:switchpeers().
#{<<"1">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"bytes_recvd">> => 1337512,<<"bytes_sent">> => 966612,
        <<"coords">> => <<"[1 79 1]">>,
        <<"endpoint">> => <<"85.17.15.221">>,
        <<"ip">> => <<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">>,
        <<"port">> => 1,<<"proto">> => <<"tcp">>},
  <<"2">> =>
      #{<<"box_pub_key">> =>
            <<"120915e044fb981b4681448072079592bf92dadd2c1256d35f1e1d4e480d2e2e">>,
        <<"bytes_recvd">> => 127422,<<"bytes_sent">> => 83703,
        <<"coords">> => <<"[1 79 1]">>,
        <<"endpoint">> => <<"2001:1af8:4700:a119:7::1">>,
        <<"ip">> => <<"201:6a49:40cc:c40e:db1c:33d0:bb14:93ec">>,
        <<"port">> => 2,<<"proto">> => <<"tcp">>},
  <<"3">> =>
      #{<<"box_pub_key">> =>
            <<"15bfab4e09218e033bd7feb290e9d4991dbe99f064e606f1718c27568dbb5d71">>,
        <<"bytes_recvd">> => 9907,<<"bytes_sent">> => 30064,
        <<"coords">> => <<"[1 16 1]">>,
        <<"endpoint">> => <<"212.129.52.193">>,
        <<"ip">> => <<"204:4738:37c6:d295:1b34:2722:62dd:e8de">>,
        <<"port">> => 3,<<"proto">> => <<"tls">>}}

```        


                           
# getRoutes #


Returns zero or more records where the subnet (string) is mapped to the public key (string).

```
19> yggdrasil_app:getroutes().
#{}

```

# TODO #

*Create a confiig file to use all the default values like ports and file path.
*Currently the code is not in the proper state it just work.So cleanup
*Write a better README to understand the functionalities.




I was trying to find repository where UNIX socket in hackney have been used.I only found this using UNIX socket where looking at the code I understood that I can never write them. So I started to look at the output of the below repository and found out that even I can make it by doing the tricks differently.(which ofcouse is a vey naive way but it works 100%)

https://github.com/emmanuelJet/aedoc/blob/f322d88eb41fbade4794d24700555d53cc188913/aeternity/system_test/common/helpers/aest_docker_api.erl