/** Listen on endpoint. */ static int net_listen(lua_State *L) { /* Check parameters */ int n = lua_gettop(L); int port = KR_DNS_PORT; if (n > 1 && lua_isnumber(L, 2)) { port = lua_tointeger(L, 2); } /* Process interface or (address, port) pair. */ if (lua_istable(L, 1)) { return net_listen_iface(L, port); } else if (n < 1 || !lua_isstring(L, 1)) { format_error(L, "expected 'listen(string addr, number port = 53)'"); lua_error(L); } /* Open resolution context cache */ struct engine *engine = engine_luaget(L); int ret = network_listen(&engine->net, lua_tostring(L, 1), port, NET_TCP|NET_UDP); if (ret != 0) { format_error(L, kr_strerror(ret)); lua_error(L); } lua_pushboolean(L, true); return 1; }
/** Listen on interface address list. */ static int net_listen_iface(lua_State *L, int port) { /* Expand 'addr' key if exists */ lua_getfield(L, 1, "addr"); if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_pushvalue(L, 1); } /* Bind to address list */ struct engine *engine = engine_luaget(L); size_t count = lua_rawlen(L, -1); for (size_t i = 0; i < count; ++i) { lua_rawgeti(L, -1, i + 1); int ret = network_listen(&engine->net, lua_tostring(L, -1), port, NET_TCP|NET_UDP); if (ret != 0) { format_error(L, kr_strerror(ret)); lua_error(L); } lua_pop(L, 1); } lua_pushboolean(L, true); return 1; }
int ps4link_connect(char *hostname) { // Listen datagram socket port for debugnet console. console_socket = network_listen(0x4712, SOCK_DGRAM); // Create the console thread. if (console_socket > 0) { pthread_create(&console_thread_id, NULL, ps4link_thread_console, (void *)&console_thread_id); } // Connect to the request port. request_socket = network_connect(hostname, 0x4711, SOCK_STREAM); // request_socket = network_listen(0x4711, SOCK_STREAM); // Create the request thread. while(request_socket<0) { request_socket = network_connect(hostname, 0x4711, SOCK_STREAM); sleep(1); debugNetPrintf(DEBUG,"waiting ps4...\n"); } if (request_socket > 0) { pthread_create(&request_thread_id, NULL, ps4link_thread_request, (void *)&request_thread_id); } // Connect to the command port future use to send commands to psp2 //command_socket = network_connect(hostname, 0x4712, SOCK_DGRAM); // Delay for a moment to let ps2link finish setup. #ifdef _WIN32 Sleep(1); #else sleep(1); #endif // End function. return 0; }
void system_lineCodingBitRateHandler(uint32_t bitrate) { // todo - ideally the system should post a reset pending event before // resetting. This does mean the application can block entering listening mode #ifdef START_DFU_FLASHER_SERIAL_SPEED if (bitrate == start_dfu_flasher_serial_speed) { network.connect_cancel(true); //Reset device and briefly enter DFU bootloader mode System.dfu(false); } #endif #ifdef START_YMODEM_FLASHER_SERIAL_SPEED if (!network_listening(0, 0, NULL) && bitrate == start_ymodem_flasher_serial_speed) { network_listen(0, 0, 0); } #endif }
void SystemSetupConsole<Config>::exit() { network_listen(0, NETWORK_LISTEN_EXIT, nullptr); }
int main(int argc, char **argv) { int forks = 1; array_t(char*) addr_set; array_init(addr_set); char *keyfile = NULL; const char *config = NULL; char *keyfile_buf = NULL; /* Long options. */ int c = 0, li = 0, ret = 0; struct option opts[] = { {"addr", required_argument, 0, 'a'}, {"config", required_argument, 0, 'c'}, {"keyfile",required_argument, 0, 'k'}, {"forks",required_argument, 0, 'f'}, {"verbose", no_argument, 0, 'v'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while ((c = getopt_long(argc, argv, "a:c:f:k:vVh", opts, &li)) != -1) { switch (c) { case 'a': array_push(addr_set, optarg); break; case 'c': config = optarg; break; case 'f': g_interactive = 0; forks = atoi(optarg); if (forks == 0) { kr_log_error("[system] error '-f' requires number, not '%s'\n", optarg); return EXIT_FAILURE; } #if (!defined(UV_VERSION_HEX)) || (!defined(SO_REUSEPORT)) if (forks > 1) { kr_log_error("[system] libuv 1.7+ is required for SO_REUSEPORT support, multiple forks not supported\n"); return EXIT_FAILURE; } #endif break; case 'k': keyfile_buf = malloc(PATH_MAX); assert(keyfile_buf); /* Check if the path is absolute */ if (optarg[0] == '/') { keyfile = strdup(optarg); } else { /* Construct absolute path, the file may not exist */ keyfile = realpath(".", keyfile_buf); if (keyfile) { int len = strlen(keyfile); int namelen = strlen(optarg); if (len + namelen < PATH_MAX - 1) { keyfile[len] = '/'; memcpy(keyfile + len + 1, optarg, namelen + 1); keyfile = strdup(keyfile); /* Duplicate */ } else { keyfile = NULL; /* Invalidate */ } } } free(keyfile_buf); if (!keyfile) { kr_log_error("[system] keyfile '%s': not writeable\n", optarg); return EXIT_FAILURE; } break; case 'v': kr_debug_set(true); break; case 'V': kr_log_info("%s, version %s\n", "Knot DNS Resolver", PACKAGE_VERSION); return EXIT_SUCCESS; case 'h': case '?': help(argc, argv); return EXIT_SUCCESS; default: help(argc, argv); return EXIT_FAILURE; } } /* Switch to rundir. */ if (optind < argc) { const char *rundir = argv[optind]; if (access(rundir, W_OK) != 0) { kr_log_error("[system] rundir '%s': %s\n", rundir, strerror(errno)); return EXIT_FAILURE; } ret = chdir(rundir); if (ret != 0) { kr_log_error("[system] rundir '%s': %s\n", rundir, strerror(errno)); return EXIT_FAILURE; } if(config && access(config, R_OK) != 0) { kr_log_error("[system] rundir '%s'\n", rundir); kr_log_error("[system] config '%s': %s\n", config, strerror(errno)); return EXIT_FAILURE; } } kr_crypto_init(); /* Fork subprocesses if requested */ int fork_count = forks; while (--forks > 0) { int pid = fork(); if (pid < 0) { perror("[system] fork"); return EXIT_FAILURE; } /* Forked process */ if (pid == 0) { kr_crypto_reinit(); break; } } /* Block signals. */ uv_loop_t *loop = uv_default_loop(); uv_signal_t sigint, sigterm; uv_signal_init(loop, &sigint); uv_signal_init(loop, &sigterm); uv_signal_start(&sigint, signal_handler, SIGINT); uv_signal_start(&sigterm, signal_handler, SIGTERM); /* Create a server engine. */ knot_mm_t pool = { .ctx = mp_new (4096), .alloc = (knot_mm_alloc_t) mp_alloc }; struct engine engine; ret = engine_init(&engine, &pool); if (ret != 0) { kr_log_error("[system] failed to initialize engine: %s\n", kr_strerror(ret)); return EXIT_FAILURE; } /* Create worker */ struct worker_ctx *worker = init_worker(loop, &engine, &pool, forks, fork_count); if (!worker) { kr_log_error("[system] not enough memory\n"); return EXIT_FAILURE; } /* Bind to sockets and run */ for (size_t i = 0; i < addr_set.len; ++i) { int port = 53; const char *addr = set_addr(addr_set.at[i], &port); ret = network_listen(&engine.net, addr, (uint16_t)port, NET_UDP|NET_TCP); if (ret != 0) { kr_log_error("[system] bind to '%s#%d' %s\n", addr, port, knot_strerror(ret)); ret = EXIT_FAILURE; } } /* Start the scripting engine */ if (ret == 0) { ret = engine_start(&engine, config ? config : "config"); if (ret == 0) { if (keyfile) { auto_free char *cmd = afmt("trust_anchors.file = '%s'", keyfile); if (!cmd) { kr_log_error("[system] not enough memory\n"); return EXIT_FAILURE; } engine_cmd(&engine, cmd); lua_settop(engine.L, 0); } /* Run the event loop */ ret = run_worker(loop, &engine); } } /* Cleanup. */ array_clear(addr_set); engine_deinit(&engine); worker_reclaim(worker); mp_delete(pool.ctx); if (ret != 0) { ret = EXIT_FAILURE; } kr_crypto_cleanup(); return ret; }