{"id":27296126,"url":"https://github.com/corendos/ztun","last_synced_at":"2025-04-11T23:30:15.738Z","repository":{"id":62662640,"uuid":"502634901","full_name":"Corendos/ztun","owner":"Corendos","description":"An implementation of the STUN Protocol in Zig","archived":false,"fork":false,"pushed_at":"2023-12-13T21:15:56.000Z","size":192,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-12-13T22:39:55.369Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Corendos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-06-12T14:13:48.000Z","updated_at":"2023-12-13T22:39:55.969Z","dependencies_parsed_at":"2023-11-06T12:38:33.854Z","dependency_job_id":"98eb89ab-c701-4fa7-b843-15011993b403","html_url":"https://github.com/Corendos/ztun","commit_stats":null,"previous_names":[],"tags_count":8,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corendos%2Fztun","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corendos%2Fztun/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corendos%2Fztun/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Corendos%2Fztun/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Corendos","download_url":"https://codeload.github.com/Corendos/ztun/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248494636,"owners_count":21113473,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-04-11T23:30:14.994Z","updated_at":"2025-04-11T23:30:15.706Z","avatar_url":"https://github.com/Corendos.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/Corendos/ztun/actions/workflows/main.yaml\" alt=\"Actions\"\u003e\n        \u003cimg src=\"https://github.com/Corendos/ztun/actions/workflows/main.yaml/badge.svg\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n`ztun` is an implementation of Session Traversal Utilities for NAT (STUN) (a.k.a [RFC 8489](https://www.rfc-editor.org/rfc/rfc8489.html)) in Zig.\n\n## What is STUN ?\n\nHere is the abstract of the RFC 8489:\n\n\u003e Session Traversal Utilities for NAT (STUN) is a protocol that serves as a tool for other protocols in dealing with NAT traversal.  It can be used by an endpoint to determine the IP address and port allocated to it by a NAT.  It can also be used to check connectivity between two endpoints and as a keep-alive protocol to maintain NAT bindings. STUN works with many existing NATs and does not require any special behavior from them.\n\u003e\n\u003e STUN is not a NAT traversal solution by itself.  Rather, it is a tool to be used in the context of a NAT traversal solution.\n\nFor instance, STUN is used in the ICE protocol to gather Server Reflexive candidates and perform connectivity checks.\n\n## Features\n\n### Platform agnostic core\n\nThe core part of `ztun` is written without any platform-specific code. Thus, it's ready to use on any OS targeted by Zig.\n\n### Implementation of a STUN server\n\n`ztun` gives you access to a STUN server that is ready to use and supports the following features:\n* Short-Term authentication\n* Long-Term authentication\n* Username Anonymity\n* MD5 and Sha256 password algorithms\n\nData transport management is not handled by the server. This lets you integrate `ztun` in your codebase with ease.\n\n## Usage\n\nThe simplest way you can use `ztun` is by using it's STUN message building capabilities. It exposes an easy to use `MessageBuilder` that can be used as such:\n\n```zig\nconst ztun = @import(\"ztun\");\n\nvar builder = ztun.MessageBuilder.init(allocator);\ndefer builder.deinit(allocator);\n\nbuilder.setClass(ztun.Class.request);\nbuilder.setMethod(ztun.Method.binding);\nbuilder.randomTransactionId();\nbuilder.addFingerprint();\n\nconst message = try builder.build();\ndefer message.deinit(allocator);\n```\n\nThis snippet simply builds a STUN Binding request, using a random transaction ID and adds a fingerprint to the message. The message is allocated using the allocator given to the builder.\n\nYou can also add attributes to the message using the `builder.addAttribute()` method. For convenience, common attributes are defined in `ztun.attr.common` and can easily be converted to/from raw attributes.\n\nFor example, let's say you want to add a `SOFTWARE` attribute to a STUN message, here is how you would do it:\n```zig\nconst ztun = @import(\"ztun\");\n\nvar builder = ztun.MessageBuilder.init(allocator);\ndefer builder.deinit(allocator);\n\nbuilder.setClass(ztun.Class.request);\nbuilder.setMethod(ztun.Method.binding);\nbuilder.randomTransactionId();\n\nconst software_attribute = ztun.attr.common.Software{ .value = \"My software name\" };\nconst raw_software_attribute = software_attribute.toAttribute(allocator);\nerrdefer allocator.free(raw_software_attribute.data);\nbuilder.addAttribute(raw_software_attribute);\n\nbuilder.addFingerprint();\n\nconst message = try builder.build();\ndefer message.deinit(allocator);\n```\n\nTo get a list of all the supported common attributes, take a look at the [`src/ztun/attributes.zig`](src/ztun/attributes.zig) file.\n\nAlternatively, you can also make use of the `Server` struct. It exposes a ready to use STUN Server:\n\n```zig\nconst ztun = @import(\"ztun\");\n\nvar server = ztun.Server.init(allocator, ztun.Server.Options{\n        // The authentication type to use, valid enums are .none, .short_term and .long_term.\n        .authentication_type = .none\n        // (For Long-Term authentication only) The realm to use to authenticate users.\n        .realm = \"default\"\n        // (For Long-Term authentication only) The supported password algorithms\n        .algorithms = \u0026.{\n            .{ .type = ztun.auth.AlgorithmType.md5, .parameters = \u0026.{} },\n            .{ .type = ztun.auth.AlgorithmType.sha256, .parameters = \u0026.{} },\n        },\n    });\ndefer server.deinit();\n\ntry server.registerShortTermUser(\"user\", \"password\");\ntry server.registerLongTermUser(\"user\". \"password\");\n\nconst message_source: std.net.Address = ...; // The source IP that sent the message.\nconst raw_message = ...; // Read a message from the data transport.\nconst reader = std.io.fixedBufferStream(raw_message).reader();\nconst message = try ztun.Message.readAlloc(allocator, reader);\ndefer message.deinit(allocator);\n\nconst result = server.handleMessage(allocator, message, message_source);\nswitch (result) {\n    .ok =\u003e {\n        // Nothing to do.\n    },\n    .response =\u003e |response| {\n        // Send the response back.\n    }\n    .discard =\u003e {\n        // The message was discarded.\n    },\n}\n```\n\nSamples are available in the [`samples`](samples/) subfolder if you want to have a better overview.\n\n## Supported zig version\n`0.12.0-dev.1768+39a966b0a`\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorendos%2Fztun","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcorendos%2Fztun","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcorendos%2Fztun/lists"}