-
Notifications
You must be signed in to change notification settings - Fork 5
/
common.zig
188 lines (166 loc) · 6.68 KB
/
common.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const std = @import("std");
const x = @import("x.zig");
const common = @This();
pub const SocketReader = std.io.Reader(std.posix.socket_t, std.posix.RecvFromError, readSocket);
pub fn send(sock: std.posix.socket_t, data: []const u8) !void {
const sent = try x.writeSock(sock, data, 0);
if (sent != data.len) {
std.log.err("send {} only sent {}\n", .{data.len, sent});
return error.DidNotSendAllData;
}
}
pub const ConnectResult = struct {
sock: std.posix.socket_t,
setup: x.ConnectSetup,
pub fn reader(self: ConnectResult) SocketReader {
return .{ .context = self.sock };
}
pub fn send(self: ConnectResult, data: []const u8) !void {
try common.send(self.sock, data);
}
};
pub fn connectSetupMaxAuth(
sock: std.posix.socket_t,
comptime max_auth_len: usize,
auth_name: x.Slice(u16, [*]const u8),
auth_data: x.Slice(u16, [*]const u8),
) !?u16 {
var buf: [x.connect_setup.auth_offset + max_auth_len]u8 = undefined;
const len = x.connect_setup.getLen(auth_name.len, auth_data.len);
if (len > max_auth_len)
return error.AuthTooBig;
return connectSetup(sock, buf[0 .. len], auth_name, auth_data);
}
pub fn connectSetup(
sock: std.posix.socket_t,
msg: []u8,
auth_name: x.Slice(u16, [*]const u8),
auth_data: x.Slice(u16, [*]const u8),
) !?u16 {
std.debug.assert(msg.len == x.connect_setup.getLen(auth_name.len, auth_data.len));
x.connect_setup.serialize(msg.ptr, 11, 0, auth_name, auth_data);
try send(sock, msg);
const reader = SocketReader { .context = sock };
const connect_setup_header = try x.readConnectSetupHeader(reader, .{});
switch (connect_setup_header.status) {
.failed => {
std.log.err("connect setup failed, version={}.{}, reason='{s}'", .{
connect_setup_header.proto_major_ver,
connect_setup_header.proto_minor_ver,
connect_setup_header.readFailReason(reader),
});
return error.ConnectSetupFailed;
},
.authenticate => {
std.log.err("AUTHENTICATE! not implemented", .{});
return error.NotImplemetned;
},
.success => {
// TODO: check version?
std.log.debug("SUCCESS! version {}.{}", .{connect_setup_header.proto_major_ver, connect_setup_header.proto_minor_ver});
return connect_setup_header.getReplyLen();
},
else => |status| {
std.log.err("Error: expected 0, 1 or 2 as first byte of connect setup reply, but got {}", .{status});
return error.MalformedXReply;
}
}
}
fn connectSetupAuth(
display_num: ?u32,
sock: std.posix.socket_t,
auth_filename: []const u8,
) !?u16 {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// TODO: test bad auth
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//if (try connectSetupMaxAuth(sock, 1000, .{ .ptr = "wat", .len = 3}, .{ .ptr = undefined, .len = 0})) |_|
// @panic("todo");
const auth_mapped = try x.MappedFile.init(auth_filename, .{});
defer auth_mapped.unmap();
var auth_filter = x.AuthFilter{
.addr = .{ .family = .wild, .data = &[0]u8{ } },
.display_num = display_num,
};
var addr_buf: [x.max_sock_filter_addr]u8 = undefined;
if (auth_filter.applySocket(sock, &addr_buf)) {
std.log.debug("applied address filter {}", .{auth_filter.addr});
} else |err| {
// not a huge deal, we'll just try all auth methods
std.log.warn("failed to apply socket to auth filter with {s}", .{@errorName(err)});
}
var auth_it = x.AuthIterator{ .mem = auth_mapped.mem };
while (auth_it.next() catch {
std.log.warn("auth file '{s}' is invalid", .{auth_filename});
return null;
}) |entry| {
if (auth_filter.isFiltered(auth_mapped.mem, entry)) |reason| {
std.log.debug("ignoring auth because {s} does not match: {}", .{@tagName(reason), entry.fmt(auth_mapped.mem)});
continue;
}
const name = entry.name(auth_mapped.mem);
const data = entry.data(auth_mapped.mem);
const name_x = x.Slice(u16, [*]const u8){
.ptr = name.ptr,
.len = @intCast(name.len),
};
const data_x = x.Slice(u16, [*]const u8){
.ptr = data.ptr,
.len = @intCast(data.len),
};
std.log.debug("trying auth {}", .{entry.fmt(auth_mapped.mem)});
if (try connectSetupMaxAuth(sock, 1000, name_x, data_x)) |reply_len|
return reply_len;
}
return null;
}
pub fn connect(allocator: std.mem.Allocator) !ConnectResult {
const display = x.getDisplay();
const parsed_display = x.parseDisplay(display) catch |err| {
std.log.err("invalid display '{s}': {s}", .{display, @errorName(err)});
std.process.exit(0xff);
};
const sock = x.connect(display, parsed_display) catch |err| {
std.log.err("failed to connect to display '{s}': {s}", .{display, @errorName(err)});
std.process.exit(0xff);
};
errdefer x.disconnect(sock);
const setup_reply_len: u16 = blk: {
if (try x.getAuthFilename(allocator)) |auth_filename| {
defer auth_filename.deinit(allocator);
if (try connectSetupAuth(parsed_display.display_num, sock, auth_filename.str)) |reply_len|
break :blk reply_len;
}
// Try no authentication
std.log.debug("trying no auth", .{});
var msg_buf: [x.connect_setup.getLen(0, 0)]u8 = undefined;
if (try connectSetup(
sock,
&msg_buf,
.{ .ptr = undefined, .len = 0 },
.{ .ptr = undefined, .len = 0 },
)) |reply_len| {
break :blk reply_len;
}
std.log.err("the X server rejected our connect setup message", .{});
std.process.exit(0xff);
};
const connect_setup = x.ConnectSetup {
.buf = try allocator.allocWithOptions(u8, setup_reply_len, 4, null),
};
std.log.debug("connect setup reply is {} bytes", .{connect_setup.buf.len});
const reader = SocketReader { .context = sock };
try x.readFull(reader, connect_setup.buf);
return ConnectResult{ .sock = sock, .setup = connect_setup };
}
pub fn asReply(comptime T: type, msg_bytes: []align(4) u8) !*T {
const generic_msg: *x.ServerMsg.Generic = @ptrCast(msg_bytes.ptr);
if (generic_msg.kind != .reply) {
std.log.err("expected reply but got {}", .{generic_msg});
return error.UnexpectedReply;
}
return @alignCast(@ptrCast(generic_msg));
}
fn readSocket(sock: std.posix.socket_t, buffer: []u8) !usize {
return x.readSock(sock, buffer, 0);
}