aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.envrc1
-rw-r--r--.gitignore2
-rw-r--r--README.md5
-rw-r--r--npins/default.nix260
-rw-r--r--npins/sources.json12
-rw-r--r--shell.nix10
-rw-r--r--src/main.c56
-rw-r--r--tags71
8 files changed, 417 insertions, 0 deletions
diff --git a/.envrc b/.envrc
new file mode 100644
index 0000000..1d953f4
--- /dev/null
+++ b/.envrc
@@ -0,0 +1 @@
+use nix
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..04e8bb4
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.direnv/
+out/
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..8a85d83
--- /dev/null
+++ b/README.md
@@ -0,0 +1,5 @@
+# Servidor de echo em C
+
+Um servidor de eco usando as bibliotecas padrão do C, pra estudar programação de rede.
+
+Desenvolvido seguindo o livro [Beej's Guide to Network Programming](https://beej.us/guide/bgnet/).
diff --git a/npins/default.nix b/npins/default.nix
new file mode 100644
index 0000000..8ec5eca
--- /dev/null
+++ b/npins/default.nix
@@ -0,0 +1,260 @@
+/*
+ This file is provided under the MIT licence:
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+# Generated by npins. Do not modify; will be overwritten regularly
+let
+ # Backwards-compatibly make something that previously didn't take any arguments take some
+ # The function must return an attrset, and will unfortunately be eagerly evaluated
+ # Same thing, but it catches eval errors on the default argument so that one may still call it with other arguments
+ mkFunctor =
+ fn:
+ let
+ e = builtins.tryEval (fn { });
+ in
+ (if e.success then e.value else { error = fn { }; }) // { __functor = _self: fn; };
+
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
+ range =
+ first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
+
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
+ stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
+
+ # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
+ stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
+ concatStrings = builtins.concatStringsSep "";
+
+ # If the environment variable NPINS_OVERRIDE_${name} is set, then use
+ # the path directly as opposed to the fetched source.
+ # (Taken from Niv for compatibility)
+ mayOverride =
+ name: path:
+ let
+ envVarName = "NPINS_OVERRIDE_${saneName}";
+ saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
+ ersatz = builtins.getEnv envVarName;
+ in
+ if ersatz == "" then
+ path
+ else
+ # this turns the string into an actual Nix path (for both absolute and
+ # relative paths)
+ builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
+ if builtins.substring 0 1 ersatz == "/" then
+ /. + ersatz
+ else
+ /. + builtins.getEnv "PWD" + "/${ersatz}"
+ );
+
+ mkSource =
+ name: spec:
+ {
+ pkgs ? null,
+ }:
+ assert spec ? type;
+ let
+ # Unify across builtin and pkgs fetchers.
+ # `fetchGit` requires a wrapper because of slight API differences.
+ fetchers =
+ if pkgs == null then
+ {
+ inherit (builtins) fetchTarball fetchurl;
+ # Frustratingly, due to flakes and `fetchTree`, `fetchGit`
+ # has a different signature than the other builtin
+ # fetchers
+ fetchGit = args: (builtins.fetchGit args).outPath;
+ }
+ else
+ {
+ fetchTarball =
+ {
+ url,
+ sha256,
+ }:
+ pkgs.fetchzip {
+ inherit url sha256;
+ extension = "tar";
+ };
+ inherit (pkgs) fetchurl;
+ fetchGit =
+ {
+ url,
+ submodules,
+ rev,
+ name,
+ narHash,
+ }:
+ pkgs.fetchgit {
+ inherit url rev name;
+ fetchSubmodules = submodules;
+ hash = narHash;
+ };
+ };
+
+ path =
+ if spec.type == "Git" then
+ mkGitSource fetchers spec
+ else if spec.type == "GitRelease" then
+ mkGitSource fetchers spec
+ else if spec.type == "PyPi" then
+ mkPyPiSource fetchers spec
+ else if spec.type == "Channel" then
+ mkChannelSource fetchers spec
+ else if spec.type == "Url" || spec.type == "MutableUrl" then
+ mkUrlSource fetchers spec
+ else if spec.type == "Container" then
+ mkContainerSource pkgs spec
+ else
+ builtins.throw "Unknown source type ${spec.type}";
+ in
+ spec // { outPath = mayOverride name path; };
+
+ mkGitSource =
+ {
+ fetchTarball,
+ fetchGit,
+ ...
+ }:
+ {
+ repository,
+ revision,
+ url ? null,
+ submodules,
+ hash,
+ ...
+ }:
+ assert repository ? type;
+ # At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
+ # In the latter case, there we will always be an url to the tarball
+ if url != null && !submodules then
+ fetchTarball {
+ inherit url;
+ sha256 = hash;
+ }
+ else
+ let
+ url =
+ if repository.type == "Git" then
+ repository.url
+ else if repository.type == "GitHub" then
+ "https://github.com/${repository.owner}/${repository.repo}.git"
+ else if repository.type == "GitLab" then
+ "${repository.server}/${repository.repo_path}.git"
+ else if repository.type == "Forgejo" then
+ "${repository.server}/${repository.owner}/${repository.repo}.git"
+ else
+ throw "Unrecognized repository type ${repository.type}";
+ urlToName =
+ url: rev:
+ let
+ matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
+
+ short = builtins.substring 0 7 rev;
+
+ appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
+ in
+ "${if matched == null then "source" else builtins.head matched}${appendShort}";
+ name = urlToName url revision;
+ in
+ fetchGit {
+ rev = revision;
+ narHash = hash;
+
+ inherit name submodules url;
+ };
+
+ mkPyPiSource =
+ { fetchurl, ... }:
+ {
+ url,
+ hash,
+ ...
+ }:
+ fetchurl {
+ inherit url;
+ sha256 = hash;
+ };
+
+ mkChannelSource =
+ { fetchTarball, ... }:
+ {
+ url,
+ hash,
+ ...
+ }:
+ fetchTarball {
+ inherit url;
+ sha256 = hash;
+ };
+
+ mkUrlSource =
+ {
+ fetchTarball,
+ fetchurl,
+ ...
+ }:
+ {
+ url,
+ hash,
+ unpack,
+ ...
+ }:
+ (if unpack then fetchTarball else fetchurl) {
+ inherit url;
+ sha256 = hash;
+ };
+
+ mkContainerSource =
+ pkgs:
+ {
+ image_name,
+ image_tag,
+ image_digest,
+ hash,
+ ...
+ }@args:
+ if pkgs == null then
+ builtins.throw "container sources require passing in a Nixpkgs value: https://github.com/andir/npins/blob/master/README.md#using-the-nixpkgs-fetchers"
+ else
+ pkgs.dockerTools.pullImage (
+ {
+ imageName = image_name;
+ imageDigest = image_digest;
+ finalImageTag = image_tag;
+ hash = hash;
+ }
+ // (if args.arch or null != null then { arch = args.arch; } else { })
+ );
+
+in
+mkFunctor (
+ {
+ input ? ./sources.json,
+ }:
+ let
+ data =
+ if builtins.isPath input then
+ # while `readFile` will throw an error anyways if the path doesn't exist,
+ # we still need to check beforehand because *our* error can be caught but not the one from the builtin
+ # See: <https://git.lix.systems/lix-project/lix/issues/1098>
+ if builtins.pathExists input then
+ builtins.fromJSON (builtins.readFile input)
+ else
+ throw "Input path ${toString input} does not exist"
+ else if builtins.isAttrs input then
+ input
+ else
+ throw "Unsupported input type ${builtins.typeOf input}, must be a path or an attrset";
+ version = data.version;
+ in
+ if version == 8 then
+ builtins.mapAttrs (name: spec: mkFunctor (mkSource name spec)) data.pins
+ else
+ throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"
+)
diff --git a/npins/sources.json b/npins/sources.json
new file mode 100644
index 0000000..35c046e
--- /dev/null
+++ b/npins/sources.json
@@ -0,0 +1,12 @@
+{
+ "pins": {
+ "nixpkgs": {
+ "type": "Channel",
+ "name": "nixpkgs-unstable",
+ "artifact": "nixexprs.tar.xz",
+ "url": "https://releases.nixos.org/nixpkgs/nixpkgs-26.11pre1075288.a32edd765451/nixexprs.tar.xz",
+ "hash": "sha256-j08lBbqaYwjsW0xy6Dh4MNZpQ/oSvLU1MLAo38OXphM="
+ }
+ },
+ "version": 8
+}
diff --git a/shell.nix b/shell.nix
new file mode 100644
index 0000000..ee07662
--- /dev/null
+++ b/shell.nix
@@ -0,0 +1,10 @@
+let
+ sources = import ./npins;
+ pkgs = import sources.nixpkgs {};
+in
+ pkgs.mkShell {
+ packages = with pkgs; [
+ universal-ctags
+ pkg-config
+ ];
+ }
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..1c5c6ff
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,56 @@
+#include<stdio.h>
+#include<arpa/inet.h>
+#include<sys/types.h>
+#include<sys/socket.h>
+#include<netdb.h>
+#include<string.h>
+#include<stdlib.h>
+#include<errno.h>
+
+int
+main()
+{
+ int gai_status, b_status;
+ int socketfd;
+ struct addrinfo hints, *res;
+ char ipstr[INET6_ADDRSTRLEN];
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;
+
+ gai_status = getaddrinfo(NULL, "9123", &hints, &res);
+ if (gai_status != 0)
+ {
+ fprintf(stderr, "gai error: %s\n", gai_strerror(gai_status));
+ exit(1);
+ }
+
+ socketfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+ if (socketfd < 0)
+ {
+ fprintf(stderr, "socket error: %d\n", errno);
+ exit(1);
+ }
+
+ b_status = bind(socketfd, res->ai_addr, res->ai_addrlen);
+ if (b_status < 0)
+ {
+ fprintf(stderr, "bind error: %d\n", errno);
+ exit(1);
+ }
+
+ inet_ntop(res->ai_family, &(((struct sockaddr_in *) res->ai_addr)->sin_addr), ipstr, sizeof ipstr);
+ if (ipstr == NULL)
+ {
+ fprintf(stderr, "string error: %d\n", errno);
+ exit(1);
+ }
+
+ printf("Endereço: %s", ipstr);
+
+ freeaddrinfo(res);
+
+ return 0;
+}
diff --git a/tags b/tags
new file mode 100644
index 0000000..a6441d8
--- /dev/null
+++ b/tags
@@ -0,0 +1,71 @@
+!_TAG_EXTRA_DESCRIPTION anonymous /Include tags for non-named objects like lambda/
+!_TAG_EXTRA_DESCRIPTION fileScope /Include tags of file scope/
+!_TAG_EXTRA_DESCRIPTION pseudo /Include pseudo tags/
+!_TAG_EXTRA_DESCRIPTION subparser /Include tags generated by subparsers/
+!_TAG_FIELD_DESCRIPTION epoch /the last modified time of the input file (only for F\/file kind tag)/
+!_TAG_FIELD_DESCRIPTION file /File-restricted scoping/
+!_TAG_FIELD_DESCRIPTION input /input file/
+!_TAG_FIELD_DESCRIPTION name /tag name/
+!_TAG_FIELD_DESCRIPTION pattern /pattern/
+!_TAG_FIELD_DESCRIPTION typeref /Type and name of a variable or typedef/
+!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
+!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
+!_TAG_KIND_DESCRIPTION!C d,macro /macro definitions/
+!_TAG_KIND_DESCRIPTION!C e,enumerator /enumerators (values inside an enumeration)/
+!_TAG_KIND_DESCRIPTION!C f,function /function definitions/
+!_TAG_KIND_DESCRIPTION!C g,enum /enumeration names/
+!_TAG_KIND_DESCRIPTION!C h,header /included header files/
+!_TAG_KIND_DESCRIPTION!C m,member /struct, and union members/
+!_TAG_KIND_DESCRIPTION!C s,struct /structure names/
+!_TAG_KIND_DESCRIPTION!C t,typedef /typedefs/
+!_TAG_KIND_DESCRIPTION!C u,union /union names/
+!_TAG_KIND_DESCRIPTION!C v,variable /variable definitions/
+!_TAG_KIND_DESCRIPTION!JSON a,array /arrays/
+!_TAG_KIND_DESCRIPTION!JSON b,boolean /booleans/
+!_TAG_KIND_DESCRIPTION!JSON n,number /numbers/
+!_TAG_KIND_DESCRIPTION!JSON o,object /objects/
+!_TAG_KIND_DESCRIPTION!JSON s,string /strings/
+!_TAG_KIND_DESCRIPTION!JSON z,null /nulls/
+!_TAG_KIND_DESCRIPTION!Sh a,alias /aliases/
+!_TAG_KIND_DESCRIPTION!Sh f,function /functions/
+!_TAG_KIND_DESCRIPTION!Sh h,heredoc /label for here document/
+!_TAG_KIND_DESCRIPTION!Sh s,script /script files/
+!_TAG_KIND_DESCRIPTION!WindRes a,accelerators /accelerators/
+!_TAG_KIND_DESCRIPTION!WindRes b,bitmap /bitmaps/
+!_TAG_KIND_DESCRIPTION!WindRes c,cursor /cursors/
+!_TAG_KIND_DESCRIPTION!WindRes d,dialog /dialogs/
+!_TAG_KIND_DESCRIPTION!WindRes f,font /fonts/
+!_TAG_KIND_DESCRIPTION!WindRes i,icon /icons/
+!_TAG_KIND_DESCRIPTION!WindRes m,menu /menus/
+!_TAG_KIND_DESCRIPTION!WindRes v,version /versions/
+!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/
+!_TAG_OUTPUT_FILESEP slash /slash or backslash/
+!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
+!_TAG_OUTPUT_VERSION 1.1 /current.age/
+!_TAG_PARSER_VERSION!C 1.1 /current.age/
+!_TAG_PARSER_VERSION!JSON 0.0 /current.age/
+!_TAG_PARSER_VERSION!Sh 0.0 /current.age/
+!_TAG_PARSER_VERSION!WindRes 0.0 /current.age/
+!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/
+!_TAG_PROC_CWD /home/mino/projetos/c-echo/ //
+!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
+!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
+!_TAG_PROGRAM_URL https://ctags.io/ /official site/
+!_TAG_PROGRAM_VERSION 6.2.1 //
+!_TAG_ROLE_DESCRIPTION!C!function foreigndecl /declared in foreign languages/
+!_TAG_ROLE_DESCRIPTION!C!header local /local header/
+!_TAG_ROLE_DESCRIPTION!C!header system /system header/
+!_TAG_ROLE_DESCRIPTION!C!macro undef /undefined/
+!_TAG_ROLE_DESCRIPTION!C!struct foreigndecl /declared in foreign languages/
+!_TAG_ROLE_DESCRIPTION!Sh!heredoc endmarker /end marker/
+!_TAG_ROLE_DESCRIPTION!Sh!script loaded /loaded/
+MAX_BYTES src/main.c /^#define MAX_BYTES$/;" d file:
+artifact npins/sources.json /^ "artifact": "nixexprs.tar.xz",$/;" s object:pins.nixpkgs
+hash npins/sources.json /^ "hash": "sha256-j08lBbqaYwjsW0xy6Dh4MNZpQ\/oSvLU1MLAo38OXphM="$/;" s object:pins.nixpkgs
+main src/main.c /^main()$/;" f typeref:typename:int
+name npins/sources.json /^ "name": "nixpkgs-unstable",$/;" s object:pins.nixpkgs
+nixpkgs npins/sources.json /^ "nixpkgs": {$/;" o object:pins
+pins npins/sources.json /^ "pins": {$/;" o
+type npins/sources.json /^ "type": "Channel",$/;" s object:pins.nixpkgs
+url npins/sources.json /^ "url": "https:\/\/releases.nixos.org\/nixpkgs\/nixpkgs-26.11pre1075288.a32edd765451\/nixex/;" s object:pins.nixpkgs
+version npins/sources.json /^ "version": 8$/;" n