📄 main.c
字号:
log_debug(ZONE, "checking connection state for %s", key); if(xhash_iter_first(conn->states)) do { xhash_iter_get(conn->states, NULL, (void *) &state); /* drop invalid */ if(state == conn_INVALID) { xhash_zap(conn->states, key); log_debug(ZONE, "dropping invalid connection for conn key %s", key); } } while(xhash_iter_next(conn->states)); } while(xhash_iter_next(s2s->out)); } /* keepalives */ if(xhash_iter_first(s2s->out)) do { xhash_iter_get(s2s->out, NULL, (void **) &conn); if(s2s->check_keepalive > 0 && conn->last_activity > 0 && now > conn->last_activity + s2s->check_keepalive && conn->s->state >= state_STREAM) { log_debug(ZONE, "sending keepalive for %d", conn->fd); sx_raw_write(conn->s, " ", 1); mio_write(s2s->mio, conn->fd); } } while(xhash_iter_next(s2s->out));}int main(int argc, char **argv) { s2s_t s2s; char *config_file; int optchar;#ifdef POOL_DEBUG time_t pool_time = 0;#endif#ifdef HAVE_UMASK umask((mode_t) 0027);#endif srand(time(NULL));#ifdef HAVE_WINSOCK2_H/* get winsock running */ { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 2 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { /* !!! tell user that we couldn't find a usable winsock dll */ return 0; } }#endif jabber_signal(SIGINT, _s2s_signal); jabber_signal(SIGTERM, _s2s_signal);#ifdef SIGHUP jabber_signal(SIGHUP, _s2s_signal_hup);#endif#ifdef SIGPIPE jabber_signal(SIGPIPE, SIG_IGN);#endif s2s = (s2s_t) malloc(sizeof(struct s2s_st)); memset(s2s, 0, sizeof(struct s2s_st)); /* load our config */ s2s->config = config_new(); config_file = CONFIG_DIR "/s2s.xml"; /* cmdline parsing */ while((optchar = getopt(argc, argv, "Dc:h?")) >= 0) { switch(optchar) { case 'c': config_file = optarg; break; case 'D':#ifdef DEBUG set_debug_flag(1);#else printf("WARN: Debugging not enabled. Ignoring -D.\n");#endif break; case 'h': case '?': default: fputs( "s2s - jabberd server-to-server connector (" VERSION ")\n" "Usage: s2s <options>\n" "Options are:\n" " -c <config> config file to use [default: " CONFIG_DIR "/s2s.xml]\n"#ifdef DEBUG " -D Show debug output\n"#endif , stdout); config_free(s2s->config); free(s2s); return 1; } } if(config_load(s2s->config, config_file) != 0) { fputs("s2s: couldn't load config, aborting\n", stderr); config_free(s2s->config); free(s2s); return 2; } _s2s_config_expand(s2s); s2s->log = log_new(s2s->log_type, s2s->log_ident, s2s->log_facility); log_write(s2s->log, LOG_NOTICE, "starting up"); _s2s_pidfile(s2s); s2s->outq = xhash_new(401); s2s->out = xhash_new(401); s2s->in = xhash_new(401); s2s->dnscache = xhash_new(401); s2s->pc = prep_cache_new(); s2s->dead = jqueue_new(); s2s->sx_env = sx_env_new();#ifdef HAVE_SSL /* get the ssl context up and running */ if(s2s->local_pemfile != NULL) { s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, s2s->local_pemfile, NULL); if(s2s->sx_ssl == NULL) { log_write(s2s->log, LOG_ERR, "failed to load local SSL pemfile, SSL will not be available to peers"); s2s->local_pemfile = NULL; } else log_debug(ZONE, "loaded pemfile for SSL connections to peers"); } /* try and get something online, so at least we can encrypt to the router */ if(s2s->sx_ssl == NULL && s2s->router_pemfile != NULL) { s2s->sx_ssl = sx_env_plugin(s2s->sx_env, sx_ssl_init, s2s->router_pemfile, NULL); if(s2s->sx_ssl == NULL) { log_write(s2s->log, LOG_ERR, "failed to load router SSL pemfile, channel to router will not be SSL encrypted"); s2s->router_pemfile = NULL; } }#endif /* get sasl online */ s2s->sx_sasl = sx_env_plugin(s2s->sx_env, sx_sasl_init, NULL, NULL, 0); if(s2s->sx_sasl == NULL) { log_write(s2s->log, LOG_ERR, "failed to initialise SASL context, aborting"); exit(1); } s2s->sx_db = sx_env_plugin(s2s->sx_env, s2s_db_init); s2s->mio = mio_new(1024); s2s->retry_left = s2s->retry_init; _s2s_router_connect(s2s); while(!s2s_shutdown) { mio_run(s2s->mio, 5); if(s2s_logrotate) { log_write(s2s->log, LOG_NOTICE, "reopening log ..."); log_free(s2s->log); s2s->log = log_new(s2s->log_type, s2s->log_ident, s2s->log_facility); log_write(s2s->log, LOG_NOTICE, "log started"); s2s_logrotate = 0; } if(s2s_lost_router) { if(s2s->retry_left < 0) { log_write(s2s->log, LOG_NOTICE, "attempting reconnect"); sleep(s2s->retry_sleep); s2s_lost_router = 0; _s2s_router_connect(s2s); } else if(s2s->retry_left == 0) { s2s_shutdown = 1; } else { log_write(s2s->log, LOG_NOTICE, "attempting reconnect (%d left)", s2s->retry_left); s2s->retry_left--; sleep(s2s->retry_sleep); s2s_lost_router = 0; _s2s_router_connect(s2s); } } /* cleanup dead sx_ts */ while(jqueue_size(s2s->dead) > 0) sx_free((sx_t) jqueue_pull(s2s->dead)); /* time checks */ if(s2s->check_interval > 0 && time(NULL) >= s2s->next_check) { log_debug(ZONE, "running time checks"); _s2s_time_checks(s2s); s2s->next_check = time(NULL) + s2s->check_interval; log_debug(ZONE, "next time check at %d", s2s->next_check); }#ifdef POOL_DEBUG if(time(NULL) > pool_time + 60) { pool_stat(1); pool_time = time(NULL); }#endif } log_write(s2s->log, LOG_NOTICE, "shutting down"); /* !!! close conns */ /* !!! walk & free resolve queues */ xhash_free(s2s->outq); xhash_free(s2s->out); xhash_free(s2s->in); xhash_free(s2s->dnscache); prep_cache_free(s2s->pc); sx_free(s2s->router); sx_env_free(s2s->sx_env); mio_free(s2s->mio); log_free(s2s->log); config_free(s2s->config); free(s2s->local_secret); free(s2s);#ifdef POOL_DEBUG pool_stat(1);#endif return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -