summaryrefslogtreecommitdiff
path: root/src/lib/smtp_parse.zig
diff options
context:
space:
mode:
authorNic Gaffney <gaffney_nic@protonmail.com>2026-10-19 20:25:09 -0500
committerNic Gaffney <gaffney_nic@protonmail.com>2026-10-19 20:25:09 -0500
commit73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690 (patch)
tree5360ab60fa5e67a7ce356253d3eab875c007be98 /src/lib/smtp_parse.zig
parentdbf5e0958a6e13c9e36a0b1a29771a708f407c20 (diff)
downloadnvlope-73e4e4490696a7f680cc0ef5e3b6d2d8ebc9c690.tar.gz
working on HELO/EHLO response
Diffstat (limited to 'src/lib/smtp_parse.zig')
-rw-r--r--src/lib/smtp_parse.zig27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/lib/smtp_parse.zig b/src/lib/smtp_parse.zig
index 886ae44..44822ff 100644
--- a/src/lib/smtp_parse.zig
+++ b/src/lib/smtp_parse.zig
@@ -40,23 +40,22 @@ pub fn parse(msg: []const u8) !Message {
};
}
-fn ehlo(msg: []const u8) !Identity {
- var parser: Parser = .{ .str = msg };
- try parser.consume(' ');
- const ip6 = net.Ip6Address.parse(msg[1 .. msg.len - 2], 0) catch blk: {
- // std.debug.print("IP6: {s}\nERR: {}\n", .{ msg[1 .. msg.len - 2], e });
- break :blk null;
- };
- const ip4 = net.Ip4Address.parse(msg[1 .. msg.len - 2], 0) catch blk: {
- // std.debug.print("IP4: {s}\nERR: {}\n", .{ msg[1 .. msg.len - 2], e });
- break :blk null;
- };
- const ipaddr = if (ip6) |ip|
+// Expects a string in the format of "[ipaddr]" no \r\n
+fn address(msg: []const u8) !?net.IpAddress {
+ const ip6 = net.Ip6Address.parse(msg[1 .. msg.len - 1], 0) catch null;
+ const ip4 = net.Ip4Address.parse(msg[1 .. msg.len - 1], 0) catch null;
+ return if (ip6) |ip|
net.IpAddress{ .ip6 = ip }
else if (ip4) |ip|
net.IpAddress{ .ip4 = ip }
else
null;
+}
+
+fn ehlo(msg: []const u8) !Identity {
+ var parser: Parser = .{ .str = msg };
+ try parser.consume(' ');
+ const ipaddr = try address(msg[1 .. msg.len - 3]);
parser.skipMany(msg[1 .. msg.len - 2].len);
@@ -119,13 +118,13 @@ test "parse_crlf: lone CR inside the line is not a terminator" {
// }
test "ehlo: IPv4 address literal" {
- const id = try ehlo(" 192.0.2.1\r\n");
+ const id = try ehlo(" [192.0.2.1]\r\n");
std.debug.print("{}\n", .{id.addr});
try t.expect(id == Identity.addr);
}
test "ehlo: IPv6 address literal" {
- const id = try ehlo(" 2001:db8::1\r\n");
+ const id = try ehlo(" [IPv6:2001:db8::1]\r\n");
try t.expect(id == .addr);
}