static void client_opts(int argc, char **argv) { int long_index = 0; int opt; char *errptr; optind = 0; while ((opt = getopt_long(argc, argv, "?hp:t:f:r:s:w:d:m:", client_options, &long_index)) != -1) { switch(opt) { case 'h': case '?': client_usage(); exit(0); case 'p': errno = 0; port = strtoul(optarg, &errptr, 10); if (*errptr != '\0' || errno) { client_usage(); exit(-1); } break; case 't': errno = 0; workers = strtoul(optarg, &errptr, 10); if (*errptr != '\0' || errno || !workers || workers > MAXNUMWORKERS) { client_usage(); exit(-1); } break; case 'r': errno = 0; requests = strtoul(optarg, &errptr, 10); if (*errptr != '\0' || errno || !requests || requests > 1000) { client_usage(); exit(-1); } break; case 's': address = optarg; break; case 'w': workloadfile = optarg; break; case 'd': downloaddir = optarg; break; case 'm': metricsfile = optarg; break; default: client_usage(); exit(-1); } } }
int client_setup(krb5_context *context, int *argc, char **argv) { int optidx = *argc; int port = common_setup(context, &optidx, argv, client_usage); if(*argc - optidx != 1) client_usage(1, args, num_args); *argc = optidx; return port; }
void my_error(int key, int key2) { if (key == 1) signal_error(key2); else if (key == 2) client_usage(key2); else if (key == 3) wrong_param(key2); exit(0); }
static int denyORallow( char * option ) { #if 0 fprintf( stderr, "denyORallow \"%s\"\n", option ); #endif if (strcmp( option, "allow" ) == 0) return UFDB_ALLOW; else if (strcmp( option, "deny" ) == 0 || strcmp( option, "block" ) == 0) return UFDB_DENY; fprintf( stderr, "-e option must be followed by 'allow' or 'deny'\n" ); client_usage(); exit( 1 ); return UFDB_ALLOW; /* make compiler happy */ }
int main (int argc, char *argv[] ) { lscp_client_t *pClient; char *pszHost = "localhost"; char szLine[1024]; int cchLine; lscp_status_t ret; #if defined(WIN32) if (WSAStartup(MAKEWORD(1, 1), &_wsaData) != 0) { fprintf(stderr, "lscp_client: WSAStartup failed.\n"); return -1; } #endif if (argc > 1) pszHost = argv[1]; pClient = lscp_client_create(pszHost, SERVER_PORT, client_callback, NULL); if (pClient == NULL) return -1; client_usage(); client_prompt(); while (fgets(szLine, sizeof(szLine) - 3, stdin)) { cchLine = strlen(szLine); while (cchLine > 0 && (szLine[cchLine - 1] == '\n' || szLine[cchLine - 1] == '\r')) cchLine--; szLine[cchLine] = '\0'; if (strcmp(szLine, "exit") == 0 || strcmp(szLine, "quit") == 0) break; else if (strcmp(szLine, "subscribe") == 0) lscp_client_subscribe(pClient, LSCP_EVENT_MISCELLANEOUS); else if (strcmp(szLine, "unsubscribe") == 0) lscp_client_unsubscribe(pClient, LSCP_EVENT_MISCELLANEOUS); else if (strcmp(szLine, "test") == 0) client_test_all(pClient, 0); else if (strcmp(szLine, "teststep") == 0 || strcmp(szLine, "test step") == 0) client_test_all(pClient, 1); else if (cchLine > 0 && strcmp(szLine, "help") != 0) { szLine[cchLine++] = '\r'; szLine[cchLine++] = '\n'; szLine[cchLine] = '\0'; ret = lscp_client_query(pClient, szLine); printf("%s\n(errno = %d)\n", lscp_client_get_result(pClient), lscp_client_get_errno(pClient)); if (ret == LSCP_QUIT) break; } else client_usage(); client_prompt(); } lscp_client_destroy(pClient); #if defined(WIN32) WSACleanup(); #endif return 0; }
int main(int argc, char **argv) { /* local client data */ int sockfd; /* file descriptor for endpoint */ struct sockaddr_in client_sockaddr; /* address/port pair */ struct in_addr client_addr; /* network-format address */ char client_dotted[INET_ADDRSTRLEN];/* human-readable address */ int client_port; /* local port */ /* remote server data */ char *server_dotted; /* human-readable address */ int server_port; /* remote port */ /* the request */ char *filename; /* filename to request */ /* read arguments */ if (argc != 5) { fprintf(stderr, "client: wrong number of arguments\n"); client_usage(); exit(1); } server_dotted = argv[1]; server_port = atoi(argv[2]); client_port = atoi(argv[3]); filename = argv[4]; if (!client_arguments_valid( server_dotted, server_port, client_port, filename)) { client_usage(); exit(1); } /* get the primary IP address of this host */ get_primary_addr(&client_addr); inet_ntop(AF_INET, &client_addr, client_dotted, INET_ADDRSTRLEN); /* construct an endpoint address with primary address and desired port */ memset(&client_sockaddr, 0, sizeof(client_sockaddr)); client_sockaddr.sin_family = PF_INET; memcpy(&client_sockaddr.sin_addr,&client_addr,sizeof(struct in_addr)); client_sockaddr.sin_port = htons(client_port); /* make a socket*/ sockfd = socket(PF_INET, SOCK_DGRAM, 0); if (sockfd<0) { perror("can't open socket"); exit(1); } /* bind it to an appropriate local address and port */ if (bind(sockfd, (struct sockaddr *) &client_sockaddr, sizeof(client_sockaddr))<0) { perror("can't bind local address"); exit(1); } fprintf(stderr, "client: Receiving on %s, port %d\n", client_dotted, client_port); /* send a command */ send_command(sockfd, server_dotted, server_port, filename, 0, MAXINT); fprintf(stderr, "client: requesting %s blocks %d-%d\n", filename, 0, MAXINT); /* receive the whole document and make naive assumptions */ int done = FALSE; // set to TRUE when you think you're done int init = TRUE; /* create a bit array to test which blocks have been received */ struct bits blocksRecv; /* open a file to write to in the current directory */ FILE *download = fopen(filename, "w"); fclose(download); int outfd = open(filename, O_RDWR); while (!done) { int retval; again: if ((retval = select_block(sockfd, 0, 20000))==0) { /* timeout */ struct range nBlocks[12]; int currentRange = 0; int i; int check = 0; int numBlocks = blocksRecv.nbits; for(i = 0; i < numBlocks; i++){ /* look for the start of a missed block */ if(check == 0 && bits_testbit(&blocksRecv, i)){ nBlocks[currentRange].first_block = i; nBlocks[currentRange].last_block = numBlocks-1; check++; } /* look for the end of a missed block */ else if(check == 1 && !(bits_testbit(&blocksRecv, i))){ nBlocks[currentRange].last_block = i-1; currentRange ++; /* if you have found 12 missed blocks, send_commands */ if(currentRange == 12){ send_commands(sockfd, server_dotted, server_port, filename, nBlocks, currentRange); currentRange = 0; } check = 0; } } /* send any left over blocks from the loop above */ send_commands(sockfd, server_dotted, server_port, filename, nBlocks, currentRange); if(bits_empty(&blocksRecv)){ done = TRUE; } } else if (retval<0) { /* error */ perror("select"); fprintf(stderr, "client: receive error\n"); } else { /* input is waiting, read it */ struct sockaddr_in resp_sockaddr; /* address/port pair */ int resp_len; /* length used */ char resp_dotted[INET_ADDRSTRLEN]; /* human-readable address */ int resp_port; /* port */ int resp_mesglen; /* length of message */ struct block one_block; /* use helper routine to receive a block */ recv_block(sockfd, &one_block, &resp_sockaddr); /* get human-readable internet address */ inet_ntop(AF_INET, (void *)&(resp_sockaddr.sin_addr.s_addr), resp_dotted, INET_ADDRSTRLEN); resp_port = ntohs(resp_sockaddr.sin_port); fprintf(stderr, "client: %s:%d sent %s block %d (range 0-%d)\n", resp_dotted, resp_port, one_block.filename, one_block.which_block, one_block.total_blocks); /* check block data for errors */ if (strcmp(filename, one_block.filename)!=0) { fprintf(stderr, "client: received block with incorrect filename %s\n", one_block.filename); goto again; } /* init the bit array once you know how many blocks the file contains */ if(init){ //fprintf(stderr, "total blocks: %d\n", one_block.total_blocks); bits_alloc(&blocksRecv, one_block.total_blocks); bits_setrange(&blocksRecv, 0, one_block.total_blocks - 1); init = FALSE; } /* if you have not received the current block, write it and flag it as received */ if(bits_testbit(&blocksRecv, one_block.which_block)){ bits_clearbit(&blocksRecv, one_block.which_block); lseek(outfd, one_block.which_block*PAYLOADSIZE, SEEK_SET); write(outfd, one_block.payload, one_block.paysize); } /* if all blocks have been received, done */ if(bits_empty(&blocksRecv)){ done = TRUE; } } } /* close the file stream */ close(outfd); }
int main( int argc, char ** argv ) { int s; struct stat stbuf; struct tms timer; struct timeval start_time; globalPid = getpid(); strcpy( progname, "ufdbgclient" ); strcpy( serverName, "localhost" ); portNum = UFDB_DAEMON_PORT; while ((s = getopt(argc, argv, "e:E:NChdrTvql:p:S:t:")) != EOF) { switch (s) { case 'd': UFDBglobalDebug = 1; break; case 'e': connectionErrorBehaviour = denyORallow( optarg ); break; case 'E': strcpy( connectErrorRedirect, optarg ); break; case 'r': debugRedirect = 1; break; case 'l': UFDBglobalLogDir = optarg; if (stat( UFDBglobalLogDir, &stbuf ) != 0) { fprintf( stderr, "ufdbgclient: error in -l %s: %s\n", UFDBglobalLogDir, strerror(errno) ); exit( 1 ); } if (!S_ISDIR(stbuf.st_mode)) { fprintf( stderr, "ufdbgclient: %s is not a directory\n", UFDBglobalLogDir ); exit( 1 ); } break; case 'N': ufdbGlobalSetLogging( 0 ); break; case 'C': squidConcurrentInput = 1; break; case 'p': portNum = atoi( optarg ); if (portNum < 1) { fprintf( stderr, "ufdbgclient: port number must be > 0\n" ); exit( 1 ); } break; case 'q': be_quiet = 1; break; case 'v': fprintf( stderr, "ufdbgclient: %s\n", VERSION ); exit( 0 ); break; case 'S': strcpy( serverName, optarg ); break; case 't': timeout = atoi( optarg ); if (timeout < 2) timeout = 2; if (timeout > 200) timeout = 200; break; case 'T': fprintf( stderr, "-T option found. Going into test mode.\n" ); noRedirects = 1; /* Test mode means noRedirects */ break; case '?': case 'h': default: client_usage(); exit( 0 ); } } ufdbSetGlobalErrorLogFile(); signal( SIGPIPE, SIG_IGN ); s = connect_to_server(); /* * process one query from the command line ? */ if (optind < argc - 2) { if (s < 0) { int nbytes; char line[1024]; if (connectionErrorBehaviour == UFDB_ALLOW) strcpy( line, "\n" ); else sprintf( line, "%s\n", connectErrorRedirect ); nbytes = strlen( line ); if (fwrite( line, nbytes, 1, stdout )) ; ufdbLogError( "cannot connect to port %d on server %s: %s", portNum, serverName, strerror(errno) ); exit( 1 ); } process_cmd( s, argv[optind], argv[optind+1], argv[optind+2] ); close( s ); exit( 0 ); } if (!be_quiet) { gettimeofday( &start_time, NULL ); ufdbLogMessage( "ufdbgclient " VERSION " started" ); UFDBtimerInit( &timer ); } process_input( s ); if (!be_quiet) { char cpuusagetxt[200]; UFDBtimerStop( &timer ); UFDBtimerPrintString( cpuusagetxt, &timer, "CPU times" ); gettimeofday( &start_time, NULL ); ufdbLogMessage( "ufdbgclient " VERSION " finished\n%s", cpuusagetxt ); } close( s ); exit( 0 ); }