void cmd_tcp_connect_cb(evutil_socket_t fd, short which, void *arg) { struct tcp_con *con = arg; int error = 0; socklen_t errsz = sizeof(error); /* Everything is ready */ cmd_ready_fd(&con->cmd, &cb_tcp, con); if (which == EV_TIMEOUT) goto out; /* Check if the connection completed */ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &errsz) == -1) { warn("%s: getsockopt for %d", __FUNCTION__, fd); goto out; } if (error) { warnx("%s: getsockopt: %s", __FUNCTION__, strerror(error)); goto out; } cmd_trigger_read(&con->cmd, con->psize - con->plen); cmd_trigger_write(&con->cmd, con->rlen); return; out: /* Connection failed, bring this down gracefully */ cmd_free(&con->cmd); tcp_sendfin(con); }
void cmd_udp_connect_cb(int fd, short which, void *arg) { struct udp_con *con = arg; /* Everything is ready */ cmd_ready_fd(&con->cmd, &cb_udp, con); cmd_trigger_read(&con->cmd, 1); cmd_trigger_write(&con->cmd, TAILQ_FIRST(&con->incoming) != NULL); return; }
void tcp_drain_payload(struct tcp_con *con, u_int len) { if (len == 0 || con->payload == NULL) return; if (len >= con->plen) { con->plen = 0; con->poff = 0; goto out; } memmove(con->payload, con->payload + len, con->plen - len); con->plen -= len; con->poff -= len; out: cmd_trigger_read(&con->cmd, con->psize - con->plen); }
void cmd_udp_read(int fd, short which, void *arg) { struct udp_con *con = arg; u_char buf[2048]; ssize_t len; TRACE(fd, len = read(fd, buf, sizeof(buf))); if (len == -1) { if (errno == EINTR || errno == EAGAIN) goto again; } if (len <= 0) { udp_free(con); return; } udp_send(con, buf, len); again: cmd_trigger_read(&con->cmd, 1); }
void cmd_tcp_read(evutil_socket_t fd, short which, void *arg) { struct tcp_con *con = arg; int len, space; struct command *cmd = &con->cmd; space = con->psize - con->plen; if (space <= 0) return; TRACE(fd, len = read(fd, con->payload + con->plen, space)); if (len == -1) { if (errno == EINTR || errno == EAGAIN) goto again; cmd_free(&con->cmd); tcp_sendfin(con); return; } else if (len == 0) { if (cmd->perrfd != -1 && !cmd->fdwantclose) { cmd->fdwantclose = 1; return; } cmd_free(&con->cmd); tcp_sendfin(con); return; } con->plen += len; if (con->plen == con->psize) tcp_increase_buf(&con->payload, &con->psize, TCP_MAX_SIZE); /* XXX - Trigger write */ tcp_senddata(con, TH_ACK); again: cmd_trigger_read(&con->cmd, con->psize - con->plen); }