static void handle_datagram(int fd, short what, void *arg) { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); char msgbuf[DGRAM_MSGBUF_LEN]; int msglen; char newname[1024]; if ((msglen = recvfrom(fd, msgbuf, DGRAM_MSGBUF_LEN, 0, (struct sockaddr *) &addr, &addrlen)) >= 0) { unsigned short newport = ntohs(addr.sin_port); get_addr_name(newname, (unsigned char *) &addr.sin_addr.s_addr); log_fmt("UDP: %s:%d sent %d bytes:\n", newname, newport, msglen); dump_buffer_to_stdout(msgbuf, msglen, cfg.hexmode); fflush(stdout); if (cfg.echo_udp) { if (sendto(fd, msgbuf, msglen, 0, (struct sockaddr *) &addr, addrlen) == -1) log_fmt("UDP: echo failed, errno = %d\n", errno); else log_fmt("UDP: echo succeeded\n"); } if (cfg.have_otherhost) { struct sockaddr_in oa; setup_other_sockaddr(&oa); if (sendto(fd, msgbuf, msglen, 0, (struct sockaddr *) &oa, sizeof(oa)) == -1) log_fmt("UDP: pass through failed, errno = %d\n", errno); else log_fmt("UDP: pass through succeeded\n"); } } }
static void print_stack(log_sev_t logsev, const char *str, struct parse_frame *frm, chunk_t *pc) { (void)pc; LOG_FUNC_ENTRY(); if (log_sev_on(logsev)) { int idx; log_fmt(logsev, "%8.8s", str); for (idx = 1; idx <= frm->pse_tos; idx++) { if (frm->pse[idx].stage != BS_NONE) { LOG_FMT(logsev, " [%s - %d]", get_token_name(frm->pse[idx].type), frm->pse[idx].stage); } else { LOG_FMT(logsev, " [%s]", get_token_name(frm->pse[idx].type)); } } log_fmt(logsev, "\n"); } }
static void align_log_al(log_sev_t sev, int line) { int idx; if (log_sev_on(sev)) { log_fmt(sev, "%s: line %d, %d)", __func__, line, cpd.al_cnt); for (idx = 0; idx < cpd.al_cnt; idx++) { log_fmt(sev, " %d/%d=%s", cpd.al[idx].col, cpd.al[idx].len, get_token_name(cpd.al[idx].type)); } log_fmt(sev, "\n"); } }
void log_pcf_flags(log_sev_t sev, UINT64 flags) { if (!log_sev_on(sev)) { return; } log_fmt(sev, "[0x%" PRIx64 ":", flags); const char *tolog = NULL; for (int i = 0; i < (int)ARRAY_SIZE(pcf_names); i++) { if ((flags & (1ULL << i)) != 0) { if (tolog != NULL) { log_str(sev, tolog, strlen(tolog)); log_str(sev, ",", 1); } tolog = pcf_names[i]; } } if (tolog != NULL) { log_str(sev, tolog, strlen(tolog)); } log_str(sev, "]\n", 2); }
static void accept_connection(int fd, short what, void *arg) { struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); int newfd = accept(fd, (struct sockaddr *) &addr, &addrlen); Connection *conn; if (newfd < 0) return; conn = calloc(sizeof(Connection), 1); conn->txfer_buf = newbuf(4096); conn->collecting_from = -1; conn->last_read_time = 0; conn->name[0] = malloc(1024); conn->fd[0] = newfd; get_addr_name(conn->name[0], (unsigned char *) &addr.sin_addr.s_addr); event_set(&conn->read_event[0], conn->fd[0], EV_READ | EV_PERSIST, (event_handler_t) handle_data0, conn); conn->name[1] = cfg.otherhostname; conn->fd[1] = open_otherhost(); event_set(&conn->read_event[1], conn->fd[1], EV_READ | EV_PERSIST, (event_handler_t) handle_data1, conn); log_fmt("connection accepted on I fd %d from %s to O fd %d\n", newfd, conn->name[0], conn->fd[1]); event_add(&conn->read_event[0], &cfg.timeout); event_add(&conn->read_event[1], &cfg.timeout); }
static void dump_configuration_settings(void) { log_fmt("invoked as: passthru %s%s%s%s-p %d -o %d%s%s -f %g\n", cfg.pass_udp ? "-u " : "", cfg.echo_udp ? "-E " : "", cfg.pass_tcp ? "-t " : "", cfg.hexmode ? "-x " : "-b ", cfg.portnumber, cfg.otherport, cfg.have_otherhost ? " -h " : "", cfg.have_otherhost ? cfg.otherhostname : "", (double) cfg.timeout.tv_sec + (double) cfg.timeout.tv_usec / 1000000.0); log_fmt("bound to port number %d\n", cfg.portnumber); if (cfg.have_otherhost) log_fmt("will connect to port number %d on %s\n", cfg.otherport, cfg.otherhostname); }
void log_print(int level, const char *fmt, ...) { char buf[65535]; if(level < top_level) return; memset(buf, 0, sizeof(buf)); va_list arg_list; va_start(arg_list, fmt); log_fmt(buf, sizeof(buf), level, fmt, arg_list); va_end(arg_list); LOCK(&lock); log_write(logfd, buf, strlen(buf)); UNLOCK(&lock); }
static void close_conn(Connection *conn, int closing_fd) { if (conn->txfer_buf->pos != 0) { dump_buffer(conn); } event_del(&conn->read_event[0]); event_del(&conn->read_event[1]); close(conn->fd[0]); close(conn->fd[1]); log_fmt("connection to %s between I fd %d and O fd %d closed by fd %d\n", conn->name[0], conn->fd[0], conn->fd[1], closing_fd); free(conn->name[0]); killbuf(conn->txfer_buf); free(conn); }
static void print_stack(log_sev_t logsev, const char *str, parse_frame_t *frm, chunk_t *pc) { UNUSED(pc); LOG_FUNC_ENTRY(); if (log_sev_on(logsev)) { log_fmt(logsev, "%8.8s", str); for (size_t idx = 1; idx <= frm->pse_tos; idx++) { if (frm->pse[idx].stage != brace_stage_e::NONE) { LOG_FMT(logsev, " [%s - %u]", get_token_name(frm->pse[idx].type), (unsigned int)frm->pse[idx].stage); } else { LOG_FMT(logsev, " [%s]", get_token_name(frm->pse[idx].type)); } } log_fmt(logsev, "\n"); } }
void log_open( log_t* log, const char* name ) { static struct { const char* name; int fd; } special[] = { { "stdout", 1 }, { "-", 1 }, { "stderr", 2 } }; int i, fd = -1; term_t term; log->file = NULL; log->isatty = false; term.lines = 1; term.columns = 132; term.colors = 2; term.unicode = true; for( i=0; i<N_ELEM(special); i++ ) { if( strcmp(special[i].name, name) == 0 ) { fd = special[i].fd; break; } } if( fd == -1 ) { fd = open(name, O_WRONLY); } if( fd >= 0 ) { log->file = fdopen(fd, "w+"); log->isatty = isatty(fd); } if( log->isatty && termcap( &term ) == 0 ) { log->lines = term.lines; log->columns = term.columns; log->colors = term.colors; log->unicode = term.unicode; } log_fmt( log, "colors: %hi, lines: %hi, columns: %i\n", log->colors, log->lines, log->columns ); }
static void flush_all_f_buffers(int signo) { log_fmt("interrupted with signal %d\n", signo); fflush(NULL); exit(0); }