int cmd_proxy_connect(struct tuple *hdr, struct command *cmd, struct addrinfo *ai, void *con) { syslog(LOG_DEBUG, "proxy connected-------------------------------------------------"); char ntop[NI_MAXHOST], strport[NI_MAXSERV]; char *host = ntop, *port = strport; struct callback *cb; struct timeval tv = { 10, 0 }; int fd; if (hdr->type == SOCK_STREAM) cb = &cb_tcp; else cb = &cb_udp; fd = socket(ai->ai_family, hdr->type, 0); if (fd == -1) { warn("socket"); return (-1); } if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) warn("fcntl(O_NONBLOCK)"); if (fcntl(fd, F_SETFD, 1) == -1) warn("fcntl(F_SETFD)"); TRACE(fd, cmd->pfd = fd); if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) { (*cb->cb_connect)(fd, EV_WRITE, con); return (0); } if (errno != EINPROGRESS) { warn("connect"); cmd->pfd = -1; TRACE_RESET(fd, close(fd)); return (-1); } TRACE(fd, event_set(&cmd->pwrite, fd, EV_WRITE, cb->cb_connect, con)); TRACE(cmd->pwrite.ev_fd, event_add(&cmd->pwrite, &tv)); if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop), strport, sizeof(strport), NI_NUMERICHOST | NI_NUMERICSERV) != 0) { host = "<hosterror>"; port = "<porterror>"; } syslog(LOG_INFO, "Connection established: %s -> proxy to %s:%s", honeyd_contoa(hdr), host, port); return (0); }
void cmd_free(struct command *cmd) { TRACE(cmd->pread.ev_fd, event_del(&cmd->pread)); TRACE(cmd->pwrite.ev_fd, event_del(&cmd->pwrite)); TRACE_RESET(cmd->pfd, close(cmd->pfd)); cmd->pfd = -1; cmd->pid = -1; if (cmd->perrfd != -1) { TRACE(cmd->peread.ev_fd, event_del(&cmd->peread)); TRACE_RESET(cmd->perrfd, close(cmd->perrfd)); cmd->perrfd = -1; } #ifdef HAVE_PYTHON if (cmd->state != NULL) pyextend_connection_end(cmd->state); #endif }
int fdshare_close(int fd) { assert(fds_refs != NULL); assert(fd >= 0 && fd < fds_refsize); if (--fds_refs[fd]) return (0); TRACE_RESET(fd, close(fd)); return (0); }