<commit_before>/*

  Copyright (c) 2017 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom
  the Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

#include <fcntl.h>
#include <libdill.h>
#include <string.h>
#include <unistd.h>

#include "fd.h"
#include "iol.h"
#include "utils.h"

#if defined MSG_NOSIGNAL
#define FD_NOSIGNAL MSG_NOSIGNAL
#else
#define FD_NOSIGNAL 0
#endif

void fd_initrxbuf(struct fd_rxbuf *rxbuf) {
    dill_assert(rxbuf);
    rxbuf->len = 0;
    rxbuf->pos = 0;
}

int fd_unblock(int s) {
    /* Switch to non-blocking mode. */
    int opt = fcntl(s, F_GETFL, 0);
    if (opt == -1)
        opt = 0;
    int rc = fcntl(s, F_SETFL, opt | O_NONBLOCK);
    dill_assert(rc == 0);
    /*  Allow re-using the same local address rapidly. */
    opt = 1;
    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
    dill_assert(rc == 0);
    /* If possible, prevent SIGPIPE signal when writing to the connection
        already closed by the peer. */
#ifdef SO_NOSIGPIPE
    opt = 1;
    rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof (opt));
    dill_assert (rc == 0 || errno == EINVAL);
#endif
    return 0;
}

int fd_connect(int s, const struct sockaddr *addr, socklen_t addrlen,
      int64_t deadline) {
    /* Initiate connect. */
    int rc = connect(s, addr, addrlen);
    if(rc == 0) return 0;
    if(dill_slow(errno != EINPROGRESS)) return -1;
    /* Connect is in progress. Let's wait till it's done. */
    rc = fdout(s, deadline);
    if(dill_slow(rc == -1)) return -1;
    /* Retrieve the error from the socket, if any. */
    int err = 0;
    socklen_t errsz = sizeof(err);
    rc = getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&err, &errsz);
    if(dill_slow(rc != 0)) return -1;
    if(dill_slow(err != 0)) {errno = err; return -1;}
    return 0;
}

int fd_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
      int64_t deadline) {
    int as;
    while(1) {
        /* Try to accept new connection synchronously. */
        as = accept(s, addr, addrlen);
        if(dill_fast(as >= 0))
            break;
        /* If connection was aborted by the peer grab the next one. */
        if(dill_slow(errno == ECONNABORTED)) continue;
        /* Propagate other errors to the caller. */
        if(dill_slow(errno != EAGAIN && errno != EWOULDBLOCK)) return -1;
        /* Operation is in progress. Wait till new connection is available. */
        int rc = fdin(s, deadline);
        if(dill_slow(rc < 0)) return -1;
    }
    int rc = fd_unblock(as);
    dill_assert(rc == 0);
    return as;
}

int fd_send(int s, struct iolist *first, struct iolist *last,
      int64_t deadline) {
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    /* Message header will act as an iterator in the following loop. */
    struct msghdr hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = niov;
    /* It is very likely that at least one byte can be sent. Therefore,
       to improve efficiency, try to send and resort to fdout() only after
       send failed. */
    while(1) {
        ssize_t sz = sendmsg(s, &hdr, FD_NOSIGNAL);
        if(sz < 0) {
            if(dill_slow(errno != EWOULDBLOCK && errno != EAGAIN)) {
                if(errno == EPIPE) errno = ECONNRESET;
                return -1;
            }
            sz = 0;
        }
        /* Adjust the iovec array so that it doesn't contain data
           that was already sent. */
        while(sz) {
            struct iovec *head = &hdr.msg_iov[0];
            if(head->iov_len > sz) {
                head->iov_base += sz;
                head->iov_len -= sz;
                break;
            }
            sz -= head->iov_len;
            hdr.msg_iov++;
            hdr.msg_iovlen--;
            if(!hdr.msg_iovlen) return 0;
        }
        /* Wait till more data can be sent. */
        int rc = fdout(s, deadline);
        if(dill_slow(rc < 0)) return -1;
    }
}

/* Same as fd_recv() but with no rx buffering. */
static int fd_recv_(int s, struct iolist *first, struct iolist *last,
      int64_t deadline) {
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    /* Message header will act as an iterator in the following loop. */
    struct msghdr hdr;<commit_msg>Bug in fd.c fixed<commit_after>/*

  Copyright (c) 2017 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom
  the Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

*/

#include <fcntl.h>
#include <libdill.h>
#include <string.h>
#include <unistd.h>

#include "fd.h"
#include "iol.h"
#include "utils.h"

#if defined MSG_NOSIGNAL
#define FD_NOSIGNAL MSG_NOSIGNAL
#else
#define FD_NOSIGNAL 0
#endif

void fd_initrxbuf(struct fd_rxbuf *rxbuf) {
    dill_assert(rxbuf);
    rxbuf->len = 0;
    rxbuf->pos = 0;
}

int fd_unblock(int s) {
    /* Switch to non-blocking mode. */
    int opt = fcntl(s, F_GETFL, 0);
    if (opt == -1)
        opt = 0;
    int rc = fcntl(s, F_SETFL, opt | O_NONBLOCK);
    dill_assert(rc == 0);
    /*  Allow re-using the same local address rapidly. */
    opt = 1;
    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
    dill_assert(rc == 0);
    /* If possible, prevent SIGPIPE signal when writing to the connection
        already closed by the peer. */
#ifdef SO_NOSIGPIPE
    opt = 1;
    rc = setsockopt (s, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof (opt));
    dill_assert (rc == 0 || errno == EINVAL);
#endif
    return 0;
}

int fd_connect(int s, const struct sockaddr *addr, socklen_t addrlen,
      int64_t deadline) {
    /* Initiate connect. */
    int rc = connect(s, addr, addrlen);
    if(rc == 0) return 0;
    if(dill_slow(errno != EINPROGRESS)) return -1;
    /* Connect is in progress. Let's wait till it's done. */
    rc = fdout(s, deadline);
    if(dill_slow(rc == -1)) return -1;
    /* Retrieve the error from the socket, if any. */
    int err = 0;
    socklen_t errsz = sizeof(err);
    rc = getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)&err, &errsz);
    if(dill_slow(rc != 0)) return -1;
    if(dill_slow(err != 0)) {errno = err; return -1;}
    return 0;
}

int fd_accept(int s, struct sockaddr *addr, socklen_t *addrlen,
      int64_t deadline) {
    int as;
    while(1) {
        /* Try to accept new connection synchronously. */
        as = accept(s, addr, addrlen);
        if(dill_fast(as >= 0))
            break;
        /* If connection was aborted by the peer grab the next one. */
        if(dill_slow(errno == ECONNABORTED)) continue;
        /* Propagate other errors to the caller. */
        if(dill_slow(errno != EAGAIN && errno != EWOULDBLOCK)) return -1;
        /* Operation is in progress. Wait till new connection is available. */
        int rc = fdin(s, deadline);
        if(dill_slow(rc < 0)) return -1;
    }
    int rc = fd_unblock(as);
    dill_assert(rc == 0);
    return as;
}

int fd_send(int s, struct iolist *first, struct iolist *last,
      int64_t deadline) {
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    /* Message header will act as an iterator in the following loop. */
    struct msghdr hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.msg_iov = iov;
    hdr.msg_iovlen = niov;
    /* It is very likely that at least one byte can be sent. Therefore,
       to improve efficiency, try to send and resort to fdout() only after
       send failed. */
    while(1) {
        ssize_t sz = sendmsg(s, &hdr, FD_NOSIGNAL);
        if(sz < 0) {
            if(dill_slow(errno != EWOULDBLOCK && errno != EAGAIN)) {
                if(errno == EPIPE) errno = ECONNRESET;
                return -1;
            }
            sz = 0;
        }
        /* Adjust the iovec array so that it doesn't contain data
           that was already sent. */
        while(sz) {
            struct iovec *head = &hdr.msg_iov[0];
            if(head->iov_len > sz) {
                head->iov_base += sz;
                head->iov_len -= sz;
                break;
            }
            sz -= head->iov_len;
            hdr.msg_iov++;
            hdr.msg_iovlen--;
            if(!hdr.msg_iovlen) return 0;
            while(!hdr.msg_iov[0].iov_len) {
                hdr.msg_iov++;
                hdr.msg_iovlen--;
            }
            if(!hdr.msg_iovlen) return 0;
        }
        /* Wait till more data can be sent. */
        int rc = fdout(s, deadline);
        if(dill_slow(rc < 0)) return -1;
    }
}

/* Same as fd_recv() but with no rx buffering. */
static int fd_recv_(int s, struct iolist *first, struct iolist *last,
      int64_t deadline) {
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    /* Message header will act as an iterator in the following loop. */
    struct msghdr hdr;
{'fraction_non_alphanumeric': 0.10770683144524545, 'fraction_numerical': 0.009440439409543425, 'mean_word_length': 2.459916864608076, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2022660', 'n_tokens_mistral': 3963, 'n_tokens_neox': 3780, 'n_words': 1354}