Skip to content

Commit

Permalink
Protocol: handle org.freedesktop.DBus.Peer methods. (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Jan 18, 2024
1 parent a59736c commit 2c9eb4c
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 14 deletions.
33 changes: 31 additions & 2 deletions src/Tmds.DBus.Protocol/DBusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Tmds.DBus.Protocol;

class DBusConnection : IDisposable
class DBusConnection : IDisposable, IMethodHandler
{
private delegate void MessageReceivedHandler(Exception? exception, Message message, object? state);

Expand Down Expand Up @@ -316,7 +316,12 @@ private async void HandleMessages(Exception? exception, Message message)

if (isMethodCall)
{
if (message.PathIsSet)
if (message.InterfaceIsSet &&
message.Interface.SequenceEqual("org.freedesktop.DBus.Peer"u8))
{
methodHandler = this;
}
else if (message.PathIsSet)
{
_pathHandlers.TryGetValue(message.PathAsString!, out methodHandler);
}
Expand Down Expand Up @@ -1255,4 +1260,28 @@ MessageBuffer CreateErrorMessage(Message methodCall, string errorName, string er
return writer.CreateMessage();
}
}

string IMethodHandler.Path => throw new NotSupportedException();

ValueTask IMethodHandler.HandleMethodAsync(MethodContext context)
{
if (context.Request.Interface.SequenceEqual("org.freedesktop.DBus.Peer"u8))
{
if (context.Request.Member.SequenceEqual("Ping"u8))
{
using var writer = context.CreateReplyWriter(null);
context.Reply(writer.CreateMessage());
}
else if (context.Request.Member.SequenceEqual("GetMachineId"u8))
{
using var writer = context.CreateReplyWriter("s");
writer.WriteString(DBusEnvironment.MachineId);
context.Reply(writer.CreateMessage());
}
}
return default;
}

bool IMethodHandler.RunMethodHandlerSynchronously(Message message)
=> true;
}
23 changes: 15 additions & 8 deletions src/Tmds.DBus.Protocol/DBusEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ static class DBusEnvironment
}
}

private static string? _machineId;

public static string MachineId
{
get
{
const string MachineUuidPath = @"/var/lib/dbus/machine-id";

if (File.Exists(MachineUuidPath))
if (_machineId == null)
{
return Guid.Parse(File.ReadAllText(MachineUuidPath).Substring(0, 32)).ToString();
}
else
{
return Guid.Empty.ToString();
const string MachineUuidPath = @"/var/lib/dbus/machine-id";

if (File.Exists(MachineUuidPath))
{
_machineId = Guid.Parse(File.ReadAllText(MachineUuidPath).Substring(0, 32)).ToString("N");
}
else
{
_machineId = Guid.Empty.ToString("N");
}
}

return _machineId;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/IMethodHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal MethodContext(Connection connection, Message request)

public bool NoReplyExpected => (Request.MessageFlags & MessageFlags.NoReplyExpected) != 0;

public MessageWriter CreateReplyWriter(string signature)
public MessageWriter CreateReplyWriter(string? signature)
{
var writer = Connection.GetMessageWriter();
writer.WriteMethodReturnHeader(
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Tmds.DBus.Protocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>10</LangVersion>
<LangVersion>11</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Tmds.DBus/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public static string MachineId
}
if (File.Exists(MachineUuidPath))
{
_machineId = Guid.Parse(File.ReadAllText(MachineUuidPath).Substring(0, 32)).ToString();
_machineId = Guid.Parse(File.ReadAllText(MachineUuidPath).Substring(0, 32)).ToString("N");
}
else
{
_machineId = Guid.Empty.ToString();
_machineId = Guid.Empty.ToString("N");
}
return _machineId;
}
Expand Down
40 changes: 40 additions & 0 deletions test/Tmds.DBus.Protocol.Tests/ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,46 @@ public async Task UnreachableAddressAsync(string address)
}
}

[Fact]
public async Task ConnectionRespondsToDBusPeerPing()
{
var connections = PairedConnection.CreatePair();
using var conn1 = connections.Item1;
using var conn2 = connections.Item2;

await conn1.CallMethodAsync(CreateMessage());

MessageBuffer CreateMessage()
{
using var writer = conn1.GetMessageWriter();
writer.WriteMethodCallHeader(
@interface: "org.freedesktop.DBus.Peer",
member: "Ping");
return writer.CreateMessage();
}
}

[Fact]
public async Task ConnectionRespondsToDBusPeerGetMachineId()
{
var connections = PairedConnection.CreatePair();
using var conn1 = connections.Item1;
using var conn2 = connections.Item2;

string machineId = await conn1.CallMethodAsync(CreateMessage(), (Message m, object? s) => m.GetBodyReader().ReadString(), null);
Assert.Equal(32, machineId.Length);
Assert.True(Guid.TryParse(machineId, out _));

MessageBuffer CreateMessage()
{
using var writer = conn1.GetMessageWriter();
writer.WriteMethodCallHeader(
@interface: "org.freedesktop.DBus.Peer",
member: "GetMachineId");
return writer.CreateMessage();
}
}

static class HelloWorldConstants
{
public const string Path = "/path";
Expand Down

0 comments on commit 2c9eb4c

Please sign in to comment.