void send_command(int fd, uint8 cmd, uint8 val) { printf("sending command \"%s\" (0x%02X) with value %d (0x%02X)\n", get_command_name(cmd), cmd, val, val); write(fd, &cmd, 1); write(fd, &val, 1); }
LIBCOUCHBASE_API bool libcouchbase_packet_debug(libcouchbase_t instance, const void *ptr) { (void)instance; const protocol_binary_request_header *req = ptr; assert((req->request.magic == PROTOCOL_BINARY_REQ) || (req->request.magic == PROTOCOL_BINARY_RES)); print_header(); dump_bytes((void*)req, ntohl(req->request.bodylen) + sizeof(*req)); printf("\n\nHeader breakdown\n"); printf("Field (offset) (value)\n"); printf("Magic (0): 0x%02x (%s)\n", req->request.magic, req->request.magic == PROTOCOL_BINARY_REQ ? "PROTOCOL_BINARY_REQ" : "PROTOCOL_BINARY_RES"); printf("Opcode (1): 0x%02x (%s)\n", req->request.opcode, get_command_name(req->request.opcode)); printf("Key length (2-3): "); print_uint16(req->request.keylen, true, true); printf("Extra length (4): 0x%02x\n", req->request.extlen); printf("Data type (5): 0x%02x\n", req->request.datatype); if (req->request.magic == PROTOCOL_BINARY_REQ) { printf("vbucket (6-7): "); print_uint16(req->request.vbucket, true, true); } else { printf("Status (6-7): "); print_uint16(req->request.vbucket, false, false); printf(" (%s)\n", get_response_name(req->request.vbucket)); } printf("Total body (8-11): "); print_uint32(req->request.bodylen, true, true); printf("Opaque (12-15): "); print_uint32(req->request.opaque, true, true); printf("CAS (16-23): "); print_uint64(req->request.cas, true, true); if (req->request.magic == PROTOCOL_BINARY_REQ) { if (packets[req->request.opcode].req != NULL) { packets[req->request.opcode].req((void*)req); } } else { if (packets[req->request.opcode].res != NULL) { packets[req->request.opcode].res((void*)req); } } return true; }
void log_device_error(struct payload_header *header) { const char *dev_name, *cmd_name; if (header == NULL || header->payload_size >= 0) return; dev_name = get_device_name(header); cmd_name = get_command_name(header); log_message(WRND_ERROR, "Error status received from the device [%" PRIu16 "] %s:%s", header->seq_num, dev_name != NULL ? dev_name : "UNEXPECTED", cmd_name != NULL ? cmd_name : "UNEXPECTED"); }
static void log_header(enum command_type type, const char *msg, struct payload_header *header) { const char *dev_name, *cmd_name; size_t size = sizeof(struct payload_header); unsigned char *bytes = (unsigned char *)header; char *header_hex = malloc(size * 3 + 1); for (size_t i = 0; i < size; i++) { sprintf(header_hex + i * 3, " %02X", *(bytes + i)); } dev_name = get_device_name(header); cmd_name = get_command_name(header); log_message(type, "%s:%" PRId16 " [%" PRIu16 ":%s:%s]%s", msg, header->payload_size, header->seq_num, dev_name != NULL ? dev_name : "UNEXPECTED", cmd_name != NULL ? cmd_name : "UNEXPECTED", header_hex); free(header_hex); }
static void state_machine_v5( socks_hash_entry_t *hash_info, tvbuff_t *tvb, int offset, packet_info *pinfo) { /* Decode V5 protocol. This is done on the first pass through the */ /* list. Based upon the current state, decode the packet and determine */ /* what the next state should be. If we had per packet information, */ /* this would be the place to load them up. */ int temp; if ( hash_info->state == None) { col_append_str(pinfo->cinfo, COL_INFO, " Connect to server request"); hash_info->state = Connecting; /* change state */ hash_info->connect_row = get_packet_ptr; temp = tvb_get_guint8(tvb, offset + 1); /* skip past auth methods */ offset = hash_info->connect_offset = offset + 1 + temp; } else if ( hash_info->state == Connecting){ guint AuthMethod = tvb_get_guint8(tvb, offset + 1); col_append_str(pinfo->cinfo, COL_INFO, " Connect to server response"); hash_info->auth_method_row = get_packet_ptr; if ( AuthMethod == NO_AUTHENTICATION) hash_info->state = V5Command; else if ( AuthMethod == USER_NAME_AUTHENTICATION) hash_info->state = UserNameAuth; else if ( AuthMethod == GSS_API_AUTHENTICATION) hash_info->state = GssApiAuth; else hash_info->state = Done; /*Auth failed or error*/ } else if ( hash_info->state == V5Command) { /* Handle V5 Command */ /** ?? guint temp; **/ hash_info->command = tvb_get_guint8(tvb, offset + 1); /* get command */ if (check_col(pinfo->cinfo, COL_INFO)) col_append_fstr(pinfo->cinfo, COL_INFO, " Command Request - %s", get_command_name(hash_info->command)); hash_info->state = V5Reply; hash_info->command_row = get_packet_ptr; offset += 3; /* skip to address type */ offset = get_address_v5(tvb, offset, hash_info); /** temp = tvb_get_guint8(tvb, offset); XX: what was this for ? **/ if (( hash_info->command == CONNECT_COMMAND) || ( hash_info->command == UDP_ASSOCIATE_COMMAND)) /* get remote port */ hash_info->port = tvb_get_ntohs(tvb, offset); } else if ( hash_info->state == V5Reply) { /* V5 Command Reply */ if (check_col(pinfo->cinfo, COL_INFO)) col_append_fstr(pinfo->cinfo, COL_INFO, " Command Response - %s", get_command_name(hash_info->command)); hash_info->cmd_reply_row = get_packet_ptr; if (( hash_info->command == CONNECT_COMMAND) || (hash_info->command == PING_COMMAND) || (hash_info->command == TRACERT_COMMAND)) hash_info->state = Done; else if ( hash_info->command == BIND_COMMAND) hash_info->state = V5BindReply; else if ( hash_info->command == UDP_ASSOCIATE_COMMAND){ offset += 3; /* skip to address type */ offset = get_address_v5(tvb, offset, hash_info); /* save server udp port and create udp conversation */ hash_info->udp_port = tvb_get_ntohs(tvb, offset); if (!pinfo->fd->flags.visited) new_udp_conversation( hash_info, pinfo); /*XXX may need else statement to handle unknowns and generate error message */ } } else if ( hash_info->state == V5BindReply) { /* V5 Bind Second Reply */ col_append_str(pinfo->cinfo, COL_INFO, " Command Response: Bind remote host info"); hash_info->bind_reply_row = get_packet_ptr; hash_info->state = Done; } else if ( hash_info->state == UserNameAuth) { /* Handle V5 User Auth*/ col_append_str(pinfo->cinfo, COL_INFO, " User authentication request"); hash_info->user_name_auth_row = get_packet_ptr; hash_info->state = UserNameAuthReply; } else if ( hash_info->state == GssApiAuth) { col_append_str(pinfo->cinfo, COL_INFO, " GSSAPI Authentication request"); hash_info->gssapi_auth_row = get_packet_ptr; hash_info->state = GssApiAuthReply; } else if ( hash_info->state == GssApiAuthReply) { if (tvb_get_guint8(tvb, offset+1) == 0xFF) { col_append_str(pinfo->cinfo, COL_INFO, " GSSAPI Authentication failure"); hash_info->gssapi_auth_failure_row = get_packet_ptr; } else { col_append_str(pinfo->cinfo, COL_INFO, " GSSAPI Authentication reply"); if (tvb_get_ntohs(tvb, offset+2) == 0) hash_info->state = V5Command; else hash_info->state = GssApiAuth; hash_info->gssapi_auth_reply_row = get_packet_ptr; } } else if ( hash_info->state == UserNameAuthReply){ /* V5 User Auth reply */ hash_info->auth_version = get_packet_ptr; col_append_str(pinfo->cinfo, COL_INFO, " User authentication reply"); hash_info->state = V5Command; } }
static void display_socks_v4(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, socks_hash_entry_t *hash_info) { /* Display the protocol tree for the V4 version. This routine uses the */ /* stored conversation information to decide what to do with the row. */ /* Per packet information would have been better to do this, but we */ /* didn't have that when I wrote this. And I didn't expect this to get */ /* so messy. */ proto_item *hidden_item; guint command; unsigned char ipaddr[4]; guint username_len, domainname_len; /* Display command from client */ if (compare_packet( hash_info->connect_row)){ proto_tree_add_text( tree, tvb, offset, 1, "Version: %u", hash_info->version); ++offset; command = tvb_get_guint8(tvb, offset); proto_tree_add_text( tree, tvb, offset, 1, "Command: %u (%s)", command, get_command_name( command)); ++offset; /* Do remote port */ proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2, FALSE); offset += 2; /* Do destination address */ tvb_memcpy(tvb, ipaddr, offset, 4); proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset, 4, FALSE); offset += 4; /*XXX check this, needs to do length checking */ /* Should perhaps do TCP reassembly as well */ if ( tvb_offset_exists(tvb, offset)) { /* display user name */ username_len = tvb_strsize(tvb, offset); proto_tree_add_item( tree, hf_user_name, tvb, offset, username_len, FALSE); offset += username_len; if ( ipaddr[0] == 0 && ipaddr[1] == 0 && ipaddr[2] == 0 && ipaddr[3] != 0) { /* 0.0.0.x , where x!=0 means v4a support */ domainname_len = tvb_strsize(tvb, offset); proto_tree_add_item( tree, hf_v4a_dns_name, tvb, offset, domainname_len, FALSE); } } } /*Display command response from server*/ else if ( compare_packet( hash_info->cmd_reply_row)){ proto_tree_add_item( tree, hf_socks_ver, tvb, offset, 1, FALSE); ++offset; /* Do results code */ proto_tree_add_item( tree, hf_socks_results_4, tvb, offset, 1, FALSE); hidden_item = proto_tree_add_item(tree, hf_socks_results, tvb, offset, 1, FALSE); PROTO_ITEM_SET_HIDDEN(hidden_item); ++offset; /* Do remote port */ proto_tree_add_item( tree, hf_socks_dstport, tvb, offset, 2, FALSE); offset += 2; /* Do remote address */ proto_tree_add_item( tree, hf_socks_ip_dst, tvb, offset, 4, FALSE); } else if ( compare_packet( hash_info->v4_user_name_row)){ /*XXX check this, needs to do length checking */ /* Should perhaps do TCP reassembly as well */ if ( tvb_offset_exists(tvb, offset)) { proto_tree_add_text( tree, tvb, offset, tvb_strsize(tvb, offset), "User Name: %s", tvb_get_ptr(tvb, offset, -1)); } } }
int main( int argc, const char *argv[] ) { const int numargs = 2; // including command name int last_count = -1; struct Terrain_Progress_Callback progress = { print_progress, &last_count }; char *endptr; int nrows; int ncols; float *data; int error; printf( "\nTiming tester for terrain texture shading program - version %s\n", version ); // Validate parameters: // command_name = "TEXTURE_TESTER"; command_name = get_command_name( argv ); if (argc == 1) { usage_exit( 0 ); } else if (argc < numargs) { usage_exit( "Not enough command-line parameters." ); } else if (argc > numargs) { usage_exit( "Too many command-line parameters." ); } nrows = (int)strtol( argv[1], &endptr, 10 ); ncols = nrows; if (endptr == argv[1] || *endptr != '\0') { usage_exit( "First parameter (detail) must be a number." ); } data = (float *)malloc( (long)nrows * (long)ncols * sizeof( float ) ); memset( data, 0, (long)nrows * (long)ncols * sizeof( float ) ); // Process data: printf( "Processing %d column x %d row array...\n", ncols, nrows ); fflush( stdout ); gettimeofday(&tp2, NULL); error = terrain_filter( data, 1.0, nrows, ncols, 1.0, 1.0, TERRAIN_METERS, 0.0, &progress ); if (error) { assert( error == TERRAIN_FILTER_MALLOC_ERROR ); prefix_error(); fprintf( stderr, "Memory allocation error occurred during processing of data.\n" ); exit( EXIT_FAILURE ); } free( data ); printf( "DONE.\n" ); return EXIT_SUCCESS; }
int main( int argc, const char *argv[] ) { const int minargs = 4; // including command name int last_count = -1; struct Terrain_Progress_Callback progress = { print_progress, &last_count }; int argnum; const char *thisarg; char *endptr; char extension[4]; // 3 chars plus null terminator char *in_dat_name; char *in_hdr_name; char *in_prj_name; char *out_dat_name; char *out_hdr_name; char *out_prj_name; double detail; FILE *in_dat_file; FILE *in_hdr_file; FILE *in_prj_file; FILE *out_dat_file; FILE *out_hdr_file; FILE *out_prj_file; int nrows; int ncols; double xmin; double xmax; double ymin; double ymax; double xdim; double ydim; float *data; char *software; enum Terrain_Coord_Type coord_type; int proj_type; int has_nulls; int all_ints; double lat1 = 0.0; // default unless -merc option used double lat2 = 0.0; // default unless -merc option used double center_lat; double temp; int error; printf( "\nTerrain texture shading program - version %s, built %s\n", sw_version, sw_date ); // Validate parameters: // command_name = "TEXTURE"; command_name = get_command_name( argv ); if (argc == 1) { usage_exit( 0 ); } else if (argc < minargs) { usage_exit( "Not enough command-line parameters." ); } argnum = 1; thisarg = argv[argnum++]; if ( strchr( thisarg, '/' ) ) { // read fraction: integer/integer detail = (double)strtol( thisarg, &endptr, 10 ); if (endptr == thisarg || *endptr != '/' || endptr[1] < '1' || endptr[1] > '9') { usage_exit( "First parameter (detail) must be a number or fraction." ); } detail /= (double)strtol( endptr+1, &endptr, 10 ); } else { // read decimal number detail = strtod( thisarg, &endptr ); } if (endptr == thisarg || *endptr != '\0') { usage_exit( "First parameter (detail) must be a number or fraction." ); } software = (char *)malloc( strlen(sw_format) + strlen(sw_name) + strlen(sw_version) + strlen(sw_date) ); if (!software) { prefix_error(); fprintf( stderr, "Memory allocation error occurred.\n" ); exit( EXIT_FAILURE ); } sprintf( software, sw_format, sw_name, sw_version, sw_date ); // Validate filenames and open files: strncpy( extension, "flt", 4 ); get_filenames( argv[argnum++], &in_dat_name, &in_hdr_name, &in_prj_name, extension ); strncpy( extension, "flt", 4 ); get_filenames( argv[argnum++], &out_dat_name, &out_hdr_name, &out_prj_name, extension ); if (!strcmp( in_hdr_name, out_hdr_name )) { usage_exit( "Input and outfile filenames must not be the same." ); } while (argnum < argc) { thisarg = argv[argnum++]; if (*thisarg != '-') { prefix_error(); fprintf( stderr, "Extra command-line parameter '%s' not recognized.\n", thisarg ); usage_exit( 0 ); } ++thisarg; if (strncmp( thisarg, "mercator", 4 ) == 0 || strncmp( thisarg, "Mercator", 4 ) == 0) { if (argnum+1 >= argc) { usage_exit( "Option -mercator must be followed by two numeric latitude values." ); } thisarg = argv[argnum++]; lat1 = strtod( thisarg, &endptr ); if (endptr == thisarg || *endptr != '\0') { usage_exit( "Option -mercator must be followed by two numeric latitude values." ); } thisarg = argv[argnum++]; lat2 = strtod( thisarg, &endptr ); if (endptr == thisarg || *endptr != '\0') { usage_exit( "Option -mercator must be followed by two numeric latitude values." ); } if (lat1 == lat2) { usage_exit( "Min & max mercator latitudes cannot be equal." ); } if (lat1 > lat2) { temp = lat1; lat1 = lat2; lat2 = temp; } if (lat1 <= -90.0 || lat2 >= 90.0) { usage_exit( "Mercator latitude limits must be between -90 and +90 (exclusive)." ); } } else if (strncmp( thisarg, "cellreg", 4 ) == 0 || strncmp( thisarg, "corner", 6 ) == 0) { // ignore flag - cellreg is currently assumed } else if (strncmp( thisarg, "gridreg", 4 ) == 0 || strncmp( thisarg, "center", 6 ) == 0) { fprintf( stderr, "\n" ); fprintf( stderr, "*** WARNING: " ); fprintf( stderr, "Option -%s is not yet implemented.\n", thisarg ); fprintf( stderr, "*** " ); fprintf( stderr, "Treating data as cell-registered (corner-aligned).\n" ); } else { prefix_error(); fprintf( stderr, "Command-line option '-%s' not recognized.\n", thisarg ); usage_exit( 0 ); } } in_hdr_file = fopen( in_hdr_name, "rb" ); // use binary mode for compatibility if (!in_hdr_file) { prefix_error(); fprintf( stderr, "Could not open input file '%s'.\n", in_hdr_name ); usage_exit( 0 ); } in_dat_file = fopen( in_dat_name, "rb" ); if (!in_dat_file) { prefix_error(); fprintf( stderr, "Could not open input file '%s'.\n", in_dat_name ); usage_exit( 0 ); } free( in_dat_name ); free( in_hdr_name ); out_hdr_file = fopen( out_hdr_name, "wb" ); // use binary mode for compatibility if (!out_hdr_file) { prefix_error(); fprintf( stderr, "Could not open output file '%s'.\n", out_hdr_name ); usage_exit( 0 ); } out_dat_file = fopen( out_dat_name, "wb" ); if (!out_dat_file) { prefix_error(); fprintf( stderr, "Could not open output file '%s'.\n", out_dat_name ); usage_exit( 0 ); } free( out_dat_name ); free( out_hdr_name ); // Read .flt and .hdr files: printf( "Reading input files...\n" ); fflush( stdout ); data = read_flt_hdr_files( in_dat_file, in_hdr_file, &nrows, &ncols, &xmin, &xmax, &ymin, &ymax, &has_nulls, &all_ints, 0 ); fclose( in_dat_file ); fclose( in_hdr_file ); if (has_nulls) { fprintf( stderr, "*** WARNING: " ); fprintf( stderr, "Input .flt file contains void (NODATA) points.\n" ); fprintf( stderr, "*** " ); fprintf( stderr, "Assuming these are ocean points - setting these elevations to 0.\n" ); } if (all_ints && detail > 0.0) { fprintf( stderr, "*** WARNING: " ); fprintf( stderr, "Input .flt file appears to contain only integer values.\n" ); fprintf( stderr, "*** " ); fprintf( stderr, "This may degrade the quality of the result.\n" ); } // Process data: xdim = (xmax - xmin) / (double)ncols; ydim = (ymax - ymin) / (double)nrows; // determine projection type proj_type = determine_projection( xmin, xmax, ymin, ymax, xdim, ydim ); if (proj_type < 0) { coord_type = TERRAIN_DEGREES; center_lat = 0.5 * (ymin + ymax); printf( "\nInput data appears to be in lat/lon (geographic) coordinates.\n" ); fflush( stdout ); } else if (proj_type > 0) { coord_type = TERRAIN_METERS; center_lat = 0.0; // ignored when coord_type == TERRAIN_METERS printf( "\nInput data appears to be projected into linear coordinates " ); printf( "(easting/northing).\n" ); fflush( stdout ); } else { prefix_error(); fprintf( stderr, "Unable to determine projection type from info in .hdr file.\n" ); exit( EXIT_FAILURE ); } if (lat1 != lat2) { if (proj_type < 0) { usage_exit( "Option -mercator is invalid for data in geographic coordinates." ); } proj_type = 2; // indicate Mercator projection printf( "Assuming input data is in normal-aspect Mercator projection.\n" ); printf( "Latitude range %.3f deg %c to %.3f deg %c.\n", fabs(lat1), lat1>=0.0 ? 'N' : 'S', fabs(lat2), lat2>=0.0 ? 'N' : 'S' ); printf( "(NOTE: Do NOT use option -mercator with UTM projection.)\n\n" ); } // check pixel aspect ratio and size of map extent check_aspect( xmin, xmax, ymin, ymax, xdim, ydim, proj_type ); if (detail <= 0.0 || detail > 2.0) { fprintf( stderr, "*** WARNING: " ); fprintf( stderr, "Unusual value for detail exponent. Is this correct?\n" ); } printf( "Processing %d column x %d row array using detail = %f...\n", ncols, nrows, detail ); fflush( stdout ); error = terrain_filter( data, detail, nrows, ncols, xdim, ydim, coord_type, center_lat, &progress ); if (error) { assert( error == TERRAIN_FILTER_MALLOC_ERROR ); prefix_error(); fprintf( stderr, "Memory allocation error occurred during processing of data.\n" ); exit( EXIT_FAILURE ); } if (lat1 != lat2) { fix_mercator( data, detail, nrows, ncols, lat1, lat2 ); } // Write .flt and .hdr files: printf( "Writing output files...\n" ); fflush( stdout ); write_flt_hdr_files( out_dat_file, out_hdr_file, nrows, ncols, xmin, xmax, ymin, ymax, data, software ); fclose( out_dat_file ); fclose( out_hdr_file ); free( data ); free( software ); // Copy optional .prj file: in_prj_file = fopen( in_prj_name, "rb" ); // use binary mode for compatibility if (in_prj_file) { out_prj_file = fopen( out_prj_name, "wb" ); // use binary mode for compatibility if (!out_prj_file) { fprintf( stderr, "*** WARNING: " ); fprintf( stderr, "Could not open output file '%s'.\n", out_prj_name ); } else { // copy file and change any "ZUNITS" line to "ZUNITS NO" copy_prj_file( in_prj_file, out_prj_file ); fclose( out_prj_file ); } fclose( in_prj_file ); } free( in_prj_name ); free( out_prj_name ); printf( "DONE.\n" ); return EXIT_SUCCESS; }
bool CmdexSync::execute() { set_error_msg(""); // clear old error if present bool slot_connected = !(signal_execute_tick.slots().begin() == signal_execute_tick.slots().end()); if (slot_connected && !signal_execute_tick.emit(status_starting)) return false; if (!cmdex_.execute()) { // try to execute debug_out_error("app", DBG_FUNC_MSG << "cmdex_.execute() failed.\n"); import_error(); // get error from cmdex and display warnings if needed // emit this for execution loggers cmdex_sync_signal_execute_finish()->emit(CmdexSyncCommandInfo(get_command_name(), get_command_args(), get_stdout_str(), get_stderr_str(), get_error_msg())); if (slot_connected) signal_execute_tick.emit(status_failed); return false; } bool stop_requested = false; // stop requested from tick function bool signals_sent = false; // stop signals sent while(!cmdex_.stopped_cleanup_needed()) { if (!stop_requested) { // running and no stop requested yet // call the tick function with "running" periodically. // if it returns false, try to stop. if (slot_connected && !signal_execute_tick.emit(status_running)) { debug_out_info("app", DBG_FUNC_MSG << "execute_tick slot returned false, trying to stop the program.\n"); stop_requested = true; } } if (stop_requested && !signals_sent) { // stop request received // send the stop request to the command if (!cmdex_.try_stop()) { // try sigterm. this returns false if it can't be done (no permissions, zombie) debug_out_warn("app", DBG_FUNC_MSG << "cmdex_.try_stop() returned false.\n"); } // set sigkill timeout to 3 sec (in case sigterm fails); won't do anything if already exited. cmdex_.set_stop_timeouts(0, forced_kill_timeout_msec); // import_error(); // don't need errors here - they will be available later anyway. signals_sent = true; } // alert the tick function if (stop_requested && slot_connected) { signal_execute_tick.emit(status_stopping); // ignore returned value here } // without this, no event sources will be processed and the program will // hang waiting for the child to exit (the watch handler won't be called). while(g_main_context_pending(NULL)) { g_main_context_iteration(NULL, false); } Glib::usleep(50*1000); // 50 msec. avoids 100% CPU usage. } // command exited, do a cleanup. cmdex_.stopped_cleanup(); import_error(); // get error from cmdex and display warnings if needed // emit this for execution loggers cmdex_sync_signal_execute_finish()->emit(CmdexSyncCommandInfo(get_command_name(), get_command_args(), get_stdout_str(), get_stderr_str(), get_error_msg())); if (slot_connected) signal_execute_tick.emit(status_stopped); // last call return true; }