static void mom_close_client( int sfds) /* connection socket */ { struct batch_request *preq; close_conn(sfds, FALSE); /* close the connection */ preq = (struct batch_request *)GET_NEXT(svr_requests); while (preq != NULL) { /* list of outstanding requests */ if (preq->rq_conn == sfds) preq->rq_conn = -1; if (preq->rq_orgconn == sfds) preq->rq_orgconn = -1; preq = (struct batch_request *)GET_NEXT(preq->rq_link); } return; } /* END mom_close_client() */
void tasks_free( job *pj) { task *tp = (task *)GET_NEXT(pj->ji_tasks); obitent *op; infoent *ip; container::item_container<struct tcp_chan *> freed_chans; while (tp != NULL) { op = (obitent *)GET_NEXT(tp->ti_obits); while (op != NULL) { delete_link(&op->oe_next); free(op); op = (obitent *)GET_NEXT(tp->ti_obits); } /* END while (op != NULL) */ ip = (infoent *)GET_NEXT(tp->ti_info); while (ip != NULL) { delete_link(&ip->ie_next); free(ip->ie_name); free(ip->ie_info); free(ip); ip = (infoent *)GET_NEXT(tp->ti_info); } if (tp->ti_chan != NULL) { char ptr[50]; sprintf(ptr,"%p",(void *)tp->ti_chan); freed_chans.lock(); if(freed_chans.insert(tp->ti_chan,ptr)) { close_conn(tp->ti_chan->sock, FALSE); DIS_tcp_cleanup(tp->ti_chan); } freed_chans.unlock(); tp->ti_chan = NULL; } delete_link(&tp->ti_jobtask); free(tp); tp = (task *)GET_NEXT(pj->ji_tasks); } /* END while (tp != NULL) */ return; } /* END tasks_free() */
/** * Only used in TCP mode. */ static err_t atcp_recv_cb(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { struct ttcp* ttcp = arg; if (err == ERR_OK && p != NULL) { INFO_TCP("pcb:%p pbuf: %p err:%d len:%d\n", pcb, p, err, p->tot_len); DATA_LED_ON(); /* for print_stats() */ ttcp->recved += p->tot_len; if ((ttcp->verbose)||(verboseDebug & INFO_TCP_FLAG)) { INFO_TCP("len:%d\n",p->tot_len); DUMP_TCP(p->payload, p->tot_len); ttcp->print_cnt++; } insert_pBuf(p, ttcp->sock, (void*) pcb); tcp_recved(pcb, p->tot_len); pbuf_free(p); DATA_LED_OFF(); } /* p will be NULL when remote end is done */ if (err == ERR_OK && p == NULL) { INFO_TCP("atcp_recv_cb p=NULL\n"); close_conn(ttcp); } if (err!=ERR_OK) WARN("err=%d p=%p\n", err, p); return ERR_OK; }
static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { struct http_state *http; http = arg; if (err == ERR_OK && p != NULL) { struct pbuf *q; for (q = p; q; q = q->next) httpd_receive(http, q->payload, q->len); pbuf_free(p); /* Inform TCP that we have taken the data. */ tcp_recved(pcb, p->tot_len); send_data(pcb, http); } if (err == ERR_OK && p == NULL) { printf("received NULL\n"); close_conn(pcb, http); } return ERR_OK; }
static err_t echo_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { os_printf("Echo recv!\n"); HTTPServer* self = (HTTPServer*) arg; int i; int len; char *pc; if (err == ERR_OK && p != NULL) { // Inform TCP that we have taken the data. tcp_recved(pcb, p->tot_len); os_printf("TCP recv no error!\n"); //pointer to the pay load pc = (char *)p->payload; //size of the pay load len = p->tot_len; uint16_t bufferLength = 1500; char buffer[bufferLength]; self->onReceive(pc); self->receiveCallback(pc, buffer); pbuf_free(p); err = tcp_write(pcb, buffer, bufferLength, 0); tcp_sent(pcb, data_sent); } if (err == ERR_OK && p == NULL) { close_conn(pcb); } return ERR_OK; }
void tasks_free( job *pj) { task *tp = (task *)GET_NEXT(pj->ji_tasks); obitent *op; infoent *ip; resizable_array *freed_chans = initialize_resizable_array(30); while (tp != NULL) { op = (obitent *)GET_NEXT(tp->ti_obits); while (op != NULL) { delete_link(&op->oe_next); free(op); op = (obitent *)GET_NEXT(tp->ti_obits); } /* END while (op != NULL) */ ip = (infoent *)GET_NEXT(tp->ti_info); while (ip != NULL) { delete_link(&ip->ie_next); free(ip->ie_name); free(ip->ie_info); free(ip); ip = (infoent *)GET_NEXT(tp->ti_info); } if (tp->ti_chan != NULL) { if (is_present(freed_chans, tp->ti_chan) == FALSE) { insert_thing(freed_chans, tp->ti_chan); close_conn(tp->ti_chan->sock, FALSE); DIS_tcp_cleanup(tp->ti_chan); } tp->ti_chan = NULL; } delete_link(&tp->ti_jobtask); free(tp); tp = (task *)GET_NEXT(pj->ji_tasks); } /* END while (tp != NULL) */ free_resizable_array(freed_chans); return; } /* END tasks_free() */
/********************************************************************** *%FUNCTION: rp_fatal *%ARGUMENTS: * str -- error message *%RETURNS: * Nothing *%DESCRIPTION: * Prints a message to stderr and syslog and exits. ***********************************************************************/ void rp_fatal(char const *str) { printErr(str); sendPADTf(conn, "RP-PPPoE: %.256s", str); close_conn(0); cleanup_conn(); exit(1); }
/*..........................................................................*/ static err_t server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { char *data; struct server_state *hs = arg; // DEBUG_PRINT("server_recv 0x%08x\n", pcb); if (err != ERR_OK) return ERR_OK; //TODO: is this return value OK? if (p == NULL) { //TODO: is this OK? close_conn(pcb, hs); return ERR_OK; } /* Inform TCP that we have taken the data. */ tcp_recved(pcb, p->tot_len); data = p->payload; // Generate output data /* if (file) { hs->file = file->data; LWIP_ASSERT("File length must be positive!", (file->len >= 0)); hs->left = file->len; hs->retries = 0; pbuf_free(p); } else { hs->file = NULL; hs->left = 0; hs->retries = 0; } */ /*Tell TCP that we wish be to informed of data that has been * successfully sent by a call to the server_sent() function. */ // tcp_sent(pcb, server_sent); /* Start sending the headers and file data. */ // send_data(pcb, hs); pbuf_free(p); // If you wanna close the connection // close_conn(pcb, hs); return ERR_OK; }
static void rw_callback(picoev_loop* loop, int fd, int events, void* cb_arg) { if ((events & PICOEV_TIMEOUT) != 0) { /* timeout */ close_conn(loop, fd); } else if ((events & PICOEV_READ) != 0) { /* update timeout, and read */ char buf[1024]; ssize_t r; picoev_set_timeout(loop, fd, TIMEOUT_SECS); r = read(fd, buf, sizeof(buf)); switch (r) { case 0: /* connection closed by peer */ close_conn(loop, fd); break; case -1: /* error */ if (errno == EAGAIN || errno == EWOULDBLOCK) { /* try again later */ break; } else { /* fatal error */ close_conn(loop, fd); } break; default: /* got some data, send back */ if (write(fd, buf, r) != r) { close_conn(loop, fd); /* failed to send all data at once, close */ } break; } } }
int main( int argc, char* argv[] ) { assert( argc == 4 ); int epoll_fd = epoll_create( 100 ); start_conn( epoll_fd, atoi( argv[ 3 ] ), argv[1], atoi( argv[2] ) ); epoll_event events[ 10000 ]; char buffer[ 2048 ]; while ( 1 ) { int fds = epoll_wait( epoll_fd, events, 10000, 2000 ); for ( int i = 0; i < fds; i++ ) { int sockfd = events[i].data.fd; if ( events[i].events & EPOLLIN ) { if ( ! read_once( sockfd, buffer, 2048 ) ) { close_conn( epoll_fd, sockfd ); } struct epoll_event event; event.events = EPOLLOUT | EPOLLET | EPOLLERR; event.data.fd = sockfd; epoll_ctl( epoll_fd, EPOLL_CTL_MOD, sockfd, &event ); } else if( events[i].events & EPOLLOUT ) { if ( ! write_nbytes( sockfd, request, strlen( request ) ) ) { close_conn( epoll_fd, sockfd ); } struct epoll_event event; event.events = EPOLLIN | EPOLLET | EPOLLERR; event.data.fd = sockfd; epoll_ctl( epoll_fd, EPOLL_CTL_MOD, sockfd, &event ); } else if( events[i].events & EPOLLERR ) { close_conn( epoll_fd, sockfd ); } } } }
static void handle_data(int fd, short what, Connection *conn, int side) { int from = conn->fd[side]; int to = conn->fd[!side]; char buf[4096]; int nread; event_add(&conn->read_event[side], &cfg.timeout); if ((what & EV_TIMEOUT) != 0) { dump_buffer(conn); return; } if (conn->collecting_from != side) { dump_buffer(conn); } nread = read(from, buf, sizeof(buf)); switch (nread) { case 0: close_conn(conn, from); break; case -1: if (errno != EAGAIN && errno != EINTR) { close_conn(conn, -1); } break; default: if (cfg.have_otherhost) { if (write(to, buf, nread) != nread) { die("Write to fd %d failed: %d (%s)\n", errno, strerror(errno)); } } buf_append_n(conn->txfer_buf, buf, nread); conn->last_read_time = time(NULL); conn->collecting_from = side; break; } }
int main(int argc,char* argv[]) { assert(argc == 4); int epoll_fd = epoll_create(100); char ip_array[600] = "192.168.6.101,192.168.6.102,192.168.6.103,192.168.6.104,192.168.6.105,192.168.6.106,192.168.6.107,192.168.6.108,192.168.6.109,192.168.6.111,192.168.6.112,192.168.6.113,192.168.6.114,192.168.6.115,192.168.6.116,192.168.6.117,192.168.6.118,192.168.6.119"; char *ori_ip = NULL; char delims[] =","; ori_ip = strtok(ip_array,delims); while(ori_ip != NULL) { start_conn(epoll_fd,atoi(argv[3]),argv[1],atoi(argv[2]),ori_ip); ori_ip = strtok(NULL,delims); } struct epoll_event events[10000]; char buffer[2048]; while(1) { int fds = epoll_wait(epoll_fd,events,10000,2000); for(int i = 0; i<fds;i++) { int sockfd = events[i].data.fd; if(events[i].events & EPOLLIN) { struct epoll_event event; event.events = EPOLLOUT | EPOLLET | EPOLLERR; event.data.fd = sockfd; epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event); } else if( events[i].events & EPOLLOUT) { struct epoll_event event; event.events = EPOLLIN | EPOLLET | EPOLLERR; event.data.fd = sockfd; epoll_ctl(epoll_fd,EPOLL_CTL_MOD,sockfd,&event); } else if (events[i].events & EPOLLERR) { close_conn(epoll_fd,sockfd); } } } }
/********************************************************************** *%FUNCTION: fatalSys *%ARGUMENTS: * str -- error message *%RETURNS: * Nothing *%DESCRIPTION: * Prints a message plus the errno value to stderr and syslog and exits. * ***********************************************************************/ void fatalSys(char const *str) { char buf[1024]; int i = errno; sprintf(buf, "%.256s: %.256s", str, strerror(i)); printErr(buf); sprintf(buf, "RP-PPPoE: %.256s: %.256s", str, strerror(i)); sendPADT(conn, buf); close_conn(0); cleanup_conn(); exit(1); }
/* Returns 1 if a failover server candidate is available. Close connection and return 0 if none was found. */ int failover_server(int conn) { int server = conns[conn].server; DEBUG(2, "failover_server(%d): server = %d", conn, server); if (server_alg & ALG_STUBBORN) { DEBUG(2, "Won't failover because we are stubborn"); close_conn(conn); return 0; } if (server == ABUSE_SERVER) { DEBUG(2, "Won't failover from abuse server (%d)", abuse_server); close_conn(conn); return 0; } if (server == EMERGENCY_SERVER) { DEBUG(2, "Already using emergency server (%d), won't fail over", emerg_server); close_conn(conn); return 0; } if (conns[conn].upfd != -1) { close(conns[conn].upfd); conns[conn].upfd = -1; } /* there needs to be at least two regular servers in order to fail over to something else */ /* and if we couldn't find a candidate for initial_server, we're not going to find one now */ if (nservers > 1 && server != NO_SERVER) { DEBUG(2, "Trying to find failover server. server = %d, initial = %d, nservers = %d", server, conns[conn].initial, nservers); do { server = (server+1) % nservers; DEBUG(2, "Intend to try server %d", server); if (try_server(server, conn)) return 1; } while (server != conns[conn].initial); } DEBUG(1, "using emergency server, remember to reset flag"); emergency = 1; if (try_server(emerg_server, conn)) return 1; close_conn(conn); return 0; }
static int pxy_http_epollout(int fd, void * private_data) { con_t * con = (con_t *)private_data; int ret; int retlen; UNUSED_ARGUMENT(fd); if (CHECK_CON_FLAG(con, CONF_SYN_SENT)) { UNSET_CON_FLAG(con, CONF_SYN_SENT); retlen=sizeof(ret); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&ret, (socklen_t *)&retlen)) { DBG_LOG(MSG, MOD_PROXYD, "getsockopt(fd=%d) failed, errno=%d", fd, errno); close_conn(fd); return TRUE; } if(ret) { DBG_LOG(MSG, MOD_PROXYD, "connect(fd=%d) failed, ret=%d", fd, ret); close_conn(fd); return TRUE; } /* * When one of the fd add to epollin event fail, close fd and peer_fd. */ if ((pxy_NM_add_event_epollin(fd)==FALSE) || (pxy_NM_add_event_epollin(con->peer_fd)==FALSE)) { close_conn(fd); } } else { /* Resent data */ forward_data_to_peer(con); } return TRUE; }
/* -------------------initiate ftp server------------------- */ int run_ftp(int port) { memset(&listen_conn, 0, sizeof(connection)); listen_conn.saddr.sin_port = htons(port); if (create_listen_socket(&listen_conn) < 0) { printlog("%s","create_listen_socket FAILED\n"); exit(1); } for (;;) { if (accept_connection( &listen_conn, &sconn) < 0) { printlog("%s","accept_connection FAILED\n"); continue; } signal(SIGCHLD, SIG_IGN); if (fork() == 0) { close_conn(listen_conn); status = CONNECTED; send_greetings(); command_loop(); close_conn(sconn); return 0; } close_conn(sconn); } return 0; }
static void call_rack_app(client_t *client, picoev_loop* loop) { int ret; if(!process_rack_app(client)){ //Internal Server Error client->bad_request_code = 500; send_error_page(client); close_conn(client, loop); return; } ret = response_start(client); /* printf("response_start done: %d\n", ret); */ switch(ret){ case -1: // Internal Server Error client->bad_request_code = 500; send_error_page(client); close_conn(client, loop); return; case 0: // continue // set callback #ifdef DEBUG printf("set write callback %d \n", ret); #endif //clear event picoev_del(loop, client->fd); picoev_add(loop, client->fd, PICOEV_WRITE, WRITE_TIMEOUT_SECS, w_callback, (void *)client); return; default: // send OK close_conn(client, loop); } }
void close_client(int sfds) { struct batch_request *preq; close_conn(sfds); /* close the connection */ preq = (struct batch_request *)GET_NEXT(svr_requests); while (preq) { /* list of outstanding requests */ if (preq->rq_conn == sfds) preq->rq_conn = -1; if (preq->rq_orgconn == sfds) preq->rq_orgconn = -1; preq = (struct batch_request *)GET_NEXT(preq->rq_link); } }
static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) { struct http_state *http; http = arg; http->retries = 0; if ((http->sendbuffer_read != http->sendbuffer_write) || (http->state_server != HTTPD_SERVER_CLOSE)) send_data(pcb, http); else close_conn(pcb, http); return ERR_OK; }
void service::process() { HTTP_CODE read_ret=process_read(); printf("process() read_ret=%d\n",read_ret); if(read_ret==NO_REQUEST) { modfd(m_epollfd,m_sockfd,EPOLLIN); return; } bool write_ret=process_write(read_ret); if(!write_ret) { close_conn(); } modfd(m_epollfd,m_sockfd,EPOLLOUT); }
void sigint_handler(int sig) { int status; printf("Cleaning up...\n"); close_conn(conn_hdls.fp_ptr, conn_hdls.conn_ptr); if (conn_hdls.htsock_ptr != NULL) { status = close(*(conn_hdls.htsock_ptr)); PRINTV("Closing socket (returned code: %d)\n", status); } free_am(); exit(EXIT_FAILURE); }
int main (int argc, char *argv[]) { struct queryopt opt; struct ofields *fields; struct select_doc *s_doc; struct query_doc *qu_doc; struct db_connect *db_conn; struct output *out; struct db_cursor *db_c; struct results *res; opt.e_skip = 0; // standard opt.e_ret = 0; // standard opt.bsever = 0; opt.blevel = 0; opt.bdate = 0; opt.bdateu = 0; opt.bdatef = 0; opt.bmsg = 0; opt.bskip = 0; opt.bsys = 0; getoptions(argc, argv, &opt); qu_doc = create_query(&opt); // crate query s_doc = create_select(); db_conn = create_conn(); // create connection out = launch_query(&opt, s_doc, qu_doc, db_conn); // launch the query db_c = open_cursor(db_conn, out); // open cursor while (cursor_next(db_c)) { res = read_data(db_c); fields = get_data(res); formater(fields); // formate output free(fields); free(res); } free_cursor(db_c); close_conn(db_conn); free(out); free(s_doc); free(qu_doc); return (0); }
void tasks_free( job *pj) { task *tp = (task *)GET_NEXT(pj->ji_tasks); obitent *op; infoent *ip; while (tp != NULL) { op = (obitent *)GET_NEXT(tp->ti_obits); while (op != NULL) { delete_link(&op->oe_next); free(op); op = (obitent *)GET_NEXT(tp->ti_obits); } /* END while (op != NULL) */ ip = (infoent *)GET_NEXT(tp->ti_info); while (ip != NULL) { delete_link(&ip->ie_next); free(ip->ie_name); free(ip->ie_info); free(ip); ip = (infoent *)GET_NEXT(tp->ti_info); } close_conn(tp->ti_fd); delete_link(&tp->ti_jobtask); free(tp); tp = (task *)GET_NEXT(pj->ji_tasks); } /* END while (tp != NULL) */ return; } /* END tasks_free() */
int CNetComm::safe_net_out (const char *sndbuf, int sndlen) { DEBUG_LOG("enter the safe_net_out ...."); int len = net_out(sndbuf, sndlen); if (len == -1) { DEBUG_LOG("reconnect the server ..."); if (open_conn() == -1) { close_conn(); DEBUG_LOG("reconnect the server fail !"); return -1; } return net_out(sndbuf, sndlen); //send the data second time } return len; }
/*-----------------------------------------------------------------------------------*/ static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) { struct http_state *hs; hs = arg; if (hs->left > 0) { send_data(pcb, hs); } else { close_conn(pcb, hs); } return ERR_OK; }
// Called when data has been sent. static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) { struct http_state *hs; CYG_TEST_INFO("Sent data"); hs = arg; hs->retries = 0; if (hs->left > 0) { send_data(pcb, hs); } else { close_conn(pcb, hs); } return ERR_OK; }
/*-----------------------------------------------------------------------------------*/ static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len) { struct http_state *hs; LWIP_UNUSED_ARG(len); hs = arg; hs->retries = 0; if (hs->left > 0) { send_data(pcb, hs); } else { close_conn(pcb, hs); } return ERR_OK; }
static inline int check_status_code(client_t *client) { request *req; req = client->request_queue->head; if(req->bad_request_code > 200){ //error //shift #ifdef DEBUG printf("bad_request_code \n"); #endif set_current_request(client); send_error_page(client); close_conn(client, main_loop); return 0; } return 1; }
/********************************************************************** * %FUNCTION: PPPOEDisconnectDevice * %ARGUMENTS: * None * %RETURNS: * Nothing * %DESCRIPTION: * Disconnects PPPoE device ***********************************************************************/ static void PPPOEDisconnectDevice(void) { struct sockaddr_pppox sp; sp.sa_family = AF_PPPOX; sp.sa_protocol = PX_PROTO_OE; sp.sa_addr.pppoe.sid = 0; memset(sp.sa_addr.pppoe.dev, 0, IFNAMSIZ); memcpy(sp.sa_addr.pppoe.dev, conn->ifName, strlen(conn->ifName)); memcpy(sp.sa_addr.pppoe.remote, conn->peerEth, ETH_ALEN); if (connect(conn->sessionSocket, (struct sockaddr *) &sp, sizeof(struct sockaddr_pppox)) < 0) { if (errno != EALREADY) warn("Failed to disconnect PPPoE socket: %d %m", errno); } /* Send PADT to reset the session unresponsive at buggy nas */ close_conn(1); }
static int receive_data(int * fd, char * buf, ssize_t max_bytes, int * poll) { ssize_t bytes; if ((bytes = read(*fd, buf, max_bytes)) < 0) { perror("read"); *poll = 1; return 0; } else if (bytes == 0) { close_conn(fd); *poll = 0; return 0; } else { if (bytes == max_bytes) *poll = 1; else *poll = 0; #if DEBUG { ssize_t i; fprintf(stderr, "Received %d bytes on %d:\n", bytes, *fd); for (i = 0; i < bytes; ++ i) { if ((i & 0xf) == 0) printf(" "); printf("%02x", buf[i] & 0xff); if (((i+1) & 0xf) == 0 || i + 1 == bytes) printf("\n"); else printf(" "); } } #endif return bytes; } }