static int resolve_remote(Connection *c) { static const struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM, .ai_flags = AI_ADDRCONFIG }; union sockaddr_union sa = {}; const char *node, *service; socklen_t salen; int r; if (path_is_absolute(arg_remote_host)) { sa.un.sun_family = AF_UNIX; strncpy(sa.un.sun_path, arg_remote_host, sizeof(sa.un.sun_path)-1); sa.un.sun_path[sizeof(sa.un.sun_path)-1] = 0; salen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path); return connection_start(c, &sa.sa, salen); } if (arg_remote_host[0] == '@') { sa.un.sun_family = AF_UNIX; sa.un.sun_path[0] = 0; strncpy(sa.un.sun_path+1, arg_remote_host+1, sizeof(sa.un.sun_path)-2); sa.un.sun_path[sizeof(sa.un.sun_path)-1] = 0; salen = offsetof(union sockaddr_union, un.sun_path) + 1 + strlen(sa.un.sun_path + 1); return connection_start(c, &sa.sa, salen); }
bool ClientHandler::handle_hello(network_message msg) { //recieve message int num; char *name = new char[msg.value]; int pointeroffset = 0; do { if ((num = read(client_socket, name+pointeroffset, msg.value-pointeroffset))== -1) { std::cout <<"recv error" << std::endl; return false; } else if(num == 0) { //disconnected std::cout <<"Disconnected" << std::endl; return false; } pointeroffset = (pointeroffset + num) % msg.value; } while(pointeroffset != 0); nick_mtx.lock(); nick = std::string(name); nick_mtx.unlock(); delete name; connection_start(); return true; }
static connection connection_get (string name, string session) { connection con= connection (name * "-" * session); if (is_nil (con)) { if (connection_start (name, session, true) != "ok") return con; con= connection (name * "-" * session); } return con; }
static int resolve_remote(Connection *c) { static const struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_STREAM, .ai_flags = AI_ADDRCONFIG }; union sockaddr_union sa = {}; const char *node, *service; int r; if (IN_SET(arg_remote_host[0], '/', '@')) { int salen; salen = sockaddr_un_set_path(&sa.un, arg_remote_host); if (salen < 0) { log_error_errno(salen, "Specified address doesn't fit in an AF_UNIX address, refusing: %m"); goto fail; } return connection_start(c, &sa.sa, salen); } service = strrchr(arg_remote_host, ':'); if (service) { node = strndupa(arg_remote_host, service - arg_remote_host); service++; } else { node = arg_remote_host; service = "80"; } log_debug("Looking up address info for %s:%s", node, service); r = sd_resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_cb, c); if (r < 0) { log_error_errno(r, "Failed to resolve remote host: %m"); goto fail; } return 0; fail: connection_free(c); return 0; /* ignore errors, continue serving */ }
static int resolve_cb(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) { Connection *c = userdata; assert(q); assert(c); if (ret != 0) { log_error("Failed to resolve host: %s", gai_strerror(ret)); goto fail; } c->resolve_query = sd_resolve_query_unref(c->resolve_query); return connection_start(c, ai->ai_addr, ai->ai_addrlen); fail: connection_free(c); return 0; /* ignore errors, continue serving */ }
/****************************************************************************** * non-static function definitions ******************************************************************************/ int main(int argc, char **argv) { int retval; struct handles handles = { .sock = -1, .sock2 = -1, .server_sock = -1, .file = NULL, .file2 = NULL, }; struct scope_parameter param; if(0 != handle_options(argc,argv, &g_options)) { usage(argv[0]); return 1; } signal_init(); if (scope_init(¶m, &g_options)) { retval = 2; goto cleanup; } if (g_options.mode == client || g_options.mode == server) { if (connection_init(&g_options, &handles)) { retval = 3; goto cleanup_scope; } } else if (g_options.mode == file) { if (file_open(&g_options, &handles)) { retval = 4; goto cleanup_scope; } } retval = 0; while (!transfer_interrupted()) { if (g_options.mode == client || g_options.mode == server) { if (connection_start(&g_options, &handles) < 0) { fprintf(stderr, "%s: problem opening connection.\n", __func__); continue; } } retval = transfer_data(¶m, &g_options, &handles); if (retval && !transfer_interrupted()) fprintf(stderr, "%s: problem transferring data.\n", __func__); if (g_options.mode == client || g_options.mode == server) connection_stop(&handles); if (g_options.mode == file) break; } connection_cleanup(&handles); file_close(&handles); cleanup_scope: scope_cleanup(¶m, &g_options); cleanup: signal_exit(); return retval; }
static int worker_server(worker *info) { /* server worker */ int nfds, fd, i; worker *w; int num = info->num; master_server *master_srv = info->master_srv; /* attach the shared mem */ int shmid; key_t key = master_srv->pid + num; if ((shmid = shmget(key, sizeof(worker), 0666)) < 0) { perror ("ERROR shmget"); exit (1); } /* attach it */ if ((w = shmat(shmid, NULL, 0)) == (char*) -1) { perror ("ERROR shmat"); exit (1); } /* process id */ w->pid = getpid(); /* worker process started */ printf (" * Worker process #%d is started.\n", num+1); /* pre-setup worker connections */ w->conns = connection_setup(master_srv); /* create a new event handler for this worker */ event_handler *ev_handler = events_create(master_srv->config->max_clients); /* share the event fd */ w->ev_handler.fd = ev_handler->fd; /* starting keep-alive clean-up thread */ printf (" * Starting keep-alive clean-up thread for worker #%d.\n", num+1); pthread_t thread_keep_alive; int rc_cleanup = pthread_create(&thread_keep_alive, NULL, worker_keep_alive_cleanup, w); /* starting heartbeat thread */ printf (" * Starting heartbeat thread for worker #%d.\n", num+1); pthread_t thread_heartbeat; int rc_heartbeat = pthread_create(&thread_heartbeat, NULL, worker_heartbeat, w); /* entering main loop... */ while (master_srv->running) { /* check for new data */ if ((nfds = events_wait(ev_handler, master_srv)) == -1) { perror ("ERROR epoll_pwait"); } for (i = 0; i < nfds; ++i) { /* data received */ fd = events_get_fd(ev_handler, i); connection *conn = w->conns[fd]; if (events_closed(ev_handler, i)) { /* the other end closed the connection */ conn->status = CONN_INACTIVE; printf (" * Other end closed the connection\n"); } else if (conn->status == CONN_INACTIVE) { /* this connection is inactive, initiate a new connection */ connection_start (master_srv, conn); } connection_handle (w, conn); /* closing */ if (conn->status == CONN_INACTIVE) { if (events_del_event(ev_handler, fd) == -1) { perror ("ERROR events_del_event"); } close (fd); } } } printf (" * Shutting down worker process #%d...\n", num+1); /* free event handler */ events_free (ev_handler, master_srv->config->max_clients); /* TODO: free all connections */ free (w->conns); /* free this workers memory */ worker_free (w); exit (0); }