Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions .github/workflows/codeql.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .lic.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
author: Mikhail Knyazhev <markus621@yandex.ru>
author: Mikhail Knyazhev <markus621@yandex.com>
lic_short: "BSD 3-Clause"
lic_file: LICENSE
21 changes: 21 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Agent instructions

## Repository

- Go module: `go.osspkg.com/network`; supported Go version: 1.26.
- Public packages: `cert`, `client`, `epoll`, `fd`, `listen`, `proto/udpframe`, `server`, and `xnet`.
- Runnable programs are in `examples/client`, `examples/server`, and `examples/epoll-server`.
- `epoll` uses Linux epoll for TCP and has a non-Linux stub. Keep Linux-specific syscalls behind its existing build constraints.

## Commands

Run commands from the repository root.

- Focused tests: `go test ./<package>`; full Go test suite: `go test ./...`.
- `make tests` runs `goppy test`.
- `make lint` runs `goppy lint`. It runs `go mod tidy`, `go generate`, `goimports`, `gofmt`, golangci-lint, and govulncheck; expect it to modify formatting, generated files, and module metadata.
- `make build` runs `goppy build --arch=amd64`.
- `make pre-commit` runs license, lint, tests, and build checks.
- CI runs `make ci`, which installs and sets up `goppy` before running pre-commit checks.

The CI workflow tests with Go 1.26 on Ubuntu. The `run_example_server_epoll` Make target is Linux-specific.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2024-2026, Mikhail Knyazhev <markus621@yandex.ru>
Copyright (c) 2024-2026, Mikhail Knyazhev <markus621@yandex.com>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
9 changes: 2 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ tests:
goppy test

.PHONY: pre-commit
pre-commit: install license lint tests build
pre-commit: license lint tests build

.PHONY: ci
ci: pre-commit
ci: install pre-commit

run_example_client_tcp:
time ADDRESS="127.0.0.1:8888" NETWORK="tcp" go run -race examples/client/main.go
Expand All @@ -43,10 +43,5 @@ run_example_client_unix:
run_example_server_unix:
ADDRESS="/tmp/unix.sock" NETWORK="unix" go run -race examples/server/main.go

run_example_client_quic:
time ADDRESS="127.0.0.1:8888" NETWORK="quic" go run -race examples/client/main.go
run_example_server_quic:
ADDRESS="127.0.0.1:8888" NETWORK="quic" go run -race examples/server/main.go

run_example_server_epoll:
go run -race examples/epoll-server/main.go
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
# go-network
# go-network

`go.osspkg.com/network` is a Go networking module with reusable clients and servers for TCP, UDP, and Unix domain sockets. It includes connection pooling, optional TLS, UDP message framing, and helpers for working with network addresses and file descriptors.

## Requirements

- Go 1.26 or later

## Installation

```sh
go get go.osspkg.com/network/client
```

Import the packages you need, for example:

```go
import (
"go.osspkg.com/network/client"
"go.osspkg.com/network/server"
)
```

## Packages

| Package | Description |
| --- | --- |
| [`client`](client/README.md) | Concurrent request handling over a bounded pool of reusable connections. Supports TCP, UDP, and Unix sockets. |
| [`server`](server/README.md) | Handler-based TCP, UDP, and Unix servers with configurable connection limits and idle timeouts. |
| [`epoll`](epoll/README.md) | Linux epoll-driven TCP server with ordered event callbacks and queued writes. |
| [`cert`](cert/README.md) | TLS configuration, certificate loading, and CA/server/client certificate generation. |
| [`listen`](listen/README.md) | TCP, UDP, and Unix listener creation, with optional TCP TLS wrapping. |
| [`proto/udpframe`](proto/udpframe/README.md) | UDP message framing, fragmentation, parsing, and reassembly. |
| [`xnet`](xnet/README.md) | Address normalization, IP helpers, and supported network constants. |
| [`fd`](fd/README.md) | File descriptor access for supported connections and `os.File` values. |

## Examples

Runnable examples are available in [`examples/client`](examples/client), [`examples/server`](examples/server), and [`examples/epoll-server`](examples/epoll-server). The regular server example implements a length-prefixed echo protocol over TCP, Unix sockets, or framed UDP. The epoll example is a Linux-only TCP byte-stream echo server.

For example, start a TCP echo server:

```sh
go run ./examples/server
```

Then run the client in another terminal:

```sh
go run ./examples/client -address 127.0.0.1:8080 -network tcp
```

On Linux, start the epoll example separately with `go run ./examples/epoll-server -address 127.0.0.1:8082`. It echoes raw TCP bytes and is not compatible with the length-prefixed client example.

See each example's `-h` output for available flags.

## Development

Run package tests with:

```sh
go test ./...
```

The repository also provides `make tests`, `make lint`, `make build`, and `make pre-commit` targets.

## License

This project is licensed under the BSD 3-Clause License. See [LICENSE](LICENSE).
135 changes: 0 additions & 135 deletions address/common.go

This file was deleted.

93 changes: 93 additions & 0 deletions cert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# cert

Package `cert` loads X.509 certificates and builds TLS configurations for the
network clients and servers. It also provides helpers for creating a private
certificate authority (CA) and issuing server and client certificates.

## Loading certificates

`Certificate` describes certificate files:

```go
certFiles := cert.Certificate{
CAFiles: []string{"certs/root-ca.pem"},
CertFile: "certs/server.pem",
KeyFile: "certs/server-key.pem",
}
```

`ParseCertificate` appends certificates from `CAFiles` to the supplied
`*x509.CertPool` and loads the certificate and private key as a TLS key pair.
Both `CertFile` and `KeyFile` are required to return a key pair. File paths
must point to PEM-encoded data accepted by Go's TLS implementation.

`DefaultTLSConfig` creates a TLS configuration with TLS 1.3 as the minimum
version. By default its root pool is empty. If the `NETWORK_NO_SYSTEM_CERT`
environment variable parses as `true`, it uses the system certificate pool
when available.

## Client and server configuration

`ClientConfig.Parse` and `ServerConfig.Parse` return a `*tls.Config` and an
error. Both accept lists of `Certificate` values and append their CA files to
the resulting `tls.Config.RootCAs` pool. In `ClientConfig`, this pool is used
to verify server certificates. Adding a CA through `ServerConfig.Certs` does
not configure `tls.Config.ClientCAs` or enable client-certificate
verification. A client can set `InsecureSkipVerify` to disable normal server
certificate verification; this should be used only when the application
supplies an alternative verification mechanism.

`ServerConfig` also accepts `NextProtos` for application protocol negotiation.
Its `Autogen` field generates a self-signed server certificate for the listed
DNS names and IP addresses when non-empty. This certificate is separate from
the key pairs loaded from `Certs` and is not signed by a configured CA.

Example:

```go
tlsConfig, err := (&cert.ServerConfig{
Certs: []cert.Certificate{{
CertFile: "certs/server.pem",
KeyFile: "certs/server-key.pem",
}},
NextProtos: []string{"h2", "http/1.1"},
}).Parse()
if err != nil {
return err
}
```

## Generating a CA and certificates

`GenerateRootCertificate` creates a self-signed CA certificate and private
key. `GenerateServerCertificate` and `GenerateClientCertificate` issue leaf
certificates signed by that CA. The returned `*tls.Certificate` for the CA
must be kept with its private key to issue leaf certificates.

```go
root, err := cert.GenerateRootCertificate("Example CA", 1, 365*24*time.Hour)
if err != nil {
return err
}

server, err := cert.GenerateServerCertificate(root, 2, []string{"api.example.test"})
if err != nil {
return err
}

client, err := cert.GenerateClientCertificate(root, 3)
if err != nil {
return err
}
```

The root common name must not be blank, its serial number must be non-zero, and
its validity must be positive. Leaf serial numbers must also be non-zero and
unique among certificates issued by the same CA. The server generator accepts
DNS names and IP addresses for its Subject Alternative Name; the client
generator does not take addresses. Generated leaf certificates are valid for
up to 30 days, bounded by the CA's validity period. Keep generated private
keys secret.

The package also exposes `ErrInvalidCertificate` when a certificate
configuration does not provide a certificate/key pair.
Loading
Loading