void tcp_stream_t::set_src_dst_from_packet(const packet_t *packet, bool swap /* construct src/dst inverted stream */) { const layer_t *tcplay = find_top_nondata_layer(packet); if (!tcplay || tcplay->type() != layer_tcp) throw format_exception("expected tcp layer"); const layer_t *iplay = packet->prev(tcplay); while (iplay && iplay->type() != layer_ipv4 && iplay->type() != layer_ipv6) iplay = packet->prev(iplay); if (!iplay) throw format_exception("expected ip layer before tcp layer"); const tcphdr &hdr1 = reinterpret_cast<const tcphdr &>(*tcplay->data()); if (iplay->type() == layer_ipv4) { const ip &hdr2 = reinterpret_cast<const ip &>(*iplay->data()); common_t::set_src_dst4( hdr2.ip_src.s_addr, hdr1.th_sport, hdr2.ip_dst.s_addr, hdr1.th_dport, swap); } else { const ip6_hdr &hdr2 = reinterpret_cast<const ip6_hdr &>(*iplay->data()); assert(iplay->type() == layer_ipv6); common_t::set_src_dst6( hdr2.ip6_src, hdr1.th_sport, hdr2.ip6_dst, hdr1.th_dport, swap); } }
size_t basic_fmt::check_command(std::ostream& formatter) { fmt_command cmd; ostream_string_output output(formatter); size_t cmd_pos = process_fmt(this->fmt_, this->pos_, cmd, output); if( cmd_pos == (size_t)-1 ) throw format_exception("basic_fmt: too many actual arguments"); if( cmd.fill ) formatter.fill(cmd.fill); if( cmd.width ) formatter.width(cmd.width); if( cmd.precision ) formatter.precision(cmd.precision); switch( cmd.command ) { case 's': case 'i': case 'd': std::dec(formatter); break; case 'x': std::hex(formatter); break; default: throw format_exception(fmt("basic_fmt: bad format command %s") % cmd.command); } return cmd_pos; }
/** * Extract the exception error string from the python interpreter. * * This function assumes the gil is acquired. See \ref python_thread_guard. * * Code adapted from http://stackoverflow.com/questions/1418015/how-to-get-python-exception-text */ std::string parse_python_error() { PyObject *exc,*val,*tb; PyErr_Fetch(&exc,&val,&tb); PyErr_NormalizeException(&exc,&val,&tb); python::handle<> hexc(exc),hval(python::allow_null(val)),htb(python::allow_null(tb)); if(!hval) { return python::extract<std::string>(python::str(hexc)); } else { python::object traceback(python::import("traceback")); python::object format_exception(traceback.attr("format_exception")); python::object formatted_list(format_exception(hexc,hval,htb)); python::object formatted(python::str("").join(formatted_list)); return python::extract<std::string>(formatted); } }
static void do_report_exception( const char * const function, openmpt_log_func const logfunc = 0, void * const user = 0, openmpt::module_impl * const impl = 0 ) { const std::string message = format_exception( function ); if ( impl ) { impl->PushToCSoundFileLog( message ); } else if ( logfunc ) { logfunc( message.c_str(), user ); } else { openmpt_log_func_default( message.c_str(), NULL ); } }
/// Adapted from http://stackoverflow.com/a/6576177/1689220 std::string pyerr_to_string(void) { PyObject *exc, *val, *tb; bp::object formatted_list, formatted; PyErr_Fetch(&exc, &val, &tb); // wrap exception, value, traceback with bp::handle for auto memory management bp::handle<> hexc(exc), hval(bp::allow_null(val)), htb(bp::allow_null(tb)); // import "traceback" module bp::object traceback(bp::import("traceback")); if (!tb) { bp::object format_exception_only(traceback.attr("format_exception_only")); formatted_list = format_exception_only(hexc, hval); } else { bp::object format_exception(traceback.attr("format_exception")); formatted_list = format_exception(hexc, hval, htb); } formatted = bp::str("\n").join(formatted_list); return bp::extract<std::string>(formatted); }
void basic_fmt::realize() { fmt_command cmd; std::ostream formatter(this->buf_); ostream_string_output output(formatter); if( process_fmt(this->fmt_, this->pos_, cmd, output) != (size_t)-1 ) throw format_exception("basic_fmt: not all arguments realized"); this->pos_ = fmt_.size(); }
int main(int argc, char *argv[]) try { std::vector<std::string> positional; bool live = false; std::string filter; uint32_t every = 5*60; for (int n=1; n<argc; ++n) { std::string arg = argv[n]; bool havenext = n+1 < argc; if (havenext && (arg == "--bpf" || arg == "--filter")) { filter = argv[n+1]; ++n; } else if (havenext && (arg == "--every" || arg == "-e")) { every = boost::lexical_cast<uint32_t>(argv[n+1]); ++n; } else if (arg == "--live") live = true; else if (arg == "-h" or arg == "--help") { printhelp(argv[0]); return -1; } else positional.push_back(arg); } if (live && positional.size()>1) throw format_exception("can only do live capture on one device (use 'any' for all)"); if (!live && positional.empty()) throw format_exception("need at least one pcap file"); my_packet_listener_t listener(every); pcap_reader_t reader(&listener); if (!live) { BOOST_FOREACH(const std::string &file, positional) reader.read_file(file, filter); } else {
/* * the::system::net::ip_address4::from_dotted_string */ the::system::net::ip_address4 the::system::net::ip_address4::from_dotted_string(const char *address) { THE_STACK_TRACE; ip_address4 retval; if (address == NULL) { throw argument_null_exception("address", __FILE__, __LINE__); } if ((retval.address.s_addr = ::inet_addr(address)) == INADDR_NONE) { throw format_exception("The specified string does not represent a " "valid IP address.", __FILE__, __LINE__); } return retval; }
std::string format_exception() { return format_exception( get_exception_info()); }
void accept_error(packet_t *packet, const char *error) { if (strncmp(error, "unsupported protocol ", 21) != 0) throw format_exception("error parsing packet '%s': %s", to_str(*packet).c_str(), error); }
size_t process_fmt(std::string fmt, size_t start, fmt_command& result, T& output) { std::string::const_iterator istart = fmt.begin() + start; std::string::const_iterator c = istart; const std::string::const_iterator end = fmt.end(); result.command = '?'; result.width = 0; result.precision = 0; result.fill = ' '; //std::cout << "pf: " << fmt << " at " << start << std::endl; enum { FREE_TEXT, BEGIN_MATCH, PADDING_GIVEN, } state = FREE_TEXT; bool in_precision = false; while( c != end ) switch( state ) { case FREE_TEXT: if( *c == '%' ) { if( c+1 == end ) throw format_exception("bad format: '%' at the end"); state = BEGIN_MATCH; in_precision = false; output.append(istart, c); ++c; } else { ++c; } continue; case BEGIN_MATCH: if( *c == '%' ) { ++c; istart = c; output.append(1,'%'); state = FREE_TEXT; continue; // %% sequence is an escape } if( *c == '0' ) { result.fill = '0'; state = PADDING_GIVEN; ++c; continue; } case PADDING_GIVEN: if( *c == '.' ) { in_precision = true; ++c; continue; } if( std::isdigit(*c) ) { if( in_precision ) { result.precision = result.precision*10 + (*c - '0'); } else { result.width = result.width*10 + (*c - '0'); } ++c; continue; } if( std::isalpha(*c) ) { result.command = *c; ++c; state = FREE_TEXT; return c - fmt.begin(); } throw format_exception("basic_fmt: bad format character"); } //std::cout << "pf: " << -1 << std::endl; output.append(istart, end); return (size_t)-1; }