|
@@ -3,26 +3,111 @@ using Riddle;
|
|
namespace RiddleDaemon {
|
|
namespace RiddleDaemon {
|
|
|
|
|
|
private static RiddleDaemonDBusService dbus_service;
|
|
private static RiddleDaemonDBusService dbus_service;
|
|
-
|
|
|
|
|
|
+ private static MainLoop loop;
|
|
|
|
+ private static int return_code = 0;
|
|
|
|
+
|
|
public static int main(string[] args) {
|
|
public static int main(string[] args) {
|
|
-
|
|
|
|
- if(args.length != 4) {
|
|
|
|
- printerr("Please specify address, port, and store location only\n");
|
|
|
|
|
|
+
|
|
|
|
+ int family = Posix.AF_INET;
|
|
|
|
+ string? iface = null;
|
|
|
|
+ int? port = 352;
|
|
|
|
+ string store_location = "/var/cache/riddled";
|
|
|
|
+
|
|
|
|
+ for(var i = 1; i < args.length; i++) {
|
|
|
|
+ if(args[i] == "-6") {
|
|
|
|
+ family = Posix.AF_INET6;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(args[i] == "-p") {
|
|
|
|
+ i++;
|
|
|
|
+ if(args.length <= i) {
|
|
|
|
+ printerr(@"Please specify port with -p\n");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ if(!int.try_parse (args[i], out port)) {
|
|
|
|
+ printerr(@"Invalid port number '$(args[i])'\n");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(args[i] == "-s") {
|
|
|
|
+ i++;
|
|
|
|
+ if(args.length <= i) {
|
|
|
|
+ printerr(@"Please specify store location with -s\n");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ store_location = args[i];
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(iface == null) {
|
|
|
|
+ iface = args[i];
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ printerr(@"Unknown argument '$(args[i])'\n");
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ var addr = get_iface_address(family, iface);
|
|
|
|
+ if(addr == null) {
|
|
|
|
+ if(family == Posix.AF_INET) {
|
|
|
|
+ printerr(@"Could not find an IPv4 address on interface '$(iface)'\n");
|
|
|
|
+ return -4;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ printerr(@"Could not find an IPv6 address on interface '$(iface)'\n");
|
|
|
|
+ return -4;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- var store = new FilesystemNameInfoStore(args[3]);
|
|
|
|
- var server = new Server(args[1], (uint16)int.parse(args[2]), store);
|
|
|
|
- dbus_service = new RiddleDaemonDBusService (args[1], (uint16)int.parse(args[2]), server.client);
|
|
|
|
|
|
+ var store = new FilesystemNameInfoStore(store_location);
|
|
|
|
+ var server = new Server(addr, (uint16)port, store);
|
|
|
|
+ dbus_service = new RiddleDaemonDBusService (addr, (uint16)port, server.client);
|
|
server.riddle_received.connect(riddle_received);
|
|
server.riddle_received.connect(riddle_received);
|
|
|
|
|
|
|
|
|
|
- var loop = new MainLoop();
|
|
|
|
- Bus.own_name (BusType.SESSION, "nz.astrologue.RiddleService", BusNameOwnerFlags.NONE, on_bus_aquired, () => {}, () => stderr.printf ("Could not aquire name\n"));
|
|
|
|
|
|
+ loop = new MainLoop();
|
|
|
|
+ Bus.own_name (BusType.SYSTEM, "nz.astrologue.RiddleService", BusNameOwnerFlags.NONE, on_bus_aquired, () => {}, lost_bus);
|
|
server.start.begin();
|
|
server.start.begin();
|
|
loop.run();
|
|
loop.run();
|
|
|
|
|
|
- return 0;
|
|
|
|
|
|
+ return return_code;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static string? get_iface_address(int family, string iface) {
|
|
|
|
+ Linux.Network.IfAddrs? address;
|
|
|
|
+ Linux.Network.getifaddrs(out address);
|
|
|
|
+
|
|
|
|
+ unowned Linux.Network.IfAddrs? next = address;
|
|
|
|
+ while(next != null) {
|
|
|
|
+ unowned var addr = next.ifa_addr;
|
|
|
|
+ if(addr != null) {
|
|
|
|
+ string? ip = null;
|
|
|
|
+ if(addr.sa_family == Posix.AF_INET) {
|
|
|
|
+ var sa = (Posix.SockAddrIn *)addr;
|
|
|
|
+ ip = Posix.inet_ntoa(sa.sin_addr);
|
|
|
|
+ }
|
|
|
|
+ if(addr.sa_family == Posix.AF_INET6) {
|
|
|
|
+ var sa = (Posix.SockAddrIn6 *)addr;
|
|
|
|
+ var saddr = new uint8[Posix.INET6_ADDRSTRLEN];
|
|
|
|
+ ip = Posix.inet_ntop(Posix.AF_INET6, &sa.sin6_addr, saddr);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if(ip != null && addr.sa_family == family && next.ifa_name == iface) {
|
|
|
|
+ printerr(@"Using IP: $(ip) on $(next.ifa_name)\n");
|
|
|
|
+ return ip;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ next = next.ifa_next;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static void lost_bus() {
|
|
|
|
+ stderr.printf ("Could not acquire name on system bus\n");
|
|
|
|
+ return_code = -2;
|
|
|
|
+ loop.quit();
|
|
}
|
|
}
|
|
|
|
|
|
private static void on_bus_aquired (DBusConnection conn) {
|
|
private static void on_bus_aquired (DBusConnection conn) {
|