int main(void){ int p1,p2,stat; OWN_PID=getpid(); printf("Master %d : Waiting...\n",getpid()); p1=fork(); if (p1==0) child(OWN_PID); p2=fork(); if (p2==0) child(p1); REFER_PID=p2; bor_signal(SIGUSR1,handler,0); bor_signal(SIGALRM,handler,0); sleep(1); srand ((unsigned int) time(NULL)+OWN_PID); kill(REFER_PID,SIGUSR1); while (1){ sleep(1); printf("[%d] Alive.\n",OWN_PID); } wait(&stat); return 0; }
/* fils */ int child(int p){ bor_signal(SIGUSR1,handler,0); bor_signal(SIGALRM,handler,0); REFER_PID=p; OWN_PID=getpid(); srand (OWN_PID); printf("Child %d: Waiting... (Referer: %d)\n",getpid(),REFER_PID); while (1){ sleep(1); printf("[%d] Alive.\n",OWN_PID); } exit(0); }
/** A basic client, note that he will create the sockets, not the server * @param path Path to the server socket */ void client_socket(char *path){ lsocket*main_socket=make_socket(path); /* Connect to the given socket address */ open_socket(main_socket,S_IWUSR); /* Creation of the two sockets */ sockets=open_communication(); /* Send the socket to listen to */ if (handshake(main_socket,sockets)<0) OUT("The server didn't accepted connection"); /* Acknoledge user requests */ bor_signal(SIGINT,gotcha,0); while (user_request(sockets)); /* Properly close connection */ self_terminate(); }
int main(int nbargs,char**kwargs){ int nb_pipes, *pipes,i,tmp_fd[2]; char size_buffer,*buffer; int p; /* Process */ int nb_ready; /* Pipes ready */ int max_fd[2]={0,0}; fd_set liste; if (nbargs<3) Exit("Incorrect number of arguments\nUsage: debitub <nb_pipes> <buffersize>"); size_buffer=atoi(kwargs[2]); nb_pipes=atoi(kwargs[1]); if (!size_buffer|!nb_pipes) Exit("Arguments can't be void") pipes=malloc(sizeof(int)*nb_pipes*2); if (!pipes) Error("(Malloc) Pipes"); for (i = 0; i < nb_pipes; i += 1){ /* tmp_fd=malloc(sizeof(int)*2);*/ /* if (!tmp_fd) Error("(Malloc) Pipes.");*/ if (pipe(tmp_fd)) Error("(Pipe) Generation error"); pipes[2*i]=tmp_fd[0]; pipes[2*i+1]=tmp_fd[1]; max_fd[0]=max_fd[0]>tmp_fd[0]?max_fd[0]:tmp_fd[0]; max_fd[1]=max_fd[1]>tmp_fd[1]?max_fd[1]:tmp_fd[1]; /* free(tmp_fd);*/ } if ((p=fork())<0) Error("(Fork) Generation Error"); if (p){ /* Father Process */ for(i=0;i<nb_pipes;i++) close(pipes[i*2+0]); buffer=malloc(sizeof(char)*size_buffer); if (!buffer) Error("(Malloc) Buffer"); for(i=0;i<nb_pipes;i++) buffer[i]='*'; buffer[size_buffer-1]='\0'; while (1){ FD_ZERO(&liste); for(i=0;i<nb_pipes;i++) FD_SET(pipes[i*2+1],&liste); nb_ready=select(max_fd[1]+1,NULL,&liste,NULL,NULL); if (nb_ready>0){ for(i=0;i<nb_pipes;i++) if (FD_ISSET(pipes[i*2+1],&liste)) write(pipes[i*2+1],&buffer,size_buffer); } } return 0; } else { /* Child Process */ for(i=0;i<nb_pipes;i++) close(pipes[i*2+1]); alarm(1); bor_signal(SIGALRM,&sig,0); while (1){ FD_ZERO(&liste); for(i=0;i<nb_pipes;i++) FD_SET(pipes[i*2+0],&liste); nb_ready=select(max_fd[0]+1,&liste,NULL,NULL,NULL); if (nb_ready>0){ for(i=0;i<nb_pipes;i++) if (FD_ISSET(pipes[i*2+0],&liste)){ buffer=malloc(sizeof(char)*size_buffer); if (!buffer) Error("(Read) Buffer creation"); read(pipes[i*2+0],&buffer,size_buffer); readed+=(long unsigned int)size_buffer; } } } return 0; } return 0; }
void server_relay(int port, int listen, int ssl) { int soc_ec_cli = -1, soc_ec = -1, maxfd, res, nc; fd_set set_read; fd_set set_write; struct sockaddr_in addrS; #ifdef _WIN32 WSADATA wsaData; int wsaInit = WSAStartup(MAKEWORD(2,2), &wsaData); if (wsaInit != 0) { ERROR(L_NOTICE, "WSAStartup failed: %d\n", wsaInit); exit(1); } #endif /* Init client tab */ for (nc = 0; nc < MAXCLI; nc++) init_socket(&socks_pool[nc]); for (nc = 0; nc < MAXCLI; nc++) init_client (&tc[nc], nc, 0, NULL); TRACE(L_NOTICE, "server: set listening client socks relay ..."); soc_ec = new_listen_socket (NULL, port, MAXCLI, &addrS); if (soc_ec < 0) goto fin_serveur; TRACE(L_NOTICE, "server: set server relay ..."); soc_ec_cli = new_listen_socket (NULL, listen, MAXCLI, &addrS); if (soc_ec_cli < 0) goto fin_serveur; #ifndef _WIN32 if ( globalArgs.background == 1 ) { TRACE(L_NOTICE, "server: background ..."); if ( daemon(0, 0) != 0 ) { perror("daemon"); exit(1); } } bor_signal (SIGINT, capte_fin, SA_RESTART); /* TODO: Find a better way to exit the select and recall the init_select * SIGUSR1 is send by a thread to unblock the select */ bor_signal (SIGUSR1, capte_usr1, SA_RESTART); #endif while (boucle_princ) { init_select_reverse(soc_ec, soc_ec_cli, tc, &maxfd, &set_read, &set_write); res = select (maxfd+1, &set_read, &set_write, NULL,NULL); if (res > 0) { /* Search eligible sockets */ if (FD_ISSET (soc_ec, &set_read)) new_connection_socket (soc_ec, socks_pool, ssl); if (FD_ISSET (soc_ec_cli, &set_read)) new_connection_reverse (soc_ec_cli, tc, socks_pool); for (nc = 0; nc < MAXCLI; nc++) { dispatch_server(&tc[nc], &set_read, &set_write); } } else if ( res == 0) { /* If timeout was set in select and expired */ } else if (res < 0) { if (errno == EINTR) ; /* Received signal, it does nothing */ else { perror ("select"); goto fin_serveur; } } } fin_serveur: #ifdef HAVE_LIBSSL if (ssl == 1) ssl_cleaning(); #endif printf ("Server: closing sockets ...\n"); if (soc_ec != -1) CLOSE_SOCKET(soc_ec); for (nc = 0; nc < MAXCLI; nc++) close_socket(&socks_pool[nc]); for (nc = 0; nc < MAXCLI; nc++) disconnection(&tc[nc]); #ifdef _WIN32 WSACleanup(); #endif }
void server(const char *bindAddr, int port, int ssl) { int soc_ec = -1, maxfd, res, nc; fd_set set_read; fd_set set_write; struct sockaddr_in addrS; char methods[2]; #ifdef _WIN32 WSADATA wsaData; int wsaInit = WSAStartup(MAKEWORD(2,2), &wsaData); if (wsaInit != 0) { ERROR(L_NOTICE, "WSAStartup failed: %d\n", wsaInit); exit(1); } #endif s_socks_conf conf; s_socks_server_config config; conf.config.srv = &config; char versions[] = { SOCKS5_V, SOCKS4_V }; config.allowed_version = versions; config.n_allowed_version = sizeof(versions); if ( globalArgsServer.fileauth[0] != 0 ) { methods[0] = 0x02; --config.n_allowed_version; /* Disable socks4 don't support auth */ } else { methods[0] = 0x00; } config.allowed_method = methods; config.n_allowed_method = 1; config.check_auth = check_auth; /* Init client tab */ for (nc = 0; nc < MAXCLI; nc++) init_client (&tc[nc], nc, M_SERVER, &conf); if(bindAddr[0] == 0) soc_ec = new_listen_socket (NULL, port, 0, &addrS); else soc_ec = new_listen_socket (bindAddr, port, 0, &addrS); if (soc_ec < 0) goto fin_serveur; #ifndef _WIN32 if ( globalArgsServer.daemon == 1 ) { TRACE(L_NOTICE, "server: mode daemon ..."); if ( daemon(0, 0) != 0 ) { perror("daemon"); exit(1); } writePID(PID_FILE); } bor_signal (SIGINT, capte_fin, SA_RESTART); /* Need in daemon to remove the PID file properly */ bor_signal (SIGTERM, capte_fin, SA_RESTART); bor_signal (SIGPIPE, capte_sigpipe, SA_RESTART); /* TODO: Find a better way to exit the select and recall the init_select * SIGUSR1 is send by a thread to unblock the select */ bor_signal (SIGUSR1, capte_usr1, SA_RESTART); #endif while (boucle_princ) { init_select_server (soc_ec, tc, &maxfd, &set_read, &set_write); res = select (maxfd+1, &set_read, &set_write, NULL, NULL); if (res > 0) { /* Search eligible sockets */ if (FD_ISSET (soc_ec, &set_read)) new_connection (soc_ec, tc, ssl); for (nc = 0; nc < MAXCLI; nc++) { dispatch_server(&tc[nc], &set_read, &set_write); } } else if ( res == 0) { } else if (res < 0) { if (errno == EINTR) ; /* Received signal, it does nothing */ else { perror ("select"); goto fin_serveur; } } } fin_serveur: #ifdef HAVE_LIBSSL if (globalArgsServer.ssl == 1) ssl_cleaning(); #endif TRACE(L_NOTICE, "server: closing sockets ..."); close_log(); for (nc = 0; nc < MAXCLI; nc++) disconnection(&tc[nc]); if (soc_ec != -1) CLOSE_SOCKET(soc_ec); if ( globalArgsServer.daemon == 1 ) removePID(PID_FILE); #ifdef _WIN32 WSACleanup(); #endif }
void execProcess(){ bor_signal(SIGUSR1, handler, 0); bor_signal(SIGALRM, onTimeout, 0); alarm(5); }