void on_event_accept(myconn_t* c) { struct sockaddr_in sin; socklen_t len = sizeof(struct sockaddr_in); int fd; // accept if ((fd = accept(c->fd, (struct sockaddr*)&sin, &len)) < 0) { if (errno != EAGAIN && errno != EINTR) { } fprintf(stderr, "accept: %s", strerror(errno)); return; } if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { fprintf(stderr, "set %d nonblocking failed: %s", fd, strerror(errno)); return; } myconn_t* conn = new_connection(fd, &echo_socket_handler, NULL); if (!conn) { fprintf(stderr, "connection poll full\n"); close(fd); return; } if (poll_connection(epollfd, EPOLLIN | EPOLLET, conn) < 0) { fprintf(stderr, "poll_connection error: del connection\n"); del_connection(conn); return; } printf("accept fd[%d]\n", /* inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), */ conn->fd); }
void end_all_sessions() { connection *cxt; int cxkey; config.llcxt = 0; for (cxkey = 0; cxkey < BUCKET_SIZE; cxkey++) { cxt = bucket[cxkey]; while (cxt != NULL) { config.llcxt++; if (cxt->prev) cxt->prev->next = cxt->next; if (cxt->next) cxt->next->prev = cxt->prev; connection *tmp = cxt; cxt = cxt->next; del_connection(tmp, &bucket[cxkey]); if (cxt == NULL) { bucket[cxkey] = NULL; } } } dlog("CXT in list before cleaning: %10u\n", config.llcxt); dlog("CXT in list after cleaning: %10u\n", config.curcxt); }
void agent_impl::close(unsigned id) { connection_ptr conn = get_connetion(id); del_connection(id); if (conn.get() != NULL) { conn->close(); } }
int poll_connection(int pollfd, int events, myconn_t* conn) { if (!conn) { fprintf(stderr, "%s bad connection\n", __FUNCTION__); return -1; } struct epoll_event epv = { .events = events, .data.ptr = conn, }; int op; if (conn->status) { op = EPOLL_CTL_MOD; } else { conn->status = 1; op = EPOLL_CTL_ADD; } if (epoll_ctl(pollfd, op, conn->fd, &epv) < 0) { fprintf(stderr, "epoll add failed[fd=%d], events=%0X: %s\n", conn->fd, events, strerror(errno)); del_connection(conn); return -1; } /* printf("Event Add OK[fd=%d], op=%d, evnets=%0X\n", ev->fd, op, events); */ return 0; } int unpoll_connection(int pollfd, myconn_t* conn) { if (epoll_ctl(pollfd, EPOLL_CTL_DEL, conn->fd, NULL) < 0) { fprintf(stderr, "epoll del failed[fd=%d]: %s\n", conn->fd, strerror(errno)); return -1; } return 0; } /* callbacks */ void on_event_hup_dft(myconn_t* arg); /* default */ void on_event_error_dft(myconn_t* arg); /* default */ extern event_handler_t accept_socket_handler; extern event_handler_t echo_socket_handler; /* listen socket */ int make_listen_socket(short port); myconn_t* make_listen_connection(short port) { int fd; if ((fd = make_listen_socket(port)) < 0) { return NULL; } myconn_t* c = new_connection(fd, &accept_socket_handler, NULL); c->last_active = -1; /* no update active */ return c; }
void end_all_sessions() { connection *cxt; int cxkey; int expired = 0; extern connection *bucket[BUCKET_SIZE]; for (cxkey = 0; cxkey < BUCKET_SIZE; cxkey++) { cxt = bucket[cxkey]; while (cxt != NULL) { expired++; connection *tmp = cxt; cxt = cxt->next; del_connection(tmp, &bucket[cxkey]); if (cxt == NULL) { bucket[cxkey] = NULL; } } } }
void end_all_sessions() { connection *cxt; int cxkey; for (cxkey = 0; cxkey < BUCKET_SIZE; cxkey++) { cxt = bucket[cxkey]; while (cxt != NULL) { connection *tmp = cxt; if(config.cflags & CONFIG_CXWRITE) log_connection(cxt, stdout, CX_ENDED); cxt = cxt->next; del_connection(tmp, &bucket[cxkey]); if (cxt == NULL) { bucket[cxkey] = NULL; } } } }
void end_sessions() { connection *cxt; time_t check_time; check_time = config.tstamp.tv_sec; int ended, expired = 0; config.llcxt = 0; int iter; for (iter = 0; iter < BUCKET_SIZE; iter++) { cxt = bucket[iter]; while (cxt != NULL) { ended = 0; config.llcxt++; /* TCP */ if (cxt->proto == IP_PROTO_TCP) { /* FIN from both sides */ if (cxt->s_tcpFlags & TF_FIN && cxt->d_tcpFlags & TF_FIN && (check_time - cxt->last_pkt_time) > 5) { ended = 1; } /* RST from either side */ else if ((cxt->s_tcpFlags & TF_RST || cxt->d_tcpFlags & TF_RST) && (check_time - cxt->last_pkt_time) > 5) { ended = 1; } else if ((check_time - cxt->last_pkt_time) > TCP_TIMEOUT) { expired = 1; } } /* UDP */ else if (cxt->proto == IP_PROTO_UDP && (check_time - cxt->last_pkt_time) > UDP_TIMEOUT) { expired = 1; } /* ICMP */ else if (cxt->proto == IP_PROTO_ICMP || cxt->proto == IP6_PROTO_ICMP) { if ((check_time - cxt->last_pkt_time) > ICMP_TIMEOUT) { expired = 1; } } /* All other protocols */ else if ((check_time - cxt->last_pkt_time) > OTHER_TIMEOUT) { expired = 1; } if (ended == 1 || expired == 1) { /* Remove from the hash */ if (cxt->prev) cxt->prev->next = cxt->next; if (cxt->next) cxt->next->prev = cxt->prev; connection *tmp = cxt; connection *tmp_pre = cxt->prev; ended = expired = 0; cxt = cxt->next; del_connection(tmp, &bucket[iter]); if (cxt == NULL && tmp_pre == NULL) { bucket[iter] = NULL; } } else { cxt = cxt->next; } } } dlog("CXT in list before cleaning: %10u\n", config.llcxt); dlog("CXT in list after cleaning: %10u\n", config.curcxt); }
void end_sessions() { connection *cxt; time_t check_time; check_time = time(NULL); int ended, expired = 0; uint32_t curcxt = 0; int iter; for (iter = 0; iter < BUCKET_SIZE; iter++) { cxt = bucket[iter]; while (cxt != NULL) { ended = 0; curcxt++; /* TCP */ if (cxt->proto == IP_PROTO_TCP) { /* * FIN from both sides */ if (cxt->s_tcpFlags & TF_FIN && cxt->d_tcpFlags & TF_FIN && (check_time - cxt->last_pkt_time) > 5) { ended = 1; } /* * RST from either side */ else if ((cxt->s_tcpFlags & TF_RST || cxt->d_tcpFlags & TF_RST) && (check_time - cxt->last_pkt_time) > 5) { ended = 1; } else if ((check_time - cxt->last_pkt_time) > TCP_TIMEOUT) { expired = 1; } } /* UDP */ else if (cxt->proto == IP_PROTO_UDP && (check_time - cxt->last_pkt_time) > 60) { expired = 1; } /* ICMP */ else if (cxt->proto == IP_PROTO_ICMP || cxt->proto == IP6_PROTO_ICMP) { if ((check_time - cxt->last_pkt_time) > 60) { expired = 1; } } /* All Other protocols */ else if ((check_time - cxt->last_pkt_time) > TCP_TIMEOUT) { expired = 1; } if (ended == 1 || expired == 1) { /* remove from the hash */ if (cxt->prev) cxt->prev->next = cxt->next; if (cxt->next) cxt->next->prev = cxt->prev; connection *tmp = cxt; if (config.cflags & CONFIG_CXWRITE) { if (expired == 1) log_connection(cxt, stdout, CX_EXPIRE); else if (ended == 1) log_connection(cxt, stdout, CX_ENDED); } ended = expired = 0; cxt = cxt->prev; //CLEAR_CXT(tmp); del_connection(tmp, &bucket[iter]); if (cxt == NULL) { bucket[iter] = NULL; } } else { cxt = cxt->prev; } } } }
void on_event_error_dft(myconn_t* c) { printf("error [%d]\n", c->fd); unpoll_connection(epollfd, c); del_connection(c); }
void on_event_hup_dft(myconn_t* c) { printf("hup [%d]\n", c->fd); unpoll_connection(epollfd, c); del_connection(c); }