| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use fmt; |
| 5 |
use net; |
| 6 |
use net::ip; |
| 7 |
use net::uri; |
| 8 |
|
| 9 |
// Dials a remote address, establishing a connection and returning the resulting |
| 10 |
// [[net::socket]]. The proto parameter should be the transport protocol (e.g. |
| 11 |
// "tcp"), the address parameter should be the remote address, and the service |
| 12 |
// should be the name of the service, or the default port to use. |
| 13 |
// |
| 14 |
// The interpretation of the address and service parameters is dependent on the |
| 15 |
// protocol in use. For IP-based protocols (such as TCP or UDP), the address |
| 16 |
// parameter may be either an IPv4 or IPv6 address, or a name, and may include a |
| 17 |
// port separated by a colon (':'). If an IPv6 address and a port are both |
| 18 |
// desired, use brackets ('[' and ']') to separate the address from the port |
| 19 |
// (e.g. "[::1]:80"). If the port is not specified, it is inferred from the |
| 20 |
// service parameter. If a name is used instead of an IP address, a DNS lookup |
| 21 |
// is performed, consulting the local /etc/hosts file or equivalent, if |
| 22 |
// possible. |
| 23 |
// |
| 24 |
// The service parameter can be a service name (e.g. "submission") or a default |
| 25 |
// port to use, if one is not specified by address. If a service name is used, |
| 26 |
// an internal list of services is consulted (see [[registersvc]]), and if not |
| 27 |
// known to Hare, the system service list (e.g. /etc/services) will be |
| 28 |
// consulted. If the connection port cannot be established, [[errors::invalid]] |
| 29 |
// is returned. The special service name "unknown" will always consult the |
| 30 |
// address parameter for a desired port, and will return [[errors::invalid]] if |
| 31 |
// one is not provided there. |
| 32 |
// |
| 33 |
// If the address parameter includes a name, but not a port, an SRV lookup will |
| 34 |
// be performed alongside the A or AAAA record lookup for that name. If the name |
| 35 |
// server provides an SRV record for the given service, it will be utilized in |
| 36 |
// lieu of the service database. |
| 37 |
export fn dial(proto: str, address: str, service: str) (net::socket | error) = { |
| 38 |
for (let i = 0z; i < len(default_protocols); i += 1) { |
| 39 |
const p = default_protocols[i]; |
| 40 |
if (p.name == proto) { |
| 41 |
return p.dial(address, service)?; |
| 42 |
}; |
| 43 |
}; |
| 44 |
for (let i = 0z; i < len(protocols); i += 1) { |
| 45 |
const p = protocols[i]; |
| 46 |
if (p.name == proto) { |
| 47 |
return p.dial(address, service)?; |
| 48 |
}; |
| 49 |
}; |
| 50 |
return net::unknownproto: net::error; |
| 51 |
}; |
| 52 |
|
| 53 |
def HOST_MAX: size = 255; |
| 54 |
|
| 55 |
// Performs a [[dial]] operation for a given URI, taking the service name from |
| 56 |
// the URI scheme and forming an address from the URI host and port. |
| 57 |
export fn dial_uri(proto: str, uri: *uri::uri) (net::socket | error) = { |
| 58 |
// XXX: Should the code to convert a URI to e.g. "[::1]:80" be |
| 59 |
// generalized for end-user use? |
| 60 |
if (uri.host is str && len(uri.host as str) > HOST_MAX) { |
| 61 |
return invalid_address; |
| 62 |
}; |
| 63 |
static let addr: [HOST_MAX + len("[]:65535")]u8 = [0...]; |
| 64 |
|
| 65 |
const colon = if (uri.port != 0) ":" else ""; |
| 66 |
const port: fmt::formattable = if (uri.port != 0) uri.port else ""; |
| 67 |
|
| 68 |
let addr = match (uri.host) { |
| 69 |
case let host: str => |
| 70 |
yield fmt::bsprintf(addr, "{}{}{}", host, colon, port)!; |
| 71 |
case let ip: ip::addr4 => |
| 72 |
const host = ip::string(ip); |
| 73 |
yield fmt::bsprintf(addr, "{}{}{}", host, colon, port)!; |
| 74 |
case let ip: ip::addr6 => |
| 75 |
const host = ip::string(ip); |
| 76 |
yield fmt::bsprintf(addr, "[{}]{}{}", host, colon, port)!; |
| 77 |
}; |
| 78 |
|
| 79 |
return dial(proto, addr, uri.scheme); |
| 80 |
}; |