int ini_server(const char * url, Protocol * p, TCFBroadcastGroup * b) { ChannelServer * serv = NULL; PeerServer * ps = NULL; Trap trap; if (!set_trap(&trap)) { bcg = NULL; proto = NULL; if (ps != NULL) peer_server_free(ps); errno = trap.error; return -1; } bcg = b; proto = p; ps = channel_peer_from_url(url); if (ps == NULL) str_exception(ERR_OTHER, "Invalid server URL"); peer_server_addprop(ps, loc_strdup("Name"), loc_strdup(PROXY_NAME)); peer_server_addprop(ps, loc_strdup("Proxy"), loc_strdup("")); SERVER_ADDPROP_HOOK; serv = channel_server(ps); if (serv == NULL) exception(errno); serv->new_conn = channel_new_connection; clear_trap(&trap); add_channel_redirection_listener(channel_redirection_listener); return 0; }
static void refresh_peer_server(int sock, PeerServer * ps) { unsigned i; const char * transport = peer_server_getprop(ps, "TransportName", NULL); assert(transport != NULL); if (strcmp(transport, "UNIX") == 0) { char str_id[64]; PeerServer * ps2 = peer_server_alloc(); ps2->flags = ps->flags | PS_FLAG_LOCAL | PS_FLAG_DISCOVERABLE; for (i = 0; i < ps->ind; i++) { peer_server_addprop(ps2, loc_strdup(ps->list[i].name), loc_strdup(ps->list[i].value)); } snprintf(str_id, sizeof(str_id), "%s:%s", transport, peer_server_getprop(ps, "Host", "")); for (i = 0; str_id[i]; i++) { /* Character '/' is prohibited in a peer ID string */ if (str_id[i] == '/') str_id[i] = '|'; } peer_server_addprop(ps2, loc_strdup("ID"), loc_strdup(str_id)); peer_server_add(ps2, PEER_DATA_RETENTION_PERIOD * 2); } else { struct sockaddr_in sin; #if defined(_WRS_KERNEL) int sinlen; #else socklen_t sinlen; #endif const char *str_port = peer_server_getprop(ps, "Port", NULL); int ifcind; struct in_addr src_addr; ip_ifc_info ifclist[MAX_IFC]; sinlen = sizeof sin; if (getsockname(sock, (struct sockaddr *)&sin, &sinlen) != 0) { trace(LOG_ALWAYS, "refresh_peer_server: getsockname error: %s", errno_to_str(errno)); return; } ifcind = build_ifclist(sock, MAX_IFC, ifclist); while (ifcind-- > 0) { char str_host[64]; char str_id[64]; PeerServer * ps2; if (sin.sin_addr.s_addr != INADDR_ANY && (ifclist[ifcind].addr & ifclist[ifcind].mask) != (sin.sin_addr.s_addr & ifclist[ifcind].mask)) { continue; } src_addr.s_addr = ifclist[ifcind].addr; ps2 = peer_server_alloc(); ps2->flags = ps->flags | PS_FLAG_LOCAL | PS_FLAG_DISCOVERABLE; for (i = 0; i < ps->ind; i++) { peer_server_addprop(ps2, loc_strdup(ps->list[i].name), loc_strdup(ps->list[i].value)); } inet_ntop(AF_INET, &src_addr, str_host, sizeof(str_host)); snprintf(str_id, sizeof(str_id), "%s:%s:%s", transport, str_host, str_port); peer_server_addprop(ps2, loc_strdup("ID"), loc_strdup(str_id)); peer_server_addprop(ps2, loc_strdup("Host"), loc_strdup(str_host)); peer_server_addprop(ps2, loc_strdup("Port"), loc_strdup(str_port)); peer_server_add(ps2, PEER_DATA_RETENTION_PERIOD * 2); } } }
static int lua_peer_setvalue(lua_State *L) { struct peer_extra *pse = NULL; assert(L == luastate); if(lua_gettop(L) != 3 || (pse = lua2peer(L, 1)) == NULL || !lua_isstring(L, 2) || !lua_isstring(L, 3)) { luaL_error(L, "wrong number or type of arguments"); } if(pse->ps == NULL) luaL_error(L, "stale peer"); trace(LOG_LUA, "lua_peer_setvalue %p", pse->ps); peer_server_addprop(pse->ps, loc_strdup(lua_tostring(L, 2)), loc_strdup(lua_tostring(L, 3))); return 0; }
static int lua_peer_server_from_url(lua_State *L) { PeerServer *ps; const char *url; assert(L == luastate); if(lua_gettop(L) != 1 || !lua_isstring(L, 1)) { luaL_error(L, "wrong number or type of arguments"); } url = lua_tostring(L, 1); ps = channel_peer_from_url(url); trace(LOG_LUA, "lua_peer_server_from_url %s %p", url, ps); if(ps == NULL) luaL_error(L, "cannot parse url: %s", lua_tostring(L, 1)); if(ps->id == NULL) { peer_server_addprop(ps, loc_strdup("ID"), loc_strdup(lua_tostring(L, 1))); } lua_alloc_pse(L, ps); return 1; }
ChannelServer * channel_np_server(PeerServer * ps) { /* const char * host = peer_server_getprop(ps, "Host", NULL);*/ const char * port = peer_server_getprop(ps, "Port", NULL); char port_str[16]; const char * host = peer_server_getprop(ps, "Host", NULL); const char * certificate = peer_server_getprop(ps, "CertificateFile", NULL); const char * key = peer_server_getprop(ps, "KeyFile", NULL); noPollCtx * np_ctx; noPollConn * np_listener; int ssl; assert(is_dispatch_thread()); if (port == NULL) { sprintf(port_str, "%d", DISCOVERY_TCF_PORT); port = port_str; } ini_nopoll(); /* create the nopoll ctx */ np_ctx = nopoll_ctx_new (); if(np_ctx == NULL) { trace(LOG_ALWAYS, "Unable to create nopoll context: %s", errno_to_str(errno)); return NULL; } /*nopoll_log_enable(np_ctx, nopoll_true);*/ ssl = strcmp(peer_server_getprop(ps, "TransportName", ""), "WSS") == 0; if (ssl) np_listener = nopoll_listener_tls_new (np_ctx, host == NULL ? "0.0.0.0" : host, port); else np_listener = nopoll_listener_new (np_ctx, host == NULL ? "0.0.0.0" : host, port); if (np_listener == NULL) { trace(LOG_ALWAYS, "Unable to create nopoll listener: %s", errno_to_str(errno)); nopoll_ctx_unref(np_ctx); return NULL; } /* configure certificates to be used by this listener */ if (ssl) { errno = 0; if (! nopoll_listener_set_certificate (np_listener, certificate, key, NULL)) { printf ("ERROR: unable to configure certificates for TLS websocket..\n"); if (errno == 0) errno = EINVAL; nopoll_conn_close (np_listener); nopoll_ctx_unref(np_ctx); return NULL; } /* register certificates at context level */ errno = 0; if (! nopoll_ctx_set_certificate (np_ctx, NULL, certificate, key, NULL)) { printf ("ERROR: unable to setup certificates at context level..\n"); if (errno == 0) errno = EINVAL; nopoll_conn_close (np_listener); nopoll_ctx_unref(np_ctx); return NULL; } } peer_server_addprop(ps, loc_strdup("Port"), loc_strdup(port)); return channel_server_create(ps, np_ctx, np_listener, ssl); }
int tcf_log(void) { #else int main(int argc, char ** argv) { int c; int ind; const char * log_name = "-"; #endif const char * url = "TCP:"; PeerServer * ps; ChannelServer * serv; int print_server_properties = 0; ini_mdep(); ini_trace(); ini_events_queue(); ini_asyncreq(); log_mode = LOG_TCFLOG; #if defined(_WRS_KERNEL) progname = "tcf"; open_log_file("-"); #else progname = argv[0]; /* Parse arguments */ for (ind = 1; ind < argc; ind++) { char * s = argv[ind]; if (*s++ != '-') break; while (s && (c = *s++) != '\0') { switch (c) { case 'h': show_help(); exit(0); case 'n': auto_redirect = 0; break; case 'S': print_server_properties = 1; break; #if ENABLE_Trace case 'l': #endif case 'L': case 's': case 'f': if (*s == '\0') { if (++ind >= argc) { fprintf(stderr, "%s: error: no argument given to option '%c'\n", progname, c); exit(1); } s = argv[ind]; } switch (c) { #if ENABLE_Trace case 'l': if (parse_trace_mode(s, &log_mode) != 0) { fprintf(stderr, "Cannot parse log level: %s\n", s); exit(1); } break; #endif case 'L': log_name = s; break; case 's': url = s; break; case 'f': if (filter_add_message_filter(s) != 0) { fprintf(stderr, "Cannot parse filter level: %s\n", s); exit(1); } break; default: fprintf(stderr, "%s: error: illegal option '%c'\n", progname, c); show_help(); exit(1); } s = NULL; break; default: ILLEGAL_OPTION_HOOK; fprintf(stderr, "%s: error: illegal option '%c'\n", progname, c); show_help(); exit(1); } } } open_log_file(log_name); if (ind < argc) { dest_url = argv[ind++]; if (!auto_redirect) { fprintf(stderr, "Automatic redirect disabled: argument '%s' ignored\n", dest_url); dest_url = NULL; } } #endif bcg = broadcast_group_alloc(); /* Default filters (use "-fi" to disable). */ filter_add_message_filter("o,E,Locator,peerHeartBeat"); filter_add_message_filter("o,E,Locator,peerAdded"); filter_add_message_filter("o,E,Locator,peerRemoved"); filter_add_message_filter("o,E,Locator,peerChanged"); filter_add_message_filter("o,C,Locator,getPeers"); filter_add_message_filter("tr10,C,FileSystem,read"); filter_add_message_filter("t10,C,FileSystem,write"); set_proxy_log_filter_listener2(filter_is_log_filtered); ps = channel_peer_from_url(url); if (ps == NULL) { fprintf(stderr, "%s: invalid server URL (-s option value): %s\n", progname, url); exit(1); } peer_server_addprop(ps, loc_strdup("Name"), loc_strdup("TCF Protocol Logger")); peer_server_addprop(ps, loc_strdup("Proxy"), loc_strdup("")); SERVER_ADDPROP_HOOK; serv = channel_server(ps); if (serv == NULL) { fprintf(stderr, "%s: cannot create TCF server: %s\n", progname, errno_to_str(errno)); exit(1); } serv->new_conn = channel_new_connection; discovery_start(); if (print_server_properties) { ChannelServer * s; char * server_properties; assert(!list_is_empty(&channel_server_root)); s = servlink2channelserverp(channel_server_root.next); server_properties = channel_peer_to_json(s->ps); printf("Server-Properties: %s\n", server_properties); fflush(stdout); trace(LOG_ALWAYS, "Server-Properties: %s", server_properties); loc_free(server_properties); } /* Process events - must run on the initial thread since ptrace() * returns ECHILD otherwise, thinking we are not the owner. */ run_event_loop(); return 0; }
int tcf_log(void) { #else int main(int argc, char ** argv) { int c; int ind; const char * log_name = "-"; #endif const char * url = "TCP:"; PeerServer * ps; ChannelServer * serv; ini_mdep(); ini_trace(); ini_events_queue(); ini_asyncreq(); log_mode = LOG_TCFLOG; #if defined(_WRS_KERNEL) progname = "tcf"; open_log_file("-"); #else progname = argv[0]; /* Parse arguments */ for (ind = 1; ind < argc; ind++) { const char * s = argv[ind]; if (*s != '-') { break; } s++; while ((c = *s++) != '\0') { switch (c) { case 'h': show_help(); exit (0); case 'l': case 'L': case 's': if (*s == '\0') { if (++ind >= argc) { fprintf(stderr, "%s: error: no argument given to option '%c'\n", progname, c); exit(1); } s = argv[ind]; } switch (c) { case 'l': if (parse_trace_mode(s, &log_mode) != 0) { fprintf(stderr, "Cannot parse log level: %s\n", s); exit(1); } break; case 'L': log_name = s; break; case 's': url = s; break; default: fprintf(stderr, "%s: error: illegal option '%c'\n", progname, c); show_help(); exit(1); } s = ""; break; default: fprintf(stderr, "%s: error: illegal option '%c'\n", progname, c); show_help(); exit(1); } } } open_log_file(log_name); if (ind < argc) { dest_url = argv[ind++]; } #endif ps = channel_peer_from_url(url); if (ps == NULL) { fprintf(stderr, "%s: invalid server URL (-s option value): %s\n", progname, url); exit(1); } peer_server_addprop(ps, loc_strdup("Name"), loc_strdup("TCF Protocol Logger")); peer_server_addprop(ps, loc_strdup("Proxy"), loc_strdup("")); serv = channel_server(ps); if (serv == NULL) { fprintf(stderr, "%s: cannot create TCF server: %s\n", progname, errno_to_str(errno)); exit(1); } serv->new_conn = channel_new_connection; discovery_start(); /* Process events - must run on the initial thread since ptrace() * returns ECHILD otherwise, thinking we are not the owner. */ run_event_loop(); return 0; }