void rx_server_init() { int i; conn_t *c = conns; // NB: implied ordering here is assumed in code elsewhere for (i=0; i<RX_CHANS; i++) { conn_init(c, STREAM_SOUND, i); c++; conn_init(c, STREAM_WATERFALL, i); c++; } }
void *search_speedtest( void *r ) { struct sigaction actions; memset(&actions, 0, sizeof(actions)); sigemptyset(&actions.sa_mask); actions.sa_flags = 0; actions.sa_handler = thread_exit_handler; sigaction(SIGUSR1,&actions,NULL); search_t *results = r; conn_t conn[1]; // int oldstate; /* Allow this thread to be killed at any time. */ // pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate ); // pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate ); memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; if( !conn_set( conn, results->url ) ) results->speed = SPEED_ERROR; else if( !conn_init( conn ) ) results->speed = SPEED_ERROR; else if( !conn_info( conn ) ) results->speed = SPEED_ERROR; else if( conn->size == results->size ) /* Add one because it mustn't be zero */ results->speed = 1 + 1000 * ( gettime() - results->speed_start_time ); else results->speed = SPEED_ERROR; conn_disconnect( conn ); return( NULL ); }
NETSERVER *netserver_open(NETADDR bindaddr, int max_clients, int flags) { int i; NETSERVER *server; NETSOCKET socket = net_udp_create(bindaddr); if(socket == NETSOCKET_INVALID) return 0; server = (NETSERVER *)mem_alloc(sizeof(NETSERVER), 1); mem_zero(server, sizeof(NETSERVER)); server->socket = socket; server->max_clients = max_clients; if(server->max_clients > NET_MAX_CLIENTS) server->max_clients = NET_MAX_CLIENTS; if(server->max_clients < 1) server->max_clients = 1; for(i = 0; i < NET_MAX_CLIENTS; i++) conn_init(&server->slots[i].conn, server->socket); /* setup all pointers for bans */ for(i = 1; i < NET_SERVER_MAXBANS-1; i++) { server->banpool[i].next = &server->banpool[i+1]; server->banpool[i].prev = &server->banpool[i-1]; } server->banpool[0].next = &server->banpool[1]; server->banpool[NET_SERVER_MAXBANS-1].prev = &server->banpool[NET_SERVER_MAXBANS-2]; server->banpool_firstfree = &server->banpool[0]; return server; }
int conn_setup( conn_t *conn ) { if( conn->ftp->fd <= 0 && conn->http->fd <= 0 ) if( !conn_init( conn ) ) return( 0 ); if( conn->proto == PROTO_FTP && !conn->proxy ) { if( !ftp_data( conn->ftp ) ) /* Set up data connnection */ return( 0 ); conn->fd = conn->ftp->data_fd; if( conn->currentbyte ) { ftp_command( conn->ftp, "REST %i", conn->currentbyte ); if( ftp_wait( conn->ftp ) / 100 != 3 && conn->ftp->status / 100 != 2 ) return( 0 ); } } else { char s[MAX_STRING]; snprintf( s, MAX_STRING, "%s%s", conn->dir, conn->file ); conn->http->firstbyte = conn->currentbyte; conn->http->lastbyte = conn->lastbyte; http_get( conn->http, s ); } return( 1 ); }
void _mainloop(void *zsock) { while(true) { zmq_msg_t *zmsg = malloc(sizeof(zmq_msg_t)); zmq_msg_init(zmsg); // zmq_recv blocks with default flags, so we don't need to usleep int rc = zmq_recv(zsock, zmsg, 0); if(rc == 0) // zmq_recv 0 is non-error { int size = zmq_msg_size(zmsg); char *_msg = malloc(size + 1); memcpy(_msg, zmq_msg_data(zmsg), size); zmq_msg_close(zmsg); _msg[size] = 0; conn_t *connection = conn_init(zsock); char *resp = conn_process(connection, _msg); zmq_msg_t *zmqresp = malloc(sizeof(zmq_msg_t)); zmq_msg_init_size(zmqresp, strlen(resp)); memcpy(zmq_msg_data(zmqresp), resp, strlen(resp)); zmq_send(zsock, zmqresp, 0); zmq_msg_close(zmqresp); free(_msg); } } }
int main(int argc, char** argv){ /* 主线程主体流程: conn_init; thread_init; 还得有一个 stat_init stats的结构是用来记录当前的状态的,stats是一个静态变量 server_socket 请求,分发 注册事件loop */ int retval; main_base = event_init(); FILE *portnumber_file = NULL; portnumber_file = fopen("/tmp/portnumber.file", "a"); stats_init(); conn_init(); thread_init(NUM_OF_THREADS); server_socket("127.0.0.1", SERVER_PORT, tcp_transport, portnumber_file); if (event_base_loop(main_base, 0) != 0) { printf("event_base_loop error"); retval = EXIT_FAILURE; } return retval; exit(0); }
int conn_setup( conn_t *conn ) { if( conn->ftp->fd <= 0 && conn->http->fd <= 0 ) if( !conn_init( conn ) ) return( 0 ); if( conn->proto == PROTO_FTP && !conn->proxy ) { if( !ftp_data( conn->ftp ) ) /* Set up data connnection */ return( 0 ); conn->fd = conn->ftp->data_fd; if( conn->currentbyte ) { ftp_command( conn->ftp, "REST %lld", conn->currentbyte ); if( ftp_wait( conn->ftp ) / 100 != 3 && conn->ftp->status / 100 != 2 ) return( 0 ); } } else { char s[MAX_STRING]; int i; snprintf( s, MAX_STRING, "%s%s", conn->dir, conn->file ); conn->http->firstbyte = conn->currentbyte; conn->http->lastbyte = conn->lastbyte; http_get( conn->http, s ); http_addheader( conn->http, "User-Agent: %s", conn->conf->user_agent ); for( i = 0; i < conn->conf->add_header_count; i++) http_addheader( conn->http, "%s", conn->conf->add_header[i] ); } return( 1 ); }
int slave(context_t* ctx) { hlink_t* hardlinks = NULL; // The hardlinks we found while performing SCAN conn_t cn; // Config (void)ctx; conn_init(&cn); cn.infd = 0; cn.outfd = 1; // Main char line[1024]; ssize_t l; while((l = conn_readline(&cn, line, sizeof line))) { proto_delegator(&cn,line,&hardlinks); } // close down conn_free(&cn); // Free hardlinks hlink_t* n = NULL; hlink_t* p; for(p = hardlinks; p; p = n) { n = (hlink_t*)((node_t*)p)->next; free(p); } hardlinks = NULL; return 0; }
void *search_speedtest( void *r ) { search_t *results = r; conn_t conn[1]; int oldstate; /* Allow this thread to be killed at any time. */ pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &oldstate ); pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, &oldstate ); memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; if( !conn_set( conn, results->url ) ) results->speed = SPEED_ERROR; else if( !conn_init( conn ) ) results->speed = SPEED_ERROR; else if( !conn_info( conn ) ) results->speed = SPEED_ERROR; else if( conn->size == results->size ) /* Add one because it mustn't be zero */ results->speed = 1 + 1000 * ( gettime() - results->speed_start_time ); else results->speed = SPEED_ERROR; conn_disconnect( conn ); return( NULL ); }
struct context * core_start(struct instance *nci) { struct context *ctx; //last = dn_msec_now(); mbuf_init(nci); msg_init(); conn_init(); ctx = core_ctx_create(nci); if (ctx != NULL) { nci->ctx = ctx; if (TRACING_LEVEL == LOG_VVERB) { crypto_check(); } return ctx; } conn_deinit(); msg_deinit(); dmsg_deinit(); mbuf_deinit(); return NULL; }
void base_server_init(const Setup *setup) { base_conf_init(setup); /*cout << "event method: " << event_base_get_method(main_base) << endl;*/ evthread_use_pthreads(); thread_init(); main_base = get_main_thread()->get_event_base(); conn_init(); clock_handler(0, 0, 0); }
Object * object_new (Object_Type type) { struct free_list_el *el; Event_Type event = 0; size_t obj_size; Any_Type arg; Object *obj; obj_size = type_size[type]; if (free_list[type]) { el = free_list[type]; free_list[type] = el->next; obj = (Object *) el; } else { obj = malloc (obj_size); if (!obj) { fprintf (stderr, "%s.object_new: %s\n", prog_name, strerror (errno)); return 0; } } memset (obj, 0, obj_size); obj->ref_count = 1; obj->type = type; switch (type) { case OBJ_CALL: call_init ((Call *) obj); event = EV_CALL_NEW; break; case OBJ_CONN: conn_init ((Conn *) obj); event = EV_CONN_NEW; break; case OBJ_SESS: sess_init ((Sess *) obj); event = EV_SESS_NEW; break; default: panic ("object_new: bad object type %d\n", type); break; } arg.l = 0; event_signal (event, obj, arg); return obj; }
int nc_connect_init(struct context *ctx) { ctx->cb = nc_alloc(sizeof(struct conn_base)); if (ctx->cb == NULL) { return NC_ENOMEM; } conn_init(ctx->cb); return NC_OK; }
static int single_cmd(int argc, char **argv, void *pool, const char *file, cmd_params_st *params) { CONN_TYPE *conn; char *line; int ret; conn = conn_init(pool, file); line = merge_args(argc, argv); ret = handle_cmd(conn, line, params); free(line); return ret; }
struct connection *conn_create(struct context *ctx) { struct connection *conn; if ((conn = TAILQ_FIRST(&ctx->conns)) != NULL && conn->fd == -1) { LOG(DEBUG, "connection get cache"); TAILQ_REMOVE(&ctx->conns, conn, next); ctx->mstats.free_conns--; } else { conn = malloc(sizeof(struct connection)); } conn_init(conn, ctx); ctx->mstats.conns++; return conn; }
void rx_server_remove(conn_t *c) { c->stop_data = TRUE; c->mc = NULL; if (c->type == STREAM_SOUND) loguser(c, "(LEAVING)"); webserver_connection_cleanup(c); if (c->user) wrx_free("user", c->user); if (c->geo) wrx_free("geo", c->geo); int task = c->task; conn_init(c, c->type, c->rx_channel); TaskRemove(task); panic("shouldn't return"); }
int conn_setup(conn_t *conn) { #if WIN32 if (INVALID_SOCKET == conn->ftp->fd && INVALID_SOCKET == conn->http->fd) #else if (conn->ftp->fd <= 0 && conn->http->fd <= 0) #endif { if (!conn_init(conn)) { return 0; } } if (conn->proto == PROTO_FTP && !conn->proxy) { /* Set up data connnection */ if(!ftp_data(conn->ftp)) { return 0; } conn->fd = conn->ftp->data_fd; if (conn->currentbyte) { ftp_command(conn->ftp, "REST %lld", conn->currentbyte); if (ftp_wait(conn->ftp) / 100 != 3 && conn->ftp->status / 100 != 2) { return 0; } } } else { char s[MAX_STRING]; int i; snprintf(s, MAX_STRING, "%s%s", conn->dir, conn->file); conn->http->firstbyte = conn->currentbyte; conn->http->lastbyte = conn->lastbyte; http_get(conn->http, s); http_addheader(conn->http, "User-Agent: %s", conn->conf->user_agent); for (i = 0; i < conn->conf->add_header_count; i++) { http_addheader(conn->http, "%s", conn->conf->add_header[i]); } } return 1; }
static conn_t *conn_alloc(void) { conn_t *c; c = genpool_alloc_page(conn_pool); if(c == NULL){ log(g_log, "genpool alloc page error\n"); return NULL; } if(conn_init(c) < 0){ genpool_release_page(conn_pool, c); return NULL; } return c; }
int main() { /*1.0 loading system parameter form configure file*/ sysparam_init(); /*1.1 initiating the environments*/ evn_init(); /*1.2 initiating the boards's hardware*/ dev_init(); /*1.3 initiating the system thread manage*/ thrd_init(); /*1.4 initiating the connection method*/ conn_init(); /*2.0 enter the main loop*/ while(1){ } exit(0); return 0; }
/** * Create a connection object without queueing * @retval 1 Success * @retval 0 failure, conslt errno for more details */ struct conn * conn_create_noq(int sock, struct sockaddr * addr) { struct conn * conn; conn = malloc(sizeof (struct conn)); if (!conn) return NULL; conn_init(conn); conn->sock=sock; if (addr) sock_copyaddr(&conn->addr,addr); /* create the CAPWAP framentation manager */ conn->fragman = fragman_create(); if (conn->fragman==NULL){ conn_destroy(conn); return NULL; } /* set packet recieve and send methods */ conn->recv_packet = conn_recv_packet; conn->recv_packet_peek = conn_recv_packet_peek; conn->send_packet = conn_send_packet; /* misc settings */ conn->last_seqnum_received=-1; conn->mtu=1500; conn->cur_packet=0; conn->recv_timeout=1; conn->seqnum=-1; conn->write = conn->send_packet; conn->read = conn->recv_packet; return conn; }
struct context * core_start(struct instance *nci) { struct context *ctx; mbuf_init(nci); msg_init(); conn_init(); ctx = core_ctx_create(nci); if (ctx != NULL) { nci->ctx = ctx; return ctx; } conn_deinit(); msg_deinit(); mbuf_deinit(); return NULL; }
static BOOL init_structs(void ) { /* * Set the machine NETBIOS name if not already * set from the config file. */ if (!init_names()) return False; conn_init(); file_init(); /* for RPC pipes */ init_rpc_pipe_hnd(); init_dptrs(); secrets_init(); return True; }
inline void worker_accept(worker_t* self) { conn_t *conn; int sock; ev_watch(&self->loop, self->conn_queue[0], EV_READABLE, self); if (read(self->conn_queue[0], &sock, sizeof(sock)) != sizeof(sock)) { if (errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) printf("error reading from conn_queue\n"); return; } if (sock == -1) { self->running = 0; return; } conn = conn_init(4096); conn->sock = sock; conn->worker = self; conn_set_nonblock(conn); conn_read(conn); }
int search_makelist( search_t *results, char *url ) { int i, size = 8192, j = 0; char *s, *s1, *s2, *s3; conn_t conn[1]; double t; memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; t = gettime(); if( !conn_set( conn, url ) ) return( -1 ); if( !conn_init( conn ) ) return( -1 ); if( !conn_info( conn ) ) return( -1 ); strcpy( results[0].url, url ); results[0].speed = 1 + 1000 * ( gettime() - t ); results[0].size = conn->size; s = malloc( size ); sprintf( s, "http://www.filesearching.com/cgi-bin/s?q=%s&w=a&l=en&" "t=f&e=on&m=%i&o=n&s1=%lld&s2=%lld&x=15&y=15", conn->file, results->conf->search_amount, conn->size, conn->size ); conn_disconnect( conn ); memset( conn, 0, sizeof( conn_t ) ); conn->conf = results->conf; if( !conn_set( conn, s ) ) { free( s ); return( 1 ); } if( !conn_setup( conn ) ) { free( s ); return( 1 ); } if( !conn_exec( conn ) ) { free( s ); return( 1 ); } while( ( i = read( conn->fd, s + j, size - j ) ) > 0 ) { j += i; if( j + 10 >= size ) { size *= 2; s = realloc( s, size ); memset( s + size / 2, 0, size / 2 ); } } conn_disconnect( conn ); s1 = strstr( s, "<pre class=list" ); s1 = strchr( s1, '\n' ) + 1; if( strstr( s1, "</pre>" ) == NULL ) { /* Incomplete list */ free( s ); return( 1 ); } for( i = 1; strncmp( s1, "</pre>", 6 ) && i < results->conf->search_amount && *s1; i ++ ) { s3 = strchr( s1, '\n' ); *s3 = 0; s2 = strrstr( s1, "<a href=" ) + 8; *s3 = '\n'; s3 = strchr( s2, ' ' ); *s3 = 0; if( strcmp( results[0].url, s2 ) ) { strncpy( results[i].url, s2, MAX_STRING ); results[i].size = results[0].size; results[i].conf = results->conf; } else { /* The original URL might show up */ i --; } for( s1 = s3; *s1 != '\n'; s1 ++ ); s1 ++; } free( s ); return( i ); }
int main(int argc, char **argv) { char *line = NULL; CONN_TYPE *conn; const char *file = NULL; void *gl_pool; cmd_params_st params; memset(¶ms, 0, sizeof(params)); gl_pool = talloc_init("occtl"); if (gl_pool == NULL) { fprintf(stderr, "talloc init error\n"); exit(1); } ocsignal(SIGPIPE, SIG_IGN); if (argc > 1) { while (argc > 1 && argv[1][0] == '-') { if (argv[1][1] == 'j' || (argv[1][1] == '-' && argv[1][2] == 'j')) { params.json = 1; argv += 1; argc -= 1; } else if (argv[1][1] == 'n' || (argv[1][1] == '-' && argv[1][2] == 'n')) { params.no_pager = 1; argv += 1; argc -= 1; } else if (argv[1][1] == 'v' || (argv[1][1] == '-' && argv[1][2] == 'v')) { version(); exit(0); } else if (argc > 2 && (argv[1][1] == 's' || (argv[1][1] == '-' && argv[1][2] == 's'))) { file = talloc_strdup(gl_pool, argv[2]); if (argc == 3) { goto interactive; } argv += 2; argc -= 2; } else { usage(); exit(0); } } /* handle all arguments as a command */ exit(single_cmd(argc, argv, gl_pool, file, ¶ms)); } interactive: conn = conn_init(gl_pool, file); initialize_readline(); version(); for (;;) { line = rl_gets(line); if (line == NULL) return 0; handle_cmd(conn, line, 0); } conn_close(conn); return 0; }
int main(int argc, char **argv) { int u, c; // Default values. arguments.verbose = DEBUG_LEVEL_INFO; arguments.port = 0; arguments.channel = 0; arguments.channel2 = arguments.channel + 1; arguments.packetsize = 1024; arguments.buffersize = 64; arguments.interval = -1; arguments.startoncycle = -1; arguments.countdown = 4; arguments.printinterval = 100; arguments.rtprio = 0; // Parse our arguments; every option seen by `parse_opt' will // be reflected in `arguments'. if ( argp_parse ( &argp, argc, argv, 0, 0, &arguments ) ) { debugError("Could not parse command line\n" ); return -1; } // fix up arguments if(arguments.interval < 0) { arguments.interval = arguments.buffersize / 2; } else if (arguments.interval*2 > arguments.buffersize) { arguments.interval = arguments.buffersize / 2; } setDebugLevel(arguments.verbose); // set_realtime_priority(10); debugOutput(DEBUG_LEVEL_INFO, "ISO test: %ld bytes/packet, %ld packets/interrupt...\n", arguments.packetsize, arguments.interval); // setup the connection using the arguments #define MAX_NB_CONNECTIONS 8 struct connection conn[MAX_NB_CONNECTIONS]; int nb_connections = 0; if(conn_init(&conn[nb_connections], arguments.port, arguments.buffersize, arguments.interval, arguments.packetsize, arguments.channel, arguments.startoncycle, process_packet, NULL) != 0) { exit(-1); } nb_connections++; if(conn_init(&conn[nb_connections], arguments.port, arguments.buffersize, arguments.interval, arguments.packetsize, arguments.channel2, arguments.startoncycle, process_packet, NULL) != 0) { exit(-1); } nb_connections++; int status; debugOutput(DEBUG_LEVEL_INFO, "Start ISO receive...\n"); // start reception for(c = 0; c < nb_connections; c++) { if(conn_start_iso(&conn[c]) != 0) { for(u = 0; u < nb_connections; u++) { conn_free(&conn[u]); } exit(-1); } } // present two views on the event union { char buf[4096]; union fw_cdev_event e; } e; // read the packets that are sent using blocking I/O int nruns = arguments.countdown; __u64 front_tsps[nb_connections]; int highest_at = 0; int ticks_per_frame = TICKS_PER_SECOND / 48000; int half_a_frame = ticks_per_frame / 2; do { debugOutput(DEBUG_LEVEL_VERBOSE, "Run %d\n", (int)(arguments.countdown - nruns)); struct pollfd fds[nb_connections]; int status; for(u = 0; u < nb_connections; u++) { fds[u].fd = conn[u].fd; fds[u].events = POLLIN; } status = poll ( fds, nb_connections, -1 ); if(status < 0) { for(u = 0; u < nb_connections; u++) { conn_free(&conn[u]); } exit(-1); } for(c = 0; c < nb_connections; c++) { if(fds[c].revents & POLLIN ) { // read from the fd int nread = read ( conn[c].fd, &e, sizeof ( e ) ); if ( nread < 1 ) { debugError("Failed to read fd\n"); exit(-1); } // determine event type switch ( e.e.common.type ) { case FW_CDEV_EVENT_BUS_RESET: debugError("BUS RESET\n"); // goto out; goto nextevent; case FW_CDEV_EVENT_RESPONSE: debugError("Event response\n"); goto out; case FW_CDEV_EVENT_REQUEST: debugError("Event request\n"); goto out; case FW_CDEV_EVENT_ISO_INTERRUPT: break; } if(1) { // we have received an ISO interrupt int npackets = e.e.iso_interrupt.header_length / conn[c].header_size; debugOutput(DEBUG_LEVEL_VERBOSE, "ISO interrupt, header_len = %d, cycle = [%1ds, %4dc], " "received %d pkt\n", (int)e.e.iso_interrupt.header_length, (int)(e.e.iso_interrupt.cycle >> 13) & 0x7, (int)(e.e.iso_interrupt.cycle >> 0) & 0x1FFF, npackets); conn_consume(&conn[c], e.e.iso_interrupt.header, npackets); } } }
ib_conn_t *ngxib_conn_get(ngxib_req_ctx *rctx) { ngx_pool_cleanup_t *cln; ib_status_t rc; ib_engine_t *ib; /* Suggested by Maxim Dounin on dev list: * Look through pool cleanups for our conn * No race condition because no threads! */ for (cln = rctx->r->connection->pool->cleanup; cln != NULL; cln = cln->next) { if (cln->handler == conn_end) { /* Our connection is already initialized and it's here */ rctx->conn = cln->data; return rctx->conn->iconn; } } /* This connection is new, so initialize our conn struct * and notify IronBee. * * No threads, so no race condition here */ /* Acquire an IronBee engine */ /* n.b. pool cleanup for this and the ib_conn is conn_end, added below */ rc = ngxib_acquire_engine(&ib, rctx->r->connection->log); if (rc != IB_OK) { cleanup_return NULL; } ngx_regex_malloc_init(rctx->r->connection->pool); rctx->conn = ngx_palloc(rctx->r->connection->pool, sizeof(ngxib_conn_t)); if (rctx->conn == NULL) { ngxib_release_engine(ib, rctx->r->connection->log); cleanup_return NULL; } rctx->conn->engine = ib; rctx->conn->log = rctx->r->connection->log; rc = ib_conn_create(rctx->conn->engine, &rctx->conn->iconn, rctx->r->connection); if (rc != IB_OK) { ngxib_release_engine(ib, rctx->r->connection->log); cleanup_return NULL; } /* Initialize the connection */ rc = conn_init(rctx->conn->iconn); if (rc != IB_OK) { ngxib_release_engine(ib, rctx->r->connection->log); cleanup_return NULL; } ib_state_notify_conn_opened(rctx->conn->engine, rctx->conn->iconn); cln = ngx_pool_cleanup_add(rctx->r->connection->pool, 0); if (cln != NULL) { cln->handler = conn_end; cln->data = rctx->conn; } cleanup_return rctx->conn->iconn; }
/* Create a new axel_t structure */ axel_t *axel_new( conf_t *conf, int count, void *url ) { search_t *res; axel_t *axel; url_t *u; char *s; int i; axel = malloc( sizeof( axel_t ) ); memset( axel, 0, sizeof( axel_t ) ); *axel->conf = *conf; axel->conn = malloc( sizeof( conn_t ) * axel->conf->num_connections ); memset( axel->conn, 0, sizeof( conn_t ) * axel->conf->num_connections ); if( axel->conf->max_speed > 0 ) { if( (float) axel->conf->max_speed / axel->conf->buffer_size < 0.5 ) { if( axel->conf->verbose >= 2 ) axel_message( axel, _("Buffer resized for this speed.") ); axel->conf->buffer_size = axel->conf->max_speed; } axel->delay_time = (int) ( (float) 1000000 / axel->conf->max_speed * axel->conf->buffer_size * axel->conf->num_connections ); } if( buffer == NULL ) buffer = malloc( max( MAX_STRING, axel->conf->buffer_size ) ); if( count == 0 ) { axel->url = malloc( sizeof( url_t ) ); axel->url->next = axel->url; strncpy( axel->url->text, (char *) url, MAX_STRING ); } else { res = (search_t *) url; u = axel->url = malloc( sizeof( url_t ) ); for( i = 0; i < count; i ++ ) { strncpy( u->text, res[i].url, MAX_STRING ); if( i < count - 1 ) { u->next = malloc( sizeof( url_t ) ); u = u->next; } else { u->next = axel->url; } } } axel->conn[0].conf = axel->conf; if( !conn_set( &axel->conn[0], axel->url->text ) ) { axel_message( axel, _("Could not parse URL.\n") ); axel->ready = -1; return( axel ); } axel->conn[0].local_if = axel->conf->interfaces->text; axel->conf->interfaces = axel->conf->interfaces->next; strncpy( axel->filename, axel->conn[0].file, MAX_STRING ); http_decode( axel->filename ); if( *axel->filename == 0 ) /* Index page == no fn */ strncpy( axel->filename, axel->conf->default_filename, MAX_STRING ); if( ( s = strchr( axel->filename, '?' ) ) != NULL && axel->conf->strip_cgi_parameters ) *s = 0; /* Get rid of CGI parameters */ if( !conn_init( &axel->conn[0] ) ) { axel_message( axel, axel->conn[0].message ); axel->ready = -1; return( axel ); } /* This does more than just checking the file size, it all depends on the protocol used. */ if( !conn_info( &axel->conn[0] ) ) { axel_message( axel, axel->conn[0].message ); axel->ready = -1; return( axel ); } s = conn_url( axel->conn ); strncpy( axel->url->text, s, MAX_STRING ); if( ( axel->size = axel->conn[0].size ) != INT_MAX ) { if( axel->conf->verbose > 0 ) axel_message( axel, _("File size: %lld bytes"), axel->size ); } /* Wildcards in URL --> Get complete filename */ if( strchr( axel->filename, '*' ) || strchr( axel->filename, '?' ) ) strncpy( axel->filename, axel->conn[0].file, MAX_STRING ); return( axel ); }
void run(EpmdVars *g) { struct EPMD_SOCKADDR_IN iserv_addr[MAX_LISTEN_SOCKETS]; int listensock[MAX_LISTEN_SOCKETS]; int num_sockets; int i; int opt; unsigned short sport = g->port; node_init(g); g->conn = conn_init(g); #ifdef HAVE_SYSTEMD_DAEMON if (g->is_systemd) { int n; dbg_printf(g,2,"try to obtain sockets from systemd"); n = sd_listen_fds(0); if (n < 0) { dbg_perror(g,"cannot obtain sockets from systemd"); epmd_cleanup_exit(g,1); } else if (n == 0) { dbg_tty_printf(g,0,"systemd provides no sockets"); epmd_cleanup_exit(g,1); } else if (n > MAX_LISTEN_SOCKETS) { dbg_tty_printf(g,0,"cannot listen on more than %d IP addresses", MAX_LISTEN_SOCKETS); epmd_cleanup_exit(g,1); } num_sockets = n; for (i = 0; i < num_sockets; i++) { g->listenfd[i] = listensock[i] = SD_LISTEN_FDS_START + i; } } else { #endif /* HAVE_SYSTEMD_DAEMON */ dbg_printf(g,2,"try to initiate listening port %d", g->port); if (g->addresses != NULL && /* String contains non-separator characters if: */ g->addresses[strspn(g->addresses," ,")] != '\000') { char *tmp; char *token; int loopback_ok = 0; if ((tmp = (char *)malloc(strlen(g->addresses) + 1)) == NULL) { dbg_perror(g,"cannot allocate memory"); epmd_cleanup_exit(g,1); } strcpy(tmp,g->addresses); for(token = strtok(tmp,", "), num_sockets = 0; token != NULL; token = strtok(NULL,", "), num_sockets++) { struct EPMD_IN_ADDR addr; #ifdef HAVE_INET_PTON int ret; if ((ret = inet_pton(FAMILY,token,&addr)) == -1) { dbg_perror(g,"cannot convert IP address to network format"); epmd_cleanup_exit(g,1); } else if (ret == 0) #elif !defined(EPMD6) if ((addr.EPMD_S_ADDR = inet_addr(token)) == INADDR_NONE) #endif { dbg_tty_printf(g,0,"cannot parse IP address \"%s\"",token); epmd_cleanup_exit(g,1); } if (IS_ADDR_LOOPBACK(addr)) loopback_ok = 1; if (num_sockets - loopback_ok == MAX_LISTEN_SOCKETS - 1) { dbg_tty_printf(g,0,"cannot listen on more than %d IP addresses", MAX_LISTEN_SOCKETS); epmd_cleanup_exit(g,1); } SET_ADDR(iserv_addr[num_sockets],addr.EPMD_S_ADDR,sport); } free(tmp); if (!loopback_ok) { SET_ADDR(iserv_addr[num_sockets],EPMD_ADDR_LOOPBACK,sport); num_sockets++; } } else { SET_ADDR(iserv_addr[0],EPMD_ADDR_ANY,sport); num_sockets = 1; } #ifdef HAVE_SYSTEMD_DAEMON } #endif /* HAVE_SYSTEMD_DAEMON */ #if !defined(__WIN32__) /* We ignore the SIGPIPE signal that is raised when we call write twice on a socket closed by the other end. */ signal(SIGPIPE, SIG_IGN); #endif /* * Initialize number of active file descriptors. * Stdin, stdout, and stderr are still open. */ g->active_conn = 3 + num_sockets; g->max_conn -= num_sockets; FD_ZERO(&g->orig_read_mask); g->select_fd_top = 0; #ifdef HAVE_SYSTEMD_DAEMON if (g->is_systemd) for (i = 0; i < num_sockets; i++) select_fd_set(g, listensock[i]); else { #endif /* HAVE_SYSTEMD_DAEMON */ for (i = 0; i < num_sockets; i++) { if ((listensock[i] = socket(FAMILY,SOCK_STREAM,0)) < 0) { dbg_perror(g,"error opening stream socket"); epmd_cleanup_exit(g,1); } g->listenfd[i] = listensock[i]; /* * Note that we must not enable the SO_REUSEADDR on Windows, * because addresses will be reused even if they are still in use. */ #if !defined(__WIN32__) opt = 1; if (setsockopt(listensock[i],SOL_SOCKET,SO_REUSEADDR,(char* ) &opt, sizeof(opt)) <0) { dbg_perror(g,"can't set sockopt"); epmd_cleanup_exit(g,1); } #endif /* In rare cases select returns because there is someone to accept but the request is withdrawn before the accept function is called. We set the listen socket to be non blocking to prevent us from being hanging in accept() waiting for the next request. */ #if (defined(__WIN32__) || defined(NO_FCNTL)) opt = 1; /* Gives warning in VxWorks */ if (ioctl(listensock[i], FIONBIO, &opt) != 0) #else opt = fcntl(listensock[i], F_GETFL, 0); if (fcntl(listensock[i], F_SETFL, opt | O_NONBLOCK) == -1) #endif /* __WIN32__ || VXWORKS */ dbg_perror(g,"failed to set non-blocking mode of listening socket %d", listensock[i]); if (bind(listensock[i], (struct sockaddr*) &iserv_addr[i], sizeof(iserv_addr[i])) < 0) { if (errno == EADDRINUSE) { dbg_tty_printf(g,1,"there is already a epmd running at port %d", g->port); epmd_cleanup_exit(g,0); } else { dbg_perror(g,"failed to bind socket"); epmd_cleanup_exit(g,1); } } if(listen(listensock[i], SOMAXCONN) < 0) { dbg_perror(g,"failed to listen on socket"); epmd_cleanup_exit(g,1); } select_fd_set(g, listensock[i]); } #ifdef HAVE_SYSTEMD_DAEMON } sd_notifyf(0, "READY=1\n" "STATUS=Processing port mapping requests...\n" "MAINPID=%lu", (unsigned long) getpid()); #endif /* HAVE_SYSTEMD_DAEMON */ dbg_tty_printf(g,2,"entering the main select() loop"); select_again: while(1) { fd_set read_mask = g->orig_read_mask; struct timeval timeout; int ret; /* If we are idle we time out now and then to enable the code below to close connections that are old and probably hanging. Make sure that select will return often enough. */ timeout.tv_sec = (g->packet_timeout < IDLE_TIMEOUT) ? 1 : IDLE_TIMEOUT; timeout.tv_usec = 0; if ((ret = select(g->select_fd_top, &read_mask, (fd_set *)0,(fd_set *)0,&timeout)) < 0) { dbg_perror(g,"error in select "); switch (errno) { case EAGAIN: case EINTR: break; default: epmd_cleanup_exit(g,1); } } else { time_t now; if (ret == 0) { FD_ZERO(&read_mask); } if (g->delay_accept) { /* Test of busy server */ sleep(g->delay_accept); } for (i = 0; i < num_sockets; i++) if (FD_ISSET(listensock[i],&read_mask)) { if (do_accept(g, listensock[i]) && g->active_conn < g->max_conn) { /* * The accept() succeeded, and we have at least one file * descriptor still free, which means that another accept() * could succeed. Go do do another select(), in case there * are more incoming connections waiting to be accepted. */ goto select_again; } } /* Check all open streams marked by select for data or a close. We also close all open sockets except ALIVE with no activity for a long period */ now = current_time(g); for (i = 0; i < g->max_conn; i++) { if (g->conn[i].open == EPMD_TRUE) { if (FD_ISSET(g->conn[i].fd,&read_mask)) do_read(g,&g->conn[i]); else if ((g->conn[i].keep == EPMD_FALSE) && ((g->conn[i].mod_time + g->packet_timeout) < now)) { dbg_tty_printf(g,1,"closing because timed out on receive"); epmd_conn_close(g,&g->conn[i]); } } } } } }
int main(int argc, char *argv[]) { static char *cmdstr = NULL; struct cmd_set **cmd_set; static struct vfs_state vfs; int i; static char *filename = NULL; /* make sure the vars that get altered (4th field) are in a fixed location or certain compilers complain */ poptContext pc; struct poptOption long_options[] = { POPT_AUTOHELP {"file", 'f', POPT_ARG_STRING, &filename, 0, }, {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" }, POPT_COMMON_SAMBA POPT_TABLEEND }; setlinebuf(stdout); pc = poptGetContext("vfstest", argc, (const char **) argv, long_options, 0); while(poptGetNextOpt(pc) != -1); poptFreeContext(pc); /* TODO: check output */ reload_services(False); /* the following functions are part of the Samba debugging facilities. See lib/debug.c */ setup_logging("vfstest", True); /* Load command lists */ cmd_set = vfstest_command_list; while(*cmd_set) { add_command_set(*cmd_set); add_command_set(separator_command); cmd_set++; } /* some basic initialization stuff */ sec_init(); conn_init(); vfs.conn = conn_new(); string_set(&vfs.conn->user,"vfstest"); for (i=0; i < 1024; i++) vfs.files[i] = NULL; /* some advanced initiliazation stuff */ smbd_vfs_init(vfs.conn); /* Do we have a file input? */ if (filename && filename[0]) { process_file(&vfs, filename); return 0; } /* Do anything specified with -c */ if (cmdstr && cmdstr[0]) { char *cmd; char *p = cmdstr; while((cmd=next_command(&p)) != NULL) { process_cmd(&vfs, cmd); } return 0; } /* Loop around accepting commands */ while(1) { pstring prompt; char *line; slprintf(prompt, sizeof(prompt) - 1, "vfstest $> "); line = smb_readline(prompt, NULL, completion_fn); if (line == NULL) break; if (line[0] != '\n') process_cmd(&vfs, line); } free(vfs.conn); return 0; }