+
+/*!
+ * Tokenize IP(4/6) addresses with an optional port into address and port
+ *
+ * @param ipurl (r) IP URL string
+ * @param address (w) IP address
+ * @param port (w) IP port
+ *
+ * @returns 0 on success, -1 on failure
+ *
+ * Tokenize IPv4, IPv4:port, IPv6, [IPv6] or [IPv6:port] URL into address and
+ * port and return two allocated strings with the address and the port.
+ *
+ * If the function returns 0, then address point to a newly allocated
+ * valid address string, port may either be NULL or point to a newly
+ * allocated port number.
+ *
+ * If the function returns -1, then the contents of address and port are
+ * undefined.
+ */
+int tokenize_ip_port(const char *ipurl, char **address, char **port)
+{
+ EC_INIT;
+ char *p = NULL;
+ char *s;
+
+ AFP_ASSERT(ipurl && address && port);
+ EC_NULL( p = strdup(ipurl));
+
+ /* Either ipv4, ipv4:port, ipv6, [ipv6] or [ipv6]:port */
+
+ if (!strchr(p, ':')) {
+ /* IPv4 address without port */
+ *address = p;
+ p = NULL; /* prevent free() */
+ *port = NULL;
+ EC_EXIT_STATUS(0);
+ }
+
+ /* Either ipv4:port, ipv6, [ipv6] or [ipv6]:port */
+
+ if (strchr(p, '.')) {
+ /* ipv4:port */
+ *address = p;
+ p = strchr(p, ':');
+ *p = '\0';
+ EC_NULL( *port = strdup(p + 1));
+ p = NULL; /* prevent free() */
+ EC_EXIT_STATUS(0);
+ }
+
+ /* Either ipv6, [ipv6] or [ipv6]:port */
+
+ if (p[0] != '[') {
+ /* ipv6 */
+ *address = p;
+ p = NULL; /* prevent free() */
+ *port = NULL;
+ EC_EXIT_STATUS(0);
+ }
+
+ /* [ipv6] or [ipv6]:port */
+
+ EC_NULL( *address = strdup(p + 1) );
+
+ if ((s = strchr(*address, ']')) == NULL) {
+ LOG(log_error, logtype_dsi, "tokenize_ip_port: malformed ipv6 address %s\n", ipurl);
+ EC_FAIL;
+ }
+ *s = '\0';
+ /* address now points to the ipv6 address without [] */
+
+ if (s[1] == ':') {
+ /* [ipv6]:port */
+ EC_NULL( *port = strdup(s + 2) );
+ } else {
+ /* [ipv6] */
+ *port = NULL;
+ }
+
+EC_CLEANUP:
+ if (p)
+ free(p);
+ EC_EXIT;
+}
+
+/**
+ * Allocate and initialize atalk socket event struct
+ **/
+struct asev *asev_init(int max)
+{
+ struct asev *asev = calloc(1, sizeof(struct asev));
+
+ if (asev == NULL) {
+ return NULL;
+ }
+
+ /* Initialize with space for all possibly active fds */
+ asev->fdset = calloc(max, sizeof(struct pollfd));
+ asev->data = calloc(max, sizeof(struct asev_data));
+
+ if (asev->fdset == NULL || asev->data == NULL) {
+ free(asev->fdset);
+ free(asev->data);
+ free(asev);
+ return NULL;
+ }
+
+ asev->max = max;
+ asev->used = 0;
+
+ return asev;
+}
+
+/**
+ * Add a fd to a dynamic pollfd array and associated data array
+ *
+ * This uses an additional array of struct polldata which stores type
+ * information (enum fdtype) and a pointer to anciliary user data.
+ **/
+bool asev_add_fd(struct asev *asev,
+ int fd,
+ enum asev_fdtype fdtype,
+ void *private)
+{
+ if (asev == NULL) {
+ return false;
+ }
+
+ if (!(asev->used < asev->max)) {
+ return false;
+ }
+
+ asev->fdset[asev->used].fd = fd;
+ asev->fdset[asev->used].events = POLLIN;
+ asev->data[asev->used].fdtype = fdtype;
+ asev->data[asev->used].private = private;
+ asev->used++;
+
+ return true;
+}
+
+/**
+ * Remove fd from asev
+ *
+ * @returns true if the fd was deleted, otherwise false
+ **/
+bool asev_del_fd(struct asev *asev, int fd)
+{
+ int i;
+ int numafter;
+
+ if (asev == NULL) {
+ return false;
+ }
+
+ if (asev->used == 0) {
+ LOG(log_error, logtype_cnid, "asev_del_fd: empty");
+ return false;
+ }
+
+ for (i = 0; i < asev->used; i++) {
+ /*
+ * Scan the array for a matching fd
+ */
+ if (asev->fdset[i].fd == fd) {
+ /*
+ * found fd
+ */
+ if ((i + 1) == asev->used) {
+ /*
+ * it's the last (or only) array element, simply null it
+ */
+ asev->fdset[i].fd = -1;
+ asev->data[i].fdtype = 0;
+ asev->data[i].private = NULL;
+ } else {
+ /*
+ * Move down by one all subsequent elements
+ */
+ numafter = asev->used - (i + 1);
+ memmove(&asev->fdset[i], &asev->fdset[i+1],
+ numafter * sizeof(struct pollfd));
+ memmove(&asev->data[i], &asev->data[i+1],
+ numafter * sizeof(struct asev_data));
+ }
+ asev->used--;
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/* Length of the space taken up by a padded control message of length len */
+#ifndef CMSG_SPACE
+#define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
+#endif
+
+/*
+ * Receive a fd on a suitable socket
+ * @args fd (r) PF_UNIX socket to receive on
+ * @args nonblocking (r) 0: fd is in blocking mode - 1: fd is nonblocking, poll for 1 sec
+ * @returns fd on success, -1 on error
+ */
+int recv_fd(int fd, int nonblocking)
+{
+ int ret;
+ struct msghdr msgh;
+ struct iovec iov[1];
+ struct cmsghdr *cmsgp = NULL;
+ char buf[CMSG_SPACE(sizeof(int))];
+ char dbuf[80];
+ struct pollfd pollfds[1];
+
+ pollfds[0].fd = fd;
+ pollfds[0].events = POLLIN;
+
+ memset(&msgh,0,sizeof(msgh));
+ memset(buf,0,sizeof(buf));
+
+ msgh.msg_name = NULL;
+ msgh.msg_namelen = 0;
+
+ msgh.msg_iov = iov;
+ msgh.msg_iovlen = 1;
+
+ iov[0].iov_base = dbuf;
+ iov[0].iov_len = sizeof(dbuf);
+
+ msgh.msg_control = buf;
+ msgh.msg_controllen = sizeof(buf);
+
+ if (nonblocking) {
+ do {
+ ret = poll(pollfds, 1, 2000); /* poll 2 seconds, evtl. multipe times (EINTR) */
+ } while ( ret == -1 && errno == EINTR );
+ if (ret != 1)
+ return -1;
+ ret = recvmsg(fd, &msgh, 0);
+ } else {
+ do {
+ ret = recvmsg(fd, &msgh, 0);
+ } while ( ret == -1 && errno == EINTR );
+ }
+
+ if ( ret == -1 ) {
+ return -1;
+ }
+
+ for ( cmsgp = CMSG_FIRSTHDR(&msgh); cmsgp != NULL; cmsgp = CMSG_NXTHDR(&msgh,cmsgp) ) {
+ if ( cmsgp->cmsg_level == SOL_SOCKET && cmsgp->cmsg_type == SCM_RIGHTS ) {
+ return *(int *) CMSG_DATA(cmsgp);
+ }
+ }
+
+ if ( ret == sizeof (int) )
+ errno = *(int *)dbuf; /* Rcvd errno */
+ else
+ errno = ENOENT; /* Default errno */
+
+ return -1;
+}
+
+/*
+ * Send a fd across a suitable socket
+ */
+int send_fd(int socket, int fd)
+{
+ int ret;
+ struct msghdr msgh;
+ struct iovec iov[1];
+ struct cmsghdr *cmsgp = NULL;
+ char *buf;
+ size_t size;
+ int er=0;
+
+ size = CMSG_SPACE(sizeof fd);
+ buf = malloc(size);
+ if (!buf) {
+ LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
+ return -1;
+ }
+
+ memset(&msgh,0,sizeof (msgh));
+ memset(buf,0, size);
+
+ msgh.msg_name = NULL;
+ msgh.msg_namelen = 0;
+
+ msgh.msg_iov = iov;
+ msgh.msg_iovlen = 1;
+
+ iov[0].iov_base = &er;
+ iov[0].iov_len = sizeof(er);
+
+ msgh.msg_control = buf;
+ msgh.msg_controllen = size;
+
+ cmsgp = CMSG_FIRSTHDR(&msgh);
+ cmsgp->cmsg_level = SOL_SOCKET;
+ cmsgp->cmsg_type = SCM_RIGHTS;
+ cmsgp->cmsg_len = CMSG_LEN(sizeof(fd));
+
+ *((int *)CMSG_DATA(cmsgp)) = fd;
+ msgh.msg_controllen = cmsgp->cmsg_len;
+
+ do {
+ ret = sendmsg(socket,&msgh, 0);
+ } while ( ret == -1 && errno == EINTR );
+ if (ret == -1) {
+ LOG(log_error, logtype_cnid, "error in sendmsg: %s", strerror(errno));
+ free(buf);
+ return -1;
+ }
+ free(buf);
+ return 0;
+}