int rtp_setup(SOCKADDR *remote, int cport, int tport) { if (running) die("rtp_setup called with active stream!"); debug(1, "rtp_setup: cport=%d tport=%d\n", cport, tport); // we do our own timing and ignore the timing port. // an audio perfectionist may wish to learn the protocol. memcpy(&rtp_client, remote, sizeof(rtp_client)); #ifdef AF_INET6 if (rtp_client.SAFAMILY == AF_INET6) { struct sockaddr_in6 *sa6 = (struct sockaddr_in6*)&rtp_client; sa6->sin6_port = htons(cport); } else #endif { struct sockaddr_in *sa = (struct sockaddr_in*)&rtp_client; sa->sin_port = htons(cport); } int sport = bind_port(remote); debug(1, "rtp listening on port %d\n", sport); please_shutdown = 0; pthread_create(&rtp_thread, NULL, &rtp_receiver, NULL); running = 1; return sport; }
static err_t idc_bind_port(uint16_t port, net_ports_port_type_t port_type) { if (is_owner) { LWIPBF_DEBUG("idc_bind_port: called by owner\n"); return bind_port(port, port_type); } LWIPBF_DEBUG("idc_bind_port: called\n"); errval_t err, msgerr; /* getting the proper buffer id's here */ err = net_ports_rpc.vtbl.bind_port(&net_ports_rpc, port_type, port, /* buffer for RX */ ((struct client_closure_NC *) driver_connection[RECEIVE_CONNECTION]->st)-> buff_ptr->buffer_id, /* buffer for TX */ ((struct client_closure_NC *) driver_connection[TRANSMIT_CONNECTION]->st)-> buff_ptr->buffer_id, appid_delete, qid_delete, &msgerr); if (err_is_fail(err)) { USER_PANIC_ERR(err, "error sending get_ip_info"); } LWIPBF_DEBUG("idc_new_tcp_port: terminated\n"); if (msgerr == PORT_ERR_IN_USE) { return ERR_USE; } // FIXME: other errors? return ERR_OK; }
static void run_recv() { int sock; int ret; char *buf; printf("Start server on port %d. My PSID: %d\n", arg_serverport, PSP_GetNodeID()); sock = socket(PF_P4S , 0, 0); if (!sock) goto err_socket; ret = bind_port(sock, arg_serverport); if (ret) goto err_bind; buf = (char*)malloc(SERVER_BUFLEN); if (!buf) goto err_malloc; while (1) { uint16_t src; int len; len = sock_recv(sock, &src, buf, SERVER_BUFLEN); if (arg_verbose > 0) { printf("Recv %d bytes from %d\n",len ,src); if ((arg_verbose > 1) && (len > 0)) { if (arg_verbose > 2) { dump(buf, 0, len, 0, 16, "recv"); } else { printf(":%s:\n", dumpstr(buf, MIN(len,32))); } } } } return; err_socket: perror("socket()"); exit(1); err_bind: perror("bind()"); exit(1); err_malloc: perror("malloc()"); exit(1); }
int main (int argc, char * argv[]) { /* availability options */ char * opts = "d"; /* processed option */ int c; int sock_id; /* dodanie handlera na SIGINT */ signal(SIGINT, sigint_handler); /* check if define more than one option */ if (argc == 2) { /* iteration by each of option */ while ((c = getopt(argc, argv, opts)) != -1) { switch (c) { /* for 'u' run daemon */ case 'd': create_daemon(); break; /* for unrecognized option */ case '?': default: printf("Ignore passed options\n"); } } } printf("=== create server ===\n\n"); /* create socket */ sock_id = create_socket_tcp(); /* bind server to defined port */ bind_port(sock_id, PORT); /* server waiting for client */ listen_for_client_tcp(sock_id, MAX_QUEUE); /* handle client */ handle_incoming_client(sock_id, send_time_to_socket, 1); return 0; }
URI& URI::parse() { http_parser_url u; http_parser_url_init(&u); const auto p = uri_str_.data(); const auto result = http_parser_parse_url(p, uri_str_.size(), 0, &u); #ifdef URI_THROW_ON_ERROR if (result not_eq 0) { std::string uri{uri_str_.begin(), uri_str_.end()}; throw URI_error{"Invalid uri: " + uri}; } #endif //< URI_THROW_ON_ERROR (void)result; scheme_ = (u.field_set & (1 << UF_SCHEMA)) ? util::sview{p + u.field_data[UF_SCHEMA].off, u.field_data[UF_SCHEMA].len} : util::sview{}; userinfo_ = (u.field_set & (1 << UF_USERINFO)) ? util::sview{p + u.field_data[UF_USERINFO].off, u.field_data[UF_USERINFO].len} : util::sview{}; host_ = (u.field_set & (1 << UF_HOST)) ? util::sview{p + u.field_data[UF_HOST].off, u.field_data[UF_HOST].len} : util::sview{}; path_ = (u.field_set & (1 << UF_PATH)) ? util::sview{p + u.field_data[UF_PATH].off, u.field_data[UF_PATH].len} : util::sview{}; query_ = (u.field_set & (1 << UF_QUERY)) ? util::sview{p + u.field_data[UF_QUERY].off, u.field_data[UF_QUERY].len} : util::sview{}; fragment_ = (u.field_set & (1 << UF_FRAGMENT)) ? util::sview{p + u.field_data[UF_FRAGMENT].off, u.field_data[UF_FRAGMENT].len} : util::sview{}; auto port_str_ = (u.field_set & (1 << UF_PORT)) ? util::sview{p + u.field_data[UF_PORT].off, u.field_data[UF_PORT].len} : util::sview{}; if(not port_str_.empty()) { std::array<char, 32> buf; std::copy(port_str_.begin(), port_str_.end(), buf.begin()); buf[port_str_.size()] = 0; port_ = std::atoi(buf.data()); } else { port_ = bind_port(scheme_, u.port); } return *this; }
void* client(void *arg) { char request[1024]; int sock, ret, i, len; cpu_tick_t start, end; int server_num = (int) arg; int did_some_io = 1; int optval; // loop, making requests, & keeping stats while( 1 ) { state_idle++; // yield, if we didn't yet, ie, due to socket() or connect() failures if( !did_some_io ) sched_yield(); did_some_io = 0; state_idle--; state_connecting++; // new socket sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if( sock == -1 ) { if( debug ) printf("client() - error with socket(): %s\n", strerror(errno)); io_errors++; //perror("socket"); state_connecting--; continue; } // turn off TCP linger, to do a quick close of the socket if ( 0 ) { struct linger linger; linger.l_onoff = 1; linger.l_linger = 0; if (setsockopt (sock, SOL_SOCKET, SO_LINGER, &linger, sizeof (linger)) < 0) { if( debug ) printf("client() - error setting SO_LINGER: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } } // make the sockets reusable, so we don't wait as long for the // kernel to clean things up, and hence run out of sockets. // // FIXME: is this only meaningful for server sockets?? optval = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) < 0) { if( debug ) printf("client() - error setting SO_REUSEADDR: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } /* optval = SEND_BUFFER_SIZE; if (setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval)) < 0) { if( debug ) printf("client() - error setting SO_SNDBUF: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } optval = RECV_BUFFER_SIZE; if (setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval)) < 0) { if( debug ) printf("client() - error setting SO_RCVBUF: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } */ // Disable the Nagle algorithm so the kernel won't delay our // writes waiting for more data. if( 1 ) { optval = 1; //if (setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) if (setsockopt (sock, SOL_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) { if( debug ) printf("client() - error setting TCP_NODELAY: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } } if ( !bind_port(sock) ) { state_connecting--; continue; } // Disable the Nagle algorithm so the kernel won't delay our // writes waiting for more data. if( 1 ) { optval = 1; //if (setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) if (setsockopt (sock, SOL_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) { if( debug ) printf("client() - error setting TCP_NODELAY: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } } // connect //output("connecting...\n"); server_num++; server_num = server_num % num_servers; GET_REAL_CPU_TICKS(start); ret = connect(sock, (struct sockaddr *)&servers[server_num].addr, sizeof(servers[0].addr)); GET_REAL_CPU_TICKS(end); if( ret < 0 ) { state_connecting--; hist_add(err_connect_hist, HIST_INTERVAL(start,end)); if( errno == EADDRNOTAVAIL ) { addrnotavail++; } else { io_errors++; if( debug ) printf("client() - error with connect(): %s\n", strerror(errno)); } GET_REAL_CPU_TICKS(start); state_closing++; close(sock); state_closing--; GET_REAL_CPU_TICKS(end); hist_add(close_unconnected_hist, HIST_INTERVAL(start,end)); continue; } hist_add(connect_hist, HIST_INTERVAL(start,end)); // Disable the Nagle algorithm so the kernel won't delay our // writes waiting for more data. if( 1 ) { optval = 1; //if (setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) if (setsockopt (sock, SOL_TCP, TCP_NODELAY, &optval, sizeof (optval)) < 0) { if( debug ) printf("client() - error setting TCP_NODELAY: %s\n", strerror(errno)); io_errors++; state_connecting--; continue; } } state_connecting--; // issue requests for( i=0; i<nreqs; i++ ) { len = snprintf(request, sizeof(request)-1, "GET /%s/dir%05d/class%d_%d HTTP/1.1\r\n" "Host: %s\r\n" //"User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; windows 98)\r\n" //"Accept: */*\r\n" //"Accept-Encoding: gzip, deflate\r\n" //"Accept-Language: en/us\r\n" //"Connection: Close\r\n" "\r\n", servers[server_num].url, spec_dir(), spec_class(), spec_file(), servers[server_num].hostname ); assert(len > 0); // note that we did some IO, so this thread has already yielded. did_some_io = 1; state_writing++; GET_REAL_CPU_TICKS(start); ret = write( sock, request, len ); GET_REAL_CPU_TICKS(end); hist_add(request_hist, HIST_INTERVAL(start,end)); state_writing--; // we do this here, instead of after connect b/c w/ Capriccio // the connect() call seems to always return immediately. good_conn++; if( len != ret ) { // FIXME: for now, don't add anything to the response hist, if we get a failure here if( errno == ETIMEDOUT ) { conn_timedout++; } else { if(debug) printf("client() - error writing request: %s\n", strerror(errno)); io_errors++; } break; } //output("reading response...\n"); state_reading++; GET_REAL_CPU_TICKS(start); if( read_response( sock ) == 0 ) requests_completed++; else bad_conn++; GET_REAL_CPU_TICKS(end); hist_add(response_hist, HIST_INTERVAL(start,end)); state_reading--; // FIXME: close the connection on errors? // sleep for a second if(i < nreqs-1 && sleep_ms > 0) { state_idle++; GET_REAL_CPU_TICKS(start); usleep(sleep_ms*1000); GET_REAL_CPU_TICKS(end); hist_add(sleep_hist, HIST_INTERVAL(start,end)); state_idle--; } } //output("closing...\n"); state_closing++; GET_REAL_CPU_TICKS(start); close( sock ); GET_REAL_CPU_TICKS(end); hist_add(close_hist, HIST_INTERVAL(start,end)); state_closing--; } return NULL; }
int main(int argc, char **argv) { /* arguments */ int c; int port = DEFAULT_PORT; const char* datafile; int forknum = 0; /* network */ int fam = AF_INET; bool random_port_success; #ifdef USE_WINSOCK WSADATA wsa_data; #endif /* parse arguments */ srandom(time(NULL) ^ getpid()); logfile = stdout; prog_name = argv[0]; log_msg("%s: start\n", prog_name); while((c = getopt(argc, argv, "6f:p:rv")) != -1) { switch(c) { case '6': #ifdef AF_INET6 fam = AF_INET6; #else log_msg("cannot -6: no IP6 available\n"); exit(1); #endif break; case 'r': port = 0; break; case 'f': forknum = atoi(optarg); if(forknum < 1) error("invalid forkno %s, give number", optarg); break; case 'p': port = atoi(optarg); if (port < 1) { error("Invalid port %s, use a number.", optarg); } break; case 'v': do_verbose++; break; default: usage(); break; } } argc -= optind; argv += optind; if(argc == 0 || argc > 1) usage(); datafile = argv[0]; log_msg("Reading datafile %s\n", datafile); entries = read_datafile(datafile, 0); #ifdef USE_WINSOCK if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) error("WSAStartup failed\n"); #endif if((udp_sock = socket(fam, SOCK_DGRAM, 0)) < 0) { error("udp socket(): %s\n", strerror(errno)); } if((tcp_sock = socket(fam, SOCK_STREAM, 0)) < 0) { error("tcp socket(): %s\n", strerror(errno)); } c = 1; if(setsockopt(tcp_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&c, (socklen_t) sizeof(int)) < 0) { error("setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); } /* bind ip4 */ if (port > 0) { if (bind_port(udp_sock, port, fam)) { error("cannot bind(): %s\n", strerror(errno)); } if (bind_port(tcp_sock, port, fam)) { error("cannot bind(): %s\n", strerror(errno)); } if (listen(tcp_sock, CONN_BACKLOG) < 0) { error("listen(): %s\n", strerror(errno)); } } else { random_port_success = false; while (!random_port_success) { port = (random() % 64510) + 1025; log_msg("trying to bind to port %d\n", port); random_port_success = true; if (bind_port(udp_sock, port, fam)) { #ifdef EADDRINUSE if (errno != EADDRINUSE) { #elif defined(USE_WINSOCK) if (WSAGetLastError() != WSAEADDRINUSE) { #else if (1) { #endif perror("bind()"); return -1; } else { random_port_success = false; } } if (random_port_success) { if (bind_port(tcp_sock, port, fam)) { #ifdef EADDRINUSE if (errno != EADDRINUSE) { #elif defined(USE_WINSOCK) if (WSAGetLastError()!=WSAEADDRINUSE) { #else if (1) { #endif perror("bind()"); return -1; } else { random_port_success = false; } } } if (random_port_success) { if (listen(tcp_sock, CONN_BACKLOG) < 0) { error("listen(): %s\n", strerror(errno)); } } } } log_msg("Listening on port %d\n", port); /* forky! */ if(forknum > 0) forkit(forknum); service(); return 0; }
static void daemon_login(int argc, char *argv[], char *envp[]) { int msock, csock; /* socket for Master and Child */ FILE *fp; int listen_port = 23; int len_of_sock_addr; char buf[256]; /* setup standalone */ start_daemon(); signal_restart(SIGCHLD, reapchild); /* choose port */ if(argc == 1) listen_port = 3006; else if(argc >= 2) listen_port = atoi(argv[1]); sprintf(margs, "%s %d ", argv[0],listen_port); /* port binding */ xsin.sin_family = AF_INET; msock = bind_port(listen_port); if(msock<0) { syslog(LOG_INFO, "mbbsd bind_port failed.\n"); exit(1); } initsetproctitle(argc, argv, envp); setproctitle("%s: listening ", margs); /* Give up root privileges: no way back from here */ setgid(BBSGID); setuid(BBSUID); chdir(BBSHOME); /* mmap passwd file */ if(passwd_mmap()) exit(1); sprintf(buf, "/var/run/mbbsd.%d.pid", listen_port); if((fp = fopen(buf, "w"))) { fprintf(fp, "%d\n", getpid()); fclose(fp); } /* main loop */ for(;;) { len_of_sock_addr = sizeof(xsin); csock = accept(msock, (struct sockaddr *)&xsin, &len_of_sock_addr); if(csock < 0) { if(errno!=EINTR) sleep(1); continue; } if(check_ban_and_load(csock)) { close(csock); continue; } if(fork()==0) break; else close(csock); } /* here is only child running */ close(msock); dup2(csock, 0); close(csock); getremotename(&xsin, fromhost, remoteusername); /* RFC931 */ /* ban 掉 bad host / bad user shiuan88: 賣補帖的 */ if(bad_host(fromhost) || strstr(fromhost, "bbs.") || strstr(fromhost, "ccsun53.cc.ntu") || !strcmp(remoteusername, "shiuan88")) { outs("\n 抱歉, 本站謝絕由此處來的user...\n"); refresh(); exit(1); } { char RFC931[80]; sprintf(RFC931, "%s@%s", remoteusername, fromhost); setenv("RFC931", RFC931, 1); } telnet_init(); setproctitle("%s: ..login..", margs); }
static void run_send() { int sock; int ret; int conid; char *buf; int i; printf("Start client from port %d to port %d\n", arg_clientport, arg_serverport); sock = socket( PF_P4S , 0, 0 ); if (!sock) goto err_socket; ret = bind_port(sock, arg_clientport); if (ret) goto err_bind; buf = (char*)malloc(arg_size); if (!buf) goto err_malloc; for (i = 0; i < arg_size; i++) { buf[i] = i; } for (i = 0; i < arg_size; i++) { buf[i] = (i & ~3) >> (8*(3-(i & 3))); } // memset(buf, 0x42, arg_size); conid = connect_port(sock, arg_servernode, arg_serverport); if (conid < 0) goto err_connect; if (arg_verbose > 0) { printf("Connect to %08x:%d with id %d\n", arg_servernode, arg_serverport, conid); printf("Send %d bytes\n",arg_size); if ((arg_verbose > 1) && (arg_size > 0)) { if (arg_verbose > 2) { dump(buf, 0, arg_size, 0, 16, "send"); } else { printf(":%s:\n", dumpstr(buf, MIN(arg_size,32))); } } } { uint16_t dest = conid; int len; int offset = 0; while (offset < arg_size) { len = sock_send(sock, dest, buf + offset, arg_size - offset); if (len < 0) { perror("send"); } else { if (arg_verbose > 1) { printf("Send bytes %d - %d.\n", offset, offset + len - 1); } offset += len; } } } // sleep(10); free(buf); return; err_socket: perror("socket()"); exit(1); err_bind: perror("bind()"); exit(1); err_malloc: perror("malloc()"); exit(1); err_connect: perror("connect()"); exit(1); }
// // visit_component // int Servant_Impl::visit_component (AST_Component * node) { const ACE_CString context = ACE_CString (node->local_name ()->get_string ()) + "_Context"; const ACE_CString servant = ACE_CString (node->local_name ()->get_string ()) + "_Servant"; const char * local_name = node->local_name ()->get_string (); const char * flat_name = node->flat_name (); const char * full_name = node->full_name (); Provides_Svnt_Impl facet_servant (this->hfile_, this->sfile_, context); facet_servant.visit_scope (node); this->hfile_ << "// Type definition of the servant base type." << std::endl << "typedef " << be_global->get_servant_template_typename () << " < " << std::endl << " " << servant << "," << std::endl << " " << context << "," << std::endl << " ::CIAO_" << flat_name << "_Impl::" << local_name << "_Exec," << std::endl << " ::POA_" << full_name << " > " << servant << "_Base;" << std::endl << "class "; if (!be_global->svnt_export_macro_.empty ()) this->hfile_ << be_global->svnt_export_macro_ << " "; this->hfile_ << servant << std::endl << " : public " << servant << "_Base" << "{"; be_global->generate_class_preamble (node, this->hfile_, this->sfile_, servant); this->hfile_ << "/// typedef for generated implementation code" << std::endl << "typedef " << servant << " self_type;" << std::endl << "/// Default constructor" << std::endl << "public:" << std::endl << servant << " (const char * name," << std::endl << "::PortableServer::POA_ptr poa," << std::endl << "::CIAO_" << flat_name << "_Impl::" << local_name << "_Exec_ptr executor);" << std::endl << "/// Destructor" << std::endl << "virtual ~" << servant << " (void);" << std::endl; this->sfile_ << servant << "::" << std::endl << servant << " (const char * name," << std::endl << "::PortableServer::POA_ptr poa," << std::endl << "::CIAO_" << flat_name << "_Impl::" << local_name << "_Exec_ptr executor)" << std::endl << ": " << servant << "_Base (this, name, poa, executor)"; if (be_global->uses_default_bmi (node)) { Base_Member_Init bmi (this->sfile_, servant); bmi.visit_scope (node); } else { be_global->generate_custom_bmi (node, this->sfile_); } this->sfile_ << "{"; be_global->generate_constructor_preamble (node, this->sfile_); Bind_Port bind_port (this->sfile_, servant); bind_port.visit_scope (node); // Allow backend to generate any additionl initialization code be_global->generate_constructor_postamble (node, this->sfile_); this->sfile_ << "}" << servant << "::~" << servant << " (void)" << "{" << "}"; // Write the function for setting the implementations attribute // values via the configuration file. Set_Attributes set_attributes (this->hfile_, this->sfile_, servant); node->ast_accept (&set_attributes); // Write the remaining set of the methods that are based on the // names and types of defined ports. Subscribe_Method sm (this->hfile_, this->sfile_, servant); sm.visit_scope (node); if (be_global->uses_default_push_method (node)) { Servant_Push_Method spm (this->hfile_, this->sfile_, servant); spm.visit_scope (node); } else { be_global->generate_custom_push_method (node, this->hfile_, this->sfile_, servant); } Get_Consumer_Method gcm (this->hfile_, this->sfile_, servant); gcm.visit_scope (node); Attribute_Method attr_method (this->hfile_, this->sfile_, servant); attr_method.visit_scope (node); Consumer_Variable consumer_variables (this->hfile_, servant); consumer_variables.visit_scope (node); be_global->generate_class_postamble (node, this->hfile_, this->sfile_, servant); // Factory method for the servant. This is a C-style function // that is exported from the binary. this->hfile_ << "};"; if (!be_global->svnt_export_macro_.empty ()) { this->hfile_ << "extern \"C\" " << be_global->svnt_export_macro_ << " ::PortableServer::Servant" << std::endl << "create_" << flat_name << "_Servant (const char * name," << std::endl << "::PortableServer::POA_ptr poa," << std::endl << "::Components::EnterpriseComponent_ptr p);" << std::endl; this->sfile_ << "::PortableServer::Servant" << std::endl << "create_" << flat_name << "_Servant (const char * name," << std::endl << "::PortableServer::POA_ptr poa," << std::endl << "::Components::EnterpriseComponent_ptr p)" << "{" << "return ::iCCM::create_servant <" << " CIAO_" << flat_name << "_Impl::" << local_name << "_Exec, " << servant << " > (name, poa, p);" << "}"; } return 0; }