Beispiel #1
0
static spif_bool_t
spif_url_init_from_ipaddr(spif_url_t self, spif_ipsockaddr_t ipaddr)
{
    spif_uint8_t tries;
    spif_hostinfo_t hinfo;

    ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE);
    spif_str_init(SPIF_STR(self));
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url));
    self->proto = (spif_str_t) NULL;
    self->user = (spif_str_t) NULL;
    self->passwd = (spif_str_t) NULL;
    self->path = (spif_str_t) NULL;
    self->query = (spif_str_t) NULL;

    /* Try up to 3 times to resolve the hostname. */
    h_errno = 0;
    tries = 0;
    do {
        tries++;
        hinfo = gethostbyaddr((const char *) &(ipaddr->sin_addr), sizeof(ipaddr->sin_addr), AF_INET);
    } while ((tries <= 3) && (!hinfo) && (h_errno == TRY_AGAIN));
    if (!hinfo || !hinfo->h_name) {
        spif_charptr_t buff;

        buff = SPIF_CHARPTR(inet_ntoa(ipaddr->sin_addr));
        self->host = spif_str_new_from_ptr(buff);
    } else {
        self->host = spif_str_new_from_ptr(SPIF_CHARPTR(hinfo->h_name));
    }

    self->port = spif_str_new_from_num(ntohs(ipaddr->sin_port));
    return TRUE;
}
Beispiel #2
0
/**
 * Option-type-to-string translator.
 *
 * This function is used internally by spifopt_usage() to convert an
 * option's numerical type to a short string representing that type of
 * option.
 *
 * @param type The numeric option type (SPIFOPT_OPT_FLAGS()).
 * @return     A 6-char-max string describing the option type.
 *
 * @see @link DOXGRP_OPT Command Line Option Parser @endlink
 * @ingroup DOXGRP_OPT
 */
static spif_charptr_t 
get_option_type_string(spif_uint16_t type)
{
    switch (type) {
        case SPIFOPT_FLAG_BOOLEAN: return SPIF_CHARPTR("(bool)"); break;
        case SPIFOPT_FLAG_INTEGER: return SPIF_CHARPTR("(int)"); break;
        case SPIFOPT_FLAG_ARGLIST: return SPIF_CHARPTR("(strs)"); break;
        default: return SPIF_CHARPTR("(str)");
    }
    ASSERT_NOTREACHED_RVAL(NULL);
}
Beispiel #3
0
spif_str_t
spif_socket_show(spif_socket_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_SOCKET_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(socket, name, buff, indent, tmp);
        return buff;
    }
        
    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_socket_t) %s:  %10p {\n",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    indent += 2;
    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockfd_t) fd:  %d\n", self->fd);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockfamily_t) fam:  %d\n", (int) self->fam);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_socktype_t) type:  %d\n", (int) self->type);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockproto_t) proto:  %d\n", (int) self->proto);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockaddr_t) addr:  %10p\n", (spif_ptr_t) self->addr);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_sockaddr_len_t) len:  %lu\n", (unsigned long) self->len);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_uint32_t) flags:  0x%08x\n", (unsigned) self->flags);
    spif_str_append_from_ptr(buff, tmp);

    spif_url_show(self->local_url, SPIF_CHARPTR("local_url"), buff, indent);
    spif_url_show(self->remote_url, SPIF_CHARPTR("remote_url"), buff, indent);

    indent -= 2;
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "}\n");
    spif_str_append_from_ptr(buff, tmp);

    return buff;
}
Beispiel #4
0
Datei: url.c Projekt: Limsik/e17
spif_bool_t
spif_url_unparse(spif_url_t self)
{
    ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE);
    spif_str_done(SPIF_STR(self));
    spif_str_init_from_ptr(SPIF_STR(self), SPIF_CHARPTR(""));

    /* First, proto followed by a colon. */
    if (!SPIF_STR_ISNULL(self->proto)) {
        spif_str_append(SPIF_STR(self), self->proto);
        spif_str_append_char(SPIF_STR(self), ':');
    }

    /* If we have a port but no host, make it localhost. */
    if (!SPIF_STR_ISNULL(self->port) && SPIF_STR_ISNULL(self->host)) {
        self->host = spif_str_new_from_ptr(SPIF_CHARPTR("localhost"));
    }

    /* We need the // if we have a hostname. */
    if (!SPIF_STR_ISNULL(self->host)) {
        spif_str_append_from_ptr(SPIF_STR(self), SPIF_CHARPTR("//"));
    }

    if (!SPIF_STR_ISNULL(self->user)) {
        spif_str_append(SPIF_STR(self), self->user);
        if (!SPIF_STR_ISNULL(self->passwd)) {
            spif_str_append_char(SPIF_STR(self), ':');
            spif_str_append(SPIF_STR(self), self->passwd);
        }
        spif_str_append_char(SPIF_STR(self), '@');
    }

    if (!SPIF_STR_ISNULL(self->host)) {
        spif_str_append(SPIF_STR(self), self->host);
        if (!SPIF_STR_ISNULL(self->port)) {
            spif_str_append_char(SPIF_STR(self), ':');
            spif_str_append(SPIF_STR(self), self->port);
        }
    }

    if (!SPIF_STR_ISNULL(self->path)) {
        spif_str_append(SPIF_STR(self), self->path);
    }

    if (!SPIF_STR_ISNULL(self->query)) {
        spif_str_append_char(SPIF_STR(self), '?');
        spif_str_append(SPIF_STR(self), self->query);
    }
    return TRUE;
}
Beispiel #5
0
spif_charptr_t 
spiftool_join(spif_charptr_t sep, spif_charptr_t *slist)
{
    register unsigned long i;
    size_t len, slen;
    spif_charptr_t new_str;

    ASSERT_RVAL(slist != (spif_ptr_t) NULL, (spif_ptr_t) NULL);
    REQUIRE_RVAL(*slist != (spif_ptr_t) NULL, (spif_ptr_t) NULL);
    if (!sep) {
        sep = SPIF_CHARPTR("");
    }
    slen = strlen((char *) sep);
    for (i = len = 0; slist[i]; i++) {
        len += strlen((char *) slist[i]);
    }
    len += slen * (i - 1);
    new_str = (spif_charptr_t) MALLOC(len + 1);
    strcpy((char *) new_str, (char *) slist[0]);
    for (i = 1; slist[i]; i++) {
        if (slen) {
            strcat((char *) new_str, (char *) sep);
        }
        strcat((char *) new_str, (char *) slist[i]);
    }
    return new_str;
}
Beispiel #6
0
spif_bool_t
spif_socket_send(spif_socket_t self, spif_str_t data)
{
    size_t len;
    int num_written;
    struct timeval tv = { 0, 0 };

    ASSERT_RVAL(!SPIF_SOCKET_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(data), FALSE);

    len = spif_str_get_len(data);
    REQUIRE_RVAL(len > 0, FALSE);

    num_written = write(self->fd, SPIF_STR_STR(data), len);
    for (; (num_written < 0) && ((errno == EAGAIN) || (errno == EINTR)); ) {
        tv.tv_usec += 10000;
        if (tv.tv_usec == 1000000) {
            tv.tv_usec = 0;
            tv.tv_sec++;
        }
        select(0, NULL, NULL, NULL, &tv);
        num_written = write(self->fd, SPIF_STR_STR(data), len);
    }
    if (num_written < 0) {
        D_OBJ(("Unable to write to socket %d -- %s\n", self->fd, strerror(errno)));
        switch (errno) {
            case EFBIG:
                {
                    spif_bool_t b;
                    spif_str_t tmp_buf;
                    spif_charptr_t s;
                    long left;

                    for (left = len, s = SPIF_CHARPTR(SPIF_STR_STR(data)); left > 0; s += 1024, left -= 1024) {
                        tmp_buf = spif_str_new_from_buff(s, 1024);
                        b = spif_socket_send(self, tmp_buf);
                        if (b == FALSE) {
                            spif_str_del(tmp_buf);
                            return b;
                        }
                    }
                }
                break;
            case EIO:
            case EPIPE:
                close(self->fd);
                /* Drop */
            case EBADF:
            case EINVAL:
            default:
                self->fd = -1;
                SPIF_SOCKET_FLAGS_CLEAR(self, SPIF_SOCKET_FLAGS_IOSTATE);
                return FALSE;
                break;
        }
    }
    return TRUE;
}
Beispiel #7
0
Datei: url.c Projekt: Limsik/e17
spif_str_t
spif_url_show(spif_url_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_URL_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(url, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_url_t) %s:  %10p {\n",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    buff = spif_str_show(self->proto, SPIF_CHARPTR("proto"), buff, indent + 2);
    buff = spif_str_show(self->user, SPIF_CHARPTR("user"), buff, indent + 2);
    buff = spif_str_show(self->passwd, SPIF_CHARPTR("passwd"), buff, indent + 2);
    buff = spif_str_show(self->host, SPIF_CHARPTR("host"), buff, indent + 2);
    buff = spif_str_show(self->port, SPIF_CHARPTR("port"), buff, indent + 2);
    buff = spif_str_show(self->path, SPIF_CHARPTR("path"), buff, indent + 2);
    buff = spif_str_show(self->query, SPIF_CHARPTR("query"), buff, indent + 2);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}
Beispiel #8
0
Datei: tok.c Projekt: Limsik/e17
spif_str_t
spif_tok_show(spif_tok_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_TOK_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(tok, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_tok_t) %s:  %10p {\n",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }
    buff = spif_str_show(SPIF_STR(self->src), SPIF_CHARPTR("src"), buff, indent + 2);
    buff = spif_str_show(SPIF_STR(self->sep), SPIF_CHARPTR("sep"), buff, indent + 2);

    indent += 2;
    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) quote:  '%c' (0x%02x)\n",
             (char) self->quote, (unsigned int) self->quote);
    spif_str_append_from_ptr(buff, tmp);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) dquote:  '%c' (0x%02x)\n",
             (char) self->dquote, (unsigned int) self->dquote);
    spif_str_append_from_ptr(buff, tmp);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_char_t) escape:  '%c' (0x%02x)\n",
             (char) self->escape, (unsigned int) self->escape);
    spif_str_append_from_ptr(buff, tmp);

    SPIF_LIST_SHOW(self->tokens, buff, indent);
    indent -= 2;

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}
Beispiel #9
0
/**
 * Find the value for a long option.
 *
 * This function looks for and returns the value associated with a
 * long option, or NULL if one is not found.
 *
 * @param arg      The argument containing the long option.
 * @param next_arg The next word on the command line.
 * @param hasequal Address of a toggle variable to return whether or
 *                 not the value was appended to the option with an
 *                 equals sign ('=').
 * @return         The option's value string, or NULL if no value is
 *                 found.
 *
 * @see @link DOXGRP_OPT Command Line Option Parser @endlink, spifopt_parse()
 * @ingroup DOXGRP_OPT
 */
static spif_charptr_t 
find_value_long(spif_charptr_t arg, spif_charptr_t next_arg, spif_charptr_t hasequal)
{
    spif_charptr_t val_ptr;

    if ((val_ptr = SPIF_CHARPTR(strchr((char *)arg, '=')))) {
        val_ptr++;
        *hasequal = 1;
    } else {
        if (next_arg) {
            val_ptr = next_arg;
        }
        *hasequal = 0;
    }
    D_OPTIONS(("hasequal == %d  val_ptr == %10.8p \"%s\"\n", *hasequal, val_ptr, NONULL(val_ptr)));
    return val_ptr;
}
Beispiel #10
0
static spif_bool_t
spif_url_init_from_unixaddr(spif_url_t self, spif_unixsockaddr_t unixaddr)
{
    ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE);
    spif_str_init(SPIF_STR(self));
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS_VAR(url));
    self->proto = (spif_str_t) NULL;
    self->user = (spif_str_t) NULL;
    self->passwd = (spif_str_t) NULL;
    self->host = (spif_str_t) NULL;
    self->port = (spif_str_t) NULL;
    self->query = (spif_str_t) NULL;

    if (unixaddr->sun_path) {
        self->path = spif_str_new_from_ptr(SPIF_CHARPTR(unixaddr->sun_path));
    } else {
        self->path = (spif_str_t) NULL;
    }
    return TRUE;
}
Beispiel #11
0
/**
 * Parse the command line arguments for options.
 *
 * This function iterates through the command line arguments looking
 * for options which have been defined.  Each option encountered is
 * handled according to its type.
 *
 * @param argc The number of arguments.
 * @param argv The array of argument strings.
 *
 * @see @link DOXGRP_OPT Command Line Option Parser @endlink
 * @ingroup DOXGRP_OPT
 */
void
spifopt_parse(int argc, char *argv[])
{
    spif_int32_t i, j;
    spif_charptr_t opt;

    REQUIRE(argc > 1);
    REQUIRE(argv != NULL);

    /* Process each command line arg one-by-one. */
    for (i = 1, opt = SPIF_CHARPTR(argv[1]); i < argc; ) {
        spif_charptr_t val_ptr = NULL;
        spif_char_t islong = 0, hasequal = 0;

        D_OPTIONS(("argv[%d] == \"%s\", opt == \"%s\"\n", i, argv[i], opt));

        if (SPIF_PTR_ISNULL(opt)) {
            /* NEXT_ARG(); */
            break;
        } else if (opt == SPIF_CHARPTR(argv[i])) {
            /* If it's not an option, skip it. */
            if (*opt != '-') {
                NEXT_ARG();
            } else {
                opt++;
            }
        }

        /* If the second character is also a hyphen, it's a long option. */
        if (*opt == '-') {
            islong = 1;
            /* Skip the leading "--" */
            opt++;
            D_OPTIONS(("Long option detected\n"));
            if ((j = find_long_option(opt)) == -1) {
                NEXT_ARG();
            }
        } else {
            if ((j = find_short_option(*opt)) == -1) {
                NEXT_LETTER();
            }
        }
        if (!SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_PREPARSE) && SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_REMOVE_ARGS)) {
            argv[i] = NULL;
        }

        /* If a value was passed to this option, set val_ptr to point to it. */
        if (islong) {
            val_ptr = find_value_long(SPIF_CHARPTR(opt), SPIF_CHARPTR(argv[i + 1]), &hasequal);
        } else {
            val_ptr = find_value_short(opt, SPIF_CHARPTR(argv[i + 1]));
        }

        /* Boolean options may or may not have a value... */
        if (val_ptr) {
            if (SPIFOPT_OPT_IS_BOOLEAN(j) && !is_boolean_value(val_ptr)) {
                val_ptr = NULL;
            } else if (SPIFOPT_OPT_IS_ABSTRACT(j) && is_valid_option(val_ptr)) {
                val_ptr = NULL;
            }
        }
        if (val_ptr) {
            if (val_ptr == SPIF_CHARPTR(argv[i + 1])) {
                i++;
                opt += strlen((char *) opt);
            }
        }

        /* If this option is deprecated, print a warning before continuing. */
        if (SPIFOPT_OPT_IS_DEPRECATED(j)) {
            spif_str_t warn;

            warn = spif_str_new_from_buff(SPIF_CHARPTR("The "), 128);
            if (SPIFOPT_OPT_SHORT(j)) {
                spif_str_append_char(warn, '-');
                spif_str_append_char(warn, SPIFOPT_OPT_SHORT(j));
                spif_str_append_from_ptr(warn, SPIF_CHARPTR(" / --"));
            } else {
                spif_str_append_from_ptr(warn, SPIF_CHARPTR("--"));
            }
            spif_str_append_from_ptr(warn, SPIFOPT_OPT_LONG(j));
            spif_str_append_from_ptr(warn, SPIF_CHARPTR(" option is deprecated and should not be used.\n"));
            libast_print_warning((char *) SPIF_STR_STR(warn));
            spif_str_del(warn);
        }

        /* Make sure that options which require a parameter have them. */
        if (SPIFOPT_OPT_NEEDS_VALUE(j)) {
            if (!val_ptr) {
                if (islong) {
                    libast_print_error("long option --%s requires a%s value\n", SPIFOPT_OPT_LONG(j),
                                (SPIFOPT_OPT_IS_INTEGER(j)
                                 ? ("n integer")
                                 : (SPIFOPT_OPT_IS_STRING(j)
                                    ? " string"
                                    : (SPIFOPT_OPT_IS_ARGLIST(j)
                                       ? "n argument list"
                                       : ""))));
                } else {
                    libast_print_error("option -%c requires a%s value\n", SPIFOPT_OPT_SHORT(j),
                                (SPIFOPT_OPT_IS_INTEGER(j)
                                 ? ("n integer")
                                 : (SPIFOPT_OPT_IS_STRING(j)
                                    ? " string"
                                    : (SPIFOPT_OPT_IS_ARGLIST(j)
                                       ? "n argument list"
                                       : ""))));
                }
                CHECK_BAD();
                continue;
            }
            /* Also make sure we know what to do with the value. */
            if (!SPIFOPT_OPT_VALUE(j)) {
                NEXT_LOOP();
            }
        } else if (SPIFOPT_OPT_IS_ABSTRACT(j) && !SPIFOPT_OPT_VALUE(j)) {
            /* Also make sure that abstract options have a function pointer. */
            NEXT_LOOP();
        }

        if (SPIFOPT_OPT_IS_BOOLEAN(j)) {
            if (!handle_boolean(j, val_ptr, islong)) {
                i--;
            }
        } else if (SPIFOPT_OPT_IS_STRING(j)) {
            if (SHOULD_PARSE(j)) {
                handle_string(j, val_ptr);
            }
        } else if (SPIFOPT_OPT_IS_INTEGER(j)) {
            if (SHOULD_PARSE(j)) {
                handle_integer(j, val_ptr);
            }
        } else if (SPIFOPT_OPT_IS_ARGLIST(j)) {
            if (SHOULD_PARSE(j)) {
                handle_arglist(j, val_ptr, hasequal, i, argc, argv);
            }
            if (!hasequal) {
                break;
            }
        } else if (SPIFOPT_OPT_IS_ABSTRACT(j)) {
            if (SHOULD_PARSE(j)) {
                D_OPTIONS(("Abstract option detected\n"));
                ((spifopt_abstract_handler_t) SPIFOPT_OPT_VALUE(j))(val_ptr);
            }
        }
        if (!SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_PREPARSE) && SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_REMOVE_ARGS)) {
            argv[i] = NULL;
        }
        NEXT_LOOP();
    }

    if (SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_PREPARSE)) {
        SPIFOPT_FLAGS_CLEAR(SPIFOPT_SETTING_PREPARSE);
    } else if (SPIFOPT_FLAGS_IS_SET(SPIFOPT_SETTING_REMOVE_ARGS)) {
        for (i = 1, j = 1; i < argc; i++) {
            if (argv[i]) {
                argv[j] = argv[i];
                j++;
            }
        }
        if (j > 1) {
            argv[j] = NULL;
        }
    }
}
Beispiel #12
0
static spif_bool_t
spif_socket_get_proto(spif_socket_t self)
{
    spif_url_t url;
    spif_protoinfo_t proto;
    spif_str_t proto_str;
    spif_servinfo_t serv;

    ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE);

    /* If we have a remote URL, use it.  Otherwise, use the local one. */
    url = ((SPIF_URL_ISNULL(self->remote_url)) ? (self->local_url) : (self->remote_url));
    REQUIRE_RVAL(!SPIF_URL_ISNULL(url), FALSE);

    proto_str = spif_url_get_proto(url);
    if (!SPIF_STR_ISNULL(proto_str)) {
        if (SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(proto_str, SPIF_CHARPTR("raw")))) {
            spif_str_t target;

            /* Raw socket.  Could be raw UNIX or raw IP. */
            SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_TYPE_RAW);

            /* If we have a hostname, it's IP.  If we have a path only, it's UNIX. */
            target = spif_url_get_host(url);
            if (SPIF_STR_ISNULL(target)) {
                target = spif_url_get_path(url);
                if (!SPIF_STR_ISNULL(target)) {
                    SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_UNIX);
                }
            } else {
                SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_INET);
            }
        } else if (SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(proto_str, SPIF_CHARPTR("unix")))) {
            /* UNIX socket.  Assume stream-based. */
            SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_UNIX);
            SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_TYPE_STREAM);
        } else {
            /* IP socket.  See if they gave us a protocol name. */
            SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_INET);
            proto = getprotobyname((char *) SPIF_STR_STR(proto_str));
            if (!proto) {
                /* If it's not a protocol, it's probably a service. */
                serv = getservbyname((char *) SPIF_STR_STR(proto_str), "tcp");
                if (!serv) {
                    serv = getservbyname((char *) SPIF_STR_STR(proto_str), "udp");
                }
                if (serv) {
                    /* If we found one, get the protocol info too. */
                    proto = getprotobyname(serv->s_proto);
                    REQUIRE_RVAL(proto != NULL, FALSE);
                }
            }
            if (proto) {
                /* Bingo.  Set the flags appropriately. */
                self->proto = proto->p_proto;
                if (!strcmp(proto->p_name, "tcp")) {
                    SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_TYPE_STREAM);
                } else if (!strcmp(proto->p_name, "udp")) {
                    SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_TYPE_DGRAM);
                }
            }
        }
    } else {
        /* No protocol?  Assume a UNIX socket. */
        SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_FAMILY_UNIX);
        SPIF_SOCKET_FLAGS_SET(self, SPIF_SOCKET_FLAGS_TYPE_STREAM);
    }
    return TRUE;
}
Beispiel #13
0
Datei: tok.c Projekt: Limsik/e17
spif_bool_t
spif_tok_eval(spif_tok_t self)
{
    const char *pstr, *delim = NULL;
    spif_str_t tmp;
    char quote;
    size_t len;

    ASSERT_RVAL(!SPIF_TOK_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_STR_ISNULL(self->src), FALSE);

    pstr = (const char *) SPIF_STR_STR(SPIF_STR(self->src));
    len = spif_str_get_len(SPIF_STR(self->src));

    if (!SPIF_STR_ISNULL(self->sep)) {
        delim = (const char *) SPIF_STR_STR(SPIF_STR(self->sep));
    }

    if (!SPIF_LIST_ISNULL(self->tokens)) {
        SPIF_LIST_DEL(self->tokens);
    }
    self->tokens = SPIF_LIST_NEW(dlinked_list);

    /* Before we do anything, skip leading "whitespace." */
    for (; *pstr && IS_DELIM(*pstr); pstr++);

    /* The outermost for loop is where we traverse the string.  Each new
       word brings us back to the top where we resize our string list. */
    for (quote = 0; *pstr; ) {
        tmp = spif_str_new_from_buff(SPIF_CHARPTR(""), len);
        spif_str_clear(tmp, 0);

        /* This for loop is where we process each character. */
        for (; *pstr && (quote || !IS_DELIM(*pstr));) {
            if (*pstr == self->dquote || *pstr == self->quote) {
                /* It's a quote character, so set or reset the quote variable. */
                if (quote) {
                    if (quote == *pstr) {
                        quote = 0;
                    } else {
                        /* It's a single quote inside double quotes, or vice versa.  Leave it alone. */
                        spif_str_append_char(tmp, *pstr);
                    }
                } else {
                    quote = *pstr;
                }
                pstr++;
            } else {
                /* Handle any backslashes that are escaping delimiters or quotes. */
                if ((*pstr == self->escape) && (IS_DELIM(*(pstr + 1)) || IS_QUOTE(*(pstr + 1)))) {
                    /* Incrementing pstr here moves us past the backslash so that the line
                       below will copy the next character to the new token, no questions asked. */
                    pstr++;
                }
                spif_str_append_char(tmp, *pstr++);
            }
        }

        /* Reallocate the new string to be just the right size. */
        spif_str_trim(tmp);
        len -= spif_str_get_len(tmp);

        /* Add it to the list */
        SPIF_LIST_APPEND(self->tokens, tmp);

        /* Move past any trailing "whitespace." */
        for (; *pstr && IS_DELIM(*pstr); pstr++);
    }
    return TRUE;
}
Beispiel #14
0
Datei: url.c Projekt: Limsik/e17
static spif_bool_t
spif_url_parse(spif_url_t self)
{
    spif_charptr_t s = SPIF_STR_STR(SPIF_STR(self));
    spif_charptr_t pstr, pend, ptmp;

    ASSERT_RVAL(!SPIF_URL_ISNULL(self), FALSE);
    pstr = s;

    /* Check for "proto:" at the beginning. */
    pend = SPIF_CHARPTR(strchr((char *) s, ':'));
    if (pend) {
        for (; pstr < pend; pstr++) {
            if (!isalnum(*pstr)) {
                break;
            }
        }
        if (pstr == pend) {
            /* Got one. */
            self->proto = spif_str_new_from_buff(s, pend - s);
            pstr++;
        } else {
            /* Nope, reset. */
            pstr = s;
        }
    }

    if ((*pstr == '/') && (pstr[1] == '/')) {
        pstr += 2;
    }

    /* Knock out the path and query if they're there. */
    pend = SPIF_CHARPTR(strchr((char *) pstr, '/'));
    if (pend) {
        spif_charptr_t tmp = SPIF_CHARPTR(strchr((char *) pend, '?'));

        if (tmp) {
            self->query = spif_str_new_from_ptr(tmp + 1);
            self->path = spif_str_new_from_buff(pend, tmp - pend);
        } else {
          self->path = spif_str_new_from_ptr(pend);
        }
    } else if ((pend = SPIF_CHARPTR(strchr((char *)pstr, '?')))) {
        self->query = spif_str_new_from_ptr(pend + 1);
    } else {
        for (pend = pstr; *pend; pend++);
    }
    /* At this point, pend *must* point to the end of the user/pass/host/port part. */

    /* Check for an @ sign, which would mean we have auth info. */
    ptmp = SPIF_CHARPTR(strchr((char *) pstr, '@'));
    if ((ptmp) && (ptmp < pend)) {
        spif_charptr_t tmp = SPIF_CHARPTR(strchr((char *) pstr, ':'));

        if ((tmp) && (tmp < ptmp)) {
            /* Both username and password. */
            self->user = spif_str_new_from_buff(pstr, tmp - pstr);
            self->passwd = spif_str_new_from_buff((tmp + 1), ptmp - tmp - 1);
        } else {
            self->user = spif_str_new_from_buff(pstr, ptmp - pstr);
        }
        pstr = ptmp + 1;
    }

    /* All that remains now between pstr and pend is host and maybe port. */
    ptmp = SPIF_CHARPTR(strchr((char *) pstr, ':'));
    if ((ptmp) && (ptmp < pend)) {
        self->host = spif_str_new_from_buff(pstr, ptmp - pstr);
        self->port = spif_str_new_from_buff((ptmp + 1), pend - ptmp - 1);
    } else if (pstr != pend) {
        self->host = spif_str_new_from_buff(pstr, pend - pstr);
    }

    /* If we have a proto but no port, see if we can resolve the port using the proto. */
    if (SPIF_STR_ISNULL(self->port) && !SPIF_STR_ISNULL(self->proto)) {
        spif_protoinfo_t proto;
        spif_servinfo_t serv;

        proto = getprotobyname((char *) SPIF_STR_STR(self->proto));
        if (!proto) {
            /* If it's not a protocol, it's probably a service. */
            serv = getservbyname((char *) SPIF_STR_STR(self->proto), "tcp");
            if (!serv) {
                serv = getservbyname((char *) SPIF_STR_STR(self->proto), "udp");
            }
            if (serv) {
                proto = getprotobyname(serv->s_proto);
                REQUIRE_RVAL(proto != NULL, FALSE);
            }
        }
        if (proto) {
            spif_char_t buff[32];

            snprintf((char *) buff, sizeof(buff), "%d", ntohs(serv->s_port));
            self->port = spif_str_new_from_ptr(buff);
        }
    }

    return TRUE;
}