]> git.meshlink.io Git - catta/commitdiff
* avahi-daemon: Implement EntryGroup::AddRecord for arbitrary record additions
authorTrent Lloyd <lathiat@bur.st>
Thu, 27 Oct 2005 17:49:41 +0000 (17:49 +0000)
committerTrent Lloyd <lathiat@bur.st>
Thu, 27 Oct 2005 17:49:41 +0000 (17:49 +0000)
 * avahi-client: Wrap AddRecord, add simple test to client-test

git-svn-id: file:///home/lennart/svn/public/avahi/trunk@890 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-client/client-test.c
avahi-client/entrygroup.c
avahi-client/publish.h
avahi-common/dbus.c
avahi-common/dbus.h
avahi-common/error.c
avahi-common/error.h
avahi-daemon/EntryGroup.introspect
avahi-daemon/dbus-protocol.c
docs/DBUS-API

index 775eb5d24adb18b6a056dd839d49fcdc472ebe56..802584a2bf905210dcaee22687828bdb2e02dce9 100644 (file)
@@ -258,6 +258,7 @@ int main (AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
     printf("Sucessfully created entry group %p\n", (void*) group);
 
     avahi_entry_group_add_service (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "Lathiat's Site", "_http._tcp", NULL, NULL, 80, "foo=bar", NULL);
+    printf ("add_record: %d", avahi_entry_group_add_record (group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "TestX", 0x01, 0x10, 120, "\5booya", 6));
 
     avahi_entry_group_commit (group);
 
index cd845343c2ccd4cce4ede7b2ef0adafe4d5c386b..4143f2bfa827b3d01498dc6075d13ecce59f156a 100644 (file)
@@ -330,6 +330,21 @@ fail:
     return r;
 }
 
+static int append_rdata(DBusMessage *message, uint8_t *rdata, size_t size) {
+    DBusMessageIter iter, sub;
+    assert(message);
+    dbus_message_iter_init_append(message, &iter);
+    if (!(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &sub)) ||
+        !(dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &rdata, size)) ||
+        !(dbus_message_iter_close_container(&iter, &sub)))
+        return -1;
+    
+    return 0;
+}
+
 static int append_string_list(DBusMessage *message, AvahiStringList *txt) {
     DBusMessageIter iter, sub;
     int r = -1;
@@ -775,3 +790,88 @@ fail:
 
     return r;
 }
+
+/** Add an arbitrary record */
+int avahi_entry_group_add_record(
+    AvahiEntryGroup *group,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    AvahiPublishFlags flags,
+    const char *name,
+    uint16_t clazz,
+    uint16_t type,
+    uint32_t ttl,
+    uint8_t *rdata,
+    size_t size) {
+
+    DBusMessage *message = NULL, *reply = NULL;
+    int r = AVAHI_OK;
+    DBusError error;
+    AvahiClient *client;
+    int32_t i_interface, i_protocol;
+    uint32_t u_flags;
+
+    assert(name);
+
+    client = group->client;
+
+    if (!group->path || group->client->state == AVAHI_CLIENT_DISCONNECTED)
+        return avahi_client_set_errno(group->client, AVAHI_ERR_BAD_STATE);
+
+    dbus_error_init(&error);
+    
+    if (!(message = dbus_message_new_method_call (AVAHI_DBUS_NAME, group->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord"))) {
+        r = avahi_client_set_errno(client, AVAHI_ERR_NO_MEMORY);
+        goto fail;
+    }
+
+    i_interface = (int32_t) interface;
+    i_protocol = (int32_t) protocol;
+    u_flags = (uint32_t) flags;
+        
+    if (!dbus_message_append_args(
+            message,
+            DBUS_TYPE_INT32, &i_interface,
+            DBUS_TYPE_INT32, &i_protocol,
+            DBUS_TYPE_UINT32, &u_flags,
+            DBUS_TYPE_STRING, &name,
+            DBUS_TYPE_UINT16, &clazz,
+            DBUS_TYPE_UINT16, &type,
+            DBUS_TYPE_UINT32, &ttl,
+            DBUS_TYPE_INVALID) || append_rdata(message, rdata, size) < 0) {
+        r = avahi_client_set_errno(group->client, AVAHI_ERR_NO_MEMORY);
+        goto fail;
+    }
+    
+    if (!(reply = dbus_connection_send_with_reply_and_block(client->bus, message, -1, &error)) ||
+        dbus_error_is_set (&error)) {
+        r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
+        goto fail;
+    }
+    
+    if (!dbus_message_get_args(reply, &error, DBUS_TYPE_INVALID) ||
+        dbus_error_is_set (&error)) {
+        r = avahi_client_set_errno(client, AVAHI_ERR_DBUS_ERROR);
+        goto fail;
+    }
+
+    dbus_message_unref(message);
+    dbus_message_unref(reply);
+
+    return AVAHI_OK;
+
+fail:
+    
+    if (dbus_error_is_set(&error)) {
+        r = avahi_client_set_dbus_error(client, &error);
+        dbus_error_free(&error);
+    }
+
+    if (message)
+        dbus_message_unref(message);
+
+    if (reply)
+        dbus_message_unref(reply);
+
+    return r;
+}
index 7873b075bcb6583a542468c22e7098f085831c88..1207a211d14bd0f5872f1b519163f285d88219f0 100644 (file)
@@ -136,6 +136,19 @@ int avahi_entry_group_add_address(
     const char *name,
     const AvahiAddress *a);
 
+/** Add an arbitrary record */
+int avahi_entry_group_add_record(
+    AvahiEntryGroup *group,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    AvahiPublishFlags flags,
+    const char *name,
+    uint16_t clazz,
+    uint16_t type,
+    uint32_t ttl,
+    uint8_t *rdata,
+    size_t size);
+
 AVAHI_C_DECL_END
 
 #endif
index 058b8190950dc4ec09cde5390dcebf2e8b3bf88e..d8a6f480581bf69db518a326587f5bb7f9ccff90 100644 (file)
@@ -77,6 +77,7 @@ static const char * const table[- AVAHI_ERR_MAX] = {
     AVAHI_DBUS_ERR_DNS_NXRRSET,
     AVAHI_DBUS_ERR_DNS_NOTAUTH,
     AVAHI_DBUS_ERR_DNS_NOTZONE,
+    AVAHI_DBUS_ERR_INVALID_RDATA
 };
 
 int avahi_error_dbus_to_number(const char *s) {
index 56f9d914c31d3ecae9dce8ea3b8a9507ce2f261b..16582d0912babe27ba5d7df219f6dd23726da1dc 100644 (file)
@@ -86,6 +86,7 @@ AVAHI_C_DECL_BEGIN
 #define AVAHI_DBUS_ERR_DNS_NXRRSET "org.freedesktop.Avahi.DNSNXRRSET"
 #define AVAHI_DBUS_ERR_DNS_NOTAUTH "org.freedesktop.Avahi.DNSNOTAUTH"
 #define AVAHI_DBUS_ERR_DNS_NOTZONE "org.freedesktop.Avahi.DNSNOTZONE"
+#define AVAHI_DBUS_ERR_INVALID_RDATA "org.freedesktop.Avahi.InvalidRDATA"
 
 /** Convert a DBus error string into an Avahi error number */
 int avahi_error_dbus_to_number(const char *s);
index af104227cc9861f235e0ffc4fddf727f8f1a3cea..f2216b4a9541867a290250bc77bf8b0c0bd19607 100644 (file)
@@ -77,7 +77,9 @@ const char *avahi_strerror(int error) {
         "DNS failure: YXRRSET",
         "DNS failure: NXRRSET",
         "DNS failure: NOTAUTH",
-        "DNS failure: NOTZONE"
+        "DNS failure: NOTZONE",
+
+        "Invalid RDATA"
     };
 
     if (-error < 0 || -error >= -AVAHI_ERR_MAX)
index a7c97a76416edec0b508c56bed3d507834370b1c..f1a019d8efeab16fe95f42ccdfed18d8e848814f 100644 (file)
@@ -80,6 +80,8 @@ enum {
     AVAHI_ERR_DNS_NXRRSET = -43,
     AVAHI_ERR_DNS_NOTAUTH = -44,
     AVAHI_ERR_DNS_NOTZONE = -45,
+
+    AVAHI_ERR_INVALID_RDATA = -46,
     
     /****
      ****    IF YOU ADD A NEW ERROR CODE HERE, PLEASE DON'T FORGET TO ADD
@@ -89,7 +91,7 @@ enum {
      ****    Also remember to update the MAX value below.
      ****/
     
-    AVAHI_ERR_MAX = -46
+    AVAHI_ERR_MAX = -47
 };
 
 /** Return a human readable error string for the specified error code */
index 53fca44e230f2a863a2da58149a359b6004956b2..96232eac90a7cbb9c0152ce4b6c91e821c55f675 100644 (file)
       <arg name="address" type="s" direction="in"/>
     </method>
 
+    <method name="AddRecord">
+      <arg name="interface" type="i" direction="in"/>
+      <arg name="protocol" type="i" direction="in"/>
+      <arg name="flags" type="u" direction="in"/>
+      <arg name="name" type="s" direction="in"/>
+      <arg name="clazz" type="i" direction="in"/>
+      <arg name="type" type="i" direction="in"/>
+      <arg name="ttl" type="i" direction="in"/>
+      <arg name="rdata" type="ay" direction="in"/>
+    </method>
   </interface>
 </node>
index 8bb4a4db8ac4cd39209cc8a8979b8056854e00f1..60edec2c13dbd65bf4cb154d947a95316cff9c3c 100644 (file)
@@ -46,6 +46,7 @@
 #include <avahi-common/dbus-watch-glue.h>
 #include <avahi-common/alternative.h>
 #include <avahi-common/error.h>
+#include <avahi-common/domain.h>
 
 #include <avahi-core/log.h>
 #include <avahi-core/core.h>
@@ -712,6 +713,38 @@ static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntry
     dbus_message_unref(m);
 }
 
+static int read_rdata(DBusMessage *m, int idx, void **rdata, uint32_t *size) {
+    DBusMessageIter iter, sub;
+    int n, j;
+    uint8_t *k;
+
+    assert(m);
+    
+    dbus_message_iter_init(m, &iter);
+
+    for (j = 0; j < idx; j++)
+       dbus_message_iter_next(&iter);
+    
+    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY ||
+        dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_BYTE)
+        goto fail;
+
+    dbus_message_iter_recurse(&iter, &sub);
+    dbus_message_iter_get_fixed_array(&sub, &k, &n);
+
+    *rdata = k;
+    *size = n;
+    
+    return 0;
+    
+fail:
+    avahi_log_warn("Error parsing data");
+
+    *rdata = NULL;
+    size = 0;
+    return -1;
+}
+
 static int read_strlst(DBusMessage *m, int idx, AvahiStringList **l) {
     DBusMessageIter iter, sub;
     int j;
@@ -983,8 +1016,57 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
         if (!(flags & AVAHI_PUBLISH_UPDATE))
             i->n_entries ++;
         
+        return respond_ok(c, m);
+    } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddRecord")) {
+        int32_t interface, protocol;
+        uint32_t flags, ttl, size;
+        uint16_t clazz, type;
+        char *name;
+        void *rdata;
+        AvahiRecord *r;
+        
+        if (!dbus_message_get_args(
+                m, &error,
+                DBUS_TYPE_INT32, &interface,
+                DBUS_TYPE_INT32, &protocol,
+                DBUS_TYPE_UINT32, &flags,
+                DBUS_TYPE_STRING, &name,
+                DBUS_TYPE_UINT16, &clazz,
+                DBUS_TYPE_UINT16, &type,
+                DBUS_TYPE_UINT32, &ttl,
+                DBUS_TYPE_INVALID) || !name ||
+                read_rdata (m, 7, &rdata, &size)) {
+            avahi_log_warn("Error parsing EntryGroup::AddRecord message");
+            goto fail;
+        }
+
+        if (!(flags & AVAHI_PUBLISH_UPDATE) && i->n_entries >= ENTRIES_PER_ENTRY_GROUP_MAX)
+            return respond_error(c, m, AVAHI_ERR_TOO_MANY_ENTRIES, NULL);
+
+        if (!avahi_is_valid_domain_name (name))
+            return respond_error(c, m, AVAHI_ERR_INVALID_DOMAIN_NAME, NULL);
+
+        if (!(r = avahi_record_new_full (name, clazz, type, ttl)))
+            return respond_error(c, m, AVAHI_ERR_NO_MEMORY, NULL);
+
+        if (avahi_rdata_parse (r, rdata, size) < 0) {
+            avahi_record_unref (r);
+            return respond_error(c, m, AVAHI_ERR_INVALID_RDATA, NULL);
+        }
+        
+        if (avahi_server_add(avahi_server, i->entry_group, (AvahiIfIndex) interface, (AvahiProtocol) protocol, (AvahiPublishFlags) flags, r) < 0) {
+            avahi_record_unref (r);
+            return respond_error(c, m, avahi_server_errno(avahi_server), NULL);
+        }
+
+        if (!(flags & AVAHI_PUBLISH_UPDATE))
+            i->n_entries ++;
+       
+        avahi_record_unref (r); 
+
         return respond_ok(c, m);
     } 
     
     avahi_log_warn("Missed message %s::%s()", dbus_message_get_interface(m), dbus_message_get_member(m));
 
index 14c4b406d03819e955bc8e97342ba33f6256f598..8cdc2428f935e4278d63c2ef86e61b55dc8ea610 100644 (file)
@@ -1,10 +1,12 @@
 $Id$
 
 * NOTE *
-While this document provides an overview of the DBUS-API, a much better reference is the .introspect files
-in the avahi-daemon directory
+While this document provides an overview of the DBUS-API, a much better
+reference is the .introspect files in the avahi-daemon directory.
+It may also become out of date, especially in SVN, so please, check the
+.introspect files in avahi-daemon/
 
-You can find copies online, under "Developing with Avahi" here
+Or you can find copies online, under "Developing with Avahi" here
 http://www.freedesktop.org/Software/Avahi
 
 - Lathiat