int main(void) { socket_t listener; socket_set_t clients; if(false == socket_create_socket(&listener, LIBNET_PROTOCOL_TCP, LIBNET_IPV4)) { printf("server: failed to create socket\r\n"); return -1; } socket_create_set(&clients); if(false == socket_listen(&listener, PORT)) { printf("server: failed to listen at %d\r\n", PORT); return -1; } printf("server: listening at %d..\n", PORT); while(socket_set_get_client_amount(&clients) < 1) { socket_accept(&listener, &clients); } printf("server: client connected (ip: %s)\r\n", "127.0.0.1"); _sleep(2); // to make sure the client recognizes everything socket_release_set(&clients); socket_disconnect(&listener); socket_release_socket(&listener); return 0; }
/** @brief Disconnect from Jabber, and close the socket. @param session Pointer to the transport_session to be disconnected. @return 0 in all cases. */ int session_disconnect( transport_session* session ) { if( session && session->sock_id != 0 ) { socket_send(session->sock_id, "</stream:stream>"); socket_disconnect(session->sock_mgr, session->sock_id); session->sock_id = 0; } return 0; }
int fourd_close(FOURD *cnx) { if(logout(cnx,4)!=0) return 1; if(quit(cnx,5)!=0) return 1; socket_disconnect(cnx); return 0; }
int socket_send(FOURD *cnx,const char*msg) { int iResult; //Printf("Send-len:%d\n",strlen(msg)) Printf("Send:\n%s",msg); // Send an initial buffer iResult = send( cnx->socket, msg, (int)strlen(msg), 0 ); if (iResult == SOCKET_ERROR) { Printf("send failed: %d\n", WSAGetLastError()); socket_disconnect(cnx); return 1; } return 0; }
int socket_send_data(FOURD *cnx,const char*msg,int len) { int iResult; Printf("Send:%d bytes\n",len); PrintData(msg,len); Printf("\n"); // Send an initial buffer iResult = send( cnx->socket, msg, len, 0 ); if (iResult == SOCKET_ERROR) { Printf("send failed: %d\n", WSAGetLastError()); socket_disconnect(cnx); return 1; } return 0; }
void server_stop (void) { if (!server_socket) { return; } MSG_SERVER_SHOUT_AT_ALL_PLAYERS(CRITICAL, 0, 0, "SERVER GOING DOWN"); socket_tx_server_close(); socket_disconnect(server_socket); server_socket = 0; on_server = false; }
static gboolean wapi_disconnectex (guint32 fd, WapiOverlapped *overlapped, guint32 flags, guint32 reserved) { DEBUG ("%s: called on socket %d!", __func__, fd); if (reserved != 0) { WSASetLastError (WSAEINVAL); return(FALSE); } /* We could check the socket type here and fail unless its * SOCK_STREAM, SOCK_SEQPACKET or SOCK_RDM (according to msdn) * if we really wanted to */ return(socket_disconnect (fd)); }
void client_stop (client_t *c) { int i; if (c->sock == MAX_SOCK_NUM) return; // attempt to close the connection gracefully (send a FIN to other side) socket_disconnect (c->sock); // wait a second for the connection to close for (i=0; i<100; i++) { if (client_status (c) == SnSR_CLOSED) break; usleep (10000); } // if it hasn't closed, close it forcefully if (client_status(c) != SnSR_CLOSED) socket_close (c->sock); _socket_port[c->sock] = 0; c->sock = MAX_SOCK_NUM; }
int proxy_disconnect(struct proxy *self) { socket_disconnect(self->s); return 0; }
/** @brief Connect to the Jabber server as a client and open a Jabber session. @param session Pointer to a transport_session. @param username Jabber user name. @param password Jabber password. @param resource name of Jabber resource. @param connect_timeout Timeout interval, in seconds, for receiving data (see notes). @param auth_type An enum: either AUTH_PLAIN or AUTH_DIGEST (see notes). @return 1 if successful, or 0 upon error. If @a connect_timeout is -1, wait indefinitely for the Jabber server to respond. If @a connect_timeout is zero, don't wait at all. If @a timeout is positive, wait that number of seconds before timing out. If @a connect_timeout has a negative value other than -1, the results are not well defined. The value of @a connect_timeout applies to each of two stages in the logon procedure. Hence the logon may take up to twice the amount of time indicated. If we connect as a Jabber component, we send the password as an SHA1 hash. Otherwise we look at the @a auth_type. If it's AUTH_PLAIN, we send the password as plaintext; if it's AUTH_DIGEST, we send it as a hash. At this writing, we only use AUTH_DIGEST. */ int session_connect( transport_session* session, const char* username, const char* password, const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) { // Sanity checks if( ! session ) { osrfLogWarning(OSRF_LOG_MARK, "session is null in session_connect()" ); return 0; } if( session->sock_id != 0 ) { osrfLogWarning(OSRF_LOG_MARK, "transport session is already open, on socket %d", session->sock_id ); return 0; } // Open a client socket connecting to the Jabber server if(session->port > 0) { // use TCP session->sock_id = socket_open_tcp_client( session->sock_mgr, session->port, session->server ); if( session->sock_id <= 0 ) { session->sock_id = 0; return 0; } } else if(session->unix_path != NULL) { // use UNIX domain session->sock_id = socket_open_unix_client( session->sock_mgr, session->unix_path ); if( session->sock_id <= 0 ) { session->sock_id = 0; return 0; } } else { osrfLogWarning( OSRF_LOG_MARK, "Can't open session: no port or unix path" ); return 0; } const char* server = session->server; int size1 = 0; int size2 = 0; /* We establish the session in two stages. First we establish an XMPP stream with the Jabber server by sending an opening tag of stream:stream. This is not a complete XML document. We don't send the corresponding closing tag until we close the session. If the Jabber server responds by sending an opening stream:stream tag of its own, we can proceed to the second stage by sending a second stanza to log in. This stanza is an XML element with the tag <handshake> (if we're a Jabber component) or <iq> (if we're not), enclosing the username, password, and resource. If all goes well, the Jabber server responds with a <handshake> or <iq> stanza of its own, and we're logged in. If authentication fails, the Jabber server returns a <stream:error> (if we used a <handshake> or an <iq> of type "error" (if we used an <iq>). */ if( session->component ) { /* the first Jabber connect stanza */ char our_hostname[HOST_NAME_MAX + 1] = ""; gethostname(our_hostname, sizeof(our_hostname) ); our_hostname[HOST_NAME_MAX] = '\0'; size1 = 150 + strlen( username ) + strlen( our_hostname ); char stanza1[ size1 ]; snprintf( stanza1, sizeof(stanza1), "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' " "xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>", username, our_hostname ); /* send the first stanze */ session->state_machine->connecting = CONNECTING_1; if( socket_send( session->sock_id, stanza1 ) ) { osrfLogWarning(OSRF_LOG_MARK, "error sending"); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } /* wait for reply */ socket_wait(session->sock_mgr, connect_timeout, session->sock_id); /* server acknowledges our existence, now see if we can login */ if( session->state_machine->connecting == CONNECTING_2 ) { int ss = buffer_length( session->session_id ) + strlen( password ) + 5; char hashstuff[ss]; snprintf( hashstuff, sizeof(hashstuff), "%s%s", OSRF_BUFFER_C_STR( session->session_id ), password ); char* hash = shahash( hashstuff ); size2 = 100 + strlen( hash ); char stanza2[ size2 ]; snprintf( stanza2, sizeof(stanza2), "<handshake>%s</handshake>", hash ); if( socket_send( session->sock_id, stanza2 ) ) { osrfLogWarning(OSRF_LOG_MARK, "error sending"); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } } } else { /* we're not a component */ /* the first Jabber connect stanza */ size1 = 100 + strlen( server ); char stanza1[ size1 ]; snprintf( stanza1, sizeof(stanza1), "<stream:stream to='%s' xmlns='jabber:client' " "xmlns:stream='http://etherx.jabber.org/streams'>", server ); /* send the first stanze */ session->state_machine->connecting = CONNECTING_1; if( socket_send( session->sock_id, stanza1 ) ) { osrfLogWarning(OSRF_LOG_MARK, "error sending"); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } /* wait for reply */ socket_wait( session->sock_mgr, connect_timeout, session->sock_id ); /* make the timeout smarter XXX */ if( auth_type == AUTH_PLAIN ) { /* the second jabber connect stanza including login info*/ size2 = 150 + strlen( username ) + strlen( password ) + strlen( resource ); char stanza2[ size2 ]; snprintf( stanza2, sizeof(stanza2), "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>" "<username>%s</username><password>%s</password><resource>%s</resource></query></iq>", username, password, resource ); /* server acknowledges our existence, now see if we can login */ if( session->state_machine->connecting == CONNECTING_2 ) { if( socket_send( session->sock_id, stanza2 ) ) { osrfLogWarning(OSRF_LOG_MARK, "error sending"); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } } } else if( auth_type == AUTH_DIGEST ) { int ss = buffer_length( session->session_id ) + strlen( password ) + 5; char hashstuff[ss]; snprintf( hashstuff, sizeof(hashstuff), "%s%s", OSRF_BUFFER_C_STR( session->session_id ), password ); char* hash = shahash( hashstuff ); /* the second jabber connect stanza including login info */ size2 = 150 + strlen( username ) + strlen( hash ) + strlen(resource); char stanza2[ size2 ]; snprintf( stanza2, sizeof(stanza2), "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>" "<username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>", username, hash, resource ); /* server acknowledges our existence, now see if we can login */ if( session->state_machine->connecting == CONNECTING_2 ) { if( socket_send( session->sock_id, stanza2 ) ) { osrfLogWarning(OSRF_LOG_MARK, "error sending"); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } } } else { osrfLogWarning(OSRF_LOG_MARK, "Invalid auth_type parameter: %d", (int) auth_type ); socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } } // not component /* wait for reply to login request */ socket_wait( session->sock_mgr, connect_timeout, session->sock_id ); if( session->state_machine->connected ) { /* yar! */ return 1; } else { socket_disconnect( session->sock_mgr, session->sock_id ); session->sock_id = 0; return 0; } }
int sess_disconnect(struct irc_session *sess) { return socket_disconnect(sess->fd); }
static void server_alive_check (void) { gsocketp s; if (single_player_mode) { return; } sockets_quality_check(); TREE_WALK(sockets, s) { if (!socket_get_server_side_client(s)) { continue; } /* * Don't kill off new born connections. */ if (socket_get_tx(s) < 200) { LOG("Server: Sent %d packets to %s, quality %u", socket_get_tx(s), socket_get_remote_logname(s), socket_get_quality(s)); continue; } if (socket_get_quality(s) < SOCKET_PING_FAIL_THRESHOLD) { /* * Clients try forever. Server clients disconnect. */ aplayerp p = socket_get_player(s); if (s->server_side_client) { MSG_SERVER_SHOUT_AT_ALL_PLAYERS( POPUP, 0, 0, "Connection lost to %s", socket_get_remote_logname(s)); if (p) { LOG("Server: \"%s\" (ID %u) dropped out from %s", p->stats_from_client.pname, p->key, socket_get_remote_logname(s)); } else { LOG("Server: Dropped connection to %s", socket_get_remote_logname(s)); } LOG("Server: Sent %d packets, quality %u", socket_get_tx(s), (socket_get_quality(s) < SOCKET_PING_FAIL_THRESHOLD)); } else { MESG(POPUP, "Connection at %d%% quality", socket_get_quality(s)); continue; } server_rx_client_leave_implicit(s); socket_disconnect(s); } else { LOG("Server: Sent %d packets to %s, quality %u", socket_get_tx(s), socket_get_remote_logname(s), socket_get_quality(s)); } } }