]> git.meshlink.io Git - catta/blobdiff - avahi-core/netlink.c
* several glib adapter cleanups:
[catta] / avahi-core / netlink.c
index 92d614fc8df7bea8941a74bccf415d515f5b9dde..b2a6684c60445fd93356a6ec789c47863990cbdf 100644 (file)
@@ -41,49 +41,42 @@ struct AvahiNetlink {
     uint8_t* buffer;
     size_t buffer_length;
 
-    AvahiPoll *poll_api;
+    const AvahiPoll *poll_api;
     AvahiWatch *watch;
 };
 
 int avahi_netlink_work(AvahiNetlink *nl, int block) {
+    ssize_t bytes;
+    struct nlmsghdr *p;
+    
     assert(nl);
+    
+    if ((bytes = recv(nl->fd, nl->buffer, nl->buffer_length, block ? 0 : MSG_DONTWAIT)) < 0) {
+        
+        if (errno == EAGAIN || errno == EINTR)
+            return 0;
+        
+        avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
+        return -1;
+    }
 
-    for (;;) {
-        ssize_t bytes;
-        struct nlmsghdr *p;
-
-        for (;;) {
-            if ((bytes = recv(nl->fd, nl->buffer, nl->buffer_length, block ? 0 : MSG_DONTWAIT)) < 0) {
-
-                if (errno == EAGAIN || errno == EINTR)
-                    return 1;
-                
-                avahi_log_error(__FILE__": recv() failed: %s", strerror(errno));
-                return 0;
-            }
-
-            break;
+    p = (struct nlmsghdr *) nl->buffer;
+    
+    assert(nl->callback);
+    
+    for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
+        if (!NLMSG_OK(p, (size_t) bytes)) {
+            avahi_log_warn(__FILE__": packet truncated");
+            return -1;
         }
-
-        p = (struct nlmsghdr *) nl->buffer;
         
-        if (nl->callback) {
-            for (; bytes > 0; p = NLMSG_NEXT(p, bytes)) {
-                if (!NLMSG_OK(p, (size_t) bytes)) {
-                    avahi_log_warn(__FILE__": packet truncated");
-                    return 0;
-                }
-
-                nl->callback(nl, p, nl->userdata);
-            }
-        }
-
-        if (block)
-            return 1;
+        nl->callback(nl, p, nl->userdata);
     }
+    
+    return 0;
 }
 
-static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event, void *userdata) {
+static void socket_event(AvahiWatch *w, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, void *userdata) {
     AvahiNetlink *nl = userdata;
 
     assert(w);
@@ -93,7 +86,7 @@ static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent event, void *use
     avahi_netlink_work(nl, 0);
 }
 
-AvahiNetlink *avahi_netlink_new(AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
+AvahiNetlink *avahi_netlink_new(const AvahiPoll *poll_api, uint32_t groups, void (*cb) (AvahiNetlink *nl, struct nlmsghdr *n, void* userdata), void* userdata) {
     int fd = -1;
     struct sockaddr_nl addr;
     AvahiNetlink *nl = NULL;
@@ -145,9 +138,7 @@ fail:
         close(fd);
 
     if (nl) {
-        if (nl->buffer)
-            avahi_free(nl);
-        
+        avahi_free(nl->buffer);
         avahi_free(nl);
     }
 
@@ -175,7 +166,7 @@ int avahi_netlink_send(AvahiNetlink *nl, struct nlmsghdr *m, unsigned *ret_seq)
     m->nlmsg_flags |= NLM_F_ACK;
 
     if (send(nl->fd, m, m->nlmsg_len, 0) < 0) {
-        avahi_log_error(__FILE__": send(): %s\n", strerror(errno));
+        avahi_log_error(__FILE__": send(): %s", strerror(errno));
         return -1;
     }