From 09fc36d97b784e8d8d66dbfe8c60e9127c30826a Mon Sep 17 00:00:00 2001 From: Frank Lahm Date: Mon, 30 May 2011 13:32:01 +0200 Subject: [PATCH] Add fce simple test listener, use getopt in main in fce_api --- bin/misc/.gitignore | 1 + bin/misc/Makefile.am | 6 +- bin/misc/fce.c | 91 +++++++++++++++++++++++++++ etc/afpd/Makefile.am | 2 +- etc/afpd/afp_options.c | 2 +- etc/afpd/directory.c | 2 +- etc/afpd/fce_api.c | 82 ++++++++++-------------- etc/afpd/fce_api_internal.h | 8 +-- etc/afpd/fce_util.c | 3 +- etc/afpd/file.c | 2 +- etc/afpd/filedir.c | 2 +- etc/afpd/ofork.c | 2 +- include/atalk/Makefile.am | 2 +- {etc/afpd => include/atalk}/fce_api.h | 1 + 14 files changed, 142 insertions(+), 64 deletions(-) create mode 100644 bin/misc/fce.c rename {etc/afpd => include/atalk}/fce_api.h (93%) diff --git a/bin/misc/.gitignore b/bin/misc/.gitignore index 2bff10ed..84dddb39 100644 --- a/bin/misc/.gitignore +++ b/bin/misc/.gitignore @@ -2,6 +2,7 @@ Makefile Makefile.in netacnv afpldaptest +fce logger_test .deps .libs diff --git a/bin/misc/Makefile.am b/bin/misc/Makefile.am index 04a53e23..426dc317 100644 --- a/bin/misc/Makefile.am +++ b/bin/misc/Makefile.am @@ -3,7 +3,7 @@ pkgconfdir = @PKGCONFDIR@ bin_PROGRAMS = -noinst_PROGRAMS = netacnv logger_test +noinst_PROGRAMS = netacnv logger_test fce netacnv_SOURCES = netacnv.c netacnv_LDADD = $(top_builddir)/libatalk/libatalk.la @@ -11,6 +11,10 @@ netacnv_LDADD = $(top_builddir)/libatalk/libatalk.la logger_test_SOURCES = logger_test.c logger_test_LDADD = $(top_builddir)/libatalk/libatalk.la +fce_SOOURCE = fce.c +fce_LDADD = $(top_builddir)/libatalk/libatalk.la +fce_CFLAGS = -I$(top_srcdir)/include + bin_PROGRAMS += afpldaptest afpldaptest_SOURCES = uuidtest.c afpldaptest_CFLAGS = -D_PATH_ACL_LDAPCONF=\"$(pkgconfdir)/afp_ldap.conf\" diff --git a/bin/misc/fce.c b/bin/misc/fce.c new file mode 100644 index 00000000..863665b4 --- /dev/null +++ b/bin/misc/fce.c @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define MAXBUFLEN 100 + +// get sockaddr, IPv4 or IPv6: +void *get_in_addr(struct sockaddr *sa) +{ + if (sa->sa_family == AF_INET) { + return &(((struct sockaddr_in*)sa)->sin_addr); + } + + return &(((struct sockaddr_in6*)sa)->sin6_addr); +} + +int main(void) +{ + int sockfd; + struct addrinfo hints, *servinfo, *p; + int rv; + int numbytes; + struct sockaddr_storage their_addr; + char buf[MAXBUFLEN]; + socklen_t addr_len; + char s[INET6_ADDRSTRLEN]; + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4 + hints.ai_socktype = SOCK_DGRAM; +// hints.ai_flags = AI_PASSIVE; // use my IP + + if ((rv = getaddrinfo(NULL, FCE_DEFAULT_PORT_STRING, &hints, &servinfo)) != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + return 1; + } + + // loop through all the results and bind to the first we can + for(p = servinfo; p != NULL; p = p->ai_next) { + if ((sockfd = socket(p->ai_family, p->ai_socktype, + p->ai_protocol)) == -1) { + perror("listener: socket"); + continue; + } + + if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { + close(sockfd); + perror("listener: bind"); + continue; + } + + break; + } + + if (p == NULL) { + fprintf(stderr, "listener: failed to bind socket\n"); + return 2; + } + + freeaddrinfo(servinfo); + + printf("listener: waiting to recvfrom...\n"); + + addr_len = sizeof their_addr; + if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, + (struct sockaddr *)&their_addr, &addr_len)) == -1) { + perror("recvfrom"); + exit(1); + } + + printf("listener: got packet from %s\n", + inet_ntop(their_addr.ss_family, + get_in_addr((struct sockaddr *)&their_addr), + s, sizeof s)); + printf("listener: packet is %d bytes long\n", numbytes); + buf[numbytes] = '\0'; + printf("listener: packet contains \"%s\"\n", buf); + + close(sockfd); + + return 0; +} diff --git a/etc/afpd/Makefile.am b/etc/afpd/Makefile.am index e1389dbc..35580d93 100644 --- a/etc/afpd/Makefile.am +++ b/etc/afpd/Makefile.am @@ -70,7 +70,7 @@ afpd_SOURCES += acls.c endif -noinst_HEADERS = auth.h afp_config.h desktop.h directory.h fce_api.h fce_api_internal.h file.h \ +noinst_HEADERS = auth.h afp_config.h desktop.h directory.h fce_api_internal.h file.h \ filedir.h fork.h globals.h icon.h mangle.h misc.h status.h switch.h \ uam_auth.h uid.h unix.h volume.h hash.h acls.h acl_mappings.h extattrs.h \ dircache.h afp_zeroconf.h afp_avahi.h diff --git a/etc/afpd/afp_options.c b/etc/afpd/afp_options.c index 6fff02fd..7a2d405f 100644 --- a/etc/afpd/afp_options.c +++ b/etc/afpd/afp_options.c @@ -34,12 +34,12 @@ #include #include #include +#include #include "globals.h" #include "status.h" #include "auth.h" #include "dircache.h" -#include "fce_api.h" #ifndef MIN #define MIN(a, b) ((a) < (b) ? (a) : (b)) diff --git a/etc/afpd/directory.c b/etc/afpd/directory.c index 06ad94f3..7b258491 100644 --- a/etc/afpd/directory.c +++ b/etc/afpd/directory.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "directory.h" #include "dircache.h" @@ -41,7 +42,6 @@ #include "unix.h" #include "mangle.h" #include "hash.h" -#include "fce_api.h" /* * FIXMEs, loose ends after the dircache rewrite: diff --git a/etc/afpd/fce_api.c b/etc/afpd/fce_api.c index 37f06732..601f02e2 100755 --- a/etc/afpd/fce_api.c +++ b/etc/afpd/fce_api.c @@ -46,6 +46,7 @@ #include #include #include +#include #include "fork.h" #include "file.h" @@ -54,8 +55,6 @@ #include "desktop.h" #include "volume.h" -#include "fce_api.h" - // ONLY USED IN THIS FILE #include "fce_api_internal.h" @@ -458,64 +457,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); diff --git a/etc/afpd/fce_api_internal.h b/etc/afpd/fce_api_internal.h index 959ce9ea..bf6cea3e 100755 --- a/etc/afpd/fce_api_internal.h +++ b/etc/afpd/fce_api_internal.h @@ -8,6 +8,7 @@ #ifndef _FCE_API_INTERNAL_H #define _FCE_API_INTERNAL_H +/* fce_packet.mode */ #define FCE_FILE_MODIFY 1 #define FCE_FILE_DELETE 2 #define FCE_DIR_DELETE 3 @@ -16,17 +17,14 @@ #define FCE_CONN_START 42 #define FCE_CONN_BROKEN 99 +/* fce_packet.fce_magic */ +#define FCE_PACKET_MAGIC "at_fcapi" #define FCE_MAX_PATH_LEN 1024 - #define FCE_MAX_UDP_SOCKS 5 /* Allow a maximum of udp listeners for file change events */ #define FCE_MAX_IP_LEN 255 /* Man len of listener name */ #define FCE_SOCKET_RETRY_DELAY_S 600 /* Pause this time in s after socket was broken */ #define FCE_PACKET_VERSION 1 - - -#define FCE_PACKET_MAGIC "at_fcapi" /* Must fit to size of fce_packet.fce_magic */ - #define FCE_HISTORY_LEN 10 /* This is used to coalesce events */ #define MAX_COALESCE_TIME_MS 1000 /* Events oldeer than this are not coalesced */ diff --git a/etc/afpd/fce_util.c b/etc/afpd/fce_util.c index 8a89e244..c1377ee6 100755 --- a/etc/afpd/fce_util.c +++ b/etc/afpd/fce_util.c @@ -48,6 +48,7 @@ #include #include #include +#include #include "fork.h" #include "file.h" @@ -56,8 +57,6 @@ #include "desktop.h" #include "volume.h" -#include "fce_api.h" - // ONLY USED IN THIS FILE #include "fce_api_internal.h" diff --git a/etc/afpd/file.c b/etc/afpd/file.c index 8257d18b..c165eb93 100644 --- a/etc/afpd/file.c +++ b/etc/afpd/file.c @@ -37,6 +37,7 @@ char *strchr (), *strrchr (); #include #include #include +#include #include "directory.h" #include "dircache.h" @@ -47,7 +48,6 @@ char *strchr (), *strrchr (); #include "filedir.h" #include "globals.h" #include "unix.h" -#include "fce_api.h" /* the format for the finderinfo fields (from IM: Toolbox Essentials): * field bytes subfield bytes diff --git a/etc/afpd/filedir.c b/etc/afpd/filedir.c index 097ba114..5f6e0f64 100644 --- a/etc/afpd/filedir.c +++ b/etc/afpd/filedir.c @@ -40,6 +40,7 @@ char *strchr (), *strrchr (); #include #include #include +#include #include "directory.h" #include "dircache.h" @@ -50,7 +51,6 @@ char *strchr (), *strrchr (); #include "globals.h" #include "filedir.h" #include "unix.h" -#include "fce_api.h" #ifdef DROPKLUDGE int matchfile2dirperms( diff --git a/etc/afpd/ofork.c b/etc/afpd/ofork.c index ed5f7328..c4901285 100644 --- a/etc/afpd/ofork.c +++ b/etc/afpd/ofork.c @@ -21,12 +21,12 @@ #include #include #include +#include #include "globals.h" #include "volume.h" #include "directory.h" #include "fork.h" -#include "fce_api.h" /* we need to have a hashed list of oforks (by dev inode). just hash * by first letter. */ diff --git a/include/atalk/Makefile.am b/include/atalk/Makefile.am index 2bf09187..80bdc14b 100644 --- a/include/atalk/Makefile.am +++ b/include/atalk/Makefile.am @@ -8,4 +8,4 @@ atalkinclude_HEADERS = \ server_ipc.h tdb.h uam.h unicode.h util.h uuid.h volinfo.h \ zip.h ea.h acl.h unix.h directory.h hash.h volume.h -noinst_HEADERS = cnid_dbd_private.h cnid_private.h bstradd.h bstrlib.h errchk.h ftw.h +noinst_HEADERS = cnid_dbd_private.h cnid_private.h bstradd.h bstrlib.h errchk.h ftw.h fce_api.h diff --git a/etc/afpd/fce_api.h b/include/atalk/fce_api.h similarity index 93% rename from etc/afpd/fce_api.h rename to include/atalk/fce_api.h index cabb9959..a4609332 100755 --- a/etc/afpd/fce_api.h +++ b/include/atalk/fce_api.h @@ -22,6 +22,7 @@ int fce_add_udp_socket( char *target ); // IP or IP:Port int fce_set_coalesce( char *coalesce_opt ); // all|delete|create #define FCE_DEFAULT_PORT 12250 +#define FCE_DEFAULT_PORT_STRING "12250" #endif /* _FCE_API_H */ -- 2.39.2