]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/fce_api.c
Add FCE type names and adjust logtypes to new fce facility
[netatalk.git] / etc / afpd / fce_api.c
index fb7535b6c485e68b2dd8bfe269d2aabb8d89eca8..bb42bb1e42e228e08ef99fa7defcf8e9bddb108a 100644 (file)
@@ -37,8 +37,6 @@
 #include <arpa/inet.h>
 #include <netdb.h>
 
-#include <netatalk/at.h>
-
 #include <atalk/adouble.h>
 #include <atalk/vfs.h>
 #include <atalk/logger.h>
@@ -82,6 +80,16 @@ static const char *skip_files[] =
 };
 static struct fce_close_event last_close_event;
 
+static char *fce_event_names[] = {
+    "",
+    "FCE_FILE_MODIFY",
+    "FCE_FILE_DELETE",
+    "FCE_DIR_DELETE",
+    "FCE_FILE_CREATE",
+    "FCE_DIR_CREATE",
+    "FCE_TM_SIZE",
+};
+
 /*
  *
  * Initialize network structs for any listeners
@@ -108,7 +116,7 @@ void fce_init_udp()
             close(udp_entry->sock);
 
         if ((rv = getaddrinfo(udp_entry->addr, udp_entry->port, &hints, &servinfo)) != 0) {
-            LOG(log_error, logtype_afpd, "fce_init_udp: getaddrinfo(%s:%s): %s",
+            LOG(log_error, logtype_fce, "fce_init_udp: getaddrinfo(%s:%s): %s",
                 udp_entry->addr, udp_entry->port, gai_strerror(rv));
             continue;
         }
@@ -116,7 +124,7 @@ void fce_init_udp()
         /* loop through all the results and make a socket */
         for (p = servinfo; p != NULL; p = p->ai_next) {
             if ((udp_entry->sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
-                LOG(log_error, logtype_afpd, "fce_init_udp: socket(%s:%s): %s",
+                LOG(log_error, logtype_fce, "fce_init_udp: socket(%s:%s): %s",
                     udp_entry->addr, udp_entry->port, strerror(errno));
                 continue;
             }
@@ -124,7 +132,7 @@ void fce_init_udp()
         }
 
         if (p == NULL) {
-            LOG(log_error, logtype_afpd, "fce_init_udp: no socket for %s:%s",
+            LOG(log_error, logtype_fce, "fce_init_udp: no socket for %s:%s",
                 udp_entry->addr, udp_entry->port);
         }
         udp_entry->addrinfo = *p;
@@ -155,42 +163,44 @@ void fce_cleanup()
     udp_initialized = FCE_FALSE;
 }
 
-
 /*
  * Construct a UDP packet for our listeners and return packet size
  * */
 static ssize_t build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id )
 {
-    size_t pathlen;
+    size_t pathlen = 0;
     ssize_t data_len = 0;
+    uint64_t *t;
 
-    strncpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
+    /* Set content of packet */
+    memcpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
     packet->version = FCE_PACKET_VERSION;
     packet->mode = mode;
-    packet->event_id = event_id;
-
-    pathlen = strlen(path) + 1; /* include string terminator */
+   
+    packet->event_id = event_id; 
 
+    pathlen = strlen(path); /* exclude string terminator */
+    
     /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
     if (pathlen >= MAXPATHLEN)
         pathlen = MAXPATHLEN - 1;
 
-    /* This is the payload len. Means: the stream has len bytes more until packet is finished */
-    /* A server should read the first 16 byte, decode them and then fetch the rest */
-    data_len = FCE_PACKET_HEADER_SIZE + pathlen;
     packet->datalen = pathlen;
 
+    /* This is the payload len. Means: the packet has len bytes more until packet is finished */
+    data_len = FCE_PACKET_HEADER_SIZE + pathlen;
+
     switch (mode) {
     case FCE_TM_SIZE:
-        tm_used = hton64(tm_used);
-        memcpy(packet->data, &tm_used, sizeof(tm_used));
-        strncpy(packet->data + sizeof(tm_used), path, pathlen);
-
-        packet->datalen += sizeof(tm_used);
+        t = (uint64_t *)packet->data;
+        *t = hton64(tm_used);
+        memcpy(packet->data + sizeof(tm_used), path, pathlen);
+        
+        packet->datalen = pathlen + sizeof(tm_used);
         data_len += sizeof(tm_used);
         break;
     default:
-        strncpy(packet->data, path, pathlen);
+        memcpy(packet->data, path, pathlen);
         break;
     }
 
@@ -198,7 +208,10 @@ static ssize_t build_fce_packet( struct fce_packet *packet, char *path, int mode
     return data_len;
 }
 
-static int pack_fce_packet(struct fce_packet *packet, unsigned char *buf)
+/*
+ * Handle Endianess and write into buffer w/o padding
+ **/ 
+static void pack_fce_packet(struct fce_packet *packet, unsigned char *buf, int maxlen)
 {
     unsigned char *p = buf;
 
@@ -211,18 +224,17 @@ static int pack_fce_packet(struct fce_packet *packet, unsigned char *buf)
     *p = packet->mode;
     p++;
     
-    uint32_t id = htonl(packet->event_id);
-    memcpy(p, &id, sizeof(id));
+    uint32_t *id = (uint32_t*)p;
+    *id = htonl(packet->event_id);
     p += sizeof(packet->event_id);
 
-    uint16_t l = htons(packet->datalen);
-    memcpy(p, &l, sizeof(l));
-    p += sizeof(l);
-
-    memcpy(p, &packet->data[0], packet->datalen);
-    p += packet->datalen;
+    uint16_t *l = ( uint16_t *)p;
+    *l = htons(packet->datalen);
+    p += sizeof(packet->datalen);
 
-    return 0;
+    if (((p - buf) +  packet->datalen) < maxlen) {
+        memcpy(p, &packet->data[0], packet->datalen);
+    }
 }
 
 /*
@@ -238,7 +250,7 @@ static void send_fce_event( char *path, int mode )
     static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
     time_t now = time(NULL);
 
-    LOG(log_debug, logtype_afpd, "send_fce_event: start");
+    LOG(log_debug, logtype_fce, "send_fce_event: start");
 
     /* initialized ? */
     if (first_event == FCE_TRUE) {
@@ -250,7 +262,7 @@ static void send_fce_event( char *path, int mode )
 
     /* build our data packet */
     ssize_t data_len = build_fce_packet( &packet, path, mode, ++event_id );
-    pack_fce_packet(&packet, iobuf);
+    pack_fce_packet(&packet, iobuf, MAXIOBUF);
 
     for (int i = 0; i < udp_sockets; i++)
     {
@@ -271,7 +283,7 @@ static void send_fce_event( char *path, int mode )
             
             if (udp_entry->sock == -1) {
                 /* failed again, so go to rest again */
-                LOG(log_error, logtype_afpd, "Cannot recreate socket for fce UDP connection: errno %d", errno  );
+                LOG(log_error, logtype_fce, "Cannot recreate socket for fce UDP connection: errno %d", errno  );
 
                 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
                 continue;
@@ -281,7 +293,7 @@ static void send_fce_event( char *path, int mode )
 
             /* Okay, we have a running socket again, send server that we had a problem on our side*/
             data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
-            pack_fce_packet(&packet, iobuf);
+            pack_fce_packet(&packet, iobuf, MAXIOBUF);
 
             sendto(udp_entry->sock,
                    iobuf,
@@ -292,7 +304,7 @@ static void send_fce_event( char *path, int mode )
 
             /* Rebuild our original data packet */
             data_len = build_fce_packet( &packet, path, mode, event_id );
-            pack_fce_packet(&packet, iobuf);
+            pack_fce_packet(&packet, iobuf, MAXIOBUF);
         }
 
         sent_data = sendto(udp_entry->sock,
@@ -305,7 +317,7 @@ static void send_fce_event( char *path, int mode )
         /* Problems ? */
         if (sent_data != data_len) {
             /* Argh, socket broke, we close and retry later */
-            LOG(log_error, logtype_afpd, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
+            LOG(log_error, logtype_fce, "send_fce_event: error sending packet to %s:%s, transfered %d of %d: %s",
                 udp_entry->addr, udp_entry->port, sent_data, data_len, strerror(errno));
 
             close( udp_entry->sock );
@@ -321,7 +333,7 @@ static int add_udp_socket(const char *target_ip, const char *target_port )
         target_port = FCE_DEFAULT_PORT_STRING;
 
     if (udp_sockets >= FCE_MAX_UDP_SOCKS) {
-        LOG(log_error, logtype_afpd, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
+        LOG(log_error, logtype_fce, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
         return AFPERR_PARAM;
     }
 
@@ -348,7 +360,7 @@ static void save_close_event(const char *path)
         send_fce_event(last_close_event.path, FCE_FILE_MODIFY);
     }
 
-    LOG(log_debug, logtype_afpd, "save_close_event: %s", path);
+    LOG(log_debug, logtype_fce, "save_close_event: %s", path);
 
     last_close_event.time = now;
     strncpy(last_close_event.path, path, MAXPATHLEN);
@@ -363,6 +375,11 @@ static int register_fce(const char *u_name, int is_dir, int mode)
 {
     static int first_event = FCE_TRUE;
 
+    AFP_ASSERT(mode >= FCE_FIRST_EVENT && mode <= FCE_LAST_EVENT);
+
+    LOG(log_debug, logtype_fce, "register_fce(path: %s, type: %s, event: %s",
+        fullpathname(u_name), is_dir ? "dir" : "file", fce_event_names[mode]);
+
     if (udp_sockets == 0)
         /* No listeners configured */
         return AFP_OK;
@@ -391,13 +408,13 @@ static int register_fce(const char *u_name, int is_dir, int mode)
         strlcpy(full_path_buffer, u_name, MAXPATHLEN);
     } else if (!is_dir || mode == FCE_DIR_DELETE) {
                if (strlen( cwd ) + strlen( u_name) + 1 >= MAXPATHLEN) {
-                       LOG(log_error, logtype_afpd, "FCE file name too long: %s/%s", cwd, u_name );
+                       LOG(log_error, logtype_fce, "FCE file name too long: %s/%s", cwd, u_name );
                        return AFPERR_PARAM;
                }
                sprintf( full_path_buffer, "%s/%s", cwd, u_name );
        } else {
                if (strlen( cwd ) >= MAXPATHLEN) {
-                       LOG(log_error, logtype_afpd, "FCE directory name too long: %s", cwd);
+                       LOG(log_error, logtype_fce, "FCE directory name too long: %s", cwd);
                        return AFPERR_PARAM;
                }
                strcpy( full_path_buffer, cwd);
@@ -406,11 +423,11 @@ static int register_fce(const char *u_name, int is_dir, int mode)
        /* Can we ignore this event based on type or history? */
        if (!(mode & FCE_TM_SIZE) && fce_handle_coalescation( full_path_buffer, is_dir, mode ))
        {
-               LOG(log_debug9, logtype_afpd, "Coalesced fc event <%d> for <%s>", mode, full_path_buffer );
+               LOG(log_debug9, logtype_fce, "Coalesced fc event <%d> for <%s>", mode, full_path_buffer );
                return AFP_OK;
        }
 
-       LOG(log_debug9, logtype_afpd, "Detected fc event <%d> for <%s>", mode, full_path_buffer );
+       LOG(log_debug9, logtype_fce, "Detected fc event <%d> for <%s>", mode, full_path_buffer );
 
     if (mode & FCE_FILE_MODIFY) {
         save_close_event(full_path_buffer);
@@ -428,7 +445,7 @@ static void check_saved_close_events(int fmodwait)
 
     /* check if configured holdclose time has passed */
     if (last_close_event.time && ((last_close_event.time + fmodwait) < now)) {
-        LOG(log_debug, logtype_afpd, "check_saved_close_events: sending event: %s", last_close_event.path);
+        LOG(log_debug, logtype_fce, "check_saved_close_events: sending event: %s", last_close_event.path);
         /* yes, send event */
         send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY);
         last_close_event.path[0] = 0;
@@ -459,7 +476,7 @@ int fce_register_delete_file( struct path *path )
     if (!(fce_ev_enabled & (1 << FCE_FILE_DELETE)))
         return ret;
        
-    ret = register_fce( path->u_name, FALSE, FCE_FILE_DELETE );
+    ret = register_fce( path->u_name, false, FCE_FILE_DELETE );
 
     return ret;
 }
@@ -473,7 +490,7 @@ int fce_register_delete_dir( char *name )
     if (!(fce_ev_enabled & (1 << FCE_DIR_DELETE)))
         return ret;
        
-    ret = register_fce( name, TRUE, FCE_DIR_DELETE);
+    ret = register_fce( name, true, FCE_DIR_DELETE);
 
     return ret;
 }
@@ -488,7 +505,7 @@ int fce_register_new_dir( struct path *path )
     if (!(fce_ev_enabled & (1 << FCE_DIR_CREATE)))
         return ret;
 
-    ret = register_fce( path->u_name, TRUE, FCE_DIR_CREATE );
+    ret = register_fce( path->u_name, true, FCE_DIR_CREATE );
 
     return ret;
 }
@@ -504,31 +521,22 @@ int fce_register_new_file( struct path *path )
     if (!(fce_ev_enabled & (1 << FCE_FILE_CREATE)))
         return ret;
 
-    ret = register_fce( path->u_name, FALSE, FCE_FILE_CREATE );
+    ret = register_fce( path->u_name, false, FCE_FILE_CREATE );
 
     return ret;
 }
 
 int fce_register_file_modification( struct ofork *ofork )
 {
-    char *u_name = NULL;
-    struct vol *vol;
     int ret = AFP_OK;
 
-    if (ofork == NULL || ofork->of_vol == NULL)
+    if (ofork == NULL)
         return AFPERR_PARAM;
 
     if (!(fce_ev_enabled & (1 << FCE_FILE_MODIFY)))
         return ret;
 
-    vol = ofork->of_vol;
-
-    if (NULL == (u_name = mtoupath(vol, of_name(ofork), ofork->of_did, utf8_encoding()))) 
-    {
-        return AFPERR_MISC;
-    }
-    
-    ret = register_fce( u_name, FALSE, FCE_FILE_MODIFY );
+    ret = register_fce(of_name(ofork), false, FCE_FILE_MODIFY );
     
     return ret;    
 }
@@ -544,7 +552,7 @@ int fce_register_tm_size(const char *vol, size_t used)
         return ret;
 
     tm_used = used;             /* oh what a hack */
-    ret = register_fce(vol, FALSE, FCE_TM_SIZE);
+    ret = register_fce(vol, false, FCE_TM_SIZE);
 
     return ret;
 }
@@ -582,7 +590,7 @@ int fce_set_events(const char *events)
 
     fce_ev_enabled = 0;
 
-    for (p = strtok(e, ","); p; p = strtok(NULL, ",")) {
+    for (p = strtok(e, ", "); p; p = strtok(NULL, ", ")) {
         if (strcmp(p, "fmod") == 0) {
             fce_ev_enabled |= (1 << FCE_FILE_MODIFY);
         } else if (strcmp(p, "fdel") == 0) {
@@ -599,6 +607,8 @@ int fce_set_events(const char *events)
     }
 
     free(e);
+
+    return AFP_OK;
 }
 
 #ifdef FCE_TEST_MAIN