X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=etc%2Fafpd%2Ffce_api.c;h=8df502b6e7b77351f8d778fd5358c5b016f2203e;hb=8d863e719b3670e3e9db3001da875d085f4d93f1;hp=6443bf302c6c9484b0dbde85e8c2be1a12081f1d;hpb=33f44ea7f93b30678659d7e50b23c5056991dfd0;p=netatalk.git diff --git a/etc/afpd/fce_api.c b/etc/afpd/fce_api.c index 6443bf30..8df502b6 100755 --- a/etc/afpd/fce_api.c +++ b/etc/afpd/fce_api.c @@ -1,6 +1,4 @@ /* - * $Id: fce_api.c,v 0.01 2010-10-01 00:00:0 mw Exp $ - * * Copyright (c) 2010 Mark Williams * * File change event API for netatalk @@ -48,6 +46,7 @@ #include #include #include +#include #include "fork.h" #include "file.h" @@ -56,8 +55,6 @@ #include "desktop.h" #include "volume.h" -#include "fce_api.h" - // ONLY USED IN THIS FILE #include "fce_api_internal.h" @@ -84,48 +81,50 @@ 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); + memcpy(&udp_entry->addrinfo, p, sizeof(struct addrinfo)); + freeaddrinfo(servinfo); } - udp_initialized = FCE_TRUE; + udp_initialized = FCE_TRUE; } + void fce_cleanup() { if (udp_initialized == FCE_FALSE ) @@ -221,13 +220,23 @@ 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 ); - sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) ); + sendto(udp_entry->sock, + data, + data_len, + 0, + udp_entry->addrinfo.ai_addr, + udp_entry->addrinfo.ai_addrlen); /* Rebuild our original data packet */ data_len = build_fce_packet( &packet, path, mode, event_id ); } - sent_data = sendto( udp_entry->sock, data, data_len, 0, &udp_entry->addr, sizeof(udp_entry->addr) ); + sent_data = sendto(udp_entry->sock, + data, + data_len, + 0, + udp_entry->addrinfo.ai_addr, + udp_entry->addrinfo.ai_addrlen); /* Problems ? */ if (sent_data != data_len) @@ -243,21 +252,20 @@ 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 sockaddr_in) ); udp_socket_list[udp_sockets].next_try_on_error = 0; udp_sockets++; @@ -272,6 +280,10 @@ static int add_udp_socket( char *target_ip, int target_port ) * */ static int register_fce( char *u_name, int is_dir, int mode ) { + if (udp_sockets == 0) + /* No listeners configured */ + return AFP_OK; + if (u_name == NULL) return AFPERR_PARAM; @@ -406,17 +418,15 @@ int fce_register_new_file( struct path *path ) int fce_register_file_modification( struct ofork *ofork ) { char *u_name = NULL; - struct dir *dir; struct vol *vol; int ret = AFP_OK; - if (ofork == NULL || ofork->of_vol == NULL || ofork->of_dir == NULL) + if (ofork == NULL || ofork->of_vol == NULL) return AFPERR_PARAM; vol = ofork->of_vol; - dir = ofork->of_dir; - if (NULL == (u_name = mtoupath(vol, of_name(ofork), dir->d_did, utf8_encoding()))) + if (NULL == (u_name = mtoupath(vol, of_name(ofork), ofork->of_did, utf8_encoding()))) { return AFPERR_MISC; } @@ -432,19 +442,19 @@ 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); } @@ -458,64 +468,49 @@ void shortsleep( unsigned int us ) } int main( int argc, char*argv[] ) { - int port = 11250; - char *host = NULL; + int c,ret; + + char *port = FCE_DEFAULT_PORT_STRING; + char *host = "localhost"; int delay_between_events = 1000; int event_code = FCE_FILE_MODIFY; char pathbuff[1024]; int duration_in_seconds = 0; // TILL ETERNITY - + char target[256]; char *path = getcwd( pathbuff, sizeof(pathbuff) ); // FULLSPEED TEST IS "-s 1001" -> delay is 0 -> send packets without pause - if (argc == 1) - { - 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]); - exit( 1 ); - } - int ret = AFP_OK; - - for (int i = 1; i < argc; i++) - { - char *p = argv[i]; - if (*p == '-' && p[1]) - { - char *arg = argv[i + 1]; - switch (p[1]) - { - case 'p': if (arg) port = atoi( arg ), i++; break; - case 'P': if (arg) path = arg, i++; break; - case 's': if (arg) delay_between_events = atoi( arg ), i++; break; - case 'e': if (arg) event_code = atoi( arg ), i++; break; - case 'd': if (arg) duration_in_seconds = atoi( arg ), i++; break; - case 'h': - { - if (arg) - { - host = arg; - char target[256]; - sprintf( target, "%s:%d", host, port ); - ret += fce_add_udp_socket( target ); - i++; - } - break; - } - } + while ((c = getopt(argc, argv, "d:e:h:p:P:s:")) != -1) { + switch(c) { + case '?': + 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]); + exit(1); + break; + case 'd': + duration_in_seconds = atoi(optarg); + break; + case 'e': + event_code = atoi(optarg); + break; + case 'h': + host = strdup(optarg); + break; + case 'p': + port = strdup(optarg); + break; + case 'P': + path = strdup(optarg); + break; + case 's': + delay_between_events = atoi(optarg); + break; } } - - - if (host == NULL) - { - char target[256]; - sprintf( target, "127.0.0.1:%d", port ); - ret += fce_add_udp_socket( target ); - } - - if (ret) - return ret; + sprintf(target, "%s:%s", host, port); + if (fce_add_udp_socket(target) != 0) + return 1; int ev_cnt = 0; time_t start_time = time(NULL); @@ -536,7 +531,7 @@ int main( int argc, char*argv[] ) if (end_time && now >= end_time) break; - register_fce( path, event_code ); + register_fce( path, 0, event_code ); ev_cnt++;