KernelNetlinkProtocol::KernelNetlinkProtocol() { register_netlink_handlers(); register_message_handlers(); setup_sockets(); }
void amiga_sockinit() { if ((SockBase = OpenLibrary("socket.library", 1L)) == NULL) { printf("Error opening socket.library\n"); exit(10); } setup_sockets(MAXSOCKS, &errno); }
int checksocketlib(void) { if(!SockBase) { if(SockBase=OpenLibrary("inet:libs/socket.library",4)) { setup_sockets(FD_SETSIZE, &errno); } else { PyErr_SetString(PyExc_SystemError, "Couldn't open inet:libs/socket.library V4+ (I-Net225 started?)"); return 0; } } return 1; }
int main(int argc, char *argv[]) { int stcp = -1; int sudp = -1; int rc = -1; if (argc != 2) { fprintf(stderr, "Usage: %s [port]\n", argv[0]); exit(1); } /* Limit memory usage by SQLite to a reasonable, relatively small level. */ #ifdef WWDEBUG printf("Current SQLite heap size limit is %ld. Setting it to 8MB.\n", sqlite3_soft_heap_limit(-1)); #endif sqlite3_soft_heap_limit(8*1024*1024); #ifdef WWDEBUG printf("New SQLite heap size limit is %ld.\n", sqlite3_soft_heap_limit(-1)); #endif /* Include this only if we are *not* compiling a debug version */ #ifndef WWDEBUG /* Fork and background (basically) */ pid_t pid = fork(); if (pid != 0) { if (pid < 0) { perror("AGGREGATOR: Failed to fork"); exit(1); } exit(0); } #endif bzero(sock_data, sizeof(sock_data)); // Get the database ready // Attempt to open database & check for failure #ifdef WWDEBUG printf("Attempting to open database: %s\n", SQLITE_DB_FNAME); #endif rc = sqlite3_open(SQLITE_DB_FNAME, &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else { // Now check & create tables if required createTable(db, 1); createTable(db, 2); #ifdef WWDEBUG printf("Database ready for reading and writing...\n"); #endif } // Prepare to accept clients FD_ZERO(&rfds); FD_ZERO(&wfds); // Open TCP (SOCK_STREAM) & UDP (SOCK_DGRAM) sockets, bind to the port // given and listen on TCP sock for connections if ((setup_sockets(atoi(argv[1]), &stcp, &sudp)) < 0) { perror("WWAGGREGATOR: Error setting up Sockets\n"); exit(1); } #ifdef WWDEBUG printf("Our listen sock # is - %d & UDP sock # is - %d \n", stcp, sudp); #endif //printf("FD_SETSIZE - %d\n",FD_SETSIZE); //Add the created TCP & UDP sockets to the read set of file descriptors FD_SET(stcp, &rfds); FD_SET(sudp, &rfds); // Event loop while (1) { int n = 0; fd_set _rfds, _wfds; memcpy(&_rfds, &rfds, sizeof(fd_set)); memcpy(&_wfds, &wfds, sizeof(fd_set)); // Block until there's an event to handle // Select function call is made; return value 'n' gives the number of FDs ready to be serviced if (((n = select(FD_SETSIZE, &_rfds, &_wfds, NULL, 0)) < 0) && (errno != EINTR)) { perror("select"); exit(1); } // Handle events for (int i = 0; (i < FD_SETSIZE) && n; i++) { if (FD_ISSET(i, &_rfds)) { // Handle our main mother, TCP listening socket differently if (i == stcp) { if (accept_conn(stcp) < 0) exit(1); // Handle our UDP socket differently } else if (i == sudp) { read_and_dump_data(sudp); } else { #ifdef WWDEBUG fprintf(stderr, "File descriptor %d is ready for reading .. call'g readHLR\n", i); #endif read_handler(i); } n--; } if (FD_ISSET(i, &_wfds)) { #ifdef WWDEBUG fprintf(stderr, "File descriptor %d is ready for writing .. call'g writeHLR\n", i); #endif write_handler(i); n--; } } } }
/***************************************************************************** * tc_initialize -- called by test harness to initialize the testcase *****************************************************************************/ tc_status tc_initialize(void) { tc_status status = TC_OK; int i = 0; tc_task_body_fptr_t fp = NULL; if (op_type == USE_UNDEFINED) { op_type = USE_MESSAGES; } /* check the operation size & async-ness */ switch (op_type) { case USE_MESSAGES: if (op_size < 1 || op_size > MCAPI_MAX_MSG_SIZE || op_size > MAX_ECHO_SIZE) { th_log_error("number of bytes to send is out of range: %d\n",op_size); status = TC_ERROR; } break; case USE_PACKETS: if (op_size < 1 || op_size > MCAPI_MAX_PKT_SIZE || op_size > MAX_ECHO_SIZE) { th_log_error("number of bytes to send is out of range: %d\n",op_size); status = TC_ERROR; } break; case USE_SCALARS: if (op_size != 1 && op_size != 2 && op_size != 4 && op_size != 8) { th_log_error("number of bytes to send is out of range: %d\n",op_size); status = TC_ERROR; } else if (use_async) { th_log_error("async cannot be used with scalars\n"); status = TC_ERROR; } break; case USE_PIPES: if (op_size < 1 || op_size > MAX_ECHO_SIZE) { th_log_error("number of bytes to send is out of range: %d\n",op_size); status = TC_ERROR; } else if (use_async) { th_log_error("async cannot be used with pipes\n"); status = TC_ERROR; } break; case USE_SOCKETS: if (op_size < 1 || op_size > MAX_ECHO_SIZE) { th_log_error("number of bytes to send is out of range: %d\n",op_size); status = TC_ERROR; } else if (use_async) { th_log_error("async cannot be used with sockets\n"); status = TC_ERROR; } break; default: break; } /* check the operation count */ if (op_count < 1) { th_log_error("number of sends must be > 0\n"); status = TC_ERROR; } /* check the number of hops */ if (num_tasks < 2 || num_tasks > CONKER_MAX_TASKS) { th_log_error("number of hops out of range [2-%d]\n", CONKER_MAX_TASKS); status = TC_ERROR; } /* if we got here we must have good args ... */ th_log_info("[TC_ECHO] -- tc_initialize, pid = %d\n", getpid()); /* additional initialization items */ for (i = 0; i < CONKER_MAX_TASKS; i++) { fds_array[i].fds[READ_IDX] = fds_array[i].fds[WRITE_IDX] = -1; } if (strcmp(th_get_impl_string(),"pthread") == 0) { using_pthreads = 1; } /* print config */ switch (op_type) { case USE_MESSAGES: th_log("[TC_ECHO] -- using MCAPI messages\n"); break; case USE_PACKETS: th_log("[TC_ECHO] -- using MCAPI packets\n"); break; case USE_SCALARS: th_log("[TC_ECHO] -- using MCAPI scalars\n"); break; case USE_PIPES: th_log("[TC_ECHO] -- using pipes\n"); setup_pipes(); break; case USE_SOCKETS: th_log("[TC_ECHO] -- using sockets\n"); setup_sockets(); break; default: break; } if (use_async) { th_log("[TC_ECHO] -- using non-blocking send/recv\n"); } th_log("[TC_ECHO] -- node offset:\t%d\n", th_get_node_offset()); th_log("[TC_ECHO] -- #sends:\t%d\n", op_count); th_log("[TC_ECHO] -- #bytes/send:\t%d\n", op_size); th_log("[TC_ECHO] -- #hops:\t%d\n", num_tasks - 1); if (filename) { /* TODO -- how to deal with output log files in bare metal */ th_log("[TC_ECHO] -- results will be appended to file: %s\n",filename); outfile = fopen(filename, "a+"); } for (i = 0; status == TC_OK && i < num_tasks; i++) { switch (op_type) { case USE_MESSAGES: if (!use_async) { fp = task_body_msg_blocking; } else { fp = task_body_msg_non_blocking; } break; case USE_PACKETS: if (!use_async) { fp = task_body_pkt_blocking; } else { fp = task_body_pkt_non_blocking; } break; case USE_SCALARS: fp = task_body_scalar_blocking; break; case USE_PIPES: fp = task_body_pipes_blocking; break; case USE_SOCKETS: fp = task_body_sockets_blocking; break; default: /* TODO - this is an error and should be handled !! */ break; } if (th_register_task(i,"echo_task",fp,NULL,0) != TH_OK) { th_log_error("Unable to register task number %d. Status = %d\n", i, status); status = TC_ERROR; } else { th_log_info("[TC_ECHO] -- task registered, id = %d\n", i); } } return status; }
int BasicASIOTransport::setup(void) { // call base setup method to initialize certain common variables if (Base::setup() < 0) { return -1; } // resize addresses to be the size of the list of hosts addresses_.clear(); addresses_.reserve(this->settings_.hosts.size()); if (settings_.hosts.size() == 0) { madara_logger_log(context_.get_logger(), logger::LOG_MINOR, "BasicASIOTransport::setup:" " No host addresses. Aborting setup.\n"); this->invalidate_transport(); return -1; } // convert the string host:port into an asio address for (unsigned int i = 0; i < settings_.hosts.size(); ++i) { try { auto addr_parts = utility::parse_address(settings_.hosts[i]); auto addr = ip::address::from_string(addr_parts.first); addresses_.emplace_back(addr, addr_parts.second); madara_logger_log(context_.get_logger(), logger::LOG_MAJOR, "BasicASIOTransport::setup:" " settings address[%d] to %s:%d\n", i, addresses_.back().address().to_string().c_str(), addresses_.back().port()); } catch (const boost::system::system_error& e) { madara_logger_log(context_.get_logger(), logger::LOG_MAJOR, "BasicASIOTransport::setup:" " Error parsing address %s: %s\n", settings_.hosts[i].c_str(), e.what()); } } if (addresses_.size() == 0) { madara_logger_log(context_.get_logger(), logger::LOG_MAJOR, "BasicASIOTransport::setup:" " No valid addresses. Aborting setup.\n"); this->invalidate_transport(); return -1; } try { socket_.open(addresses_[0].protocol()); } catch (const boost::system::system_error& e) { madara_logger_log(context_.get_logger(), logger::LOG_MAJOR, "BasicASIOTransport::setup:" " Error opening sockets: %s\n", e.what()); this->invalidate_transport(); return -1; } int ret = setup_sockets(); if (ret < 0) { return ret; } ret = setup_read_threads(); if (ret < 0) { return ret; } return this->validate_transport(); }