int main() { SOCKET accept; HANDLE h_exit; int err; if ((err = nw_init()) != 0) { fprintf(stderr, "Error initializing network (%d)\n", err); return 1; } if ((err = nw_listen(&accept, 44556)) != 0) { fprintf(stderr, "Error on listening (%d)\n", err); nw_shutdown(); return 1; } h_exit = nw_install_break_handler(); err = nw_accept_loop(accept, h_exit, accept_callback); fprintf(stderr, "nw_accept_loop() exited with %d\n", err); finish_threads(); CloseHandle(h_exit); nw_closesocket(accept); nw_shutdown(); return 0; }
void main(void) { mq_t *mq; int r; int source, m_type, timerand, fd; u32_t tasknr; struct fssignon device; u8_t randbits[32]; struct timeval tv; #if DEBUG printk("Starting inet...\n"); printk("%s\n", version); #endif #if HZ_DYNAMIC system_hz = sys_hz(); #endif /* Read configuration. */ nw_conf(); /* Get a random number */ timerand= 1; fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK); if (fd != -1) { r= read(fd, randbits, sizeof(randbits)); if (r == sizeof(randbits)) timerand= 0; else { printk("inet: unable to read random data from %s: %s\n", RANDOM_DEV_NAME, r == -1 ? strerror(errno) : r == 0 ? "EOF" : "not enough data"); } close(fd); } else { printk("inet: unable to open random device %s: %s\n", RANDOM_DEV_NAME, strerror(errno)); } if (timerand) { printk("inet: using current time for random-number seed\n"); r= gettimeofday(&tv, NULL); if (r == -1) { printk("sysutime failed: %s\n", strerror(errno)); exit(1); } memcpy(randbits, &tv, sizeof(tv)); } init_rand256(randbits); /* Our new identity as a server. */ r= ds_retrieve_u32("inet", &tasknr); if (r != 0) ip_panic(("inet: ds_retrieve_u32 failed for 'inet': %d", r)); this_proc= tasknr; /* Register the device group. */ device.dev= ip_dev; device.style= STYLE_CLONE; if (svrctl(FSSIGNON, (void *) &device) == -1) { printk("inet: error %d on registering ethernet devices\n", errno); pause(); } #ifdef BUF_CONSISTENCY_CHECK inet_buf_debug= (getenv("inetbufdebug") && (strcmp(getenv("inetbufdebug"), "on") == 0)); inet_buf_debug= 100; if (inet_buf_debug) { ip_warning(( "buffer consistency check enabled" )); } #endif if (getenv("killerinet")) { ip_warning(( "killer inet active" )); killer_inet= 1; } nw_init(); while (TRUE) { #ifdef BUF_CONSISTENCY_CHECK if (inet_buf_debug) { static int buf_debug_count= 0; if (++buf_debug_count >= inet_buf_debug) { buf_debug_count= 0; if (!bf_consistency_check()) break; } } #endif if (ev_head) { ev_process(); continue; } if (clck_call_expire) { clck_expire_timers(); continue; } mq= mq_get(); if (!mq) ip_panic(("out of messages")); r = kipc_module_call(KIPC_RECEIVE, 0, ENDPT_ANY, &mq->mq_mess); if (r<0) { ip_panic(("unable to receive: %d", r)); } reset_time(); source= mq->mq_mess.m_source; m_type= mq->mq_mess.m_type; if (source == VFS_PROC_NR) { sr_rec(mq); } else if (is_notify(m_type)) { if (_ENDPOINT_P(source) == CLOCK) { clck_tick(&mq->mq_mess); mq_free(mq); } else if (_ENDPOINT_P(source) == PM_PROC_NR) { /* signaled */ /* probably SIGTERM */ mq_free(mq); } else { /* A driver is (re)started. */ eth_check_drivers(&mq->mq_mess); mq_free(mq); } } else if (m_type == DL_CONF_REPLY || m_type == DL_TASK_REPLY || m_type == DL_NAME_REPLY || m_type == DL_STAT_REPLY) { eth_rec(&mq->mq_mess); mq_free(mq); } else { printk("inet: got bad message type 0x%x from %d\n", mq->mq_mess.m_type, mq->mq_mess.m_source); mq_free(mq); } } ip_panic(("task is not allowed to terminate")); }
/** * @param i3_port_num The port on which i3 should listen. */ cl_context *cl_ctx_init(const char *cfg_file, int *rc, int i3_port_num) { char usePingStr[50]; char useTCPStr[50]; cl_context *ctx; // Open the debug file to which all debug and info messages related to i3 are sent, // if it had not be previously opened. // For eg: if the i3 client api is used as part of the i3OCD, the i3OCD // sets up i3DebugFD as soon as the i3OCD dll is loaded. #ifdef _WIN32 if (i3DebugFD == NULL) { i3DebugFD = fopen ("debug_i3.txt", "w"); } #endif if (0 != nw_init()) { I3_PRINT_INFO0 (I3_INFO_LEVEL_FATAL_ERROR, "Unable to initialize the network.\n"); EXIT_ON_ERROR; return NULL; } if (!cfg_file) { *rc = CL_RET_INVALID_CFG_FILE; return NULL; } /* read configuration file */ read_parameters(cfg_file); /* create context */ ctx = cl_create_context(NULL, i3_port_num); if (ctx) *rc = CL_RET_OK; else { *rc = CL_RET_NO_CONTEXT; return ctx; } /* Should TCP be used to find the first hop i3 server? */ read_string_attribute("//I3ServerDetails", "UseTCP", useTCPStr,0); if (strcasecmp(useTCPStr, "yes") == 0 || strcasecmp(useTCPStr, "true") == 0) { ctx->is_tcp = 1; } else { ctx->is_tcp = 0; } /* check whether ping is used to find a nearby server */ read_string_attribute("//I3ServerDetails", "UsePing", usePingStr,0); if (strcasecmp(usePingStr, "yes") == 0 || strcasecmp(usePingStr, "true") == 0) { ctx->use_ping = 1; } else { ctx->use_ping = 0; } if (ctx->use_ping) { #define STATUS_URL_LEN 1024 char status_url[STATUS_URL_LEN]; /* read the url containing the list of i3 serevers */ read_string_attribute("//I3ServerDetails", "ServerListURL", status_url, 0); I3_PRINT_DEBUG1(I3_DEBUG_LEVEL_SUPER, "Using ping, server status url = %s\n\n", status_url); if (strlen(status_url) >= STATUS_URL_LEN) { I3_PRINT_INFO1(I3_INFO_LEVEL_WARNING, "cl_ctx_init: status_url file too long in %s\n", cfg_file); exit(-1); } if (strlen(status_url) == 0) { I3_PRINT_INFO1(I3_INFO_LEVEL_WARNING, "cl_ctx_init: no status_url file in %s\n", cfg_file); exit(-1); } I3_PRINT_DEBUG0(I3_DEBUG_LEVEL_VERBOSE, "Starting ping thread\n"); *rc = cl_init_ping(ctx, status_url); } // Don't read the configuration file after this point. release_params(); if (ctx->num_servers < 1 && ! ctx->use_ping) { I3_PRINT_INFO0 ( I3_INFO_LEVEL_FATAL_ERROR, "No i3 server details have been given and UsePing is turned off.\n" "There is no way to obtain i3 server details. Exiting.\n" ); EXIT_ON_ERROR; } // This function intializes the TCP socket to be used to contact the first hop i3 server, // if UseTCP flag is set in the configuration file. // If UsePing is set, this function blocks till the details of at least // one i3 server is available. init_tcp_ctx(ctx); return ctx; }
/*===========================================================================* * sef_cb_init_fresh * *===========================================================================*/ static int sef_cb_init_fresh(int type, sef_init_info_t *info) { /* Initialize the inet server. */ int r; int timerand, fd; u8_t randbits[32]; struct timeval tv; struct passwd *pw; #if DEBUG printf("Starting inet...\n"); printf("%s\n", version); #endif #if HZ_DYNAMIC system_hz = sys_hz(); #endif /* Read configuration. */ nw_conf(); /* Get a random number */ timerand= 1; fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK); if (fd != -1) { r= read(fd, randbits, sizeof(randbits)); if (r == sizeof(randbits)) timerand= 0; else { printf("inet: unable to read random data from %s: %s\n", RANDOM_DEV_NAME, r == -1 ? strerror(errno) : r == 0 ? "EOF" : "not enough data"); } close(fd); } else { printf("inet: unable to open random device %s: %s\n", RANDOM_DEV_NAME, strerror(errno)); } if (timerand) { printf("inet: using current time for random-number seed\n"); r= gettimeofday(&tv, NULL); if (r == -1) { printf("sysutime failed: %s\n", strerror(errno)); exit(1); } memcpy(randbits, &tv, sizeof(tv)); } init_rand256(randbits); /* Our new identity as a server. */ this_proc= info->endpoint; #ifdef BUF_CONSISTENCY_CHECK inet_buf_debug= (getenv("inetbufdebug") && (strcmp(getenv("inetbufdebug"), "on") == 0)); inet_buf_debug= 100; if (inet_buf_debug) { ip_warning(( "buffer consistency check enabled" )); } #endif if (getenv("killerinet")) { ip_warning(( "killer inet active" )); killer_inet= 1; } nw_init(); /* Subscribe to driver events for network drivers. */ r = ds_subscribe("drv\\.net\\..*", DSF_INITIAL | DSF_OVERWRITE); if(r != OK) { ip_panic(("inet: can't subscribe to driver events")); } /* Drop root privileges */ if ((pw = getpwnam(SERVICE_LOGIN)) == NULL) { printf("inet: unable to retrieve uid of SERVICE_LOGIN, " "still running as root"); } else if (setuid(pw->pw_uid) != 0) { ip_panic(("inet: unable to drop privileges")); } /* Announce we are up. INET announces its presence to VFS just like * any other character driver. */ chardriver_announce(); return(OK); }