예제 #1
0
static proto_t
watchman_read_with_timeout(struct watchman_connection *conn, struct timeval *timeout, struct watchman_error *error)
{
    proto_t result;
    json_error_t jerror;

    int ret = 1;

    if (!timeout || timeout->tv_sec || timeout->tv_usec)
        ret = block_on_read(fileno(conn->fp), timeout);
    if (ret == -1) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Error encountered blocking on watchman");
        return proto_null();
    }

    if (ret != 1) {
        watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                     "timed out waiting for watchman");
        return proto_null();
    }

    if (use_bser_encoding) {
        bser_t* bser = bser_parse_from_file(conn->fp, NULL);
        result = proto_from_bser(bser);
    } else {
        json_t* json = json_loadf(conn->fp, JSON_DISABLE_EOF_CHECK, &jerror);
        result = proto_from_json(json);
        if (fgetc(conn->fp) != '\n') {
            if (errno == EAGAIN || errno == EWOULDBLOCK) {
                watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                             "Timeout reading EOL from watchman");
            } else {
                watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                             "No newline at end of reply");
            }
            json_decref(json);
            return proto_null();
        }
    }
    if (proto_is_null(result)) {
        if (errno == EAGAIN) {
            watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                         "Timeout:EAGAIN reading from watchman.");
        }
        else if (errno == EWOULDBLOCK) {
            watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                         "Timeout:EWOULDBLOCK reading from watchman");
        } else {
            watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                         "Can't parse result from watchman: %s",
                         jerror.text);
        }
        return proto_null();
    }
    return result;
}
int
ccnl_fetchContentForChunkName(struct ccnl_prefix_s *prefix,
                              char* nfnexpr,
                              unsigned int *chunknum,
                              int suite,
                              unsigned char *out, int out_len,
                              int *len,
                              float wait, int sock, struct sockaddr sa) {
#ifdef USE_SUITE_CCNB
    if (suite == CCNL_SUITE_CCNB) {
        DEBUGMSG(ERROR, "CCNB not implemented\n");
        exit(-1);
    }
#endif

    ccnl_mkInterestFunc mkInterest = ccnl_suite2mkInterestFunc(suite);
    if (!mkInterest) {
        DEBUGMSG(ERROR, "unknown suite %d/not implemented\n", suite);
        exit(-1);
    }

    int nonce = random();
    *len = mkInterest(prefix, &nonce, out, out_len);
/*
        {
            int fd = open("outgoing.bin", O_WRONLY|O_CREAT|O_TRUNC);
            write(fd, out, *len);
            close(fd);
        }
*/
    if (sendto(sock, out, *len, 0, &sa, sizeof(sa)) < 0) {
        perror("sendto");
        myexit(1);
    }
    if (block_on_read(sock, wait) <= 0) {
        DEBUGMSG(WARNING, "timeout after block_on_read\n");
        return -1;
    }
    *len = recv(sock, out, out_len, 0);
/*
        {
            int fd = open("incoming.bin", O_WRONLY|O_CREAT|O_TRUNC);
            write(fd, out, *len);
            close(fd);
        }
*/
    return 0;
}
예제 #3
0
/*
 * Read from fd into buf until either `bytes` bytes have been read, or
 * EOF, or the data that has been read can be parsed as JSON.  In the
 * event of a timeout or read error, returns NULL.
 */
static proto_t read_with_timeout(int fd, char* buf, size_t bytes, struct timeval timeout)
{
    size_t read_so_far = 0;
    ssize_t bytes_read = 0;

    while (read_so_far < bytes) {
        if (timeout.tv_sec || timeout.tv_usec) {
            if (timeout.tv_sec < 0 || timeout.tv_usec < 0) {
                /* Timeout */
                return proto_null();
            }

            if (1 == block_on_read(fd, &timeout)) {
                bytes_read = read(fd, buf, bytes - read_so_far);
            } else {
                return proto_null(); /* timeout or error */
            }
        } else {
            bytes_read = read(fd, buf, bytes - read_so_far);
        }
        if (bytes_read < 0) {
            return proto_null();
        }
        if (bytes_read == 0) {
            /* EOF, but we couldn't parse the JSON we have so far */
            return proto_null();
        }
        read_so_far += bytes_read;

        /* try to parse this */
        buf[read_so_far] = 0;
        proto_t proto;
        if (buf[0] <= 0x0b) {
            bser_t* bser = bser_parse_buffer((uint8_t*)buf, read_so_far, NULL);
            proto = proto_from_bser(bser);
        } else {
            json_error_t jerror;
            json_t* json = json_loads(buf, JSON_DISABLE_EOF_CHECK, &jerror);
            proto = proto_from_json(json);
        }
        return proto;
    }
    return proto_null();
}
예제 #4
0
static json_t *
watchman_read_with_timeout(struct watchman_connection *conn, struct timeval *timeout, struct watchman_error *error)
{
    json_error_t jerror;
    int flags = JSON_DISABLE_EOF_CHECK;
    json_t *result;

    int ret = block_on_read(fileno(conn->fp), timeout);
    if (ret == -1) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Error encountered blocking on watchman");
    }

    result = json_loadf(conn->fp, flags, &jerror);
    if (!result) {
        if (errno == EAGAIN) {
            watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                         "Timeout:EAGAIN reading from watchman.");
        }
        else if (errno == EWOULDBLOCK) {
            watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                         "Timeout:EWOULDBLOCK reading from watchman");
        } else {
            watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                         "Can't parse result from watchman: %s",
                         jerror.text);
        }
        return NULL;
    }
    if (fgetc(conn->fp) != '\n') {
        if (errno == EAGAIN || errno == EWOULDBLOCK) {
            watchman_err(error, WATCHMAN_ERR_TIMEOUT,
                         "Timeout reading EOL from watchman");
        } else {
            watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                         "No newline at end of reply");
        }
        json_decref(result);
        return NULL;
    }
    return result;
}
예제 #5
0
void
request_content(int sock, int (*sendproc)(int,char*,unsigned char*,int),
                char *dest, unsigned char *out, int len, float wait)
{
    unsigned char buf[64*1024];
    int len2 = sendproc(sock, dest, out, len), rc;

    if (len2 < 0) {
        perror("sendto");
        myexit(1);
    }
    
    rc = block_on_read(sock, wait);
    if (rc == 1) {
        len2 = recv(sock, buf, sizeof(buf), 0);
        if (len2 > 0) {

            write(1, buf, len2);
            myexit(0);
        }
    }
}
예제 #6
0
/*
 * Read from fd into buf until either `bytes` bytes have been read, or
 * EOF, or the data that has been read can be parsed as JSON.  In the
 * event of a timeout or read error, returns NULL.
 */
static json_t *read_json_with_timeout(int fd, char* buf, size_t bytes, struct timeval timeout)
{
    size_t read_so_far = 0;
    ssize_t bytes_read = 0;

    while (read_so_far < bytes) {
        if (timeout.tv_sec || timeout.tv_usec) {
            if (timeout.tv_sec < 0 || timeout.tv_usec < 0) {
                /* Timeout */
                return NULL;
            }

            if (1 == block_on_read(fd, &timeout)) {
                bytes_read = read(fd, buf, bytes - read_so_far);
            } else {
                return NULL; /* timeout or error */
            }
        } else {
            bytes_read = read(fd, buf, bytes - read_so_far);
        }
        if (bytes_read < 0) {
            return NULL;
        }
        if (bytes_read == 0) {
            /* EOF, but we couldn't parse the JSON we have so far */
            return NULL;
        }
        read_so_far += bytes_read;

        /* try to parse this */
        buf[read_so_far] = 0;
        json_error_t jerror;
        json_t *json = json_loads(buf, JSON_DISABLE_EOF_CHECK, &jerror);
        if (json)
            return json;
    }
    return NULL;
}
int
main(int argc, char *argv[])
{
    unsigned char out[64*1024];
    int cnt, len, opt, sock = 0, socksize, suite = CCNL_SUITE_DEFAULT, port;
    char *addr = NULL, *udp = NULL, *ux = NULL;
    char *defaultNFNpath = "";//strdup("/ndn/ch/unibas/nfn");
    struct sockaddr sa;
    struct ccnl_prefix_s *prefix;
    float wait = 3.0;
    ccnl_mkInterestFunc mkInterest;
    ccnl_isContentFunc isContent;

    while ((opt = getopt(argc, argv, "hn:s:u:v:w:x:")) != -1) {
        switch (opt) {
        case 'n':
            defaultNFNpath = optarg;
            break;
        case 's':
            opt = ccnl_str2suite(optarg);
            if (opt < 0 || opt >= CCNL_SUITE_LAST)
                goto usage;
            suite = opt;
            break;
        case 'u':
            udp = optarg;
            break;
        case 'v':
#ifdef USE_LOGGING
            if (isdigit(optarg[0]))
                debug_level = atoi(optarg);
            else
                debug_level = ccnl_debug_str2level(optarg);
#endif
            break;
        case 'w':
            wait = atof(optarg);
            break;
        case 'x':
            ux = optarg;
            break;
        case 'h':
        default:
usage:
            fprintf(stderr, "usage: %s [options] NFNexpr\n"
            "  -n NFNPATH       default prefix towards some NFN node(s)\n"
            "  -s SUITE         (ccnb, ccnx2015, cisco2015, iot2014, ndn2013)\n"
            "  -u a.b.c.d/port  UDP destination (default is 127.0.0.1/6363)\n"
#ifdef USE_LOGGING
            "  -v DEBUG_LEVEL (fatal, error, warning, info, debug, verbose, trace)\n"
#endif
            "  -w timeout       in sec (float)\n"
            "  -x ux_path_name  UNIX IPC: use this instead of UDP\n"
            "Examples:\n"
            "%% simplenfn /ndn/edu/wustl/ping\n"
            "%% simplenfn \"echo hello world\"\n"
            "%% simplenfn \"translate 'ccnx2014 /ccnx/parc/info.txt\"\n"
            "%% simplenfn \"add 1 1\"\n",
            argv[0]);
            exit(1);
        }
    }

    if (!argv[optind] || argv[optind+1])
        goto usage;

    srandom(time(NULL));

    if (ccnl_parseUdp(udp, suite, &addr, &port) != 0) {
        exit(-1);
    }
    DEBUGMSG(TRACE, "using udp address %s/%d\n", addr, port);

    mkInterest = ccnl_suite2mkInterestFunc(suite);
    isContent = ccnl_suite2isContentFunc(suite);
    if (!mkInterest || !isContent) {
        exit(-1);
    }

    if (ux) { // use UNIX socket
        struct sockaddr_un *su = (struct sockaddr_un*) &sa;
        su->sun_family = AF_UNIX;
        strcpy(su->sun_path, ux);
        sock = ux_open();
    } else { // UDP
        struct sockaddr_in *si = (struct sockaddr_in*) &sa;
        si->sin_family = PF_INET;
        si->sin_addr.s_addr = inet_addr(addr);
        si->sin_port = htons(port);
        sock = udp_open();
    }

    prefix = exprToNfnPrefix(defaultNFNpath, suite, argv[optind]);
    if (!prefix)
        goto done;
    for (cnt = 0; cnt < 3; cnt++) {
        int nonce = random();

        len = mkInterest(prefix,
                         &nonce,
                         out, sizeof(out));

        DEBUGMSG(TRACE,
                 "sending interest(prefix=%s, suite=%s)\n",
                 ccnl_prefix_to_path(prefix),
                 ccnl_suite2str(suite));

        if(ux) {
            socksize = sizeof(struct sockaddr_un);
        } else {
            socksize = sizeof(struct sockaddr_in);
        }
        if (sendto(sock, out, len, 0,(struct sockaddr *) &sa, socksize) < 0) {
            perror("sendto");
            myexit(1);
        }

        for (;;) { // wait for a content pkt (ignore interests)
            int rc;

            if (block_on_read(sock, wait) <= 0) // timeout
                break;
            len = recv(sock, out, sizeof(out), 0);
/*
            fprintf(stderr, "received %d bytes\n", len);
            if (len > 0)
                fprintf(stderr, "  suite=%d\n", ccnl_pkt2suite(out, len));
*/
            rc = isContent(out, len);
            if (rc < 0)
                goto done;
            if (rc == 0) { // it's an interest, ignore it
                DEBUGMSG(WARNING, "skipping non-data packet\n");
                continue;
            }
            write(1, out, len);
            myexit(0);
        }
        if (cnt < 2)
            DEBUGMSG(INFO, "re-sending interest\n");
    }
    DEBUGMSG(ERROR, "timeout\n");

done:
    close(sock);
    myexit(-1);
    return 0; // avoid a compiler warning
}