From 9b189b7f4ff0982e878247f1239ede5cb9d581d8 Mon Sep 17 00:00:00 2001 From: Alain Zscheile Date: Tue, 22 Nov 2022 16:41:59 +0100 Subject: [PATCH] initial commit --- .formatter.exs | 4 ++ .gitignore | 35 +++++++++++++++ README.md | 21 +++++++++ asn1/FloofProtocol.asn1 | 97 ++++++++++++++++++++++++++++++++++++++++ lib/floof.ex | 29 ++++++++++++ lib/floof/application.ex | 24 ++++++++++ mix.exs | 31 +++++++++++++ mix.lock | 4 ++ shell.nix | 11 +++++ test/floof_test.exs | 8 ++++ test/test_helper.exs | 1 + 11 files changed, 265 insertions(+) create mode 100644 .formatter.exs create mode 100644 .gitignore create mode 100644 README.md create mode 100644 asn1/FloofProtocol.asn1 create mode 100644 lib/floof.ex create mode 100644 lib/floof/application.ex create mode 100644 mix.exs create mode 100644 mix.lock create mode 100644 shell.nix create mode 100644 test/floof_test.exs create mode 100644 test/test_helper.exs diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f7ec8b --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Files generated from /asn1/* +/src/FloofProtocol.* + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +floof-*.tar + +# Temporary files, for example, from tests. +/tmp/ + +.#* +result +result-* +flamegraph.svg +perf.data* diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcc905a --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Floof + +**TODO: Add description** + +## Installation + +If [available in Hex](https://hex.pm/docs/publish), the package can be installed +by adding `floof` to your list of dependencies in `mix.exs`: + +```elixir +def deps do + [ + {:floof, "~> 0.1.0"} + ] +end +``` + +Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) +and published on [HexDocs](https://hexdocs.pm). Once published, the docs can +be found at . + diff --git a/asn1/FloofProtocol.asn1 b/asn1/FloofProtocol.asn1 new file mode 100644 index 0000000..510f112 --- /dev/null +++ b/asn1/FloofProtocol.asn1 @@ -0,0 +1,97 @@ +FloofProtocol { 2 25 187636631533261907981725980838781846034 } +DEFINITIONS EXPLICIT TAGS ::= BEGIN + +ActId ::= OCTET STRING (SIZE(16..1024)) +ActIdBunch ::= SEQUENCE(SIZE(1..1024)) OF ActId +Summary ::= SEQUENCE { + ids ActIdBunch +} +Request ::= SEQUENCE { + ids ActIdBunch +} +Signature ::= SEQUENCE { + algorithm OBJECT IDENTIFIER, + value OCTET STRING +} +Severity ::= ENUMERATED { + emergency (0), + alert (1), + critical (2), + error (3), + warning (4), + notice (5), + info (6), + debug (7), + trace (8) +} + +RelativeDistinguishedName ::= SEQUENCE { + type OBJECT IDENTIFIER, + value ANY +} + +RDNSequence ::= SEQUENCE OF RelativeDistinguishedName + +ModificationOperation ::= CHOICE { + set [0] OCTET STRING, + delete [1] NULL, + add [2]OCTET STRING, + append [3] OCTET STRING, + patch [4] OCTET STRING +} + +Modification ::= SEQUENCE { + container RDNSequence, + op ModificationOperation +} + +Content ::= CHOICE { + freeForm OCTET STRING, + modification Modification +} + +XferInner ::= SEQUENCE { + id ActId, + + -- domain name -- + domain PrintableString (SIZE(1..253)), + + -- source system name -- + source RDNSequence, + + -- message topic -- + topic RDNSequence, + + -- message attributes -- + attrs SET OF RelativeDistinguishedName, + + -- message severity -- + severity Severity, + + data Content +} + +XferBlob ::= SEQUENCE { + -- message priority (when severity is equal) + -- used for queue priority augmention and can be modified in-transit + priority INTEGER (0..65536), + + -- message time-to-live eol timestamp in seconds since epoch + ttl INTEGER (0..MAX), + + -- message markers (e.g. review data and such) + markers SEQUENCE OF RelativeDistinguishedName, + + -- signature of DER serialization of *data* + signature Signature, + + data XferInner +} + +ProtoMessage ::= CHOICE { + summary [0] Summary, + request [1] Request, + xfer [2] XferBlob +} + +END diff --git a/lib/floof.ex b/lib/floof.ex new file mode 100644 index 0000000..9243f74 --- /dev/null +++ b/lib/floof.ex @@ -0,0 +1,29 @@ +defmodule Floof do + @moduledoc """ + Documentation for `Floof`. + """ + + require Logger + + def accept(port) do + {:ok, socket} = :gen_tcp.listen(port, [ + :binary, + packet: 4, + active: false, + reuseaddr: true + ]) + Logger.info("Accepting connections on #{port}") + loop_acceptor(socket) + end + + def loop_acceptor(socket) do + {:ok, client} = :gen_tcp.accept(socket) + {:ok, pid} = Task.Supervisor.start_child( + Floof.TaskSupervisor, fn -> serve(client) end) + :ok = :gen_tcp.controlling_process(client, pid) + loop_acceptor(socket) + end + + def serve(_) do + end +end diff --git a/lib/floof/application.ex b/lib/floof/application.ex new file mode 100644 index 0000000..bc72c8c --- /dev/null +++ b/lib/floof/application.ex @@ -0,0 +1,24 @@ +defmodule Floof.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + port = String.to_integer(System.get_env("FLOOF_PORT") || "2540") + + children = [ + # Starts a worker by calling: Floof.Worker.start_link(arg) + # {Floof.Worker, arg} + {Task.Supervisor, name: Floof.TaskSupervisor}, + {Task, fn -> Floof.accept(port) end} + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: Floof.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..313311b --- /dev/null +++ b/mix.exs @@ -0,0 +1,31 @@ +defmodule Floof.MixProject do + use Mix.Project + + def project do + [ + app: :floof, + version: "0.1.0", + elixir: "~> 1.13", + start_permanent: Mix.env() == :prod, + compilers: [:asn1] ++ Mix.compilers(), + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger, :asn1], + mod: {Floof.Application, []} + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + {:asn1ex, git: "https://github.com/vicentfg/asn1ex"}, + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..db6a19b --- /dev/null +++ b/mix.lock @@ -0,0 +1,4 @@ +%{ + "asn1ex": {:git, "https://github.com/vicentfg/asn1ex", "0255348e2fffbdfd1eef7b46f71dc733318a36a0", []}, + "ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"}, +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..5ceff12 --- /dev/null +++ b/shell.nix @@ -0,0 +1,11 @@ +{ pkgs ? import {} }: + pkgs.mkShell { + # nativeBuildInputs is usually what you want -- tools you need to run + nativeBuildInputs = + let erlpkgs = pkgs.buildPackages.beam.packages.erlangR24; + in [ + erlpkgs.elixir + erlpkgs.hex + erlpkgs.rebar3 + ]; + } diff --git a/test/floof_test.exs b/test/floof_test.exs new file mode 100644 index 0000000..01c2e96 --- /dev/null +++ b/test/floof_test.exs @@ -0,0 +1,8 @@ +defmodule FloofTest do + use ExUnit.Case + doctest Floof + + test "greets the world" do + assert Floof.hello() == :world + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start()