void multiplex_add_d9c_socket (const char *socket, void (*attach) (struct d9r_io *, void *), void (*error) (struct d9r_io *, const char *, void *), void (*close) (struct d9r_io *, void *), void *aux) { struct io *in; struct io *out; net_open_socket (socket, &in, &out); if ((in == (struct io *)0) || (out == (struct io *)0)) { if (in != (struct io *)0) { io_close (in); } if (out != (struct io *)0) { io_close (in); } return; } multiplex_add_d9c_io (in, out, attach, error, close, aux); }
int net_host_broadcast_thread(void *arg) { char ip[32],err_str[256]; unsigned char *uc_ptr; unsigned long r_addr; d3socket sock; // use broadcast err_str to flag if errors occured broadcast_listen_err_str[0]=0x0; // create host socket broadcast_listen_socket=net_udp_open_socket(); if (broadcast_listen_socket==D3_NULL_SOCKET) { strcpy(broadcast_listen_err_str,"Networking: Unable to open socket"); broadcast_listen_complete=TRUE; return(0); } net_socket_blocking(broadcast_listen_socket,TRUE); // bind to the "any" IP to gather // any broadcast messages if (!net_udp_bind_broadcast(broadcast_listen_socket,net_port_host_broadcast,broadcast_listen_err_str)) { net_close_socket(&broadcast_listen_socket); broadcast_listen_complete=TRUE; return(0); } // listener is OK, free thread to run independantly broadcast_listen_complete=TRUE; // start listening while (TRUE) { r_addr=net_udp_receive_broadcast(broadcast_listen_socket); if (r_addr==-1) break; // connect and reply that we are a server uc_ptr=(unsigned char*)&r_addr; sock=net_open_socket(); if (sock==D3_NULL_SOCKET) continue; uc_ptr=(unsigned char*)&r_addr; sprintf(ip,"%d.%d.%d.%d",uc_ptr[0],uc_ptr[1],uc_ptr[2],uc_ptr[3]); if (net_connect_block(sock,ip,net_port_host_broadcast_reply,client_timeout_wait_seconds,err_str)) { net_host_client_handle_info(sock); } net_close_socket(&sock); } return(0); }
int main(int argc, char *argv[]) { int opt, ret = 0; int terminal_mode = 0; char *host = NULL; char *pass = ""; char *port = "25575"; if(argc < 2) usage(); opterr = 1; /* default error handler enabled */ while((opt = getopt(argc, argv, "tcshH:p:P:i")) != -1) { switch(opt) { case 'H': host = optarg; break; case 'P': port = optarg; break; case 'p': pass = optarg; break; case 'C': case 'c': print_colors = 0; break; case 'S': case 's': silent_mode = 1; break; case 'T': case 't': case 'I': case 'i': terminal_mode = 1; break; case 'h': case '?': /* if(optopt == 'P' || optopt == 'H' || optopt == 'p') fprintf (stderr, "Option -%c requires an argument.\n\n", optopt); */ /* else fprintf (stderr, "Unknown option -%c\n\n", optopt); */ usage(); break; default: abort(); } } if(host == NULL) { fputs("Host not defined. Check -H flag.\n\n", stdout); usage(); } if(optind == argc && terminal_mode == 0) { fputs("No commands specified.\n\n", stdout); usage(); } /* safety features to prevent "IO: Connection reset" bug on the server side */ atexit(&exit_proc); signal(SIGABRT, &sighandler); signal(SIGTERM, &sighandler); signal(SIGINT, &sighandler); #ifdef _WIN32 net_init_WSA(); console_handle = GetStdHandle(STD_OUTPUT_HANDLE); if(console_handle == INVALID_HANDLE_VALUE) console_handle = NULL; #endif /* open socket */ rsock = net_open_socket(host, port); /* auth & commands */ if(rcon_auth(rsock, pass)) { if(terminal_mode) ret = run_terminal_mode(rsock); else ret = run_commands(argc, argv); } else /* auth failed */ { ret = -1; fprintf(stdout, "Authentication failed!\n"); } /* cleanup */ net_close_socket(rsock); rsock = -1; return ret; }