X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=etc%2Fafpd%2Ffce_api.c;h=00a04248406cfab1f1e90676fc3d5c93cf8bc75f;hb=38be99d9e96e4af9ba50ab7c33f152431224c24b;hp=601f02e215262e1bda24b93d09307636f2e903f2;hpb=09fc36d97b784e8d8d66dbfe8c60e9127c30826a;p=netatalk.git diff --git a/etc/afpd/fce_api.c b/etc/afpd/fce_api.c old mode 100755 new mode 100644 index 601f02e2..00a04248 --- a/etc/afpd/fce_api.c +++ b/etc/afpd/fce_api.c @@ -47,10 +47,10 @@ #include #include #include +#include #include "fork.h" #include "file.h" -#include "globals.h" #include "directory.h" #include "desktop.h" #include "volume.h" @@ -65,13 +65,22 @@ static struct udp_entry udp_socket_list[FCE_MAX_UDP_SOCKS]; static int udp_sockets = 0; static int udp_initialized = FCE_FALSE; - - +static unsigned long fce_ev_enabled = + (1 << FCE_FILE_MODIFY) | + (1 << FCE_FILE_DELETE) | + (1 << FCE_DIR_DELETE) | + (1 << FCE_FILE_CREATE) | + (1 << FCE_DIR_CREATE); + +static uint64_t tm_used; /* used for passing to event handler */ +#define MAXIOBUF 1024 +static char iobuf[MAXIOBUF]; static const char *skip_files[] = { ".DS_Store", NULL }; +static struct fce_close_event last_close_event; /* * @@ -81,48 +90,52 @@ static const char *skip_files[] = * */ void fce_init_udp() { + int rv; + struct addrinfo hints, *servinfo, *p; + if (udp_initialized == FCE_TRUE) return; + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; - for (int i = 0; i < udp_sockets; i++) - { + for (int i = 0; i < udp_sockets; i++) { struct udp_entry *udp_entry = udp_socket_list + i; /* Close any pending sockets */ if (udp_entry->sock != -1) - { - close( udp_entry->sock ); + 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", + udp_entry->addr, udp_entry->port, gai_strerror(rv)); + continue; } - /* resolve IP to network address */ - if (inet_aton( udp_entry->ip, &udp_entry->addr.sin_addr ) ==0 ) - { - /* Hmm, failed try to resolve host */ - struct hostent *hp = gethostbyname( udp_entry->ip ); - if (hp == NULL) - { - LOG(log_error, logtype_afpd, "Cannot resolve host name for fce UDP connection: %s (errno %d)", udp_entry->ip, errno ); + /* 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", + udp_entry->addr, udp_entry->port, strerror(errno)); continue; } - memcpy( &udp_entry->addr.sin_addr, &hp->h_addr, sizeof(udp_entry->addr.sin_addr) ); + break; } - /* Create UDP socket */ - udp_entry->sock = socket( AF_INET, SOCK_DGRAM, 0 ); - if (udp_entry->sock == -1) - { - LOG(log_error, logtype_afpd, "Cannot create socket for fce UDP connection: errno %d", errno ); - continue; + if (p == NULL) { + LOG(log_error, logtype_afpd, "fce_init_udp: no socket for %s:%s", + udp_entry->addr, udp_entry->port); } - - /* Set socket address params */ - udp_entry->addr.sin_family = AF_INET; - udp_entry->addr.sin_port = htons(udp_entry->port); + udp_entry->addrinfo = *p; + memcpy(&udp_entry->addrinfo, p, sizeof(struct addrinfo)); + memcpy(&udp_entry->sockaddr, p->ai_addr, sizeof(struct sockaddr_storage)); + freeaddrinfo(servinfo); } - udp_initialized = FCE_TRUE; + udp_initialized = FCE_TRUE; } + void fce_cleanup() { if (udp_initialized == FCE_FALSE ) @@ -146,31 +159,70 @@ void fce_cleanup() /* * Construct a UDP packet for our listeners and return packet size * */ -static unsigned short build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id ) +static ssize_t build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id ) { - unsigned short data_len = 0; + size_t pathlen; + ssize_t data_len = 0; strncpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) ); packet->version = FCE_PACKET_VERSION; packet->mode = mode; + packet->event_id = event_id; - data_len = strlen( path ); + pathlen = strlen(path) + 1; /* include string terminator */ /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */ - if (data_len >= FCE_MAX_PATH_LEN) - { - data_len = FCE_MAX_PATH_LEN - 1; - } + 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 */ - packet->len = htons( data_len); - packet->event_id = htonl( event_id ); - - strncpy( packet->data, path, data_len ); + data_len = FCE_PACKET_HEADER_SIZE + pathlen; + packet->datalen = 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); + data_len += sizeof(tm_used); + break; + default: + strncpy(packet->data, path, pathlen); + break; + } /* return the packet len */ - return sizeof(struct fce_packet) - FCE_MAX_PATH_LEN + data_len; + return data_len; +} + +static int pack_fce_packet(struct fce_packet *packet, unsigned char *buf) +{ + unsigned char *p = buf; + + memcpy(p, &packet->magic[0], sizeof(packet->magic)); + p += sizeof(packet->magic); + + *p = packet->version; + p++; + + *p = packet->mode; + p++; + + uint32_t id = htonl(packet->event_id); + memcpy(p, &id, sizeof(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; + + return 0; } /* @@ -179,15 +231,26 @@ static unsigned short build_fce_packet( struct fce_packet *packet, char *path, i * */ static void send_fce_event( char *path, int mode ) { + static int first_event = FCE_TRUE; + struct fce_packet packet; void *data = &packet; 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); - /* build our data packet */ - int data_len = build_fce_packet( &packet, path, mode, ++event_id ); + LOG(log_debug, logtype_afpd, "send_fce_event: start"); + + /* initialized ? */ + if (first_event == FCE_TRUE) { + first_event = FCE_FALSE; + fce_init_udp(); + /* Notify listeners the we start from the beginning */ + send_fce_event( "", FCE_CONN_START ); + } + /* build our data packet */ + ssize_t data_len = build_fce_packet( &packet, path, mode, ++event_id ); + pack_fce_packet(&packet, iobuf); for (int i = 0; i < udp_sockets; i++) { @@ -202,10 +265,11 @@ static void send_fce_event( char *path, int mode ) continue; /* Reopen socket */ - udp_entry->sock = socket( AF_INET, SOCK_DGRAM, 0 ); - - if (udp_entry->sock == -1) - { + udp_entry->sock = socket(udp_entry->addrinfo.ai_family, + udp_entry->addrinfo.ai_socktype, + udp_entry->addrinfo.ai_protocol); + + 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 ); @@ -217,21 +281,32 @@ 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); - sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) ); + sendto(udp_entry->sock, + iobuf, + data_len, + 0, + (struct sockaddr *)&udp_entry->sockaddr, + udp_entry->addrinfo.ai_addrlen); /* Rebuild our original data packet */ data_len = build_fce_packet( &packet, path, mode, event_id ); + pack_fce_packet(&packet, iobuf); } - sent_data = sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) ); + sent_data = sendto(udp_entry->sock, + iobuf, + data_len, + 0, + (struct sockaddr *)&udp_entry->sockaddr, + udp_entry->addrinfo.ai_addrlen); /* Problems ? */ - if (sent_data != data_len) - { + if (sent_data != data_len) { /* Argh, socket broke, we close and retry later */ - LOG(log_error, logtype_afpd, "Error while sending packet to %s for fce UDP connection: transfered: %d of %d errno %d", - udp_entry->port, sent_data, data_len, errno ); + LOG(log_error, logtype_afpd, "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 ); udp_entry->sock = -1; @@ -240,21 +315,21 @@ static void send_fce_event( char *path, int mode ) } } -static int add_udp_socket( char *target_ip, int target_port ) +static int add_udp_socket(const char *target_ip, const char *target_port ) { - if (target_port == 0) - target_port = FCE_DEFAULT_PORT; + if (target_port == NULL) + target_port = FCE_DEFAULT_PORT_STRING; - if (udp_sockets >= FCE_MAX_UDP_SOCKS) - { + 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 ); return AFPERR_PARAM; } - strncpy( udp_socket_list[udp_sockets].ip, target_ip, FCE_MAX_IP_LEN - 1); - udp_socket_list[udp_sockets].port = target_port; + udp_socket_list[udp_sockets].addr = strdup(target_ip); + udp_socket_list[udp_sockets].port = strdup(target_port); udp_socket_list[udp_sockets].sock = -1; - memset( &udp_socket_list[udp_sockets].addr, 0, sizeof(struct sockaddr_in) ); + memset(&udp_socket_list[udp_sockets].addrinfo, 0, sizeof(struct addrinfo)); + memset(&udp_socket_list[udp_sockets].sockaddr, 0, sizeof(struct sockaddr_storage)); udp_socket_list[udp_sockets].next_try_on_error = 0; udp_sockets++; @@ -262,13 +337,32 @@ static int add_udp_socket( char *target_ip, int target_port ) return AFP_OK; } +static void save_close_event(const char *path) +{ + time_t now = time(NULL); + + /* Check if it's a close for the same event as the last one */ + if (last_close_event.time /* is there any saved event ? */ + && (strcmp(path, last_close_event.path) != 0)) { + /* no, so send the saved event out now */ + send_fce_event(last_close_event.path, FCE_FILE_MODIFY); + } + + LOG(log_debug, logtype_afpd, "save_close_event: %s", path); + + last_close_event.time = now; + strncpy(last_close_event.path, path, MAXPATHLEN); +} + /* * * Dispatcher for all incoming file change events * * */ -static int register_fce( char *u_name, int is_dir, int mode ) +static int register_fce(const char *u_name, int is_dir, int mode) { + static int first_event = FCE_TRUE; + if (udp_sockets == 0) /* No listeners configured */ return AFP_OK; @@ -276,15 +370,12 @@ static int register_fce( char *u_name, int is_dir, int mode ) if (u_name == NULL) return AFPERR_PARAM; - static int first_event = FCE_TRUE; - /* do some initialization on the fly the first time */ - if (first_event) - { + if (first_event) { fce_initialize_history(); + first_event = FCE_FALSE; } - /* handle files which should not cause events (.DS_Store atc. ) */ for (int i = 0; skip_files[i] != NULL; i++) { @@ -293,22 +384,19 @@ static int register_fce( char *u_name, int is_dir, int mode ) } - char full_path_buffer[FCE_MAX_PATH_LEN + 1] = {""}; + char full_path_buffer[MAXPATHLEN + 1] = {""}; const char *cwd = getcwdpath(); - if (!is_dir || mode == FCE_DIR_DELETE) - { - if (strlen( cwd ) + strlen( u_name) + 1 >= FCE_MAX_PATH_LEN) - { + if (mode == FCE_TM_SIZE) { + 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 ); return AFPERR_PARAM; } sprintf( full_path_buffer, "%s/%s", cwd, u_name ); - } - else - { - if (strlen( cwd ) >= FCE_MAX_PATH_LEN) - { + } else { + if (strlen( cwd ) >= MAXPATHLEN) { LOG(log_error, logtype_afpd, "FCE directory name too long: %s", cwd); return AFPERR_PARAM; } @@ -316,7 +404,7 @@ static int register_fce( char *u_name, int is_dir, int mode ) } /* Can we ignore this event based on type or history? */ - if (fce_handle_coalescation( full_path_buffer, is_dir, mode )) + 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 ); return AFP_OK; @@ -324,26 +412,29 @@ static int register_fce( char *u_name, int is_dir, int mode ) LOG(log_debug9, logtype_afpd, "Detected fc event <%d> for <%s>", mode, full_path_buffer ); - - /* we do initilization on the fly, no blocking calls in here - * (except when using FQDN in broken DNS environment) - */ - if (first_event == FCE_TRUE) - { - fce_init_udp(); - - /* Notify listeners the we start from the beginning */ - send_fce_event( "", FCE_CONN_START ); - - first_event = FCE_FALSE; + if (mode & FCE_FILE_MODIFY) { + save_close_event(full_path_buffer); + return AFP_OK; } - /* Handle UDP transport */ send_fce_event( full_path_buffer, mode ); return AFP_OK; } +static void check_saved_close_events(int fmodwait) +{ + time_t now = time(NULL); + + /* 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); + /* yes, send event */ + send_fce_event(&last_close_event.path[0], FCE_FILE_MODIFY); + last_close_event.path[0] = 0; + last_close_event.time = 0; + } +} /******************** External calls start here **************************/ @@ -352,6 +443,11 @@ static int register_fce( char *u_name, int is_dir, int mode ) * */ #ifndef FCE_TEST_MAIN +void fce_pending_events(AFPObj *obj) +{ + vol_fce_tm_event(); + check_saved_close_events(obj->options.fce_fmodwait); +} int fce_register_delete_file( struct path *path ) { @@ -360,6 +456,8 @@ int fce_register_delete_file( struct path *path ) if (path == NULL) return AFPERR_PARAM; + if (!(fce_ev_enabled & (1 << FCE_FILE_DELETE))) + return ret; ret = register_fce( path->u_name, FALSE, FCE_FILE_DELETE ); @@ -372,6 +470,8 @@ int fce_register_delete_dir( char *name ) if (name == NULL) return AFPERR_PARAM; + if (!(fce_ev_enabled & (1 << FCE_DIR_DELETE))) + return ret; ret = register_fce( name, TRUE, FCE_DIR_DELETE); @@ -385,6 +485,9 @@ int fce_register_new_dir( struct path *path ) if (path == NULL) return AFPERR_PARAM; + if (!(fce_ev_enabled & (1 << FCE_DIR_CREATE))) + return ret; + ret = register_fce( path->u_name, TRUE, FCE_DIR_CREATE ); return ret; @@ -398,12 +501,14 @@ int fce_register_new_file( struct path *path ) if (path == NULL) return AFPERR_PARAM; + if (!(fce_ev_enabled & (1 << FCE_FILE_CREATE))) + return ret; + ret = register_fce( path->u_name, FALSE, FCE_FILE_CREATE ); return ret; } - int fce_register_file_modification( struct ofork *ofork ) { char *u_name = NULL; @@ -413,6 +518,9 @@ int fce_register_file_modification( struct ofork *ofork ) if (ofork == NULL || ofork->of_vol == 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()))) @@ -424,6 +532,22 @@ int fce_register_file_modification( struct ofork *ofork ) return ret; } + +int fce_register_tm_size(const char *vol, size_t used) +{ + int ret = AFP_OK; + + if (vol == NULL) + return AFPERR_PARAM; + + if (!(fce_ev_enabled & (1 << FCE_TM_SIZE))) + return ret; + + tm_used = used; /* oh what a hack */ + ret = register_fce(vol, FALSE, FCE_TM_SIZE); + + return ret; +} #endif /* @@ -431,22 +555,51 @@ int fce_register_file_modification( struct ofork *ofork ) * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times) * * */ -int fce_add_udp_socket( char *target ) +int fce_add_udp_socket(const char *target) { - int port = FCE_DEFAULT_PORT; + const char *port = FCE_DEFAULT_PORT_STRING; char target_ip[256] = {""}; - strncpy( target_ip, target, sizeof(target_ip) -1); + strncpy(target_ip, target, sizeof(target_ip) -1); + char *port_delim = strchr( target_ip, ':' ); - if (port_delim) - { + if (port_delim) { *port_delim = 0; - port = atoi( port_delim + 1); + port = port_delim + 1; } - return add_udp_socket( target_ip, port ); + return add_udp_socket(target_ip, port); } +int fce_set_events(const char *events) +{ + char *e; + char *p; + + if (events == NULL) + return AFPERR_PARAM; + + e = strdup(events); + + fce_ev_enabled = 0; + + 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) { + fce_ev_enabled |= (1 << FCE_FILE_DELETE); + } else if (strcmp(p, "ddel") == 0) { + fce_ev_enabled |= (1 << FCE_DIR_DELETE); + } else if (strcmp(p, "fcre") == 0) { + fce_ev_enabled |= (1 << FCE_FILE_CREATE); + } else if (strcmp(p, "dcre") == 0) { + fce_ev_enabled |= (1 << FCE_DIR_CREATE); + } else if (strcmp(p, "tmsz") == 0) { + fce_ev_enabled |= (1 << FCE_TM_SIZE); + } + } + free(e); +} #ifdef FCE_TEST_MAIN