2 * Copyright (c) 2010 Mark Williams
\r
4 * File change event API for netatalk
\r
6 * for every detected filesystem change a UDP packet is sent to an arbitrary list
\r
7 * of listeners. Each packet contains unix path of modified filesystem element,
\r
8 * event reason, and a consecutive event id (32 bit). Technically we are UDP client and are sending
\r
9 * out packets synchronuosly as they are created by the afp functions. This should not affect
\r
10 * performance measurably. The only delaying calls occur during initialization, if we have to
\r
11 * resolve non-IP hostnames to IP. All numeric data inside the packet is network byte order, so use
\r
12 * ntohs / ntohl to resolve length and event id. Ideally a listener receives every packet with
\r
13 * no gaps in event ids, starting with event id 1 and mode FCE_CONN_START followed by
\r
14 * data events from id 2 up to 0xFFFFFFFF, followed by 0 to 0xFFFFFFFF and so on.
\r
16 * A gap or not starting with 1 mode FCE_CONN_START or receiving mode FCE_CONN_BROKEN means that
\r
17 * the listener has lost at least one filesystem event
\r
19 * All Rights Reserved. See COPYRIGHT.
\r
22 #ifdef HAVE_CONFIG_H
\r
24 #endif /* HAVE_CONFIG_H */
\r
34 #include <sys/param.h>
\r
35 #include <sys/socket.h>
\r
36 #include <netinet/in.h>
\r
37 #include <arpa/inet.h>
\r
40 #include <netatalk/at.h>
\r
42 #include <atalk/adouble.h>
\r
43 #include <atalk/vfs.h>
\r
44 #include <atalk/logger.h>
\r
45 #include <atalk/afp.h>
\r
46 #include <atalk/util.h>
\r
47 #include <atalk/cnid.h>
\r
48 #include <atalk/unix.h>
\r
52 #include "globals.h"
\r
53 #include "directory.h"
\r
54 #include "desktop.h"
\r
57 #include "fce_api.h"
\r
59 // ONLY USED IN THIS FILE
\r
60 #include "fce_api_internal.h"
\r
65 /* We store our connection data here */
\r
66 static struct udp_entry udp_socket_list[FCE_MAX_UDP_SOCKS];
\r
67 static int udp_sockets = 0;
\r
68 static int udp_initialized = FCE_FALSE;
\r
71 static const char *skip_files[] =
\r
79 * Initialize network structs for any listeners
\r
80 * We dont give return code because all errors are handled internally (I hope..)
\r
85 if (udp_initialized == FCE_TRUE)
\r
89 for (int i = 0; i < udp_sockets; i++)
\r
91 struct udp_entry *udp_entry = udp_socket_list + i;
\r
93 /* Close any pending sockets */
\r
94 if (udp_entry->sock != -1)
\r
96 close( udp_entry->sock );
\r
99 /* resolve IP to network address */
\r
100 if (inet_aton( udp_entry->ip, &udp_entry->addr.sin_addr ) ==0 )
\r
102 /* Hmm, failed try to resolve host */
\r
103 struct hostent *hp = gethostbyname( udp_entry->ip );
\r
106 LOG(log_error, logtype_afpd, "Cannot resolve host name for fce UDP connection: %s (errno %d)", udp_entry->ip, errno );
\r
109 memcpy( &udp_entry->addr.sin_addr, &hp->h_addr, sizeof(udp_entry->addr.sin_addr) );
\r
112 /* Create UDP socket */
\r
113 udp_entry->sock = socket( AF_INET, SOCK_DGRAM, 0 );
\r
114 if (udp_entry->sock == -1)
\r
116 LOG(log_error, logtype_afpd, "Cannot create socket for fce UDP connection: errno %d", errno );
\r
120 /* Set socket address params */
\r
121 udp_entry->addr.sin_family = AF_INET;
\r
122 udp_entry->addr.sin_port = htons(udp_entry->port);
\r
124 udp_initialized = FCE_TRUE;
\r
129 if (udp_initialized == FCE_FALSE )
\r
132 for (int i = 0; i < udp_sockets; i++)
\r
134 struct udp_entry *udp_entry = udp_socket_list + i;
\r
136 /* Close any pending sockets */
\r
137 if (udp_entry->sock != -1)
\r
139 close( udp_entry->sock );
\r
140 udp_entry->sock = -1;
\r
143 udp_initialized = FCE_FALSE;
\r
148 * Construct a UDP packet for our listeners and return packet size
\r
150 static unsigned short build_fce_packet( struct fce_packet *packet, char *path, int mode, uint32_t event_id )
\r
152 unsigned short data_len = 0;
\r
154 strncpy(packet->magic, FCE_PACKET_MAGIC, sizeof(packet->magic) );
\r
155 packet->version = FCE_PACKET_VERSION;
\r
156 packet->mode = mode;
\r
158 data_len = strlen( path );
\r
160 /* This should never happen, but before we bust this server, we send nonsense, fce listener has to cope */
\r
161 if (data_len >= FCE_MAX_PATH_LEN)
\r
163 data_len = FCE_MAX_PATH_LEN - 1;
\r
166 /* This is the payload len. Means: the stream has len bytes more until packet is finished */
\r
167 /* A server should read the first 16 byte, decode them and then fetch the rest */
\r
168 packet->len = htons( data_len);
\r
169 packet->event_id = htonl( event_id );
\r
171 strncpy( packet->data, path, data_len );
\r
173 /* return the packet len */
\r
174 return sizeof(struct fce_packet) - FCE_MAX_PATH_LEN + data_len;
\r
178 * Send the fce information to all (connected) listeners
\r
179 * We dont give return code because all errors are handled internally (I hope..)
\r
181 static void send_fce_event( char *path, int mode )
\r
183 struct fce_packet packet;
\r
184 void *data = &packet;
\r
185 static uint32_t event_id = 0; /* the unique packet couter to detect packet/data loss. Going from 0xFFFFFFFF to 0x0 is a valid increment */
\r
187 time_t now = time(NULL);
\r
189 /* build our data packet */
\r
190 int data_len = build_fce_packet( &packet, path, mode, ++event_id );
\r
193 for (int i = 0; i < udp_sockets; i++)
\r
196 struct udp_entry *udp_entry = udp_socket_list + i;
\r
198 /* we had a problem earlier ? */
\r
199 if (udp_entry->sock == -1)
\r
201 /* We still have to wait ?*/
\r
202 if (now < udp_entry->next_try_on_error)
\r
205 /* Reopen socket */
\r
206 udp_entry->sock = socket( AF_INET, SOCK_DGRAM, 0 );
\r
208 if (udp_entry->sock == -1)
\r
210 /* failed again, so go to rest again */
\r
211 LOG(log_error, logtype_afpd, "Cannot recreate socket for fce UDP connection: errno %d", errno );
\r
213 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
\r
217 udp_entry->next_try_on_error = 0;
\r
219 /* Okay, we have a running socket again, send server that we had a problem on our side*/
\r
220 data_len = build_fce_packet( &packet, "", FCE_CONN_BROKEN, 0 );
\r
222 sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) );
\r
224 /* Rebuild our original data packet */
\r
225 data_len = build_fce_packet( &packet, path, mode, event_id );
\r
228 sent_data = sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) );
\r
231 if (sent_data != data_len)
\r
233 /* Argh, socket broke, we close and retry later */
\r
234 LOG(log_error, logtype_afpd, "Error while sending packet to %s for fce UDP connection: transfered: %d of %d errno %d",
\r
235 udp_entry->port, sent_data, data_len, errno );
\r
237 close( udp_entry->sock );
\r
238 udp_entry->sock = -1;
\r
239 udp_entry->next_try_on_error = now + FCE_SOCKET_RETRY_DELAY_S;
\r
244 static int add_udp_socket( char *target_ip, int target_port )
\r
246 if (target_port == 0)
\r
247 target_port = FCE_DEFAULT_PORT;
\r
249 if (udp_sockets >= FCE_MAX_UDP_SOCKS)
\r
251 LOG(log_error, logtype_afpd, "Too many file change api UDP connections (max %d allowed)", FCE_MAX_UDP_SOCKS );
\r
252 return AFPERR_PARAM;
\r
255 strncpy( udp_socket_list[udp_sockets].ip, target_ip, FCE_MAX_IP_LEN - 1);
\r
256 udp_socket_list[udp_sockets].port = target_port;
\r
257 udp_socket_list[udp_sockets].sock = -1;
\r
258 memset( &udp_socket_list[udp_sockets].addr, 0, sizeof(struct sockaddr_in) );
\r
259 udp_socket_list[udp_sockets].next_try_on_error = 0;
\r
268 * Dispatcher for all incoming file change events
\r
271 static int register_fce( char *u_name, int is_dir, int mode )
\r
273 if (u_name == NULL)
\r
274 return AFPERR_PARAM;
\r
276 static int first_event = FCE_TRUE;
\r
278 /* do some initialization on the fly the first time */
\r
281 fce_initialize_history();
\r
285 /* handle files which should not cause events (.DS_Store atc. ) */
\r
286 for (int i = 0; skip_files[i] != NULL; i++)
\r
288 if (!strcmp( u_name, skip_files[i]))
\r
293 char full_path_buffer[FCE_MAX_PATH_LEN + 1] = {""};
\r
294 const char *cwd = getcwdpath();
\r
296 if (!is_dir || mode == FCE_DIR_DELETE)
\r
298 if (strlen( cwd ) + strlen( u_name) + 1 >= FCE_MAX_PATH_LEN)
\r
300 LOG(log_error, logtype_afpd, "FCE file name too long: %s/%s", cwd, u_name );
\r
301 return AFPERR_PARAM;
\r
303 sprintf( full_path_buffer, "%s/%s", cwd, u_name );
\r
307 if (strlen( cwd ) >= FCE_MAX_PATH_LEN)
\r
309 LOG(log_error, logtype_afpd, "FCE directory name too long: %s", cwd);
\r
310 return AFPERR_PARAM;
\r
312 strcpy( full_path_buffer, cwd);
\r
315 /* Can we ignore this event based on type or history? */
\r
316 if (fce_handle_coalescation( full_path_buffer, is_dir, mode ))
\r
318 LOG(log_debug9, logtype_afpd, "Coalesced fc event <%d> for <%s>", mode, full_path_buffer );
\r
322 LOG(log_debug9, logtype_afpd, "Detected fc event <%d> for <%s>", mode, full_path_buffer );
\r
325 /* we do initilization on the fly, no blocking calls in here
\r
326 * (except when using FQDN in broken DNS environment)
\r
328 if (first_event == FCE_TRUE)
\r
332 /* Notify listeners the we start from the beginning */
\r
333 send_fce_event( "", FCE_CONN_START );
\r
335 first_event = FCE_FALSE;
\r
338 /* Handle UDP transport */
\r
339 send_fce_event( full_path_buffer, mode );
\r
345 /******************** External calls start here **************************/
\r
348 * API-Calls for file change api, called form outside (file.c directory.c ofork.c filedir.c)
\r
350 #ifndef FCE_TEST_MAIN
\r
353 int fce_register_delete_file( struct path *path )
\r
358 return AFPERR_PARAM;
\r
361 ret = register_fce( path->u_name, FALSE, FCE_FILE_DELETE );
\r
365 int fce_register_delete_dir( char *name )
\r
370 return AFPERR_PARAM;
\r
373 ret = register_fce( name, TRUE, FCE_DIR_DELETE);
\r
378 int fce_register_new_dir( struct path *path )
\r
383 return AFPERR_PARAM;
\r
385 ret = register_fce( path->u_name, TRUE, FCE_DIR_CREATE );
\r
391 int fce_register_new_file( struct path *path )
\r
396 return AFPERR_PARAM;
\r
398 ret = register_fce( path->u_name, FALSE, FCE_FILE_CREATE );
\r
404 int fce_register_file_modification( struct ofork *ofork )
\r
406 char *u_name = NULL;
\r
410 if (ofork == NULL || ofork->of_vol == NULL)
\r
411 return AFPERR_PARAM;
\r
413 vol = ofork->of_vol;
\r
415 if (NULL == (u_name = mtoupath(vol, of_name(ofork), ofork->of_did, utf8_encoding())))
\r
417 return AFPERR_MISC;
\r
420 ret = register_fce( u_name, FALSE, FCE_FILE_MODIFY );
\r
428 * Extern connect to afpd parameter, can be called multiple times for multiple listeners (up to MAX_UDP_SOCKS times)
\r
431 int fce_add_udp_socket( char *target )
\r
433 int port = FCE_DEFAULT_PORT;
\r
434 char target_ip[256] = {""};
\r
436 strncpy( target_ip, target, sizeof(target_ip) -1);
\r
437 char *port_delim = strchr( target_ip, ':' );
\r
441 port = atoi( port_delim + 1);
\r
443 return add_udp_socket( target_ip, port );
\r
448 #ifdef FCE_TEST_MAIN
\r
451 void shortsleep( unsigned int us )
\r
455 int main( int argc, char*argv[] )
\r
459 int delay_between_events = 1000;
\r
460 int event_code = FCE_FILE_MODIFY;
\r
461 char pathbuff[1024];
\r
462 int duration_in_seconds = 0; // TILL ETERNITY
\r
464 char *path = getcwd( pathbuff, sizeof(pathbuff) );
\r
466 // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause
\r
470 fprintf( stdout, "%s: -p Port -h Listener1 [ -h Listener2 ...] -P path -s Delay_between_events_in_us -e event_code -d Duration \n", argv[0]);
\r
475 for (int i = 1; i < argc; i++)
\r
478 if (*p == '-' && p[1])
\r
480 char *arg = argv[i + 1];
\r
483 case 'p': if (arg) port = atoi( arg ), i++; break;
\r
484 case 'P': if (arg) path = arg, i++; break;
\r
485 case 's': if (arg) delay_between_events = atoi( arg ), i++; break;
\r
486 case 'e': if (arg) event_code = atoi( arg ), i++; break;
\r
487 case 'd': if (arg) duration_in_seconds = atoi( arg ), i++; break;
\r
494 sprintf( target, "%s:%d", host, port );
\r
495 ret += fce_add_udp_socket( target );
\r
508 sprintf( target, "127.0.0.1:%d", port );
\r
509 ret += fce_add_udp_socket( target );
\r
517 time_t start_time = time(NULL);
\r
518 time_t end_time = 0;
\r
520 if (duration_in_seconds)
\r
521 end_time = start_time + duration_in_seconds;
\r
525 time_t now = time(NULL);
\r
526 if (now > start_time)
\r
529 fprintf( stdout, "%d events/s\n", ev_cnt );
\r
532 if (end_time && now >= end_time)
\r
535 register_fce( path, 0, event_code );
\r
539 shortsleep( delay_between_events );
\r
542 #endif /* TESTMAIN*/
\r