int _osd_sem_init(osd_sem_t *sem, int pshared, int value SEM_FILELINEARGS) { sem_t *s = &sem->posix; int rc; FUN("osd_sem_init"); if (NULL == sem) { LOG(L_ERROR,("sem is NULL%s\n", at_file_line(SEM_FILELINEVARS))); errno = EINVAL; return -1; } /* shutting down? */ if (SHUTDOWN()) { return 0; } if (MY_SEM_MAGIC == sem->magic) { LOG(L_ERROR,("sem %p already initialized%s\n", sem, at_file_line(SEM_FILELINEVARS))); sem->pid = 0; /* Destroy and initialize semaphore again */ sem_destroy(s); sem_init(s, 0, value); return 0; } sem->pid = 0; rc = sem_init(s, pshared, value); if (0 == rc) sem->magic = MY_SEM_MAGIC; return rc; }
int do_server(int port, int *ret, int (*cb)(), char *context) { int sock; char *name; int accept_socket; int i; if (!init_server(&accept_socket,port)) return(0); if (ret != NULL) { *ret=accept_socket; /* return(1);*/ } for (;;) { if (do_accept(accept_socket,&sock,&name) == 0) { SHUTDOWN(accept_socket); return(0); } i=(*cb)(name,sock, context); if (name != NULL) OPENSSL_free(name); SHUTDOWN2(sock); if (i < 0) { SHUTDOWN2(accept_socket); return(i); } } }
int do_server_unix(const char *path, int *ret, int (*cb) (char *hostname, int s, int stype, unsigned char *context), unsigned char *context, int naccept) { int sock; int accept_socket = 0; int i; if (!init_server_unix(&accept_socket, path)) return (0); if (ret != NULL) *ret = accept_socket; for (;;) { if (do_accept_unix(accept_socket, &sock) == 0) { SHUTDOWN(accept_socket); i = 0; goto out; } i = (*cb) (NULL, sock, 0, context); SHUTDOWN2(sock); if (naccept != -1) naccept--; if (i < 0 || naccept == 0) { SHUTDOWN2(accept_socket); goto out; } } out: unlink(path); return (i); }
void first_task_code (){ print1(); SHUTDOWN(); Exit(); kprintf ("Unexpected return from Exit() at first_task_code\n\r"); }
void read_code(void) { int type, tr_num, sp, i, src, dest, val; int* ptr = 0; while(1) { type = get_int(); if (type == END_OF_FILE_TYPE) break; else if (type == DIST_TYPE){ src = get_int(); dest = get_int(); val = get_int(); ptr = ((int*)DIST_TABLE_BASE) + src*USED_INDICIES + dest; *ptr = val; } else { tr_num = get_int(); sp = get_int(); i = get_int(); if(type == STOP_DIST_TYPE) { val = get_int(); ptr = ((int*)STOP_DIST_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2 + sp*2 + i; *ptr = val; } else if(type == REL_VEL_TYPE) { val = get_int(); ptr = ((int*)REL_VEL_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2 + sp*2 + i; *ptr = val; } else if(type == AVG_VEL_TYPE) { val = get_int(); ptr = ((int*)AVG_VEL_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2 + sp*2 + i; *ptr = val; } else if (type == ACC_TIME_TYPE){ int sp2 = get_int(); val = get_int(); ptr = ((int*)ACC_TIME_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2*USED_TR_SPEEDS + sp*2*USED_TR_SPEEDS + i*USED_TR_SPEEDS + sp2; *ptr = val; } else { src = get_int(); dest = get_int(); val = get_int(); if (type == VEL_COUNT_TYPE) { ptr = ((int*)VEL_COUNT_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2*USED_INDICIES*USED_INDICIES + sp*2*USED_INDICIES*USED_INDICIES + i*USED_INDICIES*USED_INDICIES + src*USED_INDICIES + dest; } else { ptr = ((int*)VEL_TICKS_TABLE_BASE) + tr_num*USED_TR_SPEEDS*2*USED_INDICIES*USED_INDICIES + sp*2*USED_INDICIES*USED_INDICIES + i*USED_INDICIES*USED_INDICIES + src*USED_INDICIES + dest; } *ptr = val; } } } SHUTDOWN(); }
static int init_server_long(int *sock, int port, char *ip, int type) { int ret=0; struct sockaddr_in server; int s= -1,i; if (!ssl_sock_init()) return(0); memset((char *)&server,0,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons((unsigned short)port); if (ip == NULL) server.sin_addr.s_addr=INADDR_ANY; else /* Added for T3E, address-of fails on bit field ([email protected]) */ #ifndef BIT_FIELD_LIMITS memcpy(&server.sin_addr.s_addr,ip,4); #else memcpy(&server.sin_addr,ip,4); #endif if (type == SOCK_STREAM) s=socket(AF_INET,SOCK_STREAM,SOCKET_PROTOCOL); else /* type == SOCK_DGRAM */ s=socket(AF_INET, SOCK_DGRAM,IPPROTO_UDP); if (s == INVALID_SOCKET) goto err; #if defined SOL_SOCKET && defined SO_REUSEADDR { int j = 1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &j, sizeof j); } #endif if (bind(s,(struct sockaddr *)&server,sizeof(server)) == -1) { #ifndef OPENSSL_SYS_WINDOWS perror("bind"); #endif goto err; } /* Make it 128 for linux */ if (type==SOCK_STREAM && listen(s,128) == -1) goto err; i=0; *sock=s; ret=1; err: if ((ret == 0) && (s != -1)) { SHUTDOWN(s); } return(ret); }
/*------------------------------------------------------------------------------ * nft_pool_thread - Thread start function to serve the pool's work queue. * * nft_pool_add generates a fresh protected reference to the pool, * and passes that reference to us. * pool->num_threads, but it has not incremented pool->idle_threads. *------------------------------------------------------------------------------ */ static void * nft_pool_thread(void * arg) { nft_pool * pool = nft_pool_cast(arg); int rc = pthread_mutex_lock(&pool->queue.mutex); assert(rc == 0); // Increment pool->idle_threads before we block in nft_queue_dequeue. // nft_pool_add has already incremented num_threads, after spawning this thread. pool->idle_threads++; // nft_queue_dequeue will not block when the queue is shutting down. // It will continue to dequeue items, and return ESHUTDOWN when the queue is empty. work_item * item; while ((0 == nft_queue_dequeue(&pool->queue, 1, (void**) &item))) { pool->idle_threads--; // We must release the mutex while the work function executes. rc = pthread_mutex_unlock(&pool->queue.mutex); assert(0 == rc); /* The nft_pool_threads are private threads, so they cannot be cancelled, * but the work function could call pthread_exit(), so we need a cleanup * function to decrement pool->num_threads and discard the reference. */ pthread_cleanup_push(pool_thread_cleanup, pool); void (* function)(void *) = item->function; void * argument = item->argument; // Free the item first, in case function calls pthread_exit(). free(item); function(argument); pthread_cleanup_pop(0); // do not execute pool_thread_cleanup rc = pthread_mutex_lock(&pool->queue.mutex); assert(0 == rc); pool->idle_threads++; } pool->idle_threads--; pool->num_threads--; // If the pool is shutting down and we are the last pool thread // to finish, signal the thread that is waiting in nft_pool_shutdown. if (pool->num_threads == 0 && SHUTDOWN(pool)) pthread_cond_signal(&pool->queue.cond); rc = pthread_mutex_unlock(&pool->queue.mutex); assert(0 == rc); nft_pool_discard(pool); return NULL; }
void PrintUsage(int invalid) { if(invalid) puts("Stopping. Invalid arguments to program.\r\n"); puts( "Usage:\r\n" "* pN : no. of processors (threads) to use\r\n" "* bN: width of board > 1, so board size is N x N cells\r\n" "* tN: width of one overlay tile, each N x N cells\r\n" "* cN: termination condition in integer percent, 1-100\r\n" "* mN: max. no. of full steps (additional terdmination condition)\r\n" "* sN: optional random seed\r\n" "* i: optional interactive mode switch\r\n" ); SHUTDOWN(-1); }
/*------------------------------------------------------------------------------ * pool_thread_cleanup - Function for use with pthread_cleanup_push/pop. * * This cleanup handler is used by pool threads, which cannot be cancelled, * but which may call pthread_exit from the work function. *------------------------------------------------------------------------------ */ static void pool_thread_cleanup(void * arg) { nft_pool * pool = nft_pool_cast(arg); assert(pool); int rc = pthread_mutex_lock(&pool->queue.mutex); assert(rc == 0); // If the pool is shutting down and we are the last pool thread // to finish, signal the thread that is waiting in nft_pool_shutdown. if (--pool->num_threads == 0 && SHUTDOWN(pool)) pthread_cond_signal(&pool->queue.cond); rc = pthread_mutex_unlock(&pool->queue.mutex); assert(0 == rc); // The nft_pool_thread holds a pool reference, which we must discard. nft_pool_discard(pool); }
int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, int stype, unsigned char *context), unsigned char *context, int naccept) { int sock; char *name = NULL; int accept_socket = 0; int i; if (!init_server(&accept_socket,port,type)) return(0); if (ret != NULL) { *ret=accept_socket; /* return(1);*/ } for (;;) { if (type==SOCK_STREAM) { #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL if (do_accept(accept_socket,&sock,NULL) == 0) #else if (do_accept(accept_socket,&sock,&name) == 0) #endif { SHUTDOWN(accept_socket); return(0); } } else sock = accept_socket; i=(*cb)(name,sock, type, context); if (name != NULL) OPENSSL_free(name); if (type==SOCK_STREAM) SHUTDOWN2(sock); if (naccept != -1) naccept--; if (i < 0 || naccept == 0) { SHUTDOWN2(accept_socket); return(i); } } }
static int init_server_unix(int *sock, const char *path) { int ret = 0; struct sockaddr_un server; int s = -1; if (strlen(path) > (UNIX_PATH_MAX + 1)) return (0); if (!ssl_sock_init()) return (0); s = socket(AF_UNIX, SOCK_STREAM, 0); if (s == INVALID_SOCKET) goto err; memset(&server, 0, sizeof(server)); server.sun_family = AF_UNIX; strcpy(server.sun_path, path); if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) { # ifndef OPENSSL_SYS_WINDOWS perror("bind"); # endif goto err; } /* Make it 128 for linux */ if (listen(s, 128) == -1) { # ifndef OPENSSL_SYS_WINDOWS perror("listen"); # endif unlink(path); goto err; } *sock = s; ret = 1; err: if ((ret == 0) && (s != -1)) { SHUTDOWN(s); } return (ret); }
int do_server(int port, int type, int *ret, int (*cb)(char *hostname, int s, unsigned char *context), unsigned char *context) { int sock; char *name = NULL; int accept_socket = 0; int i; if (!init_server(&accept_socket,port,type)) return(0); if (ret != NULL) { *ret=accept_socket; /* return(1);*/ } for (;;) { if (type==SOCK_STREAM) { if (do_accept(accept_socket,&sock,&name) == 0) { SHUTDOWN(accept_socket); return(0); } } else sock = accept_socket; i=(*cb)(name,sock, context); if (name != NULL) OPENSSL_free(name); if (type==SOCK_STREAM) SHUTDOWN2(sock); if (i < 0) { SHUTDOWN2(accept_socket); return(i); } } }
int main(int argc, char** argv) { FILE* f; int i, j; size_t size; int seedNumber; int REPORT = 0; int bytesToAllocateForArgsText = 0; puts("\r\n"); puts(argv[0]); puts("\r\n"); if(argc == 1) { PrintUsage(0); } // gather N, T, W, maxFullsteps, winPercent, seedNumber, INTERACTIVE from commandline args; N = T = W = winPercent = INTERACTIVE = 0; maxFullsteps = -1; seedNumber = time(NULL); for(i = 1; i < argc; i++) { bytesToAllocateForArgsText += strlen(argv[i]) + 1; switch(argv[i][0]) { case 'p': if(strlen(argv[i]) >= 2) { W = atoi(&(argv[i][1])); } else { printf("Invalid process count.\r\n"); PrintUsage(1); } break; case 'b': if(strlen(argv[i]) >= 2) { N = atoi(&(argv[i][1])); } else { printf("Invalid size parameter for board.\r\n"); PrintUsage(1); } break; case 't': if(strlen(argv[i]) >= 2) { T = atoi(&(argv[i][1])); } else { printf("Invalid size parameter for overlay.\r\n"); PrintUsage(1); } break; case 'c': if(strlen(argv[i]) >= 2) { winPercent = atoi(&(argv[i][1])); } else { printf("Invalid win percentage.\r\n"); PrintUsage(1); } break; case 'm': if(strlen(argv[i]) >= 2) { j = sscanf(&(argv[i][1]), "%d", &maxFullsteps); if(!j) { printf("Invalid max steps.\r\n"); PrintUsage(1); } } else { printf("Invalid max steps.\r\n"); PrintUsage(1); } break; case 's': if(strlen(argv[i]) >= 2) { j = sscanf(&(argv[i][1]), "%d", &seedNumber); if(!j) { printf("Invalid seed.\r\n"); PrintUsage(1); } } else { PrintUsage(1); } break; #ifndef DISABLE_INTERACTIVE case 'i': if(strlen(argv[i]) != 1) { printf("Invalid parameter %s.\r\n", argv[i]); PrintUsage(1); } INTERACTIVE = 1; break; #endif case 'R': REPORT = 1; break; default: printf("Invalid parameter %s.\r\n", argv[i]); PrintUsage(1); break; } } argsText = malloc(bytesToAllocateForArgsText + 1); argsText[0] = 0; for(i = 1; i < argc; i++) { strcat(argsText, argv[i]); strcat(argsText, " "); } printf("Argument text: '%s' \r\n", argsText); srand(seedNumber); if(W <= 0) { printf("Invalid or nonexistant process count, must be greater than zero.\r\n"); PrintUsage(1); } if(N < 2) { printf("Invalid or nonexistant size parameter for board, must be greater than or equal or 2.\r\n"); PrintUsage(1); } if(T <= 0 || N % T != 0) { printf("Invalid or nonexistant size parameter for overlay.\r\n"); PrintUsage(1); } if(winPercent <= 0) { printf("Invalid or nonexistant termination condition percent, must be (1-100).\r\n"); PrintUsage(1); } if(maxFullsteps < 0) { printf("Invalid or nonexistant number of max full steps, must be greater than or equal to zero.\r\n"); PrintUsage(1); } board = malloc(N * N); if(!board) { perror("Not enough memory.\r\n"); SHUTDOWN(-1); } for(i = 0; i < N * N; i++) { board[i] = rand() % 3; } // Set up the miscellaneous. maxHalfsteps = maxFullsteps * 2; percentHalfsteps = -1; maxBluePercent = maxRedPercent = 0; omp_set_num_threads(W); StartTime(); #pragma omp parallel { if(omp_get_thread_num() == 0) { W = omp_get_num_threads(); printf("W = %d\r\n", W); // If N is greater than or equal to the number of workers, there is work for every thread. if(N >= W) { // If workers divide N, then every worker does the same amount of work. if(N % W == 0) { rcLeftOver = rcSplit = N / W; } // If workers do not divide N, // Then the truncation of N / W is handed to W - 1 of W workers, // And the final worker will take all the remaining work. else { rcSplit = N / W; rcLeftOver = N - (rcSplit * (W - 1)); } } else { // Each worker up to N takes 1 row. // Leftover is set here, but won't actually used. rcSplit = 1; rcLeftOver = 1; // Set the number of workers equal to the size of the board, // so that we don't spawn threads that will always be doing nothing. W = N; } // Calculate how many times T divides N in advance. // This will be used a lot in the board evaluation phase. subregionCount = N / T; // Size of each subregion in cells. subregionCells = T * T; } #pragma omp barrier printf("%d got here\r\n", omp_get_thread_num()); ThreadExecute(omp_get_thread_num()); } if(INTERACTIVE) { if(!interactiveExit) { PrintBoard(percentHalfsteps); } } else { f = fopen("redblue.txt", "w"); PrintBoardToFile(percentHalfsteps, f); fclose(f); } free(argsText); free(board); SHUTDOWN(0); return 0; }
void prompt_code(){ Putc(COM1, 96); // GO command for trains Create(8, timer_code); int dfa_tid = Create(10, dfa_code); int train_tid = Create(10, train_code); int switch_tid = Create(10, switch_code); Create(10, sensor_code); char input, state; char buff[3]; while (1){ input = Getc(COM2); if (input == 'q'){ Putc(COM1, 97); // Stop command for trains Putc(COM2, input); // ECHO // Printf(COM2, "\033[2J\033[1;1H"); // clear the screen Printf(COM2, " ");Printf(COM2, " ");Printf(COM2, " "); SHUTDOWN(); // CALL SHUTDOWN SYSCALL to shutdown the program } Send(dfa_tid, &input, sizeof(char), &buff, 3*sizeof(char)); state = buff[2]; switch (state){ case DFA_FULL: Printf(COM2, "\033[s\033[%d;%dHPrompt is full! \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_INVALID: Printf(COM2, "\033[%d;%dHInvalid input!!! \033[%d;%dH", CMD_OUT_ROW, CMD_OUT_COL, CMD_ROW, CMD_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); break; case DFA_OK: Putc(COM2, input); // ECHO //Printf(COM2, "\033[s\033[%d;%dHTyping... \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_BACKSPACE: Printf(COM2, "%c %c", input, input); // Echo //Printf(COM2, "\033[s\033[%d;%dHRemoving... \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_NO_BACKSPACE: Printf(COM2, "\033[s\033[%d;%dH \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_TR_SP: Printf(COM2, "\033[s\033[%d;%dHSetting speed of tr#%d to %d... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[1], buff[0]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); // Send command to the train task to set speed of the train Send(train_tid, &buff, 2*sizeof(char), &input, sizeof(char)); break; case DFA_TR_RV: Printf(COM2, "\033[s\033[%d;%dHReversing tr#%d... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[1], buff[0]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[0] = 15; Send(train_tid, &buff, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW: Printf(COM2, "\033[s\033[%d;%dHThrowing sw#%d to %c... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[1], buff[0]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); Send(switch_tid, &buff, 2*sizeof(char), &input, sizeof(char)); break; default: Printf(COM2, "\033[s\033[%d;%dHDFA PROBLEM!!! \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); break; } } Putc(COM1, 97); // Stop command for trains Exit(); kprintf("Unexpected return from Exit() at prompt_code\n\r"); }
void prompt_code(){ Putc(COM1, 96); // GO command for trains Create(PR_TIMER, timer_code); int dfa_tid = Create(PR_DFA, dfa_code); int train_tid = Create(PR_TRAIN_COMMANDER, train_commander_code); int switch_tid = Create(PR_SWITCH, switch_code); Create(PR_SENSOR, sensor_code); char input, state; char buff[9]; train_server_request tsr; int node_index = 0; while (1){ input = Getc(COM2); if (input == 'q'){ Putc(COM1, 97); // Stop command for trains Putc(COM2, input); // ECHO Printf(COM2, " ");Printf(COM2, " ");Printf(COM2, " "); // send signal to calibration server to print calibration data and halt the system. // Send(WhoIs("CALIBRATIONSERVER"), &input, sizeof(char), &input, sizeof(char)); SHUTDOWN(); // CALL SHUTDOWN SYSCALL to shutdown the program } else if (input == '/'){ Debug("[Time: %d ]", Time()); continue; } Send(dfa_tid, &input, sizeof(char), &buff, 9*sizeof(char)); state = buff[0]; switch (state){ case DFA_FULL: Printf(COM2, "\033[s\033[%d;%dHPrompt is full! \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_INVALID: Printf(COM2, "\033[%d;%dHInvalid input!!! \033[%d;%dH", CMD_OUT_ROW, CMD_OUT_COL, CMD_ROW, CMD_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); break; case DFA_OK: Putc(COM2, input); // ECHO break; case DFA_BACKSPACE: Printf(COM2, "%c %c", input, input); // Echo break; case DFA_NO_BACKSPACE: Printf(COM2, "\033[s\033[%d;%dH \033[u", CMD_OUT_ROW, CMD_OUT_COL); break; case DFA_TR_SP: Printf(COM2, "\033[s\033[%d;%dHSetting speed of tr#%d to %d... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2], buff[1]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); // Send command to the train task to set speed of the train Send(train_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_TR_SP_AT_NODE: Printf(COM2, "\033[s\033[%d;%dHSetting speed of tr#%d to %d at node %s...\033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2], buff[1], (buff+3)); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); tsr.type = TRAIN_TYPE_SPEED_AT_NODE; tsr.data.speed_request.speed = buff[1]; tsr.data.speed_request.tr_num = buff[2]; strncpy(tsr.data.speed_request.node_name, buff+3, 6); Send(WhoIs("TRAINSERVER"), &tsr, sizeof(train_server_request), &input, sizeof(char)); break; case DFA_TR_RV: Printf(COM2, "\033[s\033[%d;%dHReversing tr#%d... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[1] = 15; Send(train_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW: Printf(COM2, "\033[s\033[%d;%dHThrowing sw#%d to %c... \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2], buff[1]); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_STOP_AT: Printf(COM2, "\033[s\033[%d;%dHStopping train %d at %s \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2], (buff+3)); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); // Send appropriate command to train server to stop the train at the appropriate location tsr.type = TRAIN_TYPE_SPEED_AT_NODE; tsr.data.speed_request.speed = buff[1]; tsr.data.speed_request.tr_num = buff[2]; strncpy(tsr.data.speed_request.node_name, buff+3, 6); Send(WhoIs("TRAINSERVER"), &tsr, sizeof(train_server_request), &input, sizeof(char)); break; case DFA_GO_TO: tsr.type = TRAIN_TYPE_GO_TO_NODE; tsr.data.speed_request.tr_num = buff[2]; strncpy(tsr.data.speed_request.node_name, buff+3, 6); Send(WhoIs("TRAINSERVER"), &tsr, sizeof(train_server_request), &node_index, sizeof(int)); if(node_index != -1) { Printf(COM2, "\033[s\033[%d;%dHMoving train %d to %s \033[u", CMD_OUT_ROW, CMD_OUT_COL, buff[2], (buff+3)); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); } else { Printf(COM2, "\033[s\033[%d;%dHInvalid node name \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); } break; case DFA_SW_ALL_C: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to C (curve)... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_ALL_C; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_ALL_S: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to S (straight)... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_ALL_S; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_SMALL_LOOP1: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to small loop 1... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_SMALL_LOOP1; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_SMALL_LOOP2: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to small loop 2... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_SMALL_LOOP2; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_MEDIUM_LOOP: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to medium loop... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_MEDIUM_LOOP; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_LARGE_LOOP: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to large loop... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_LARGE_LOOP; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_EXTRA_LARGE_LOOP: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to extra large loop \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_EXTRA_LARGE_LOOP; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char)); break; case DFA_SW_DEFAULT: Printf(COM2, "\033[s\033[%d;%dHInitializing switches to default state... \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); buff[2] = SWICTH_DEFAULT; Send(switch_tid, buff+1, 2*sizeof(char), &input, sizeof(char));break; break; default: Printf(COM2, "\033[s\033[%d;%dHDFA PROBLEM!!! \033[u", CMD_OUT_ROW, CMD_OUT_COL); Printf(COM2, "\033[%d;%dH\033[s \033[u", CMD_ROW, CMD_COL); break; } } Warning(0,"Unexpected return from Exit() at prompt_code\n\r"); Exit(); Warning(0,"Unexpected return from Exit() at prompt_code\n\r"); }
int MAIN(int argc, char **argv) { int off=0; SSL *con=NULL,*con2=NULL; X509_STORE *store = NULL; int s,k,width,state=0; char *cbuf=NULL,*sbuf=NULL,*mbuf=NULL; int cbuf_len,cbuf_off; int sbuf_len,sbuf_off; fd_set readfds,writefds; short port=PORT; int full_log=1; char *host=SSL_HOST_NAME; char *cert_file=NULL,*key_file=NULL; int cert_format = FORMAT_PEM, key_format = FORMAT_PEM; char *passarg = NULL, *pass = NULL; X509 *cert = NULL; EVP_PKEY *key = NULL; char *CApath=NULL,*CAfile=NULL,*cipher=NULL; int reconnect=0,badop=0,verify=SSL_VERIFY_NONE,bugs=0; int crlf=0; int write_tty,read_tty,write_ssl,read_ssl,tty_on,ssl_pending; SSL_CTX *ctx=NULL; int ret=1,in_init=1,i,nbio_test=0; int starttls_proto = PROTO_OFF; int prexit = 0, vflags = 0; SSL_METHOD *meth=NULL; #ifdef sock_type #undef sock_type #endif int sock_type=SOCK_STREAM; BIO *sbio; char *inrand=NULL; int mbuf_len=0; #ifndef OPENSSL_NO_ENGINE char *engine_id=NULL; ENGINE *e=NULL; #endif #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) struct timeval tv; #endif struct sockaddr peer; int peerlen = sizeof(peer); int enable_timeouts = 0 ; long mtu = 0; #if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3) meth=SSLv23_client_method(); #elif !defined(OPENSSL_NO_SSL3) meth=SSLv3_client_method(); #elif !defined(OPENSSL_NO_SSL2) meth=SSLv2_client_method(); #endif apps_startup(); c_Pause=0; c_quiet=0; c_ign_eof=0; c_debug=0; c_msg=0; c_showcerts=0; if (bio_err == NULL) bio_err=BIO_new_fp(stderr,BIO_NOCLOSE); if (!load_config(bio_err, NULL)) goto end; if ( ((cbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) || ((sbuf=OPENSSL_malloc(BUFSIZZ)) == NULL) || ((mbuf=OPENSSL_malloc(BUFSIZZ)) == NULL)) { BIO_printf(bio_err,"out of memory\n"); goto end; } verify_depth=0; verify_error=X509_V_OK; #ifdef FIONBIO c_nbio=0; #endif argc--; argv++; while (argc >= 1) { if (strcmp(*argv,"-host") == 0) { if (--argc < 1) goto bad; host= *(++argv); } else if (strcmp(*argv,"-port") == 0) { if (--argc < 1) goto bad; port=atoi(*(++argv)); if (port == 0) goto bad; } else if (strcmp(*argv,"-connect") == 0) { if (--argc < 1) goto bad; if (!extract_host_port(*(++argv),&host,NULL,&port)) goto bad; } else if (strcmp(*argv,"-verify") == 0) { verify=SSL_VERIFY_PEER; if (--argc < 1) goto bad; verify_depth=atoi(*(++argv)); BIO_printf(bio_err,"verify depth is %d\n",verify_depth); } else if (strcmp(*argv,"-cert") == 0) { if (--argc < 1) goto bad; cert_file= *(++argv); } else if (strcmp(*argv,"-certform") == 0) { if (--argc < 1) goto bad; cert_format = str2fmt(*(++argv)); } else if (strcmp(*argv,"-crl_check") == 0) vflags |= X509_V_FLAG_CRL_CHECK; else if (strcmp(*argv,"-crl_check_all") == 0) vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL; else if (strcmp(*argv,"-prexit") == 0) prexit=1; else if (strcmp(*argv,"-crlf") == 0) crlf=1; else if (strcmp(*argv,"-quiet") == 0) { c_quiet=1; c_ign_eof=1; } else if (strcmp(*argv,"-ign_eof") == 0) c_ign_eof=1; else if (strcmp(*argv,"-pause") == 0) c_Pause=1; else if (strcmp(*argv,"-debug") == 0) c_debug=1; #ifdef WATT32 else if (strcmp(*argv,"-wdebug") == 0) dbug_init(); #endif else if (strcmp(*argv,"-msg") == 0) c_msg=1; else if (strcmp(*argv,"-showcerts") == 0) c_showcerts=1; else if (strcmp(*argv,"-nbio_test") == 0) nbio_test=1; else if (strcmp(*argv,"-state") == 0) state=1; #ifndef OPENSSL_NO_SSL2 else if (strcmp(*argv,"-ssl2") == 0) meth=SSLv2_client_method(); #endif #ifndef OPENSSL_NO_SSL3 else if (strcmp(*argv,"-ssl3") == 0) meth=SSLv3_client_method(); #endif #ifndef OPENSSL_NO_TLS1 else if (strcmp(*argv,"-tls1") == 0) meth=TLSv1_client_method(); #endif #ifndef OPENSSL_NO_DTLS1 else if (strcmp(*argv,"-dtls1") == 0) { meth=DTLSv1_client_method(); sock_type=SOCK_DGRAM; } else if (strcmp(*argv,"-timeout") == 0) enable_timeouts=1; else if (strcmp(*argv,"-mtu") == 0) { if (--argc < 1) goto bad; mtu = atol(*(++argv)); } #endif else if (strcmp(*argv,"-bugs") == 0) bugs=1; else if (strcmp(*argv,"-keyform") == 0) { if (--argc < 1) goto bad; key_format = str2fmt(*(++argv)); } else if (strcmp(*argv,"-pass") == 0) { if (--argc < 1) goto bad; passarg = *(++argv); } else if (strcmp(*argv,"-key") == 0) { if (--argc < 1) goto bad; key_file= *(++argv); } else if (strcmp(*argv,"-reconnect") == 0) { reconnect=5; } else if (strcmp(*argv,"-CApath") == 0) { if (--argc < 1) goto bad; CApath= *(++argv); } else if (strcmp(*argv,"-CAfile") == 0) { if (--argc < 1) goto bad; CAfile= *(++argv); } else if (strcmp(*argv,"-no_tls1") == 0) off|=SSL_OP_NO_TLSv1; else if (strcmp(*argv,"-no_ssl3") == 0) off|=SSL_OP_NO_SSLv3; else if (strcmp(*argv,"-no_ssl2") == 0) off|=SSL_OP_NO_SSLv2; else if (strcmp(*argv,"-serverpref") == 0) off|=SSL_OP_CIPHER_SERVER_PREFERENCE; else if (strcmp(*argv,"-cipher") == 0) { if (--argc < 1) goto bad; cipher= *(++argv); } #ifdef FIONBIO else if (strcmp(*argv,"-nbio") == 0) { c_nbio=1; } #endif else if (strcmp(*argv,"-starttls") == 0) { if (--argc < 1) goto bad; ++argv; if (strcmp(*argv,"smtp") == 0) starttls_proto = PROTO_SMTP; else if (strcmp(*argv,"pop3") == 0) starttls_proto = PROTO_POP3; else if (strcmp(*argv,"imap") == 0) starttls_proto = PROTO_IMAP; else if (strcmp(*argv,"ftp") == 0) starttls_proto = PROTO_FTP; else goto bad; } #ifndef OPENSSL_NO_ENGINE else if (strcmp(*argv,"-engine") == 0) { if (--argc < 1) goto bad; engine_id = *(++argv); } #endif else if (strcmp(*argv,"-rand") == 0) { if (--argc < 1) goto bad; inrand= *(++argv); } else { BIO_printf(bio_err,"unknown option %s\n",*argv); badop=1; break; } argc--; argv++; } if (badop) { bad: sc_usage(); goto end; } OpenSSL_add_ssl_algorithms(); SSL_load_error_strings(); #ifndef OPENSSL_NO_ENGINE e = setup_engine(bio_err, engine_id, 1); #endif if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) { BIO_printf(bio_err, "Error getting password\n"); goto end; } if (key_file == NULL) key_file = cert_file; if (key_file) { key = load_key(bio_err, key_file, key_format, 0, pass, e, "client certificate private key file"); if (!key) { ERR_print_errors(bio_err); goto end; } } if (cert_file) { cert = load_cert(bio_err,cert_file,cert_format, NULL, e, "client certificate file"); if (!cert) { ERR_print_errors(bio_err); goto end; } } if (!app_RAND_load_file(NULL, bio_err, 1) && inrand == NULL && !RAND_status()) { BIO_printf(bio_err,"warning, not much extra random data, consider using the -rand option\n"); } if (inrand != NULL) BIO_printf(bio_err,"%ld semi-random bytes loaded\n", app_RAND_load_files(inrand)); if (bio_c_out == NULL) { if (c_quiet && !c_debug && !c_msg) { bio_c_out=BIO_new(BIO_s_null()); } else { if (bio_c_out == NULL) bio_c_out=BIO_new_fp(stdout,BIO_NOCLOSE); } } ctx=SSL_CTX_new(meth); if (ctx == NULL) { ERR_print_errors(bio_err); goto end; } if (bugs) SSL_CTX_set_options(ctx,SSL_OP_ALL|off); else SSL_CTX_set_options(ctx,off); /* DTLS: partial reads end up discarding unread UDP bytes :-( * Setting read ahead solves this problem. */ if (sock_type == SOCK_DGRAM) SSL_CTX_set_read_ahead(ctx, 1); if (state) SSL_CTX_set_info_callback(ctx,apps_ssl_info_callback); if (cipher != NULL) if(!SSL_CTX_set_cipher_list(ctx,cipher)) { BIO_printf(bio_err,"error setting cipher list\n"); ERR_print_errors(bio_err); goto end; } #if 0 else SSL_CTX_set_cipher_list(ctx,getenv("SSL_CIPHER")); #endif SSL_CTX_set_verify(ctx,verify,verify_callback); if (!set_cert_key_stuff(ctx,cert,key)) goto end; if ((!SSL_CTX_load_verify_locations(ctx,CAfile,CApath)) || (!SSL_CTX_set_default_verify_paths(ctx))) { /* BIO_printf(bio_err,"error setting default verify locations\n"); */ ERR_print_errors(bio_err); /* goto end; */ } store = SSL_CTX_get_cert_store(ctx); X509_STORE_set_flags(store, vflags); con=SSL_new(ctx); #ifndef OPENSSL_NO_KRB5 if (con && (con->kssl_ctx = kssl_ctx_new()) != NULL) { kssl_ctx_setstring(con->kssl_ctx, KSSL_SERVER, host); } #endif /* OPENSSL_NO_KRB5 */ /* SSL_set_cipher_list(con,"RC4-MD5"); */ re_start: if (init_client(&s,host,port,sock_type) == 0) { BIO_printf(bio_err,"connect:errno=%d\n",get_last_socket_error()); SHUTDOWN(s); goto end; } BIO_printf(bio_c_out,"CONNECTED(%08X)\n",s); #ifdef FIONBIO if (c_nbio) { unsigned long l=1; BIO_printf(bio_c_out,"turning on non blocking io\n"); if (BIO_socket_ioctl(s,FIONBIO,&l) < 0) { ERR_print_errors(bio_err); goto end; } } #endif if (c_Pause & 0x01) con->debug=1; if ( SSL_version(con) == DTLS1_VERSION) { struct timeval timeout; sbio=BIO_new_dgram(s,BIO_NOCLOSE); if (getsockname(s, &peer, (void *)&peerlen) < 0) { BIO_printf(bio_err, "getsockname:errno=%d\n", get_last_socket_error()); SHUTDOWN(s); goto end; } (void)BIO_ctrl_set_connected(sbio, 1, &peer); if ( enable_timeouts) { timeout.tv_sec = 0; timeout.tv_usec = DGRAM_RCV_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_RECV_TIMEOUT, 0, &timeout); timeout.tv_sec = 0; timeout.tv_usec = DGRAM_SND_TIMEOUT; BIO_ctrl(sbio, BIO_CTRL_DGRAM_SET_SEND_TIMEOUT, 0, &timeout); } if ( mtu > 0) { SSL_set_options(con, SSL_OP_NO_QUERY_MTU); SSL_set_mtu(con, mtu); } else /* want to do MTU discovery */ BIO_ctrl(sbio, BIO_CTRL_DGRAM_MTU_DISCOVER, 0, NULL); } else sbio=BIO_new_socket(s,BIO_NOCLOSE); if (nbio_test) { BIO *test; test=BIO_new(BIO_f_nbio_test()); sbio=BIO_push(test,sbio); } if (c_debug) { con->debug=1; BIO_set_callback(sbio,bio_dump_callback); BIO_set_callback_arg(sbio,(char *)bio_c_out); } if (c_msg) { SSL_set_msg_callback(con, msg_cb); SSL_set_msg_callback_arg(con, bio_c_out); } SSL_set_bio(con,sbio,sbio); SSL_set_connect_state(con); /* ok, lets connect */ width=SSL_get_fd(con)+1; read_tty=1; write_tty=0; tty_on=0; read_ssl=1; write_ssl=1; cbuf_len=0; cbuf_off=0; sbuf_len=0; sbuf_off=0; /* This is an ugly hack that does a lot of assumptions */ /* We do have to handle multi-line responses which may come in a single packet or not. We therefore have to use BIO_gets() which does need a buffering BIO. So during the initial chitchat we do push a buffering BIO into the chain that is removed again later on to not disturb the rest of the s_client operation. */ if (starttls_proto == PROTO_SMTP) { int foundit=0; BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); /* wait for multi-line response to end from SMTP */ do { mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); } while (mbuf_len>3 && mbuf[3]=='-'); /* STARTTLS command requires EHLO... */ BIO_printf(fbio,"EHLO openssl.client.net\r\n"); (void)BIO_flush(fbio); /* wait for multi-line response to end EHLO SMTP response */ do { mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); if (strstr(mbuf,"STARTTLS")) foundit=1; } while (mbuf_len>3 && mbuf[3]=='-'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); if (!foundit) BIO_printf(bio_err, "didn't found starttls in server response," " try anyway...\n"); BIO_printf(sbio,"STARTTLS\r\n"); BIO_read(sbio,sbuf,BUFSIZZ); } else if (starttls_proto == PROTO_POP3) { BIO_read(sbio,mbuf,BUFSIZZ); BIO_printf(sbio,"STLS\r\n"); BIO_read(sbio,sbuf,BUFSIZZ); } else if (starttls_proto == PROTO_IMAP) { int foundit=0; BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); BIO_gets(fbio,mbuf,BUFSIZZ); /* STARTTLS command requires CAPABILITY... */ BIO_printf(fbio,". CAPABILITY\r\n"); (void)BIO_flush(fbio); /* wait for multi-line CAPABILITY response */ do { mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); if (strstr(mbuf,"STARTTLS")) foundit=1; } while (mbuf_len>3 && mbuf[0]!='.'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); if (!foundit) BIO_printf(bio_err, "didn't found STARTTLS in server response," " try anyway...\n"); BIO_printf(sbio,". STARTTLS\r\n"); BIO_read(sbio,sbuf,BUFSIZZ); } else if (starttls_proto == PROTO_FTP) { BIO *fbio = BIO_new(BIO_f_buffer()); BIO_push(fbio, sbio); /* wait for multi-line response to end from FTP */ do { mbuf_len = BIO_gets(fbio,mbuf,BUFSIZZ); } while (mbuf_len>3 && mbuf[3]=='-'); (void)BIO_flush(fbio); BIO_pop(fbio); BIO_free(fbio); BIO_printf(sbio,"AUTH TLS\r\n"); BIO_read(sbio,sbuf,BUFSIZZ); } for (;;) { FD_ZERO(&readfds); FD_ZERO(&writefds); if (SSL_in_init(con) && !SSL_total_renegotiations(con)) { in_init=1; tty_on=0; } else { tty_on=1; if (in_init) { in_init=0; print_stuff(bio_c_out,con,full_log); if (full_log > 0) full_log--; if (starttls_proto) { BIO_printf(bio_err,"%s",mbuf); /* We don't need to know any more */ starttls_proto = PROTO_OFF; } if (reconnect) { reconnect--; BIO_printf(bio_c_out,"drop connection and then reconnect\n"); SSL_shutdown(con); SSL_set_connect_state(con); SHUTDOWN(SSL_get_fd(con)); goto re_start; } } } ssl_pending = read_ssl && SSL_pending(con); if (!ssl_pending) { #if !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_NETWARE) if (tty_on) { if (read_tty) FD_SET(fileno(stdin),&readfds); if (write_tty) FD_SET(fileno(stdout),&writefds); } if (read_ssl) FD_SET(SSL_get_fd(con),&readfds); if (write_ssl) FD_SET(SSL_get_fd(con),&writefds); #else if(!tty_on || !write_tty) { if (read_ssl) FD_SET(SSL_get_fd(con),&readfds); if (write_ssl) FD_SET(SSL_get_fd(con),&writefds); } #endif /* printf("mode tty(%d %d%d) ssl(%d%d)\n", tty_on,read_tty,write_tty,read_ssl,write_ssl);*/ /* Note: under VMS with SOCKETSHR the second parameter * is currently of type (int *) whereas under other * systems it is (void *) if you don't have a cast it * will choke the compiler: if you do have a cast then * you can either go for (int *) or (void *). */ #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) /* Under Windows/DOS we make the assumption that we can * always write to the tty: therefore if we need to * write to the tty we just fall through. Otherwise * we timeout the select every second and see if there * are any keypresses. Note: this is a hack, in a proper * Windows application we wouldn't do this. */ i=0; if(!write_tty) { if(read_tty) { tv.tv_sec = 1; tv.tv_usec = 0; i=select(width,(void *)&readfds,(void *)&writefds, NULL,&tv); #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) if(!i && (!_kbhit() || !read_tty) ) continue; #else if(!i && (!((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) || !read_tty) ) continue; #endif } else i=select(width,(void *)&readfds,(void *)&writefds, NULL,NULL); } #elif defined(OPENSSL_SYS_NETWARE) if(!write_tty) { if(read_tty) { tv.tv_sec = 1; tv.tv_usec = 0; i=select(width,(void *)&readfds,(void *)&writefds, NULL,&tv); } else i=select(width,(void *)&readfds,(void *)&writefds, NULL,NULL); } #else i=select(width,(void *)&readfds,(void *)&writefds, NULL,NULL); #endif if ( i < 0) { BIO_printf(bio_err,"bad select %d\n", get_last_socket_error()); goto shut; /* goto end; */ } } if (!ssl_pending && FD_ISSET(SSL_get_fd(con),&writefds)) { k=SSL_write(con,&(cbuf[cbuf_off]), (unsigned int)cbuf_len); switch (SSL_get_error(con,k)) { case SSL_ERROR_NONE: cbuf_off+=k; cbuf_len-=k; if (k <= 0) goto end; /* we have done a write(con,NULL,0); */ if (cbuf_len <= 0) { read_tty=1; write_ssl=0; } else /* if (cbuf_len > 0) */ { read_tty=0; write_ssl=1; } break; case SSL_ERROR_WANT_WRITE: BIO_printf(bio_c_out,"write W BLOCK\n"); write_ssl=1; read_tty=0; break; case SSL_ERROR_WANT_READ: BIO_printf(bio_c_out,"write R BLOCK\n"); write_tty=0; read_ssl=1; write_ssl=0; break; case SSL_ERROR_WANT_X509_LOOKUP: BIO_printf(bio_c_out,"write X BLOCK\n"); break; case SSL_ERROR_ZERO_RETURN: if (cbuf_len != 0) { BIO_printf(bio_c_out,"shutdown\n"); goto shut; } else { read_tty=1; write_ssl=0; break; } case SSL_ERROR_SYSCALL: if ((k != 0) || (cbuf_len != 0)) { BIO_printf(bio_err,"write:errno=%d\n", get_last_socket_error()); goto shut; } else { read_tty=1; write_ssl=0; } break; case SSL_ERROR_SSL: ERR_print_errors(bio_err); goto shut; } } #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_NETWARE) /* Assume Windows/DOS can always write */ else if (!ssl_pending && write_tty) #else else if (!ssl_pending && FD_ISSET(fileno(stdout),&writefds)) #endif { #ifdef CHARSET_EBCDIC ascii2ebcdic(&(sbuf[sbuf_off]),&(sbuf[sbuf_off]),sbuf_len); #endif i=write(fileno(stdout),&(sbuf[sbuf_off]),sbuf_len); if (i <= 0) { BIO_printf(bio_c_out,"DONE\n"); goto shut; /* goto end; */ } sbuf_len-=i;; sbuf_off+=i; if (sbuf_len <= 0) { read_ssl=1; write_tty=0; } } else if (ssl_pending || FD_ISSET(SSL_get_fd(con),&readfds)) { #ifdef RENEG { static int iiii; if (++iiii == 52) { SSL_renegotiate(con); iiii=0; } } #endif #if 1 k=SSL_read(con,sbuf,1024 /* BUFSIZZ */ ); #else /* Demo for pending and peek :-) */ k=SSL_read(con,sbuf,16); { char zbuf[10240]; printf("read=%d pending=%d peek=%d\n",k,SSL_pending(con),SSL_peek(con,zbuf,10240)); } #endif switch (SSL_get_error(con,k)) { case SSL_ERROR_NONE: if (k <= 0) goto end; sbuf_off=0; sbuf_len=k; read_ssl=0; write_tty=1; break; case SSL_ERROR_WANT_WRITE: BIO_printf(bio_c_out,"read W BLOCK\n"); write_ssl=1; read_tty=0; break; case SSL_ERROR_WANT_READ: BIO_printf(bio_c_out,"read R BLOCK\n"); write_tty=0; read_ssl=1; if ((read_tty == 0) && (write_ssl == 0)) write_ssl=1; break; case SSL_ERROR_WANT_X509_LOOKUP: BIO_printf(bio_c_out,"read X BLOCK\n"); break; case SSL_ERROR_SYSCALL: BIO_printf(bio_err,"read:errno=%d\n",get_last_socket_error()); goto shut; case SSL_ERROR_ZERO_RETURN: BIO_printf(bio_c_out,"closed\n"); goto shut; case SSL_ERROR_SSL: ERR_print_errors(bio_err); goto shut; /* break; */ } } #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) #if defined(OPENSSL_SYS_WINCE) || defined(OPENSSL_SYS_MSDOS) else if (_kbhit()) #else else if ((_kbhit()) || (WAIT_OBJECT_0 == WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 0))) #endif #elif defined (OPENSSL_SYS_NETWARE) else if (_kbhit()) #else else if (FD_ISSET(fileno(stdin),&readfds)) #endif { if (crlf) { int j, lf_num; i=read(fileno(stdin),cbuf,BUFSIZZ/2); lf_num = 0; /* both loops are skipped when i <= 0 */ for (j = 0; j < i; j++) if (cbuf[j] == '\n') lf_num++; for (j = i-1; j >= 0; j--) { cbuf[j+lf_num] = cbuf[j]; if (cbuf[j] == '\n') { lf_num--; i++; cbuf[j+lf_num] = '\r'; } } assert(lf_num == 0); } else i=read(fileno(stdin),cbuf,BUFSIZZ); if ((!c_ign_eof) && ((i <= 0) || (cbuf[0] == 'Q'))) { BIO_printf(bio_err,"DONE\n"); goto shut; } if ((!c_ign_eof) && (cbuf[0] == 'R')) { BIO_printf(bio_err,"RENEGOTIATING\n"); SSL_renegotiate(con); cbuf_len=0; } else { cbuf_len=i; cbuf_off=0; #ifdef CHARSET_EBCDIC ebcdic2ascii(cbuf, cbuf, i); #endif } write_ssl=1; read_tty=0; } } shut: SSL_shutdown(con); SHUTDOWN(SSL_get_fd(con)); ret=0; end: if(prexit) print_stuff(bio_c_out,con,1); if (con != NULL) SSL_free(con); if (con2 != NULL) SSL_free(con2); if (ctx != NULL) SSL_CTX_free(ctx); if (cert) X509_free(cert); if (key) EVP_PKEY_free(key); if (pass) OPENSSL_free(pass); if (cbuf != NULL) { OPENSSL_cleanse(cbuf,BUFSIZZ); OPENSSL_free(cbuf); } if (sbuf != NULL) { OPENSSL_cleanse(sbuf,BUFSIZZ); OPENSSL_free(sbuf); } if (mbuf != NULL) { OPENSSL_cleanse(mbuf,BUFSIZZ); OPENSSL_free(mbuf); } if (bio_c_out != NULL) { BIO_free(bio_c_out); bio_c_out=NULL; } apps_shutdown(); OPENSSL_EXIT(ret); }
int sendsms (char *host, char *gsmnumber, char *gsmtext, int checksent, char *port) { const char urlconfirm[] = "GET /sendsms.php?sent=cli&to=%s&fromform=true&confirmation=on&text=%s HTTP/1.1\r\n" "Host:%s\r\n" "Connection: close\r\n\r\n"; const char urlnoconfirm[] = "GET /sendsms.php?sent=cli&to=%s&fromform=true&text=%s HTTP/1.1\r\n" "Host:%s\r\n" "Connection: close\r\n\r\n"; const char urlcontinuecheck[] = "GET /sendsms.php?sent=cli&confirmation=on&msgid=%s HTTP/1.1\r\n" "Host:%s\r\n" "Connection: close\r\n\r\n"; char *buffer, *smsreq, *urltemplate, *out, *smsid = NULL, *smsstatus; int sd, s, flags = 0, i; struct addrinfo hints; struct addrinfo *result, *rp; smsid = (char *) malloc (50); memset (smsid, 0x0, 14); memset (&hints, 0x0, sizeof (struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = 0; hints.ai_protocol = 0; s = getaddrinfo (host, port, &hints, &result); if (s != 0) { printf ("SMSTATUS: GETADDRINFO FAILED - %s\n", gai_strerror (s)); return -1; } for (i = 0; i < 10; i++) { for (rp = result; rp != NULL; rp = rp->ai_next) { sd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (sd == -1) continue; #ifndef MINGW32 #define SHUTDOWN(fd) { shutdown((fd),0); close((fd)); } flags = fcntl (sd, F_GETFL, 0); #else #define fcntl(fd,b,c) { u_long arg=1;ioctlsocket(fd, c, &arg); } #define SHUTDOWN(fd) { shutdown((fd),0); closesocket((fd)); } #define sleep(x) Sleep((x)*1000) flags = 0; #endif fcntl (sd, F_SETFL, flags); if (connect (sd, rp->ai_addr, rp->ai_addrlen) != -1) break; close (sd); } if (rp == NULL) { printf ("SMSSTATUS: FAILED CONNECT TO SERVER\n"); return -1; } buffer = (char *) malloc (1023); memset (buffer, 0x0, 1023); if (!(strncmp (smsid, "send_", 5) == 0)) { if (checksent == 1) { urltemplate = (char *) malloc (strlen (urlconfirm)); memset (urltemplate, 0x0, strlen (urlconfirm)); strcpy (urltemplate, urlconfirm); } else { urltemplate = (char *) malloc (strlen (urlnoconfirm)); memset (urltemplate, 0x0, strlen (urlnoconfirm)); strcpy (urltemplate, urlnoconfirm); } smsreq = (char *) malloc (strlen (urltemplate) + 1023); memset (smsreq, 0x0, strlen (urltemplate) + 1023); sprintf (smsreq, urltemplate, gsmnumber, replace (gsmtext, " ", "+"), host); } else { urltemplate = (char *) malloc (strlen (urlcontinuecheck)); memset (urltemplate, 0x0, strlen (urlcontinuecheck)); strcpy (urltemplate, urlcontinuecheck); smsreq = (char *) malloc (strlen (urltemplate) + 1023); memset (smsreq, 0x0, strlen (urltemplate) + 1023); sprintf (smsreq, urltemplate, smsid, host); } if (send (sd, smsreq, strlen (smsreq), 0) != strlen (smsreq)) { printf ("SMSSTATUS: FAILED in send()\n"); return -1; } out = (char *) malloc (1024); memset (out, 0x0, 1024); recv (sd, out, 1024, 0); SHUTDOWN (sd); /* close socket... */ smsstatus = strstr (out, "SMSSTATUS: "); if (strstr (out, "HTTP/1.1 200") == NULL) { printf ("SMSSTATUS: FAILED SERVER NOT CONFIGURED\n"); return (-1); } char *tmp; if ((tmp = (char *) strchr (smsstatus, '#')) != NULL) { smsid = strdup (tmp + 1); } free (buffer); free (urltemplate); free (smsreq); free (out); if (!checksent) return 0; if (strstr (smsstatus, "SENT")) { printf ("%s\n", smsstatus); return 0; } if (strstr (smsstatus, "FAILED")) { printf ("%s\n", smsstatus); return -1; } sleep (5); } printf ("SMSSTATUS: POSSIBLE FAIL (TIMEOUT)\n"); return 1; }
/* ================= engine_stop ================= */ void engine_stop (void) { static int stopping = 0; if (stopping) { /* already stopping */ return; } stopping = 1; SHUTDOWN(g); SHUTDOWN(snd); SHUTDOWN(r); SHUTDOWN(gl); SHUTDOWN(video); SHUTDOWN(mouse); SHUTDOWN(keyboard); SHUTDOWN(image); SHUTDOWN(cvar); SHUTDOWN(cmdbuf); SHUTDOWN(cmd); SHUTDOWN(fs); }