#include <stdio.h>#include <stdlib.h>#include <string.h>Include dependency graph for util.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Classes | |
| struct | ltstr |
Defines | |
| #define | myattribute(x) |
| #define | ASSERT(x) |
| #define | DEBUG_MSG(x...) |
| #define | DEBUG_fputs(x...) |
| #define | padded_sizeof(x) (pad_size(sizeof(x))) |
Typedefs | |
| typedef unsigned int | uint32 |
| typedef unsigned short | uint16 |
| typedef unsigned char | uint8 |
Functions | |
| char * | copy_string (const char *s) |
| int | streq (const char *s1, const char *s2) |
| int | expand_escape (const char *&s) |
| char * | skip_whitespace (char *s) |
| const char * | skip_whitespace (const char *s) |
| char * | skip_whitespace (char *s, char *end_of_s) |
| const char * | skip_whitespace (const char *s, const char *end_of_s) |
| char * | skip_digits (char *s) |
| char * | get_word (char *&s) |
| void | get_word (int length, const char *s, int &pwlen, const char *&pw) |
| void | to_upper (char *s) |
| const char * | strchr_n (const char *s, const char *end_of_s, char ch) |
| const char * | strrchr_n (const char *s, const char *end_of_s, char ch) |
| int | decode_hex (char ch) |
| unsigned char | encode_hex (int h) |
| int | strcasecmp_n (int s_len, const char *s, const char *t) |
| const char * | strpbrk_n (size_t len, const char *s, const char *charset) |
| int | atoi_n (int len, const char *s, const char **end, int base, int &result) |
| int | fputs (int len, const char *s, FILE *fp) |
| bool | is_printable (const char *s, int len) |
| const char * | fmt (const char *format,...) myattribute((format(printf |
| void | fmt (int &len, const char *&b, const char *format,...) myattribute((format(printf |
| const char * | fmt_access_time (double time) |
| void | hash_md5 (size_t size, const unsigned char *bytes, unsigned char digest[16]) |
| void | hmac_md5 (size_t size, const unsigned char *bytes, unsigned char digest[16]) |
| const char * | md5_digest_print (const unsigned char digest[16]) |
| void | init_random_seed () |
| void | message (const char *msg) |
| void | warn (const char *msg) |
| void | warn (const char *msg, const char *addl) |
| void | error (const char *msg) |
| void | error (const char *msg, const char *addl) |
| void | error (const char *msg, uint32 addl) |
| void | run_time (const char *msg) |
| void | run_time (const char *fmt, BroObj *obj) |
| void | run_time (const char *fmt, const char *arg) |
| void | run_time (const char *fmt, const char *arg1, const char *arg2) |
| void | internal_error (const char *fmt,...) myattribute((volatile |
| void | format (printf, 1, 2))) |
| void | pinpoint () |
| int | int_list_cmp (const void *v1, const void *v2) |
| const char * | bro_path () |
| const char * | bro_prefixes () |
| FILE * | search_for_file (const char *filename, const char *ext, const char **full_filename) |
| double | current_time () |
| int | min (int a, int b) |
| int | max (int a, int b) |
| size_t | pad_size (size_t size) |
| void | out_of_memory (const char *where) |
| void * | safe_realloc (void *ptr, size_t size) |
| void * | safe_malloc (size_t size) |
Variables | |
| uint8 | shared_hmac_md5_key [16] |
| int | hmac_key_set |
| double | network_time |
|
|
|
Definition at line 47 of file util.h. Referenced by MIME_Entity::DebugPrintHeaders(). |
|
|
|
|
|
|
|
|
|
||||||||||||||||||||||||
|
Definition at line 278 of file util.cc. Referenced by HTTP_Entity::Deliver(), and HTTP_Entity::SubmitHeader().
00279 {
00280 int n = 0;
00281 int neg = 0;
00282
00283 if ( len > 0 && *s == '-' )
00284 {
00285 neg = 1;
00286 --len; ++s;
00287 }
00288
00289 int i;
00290 for ( i = 0; i < len; ++i )
00291 {
00292 unsigned int d;
00293
00294 if ( isdigit(s[i]) )
00295 d = s[i] - '0';
00296
00297 else if ( s[i] >= 'a' && s[i] < 'a' - 10 + base )
00298 d = s[i] - 'a' + 10;
00299
00300 else if ( s[i] >= 'A' && s[i] < 'A' - 10 + base )
00301 d = s[i] - 'A' + 10;
00302
00303 else if ( i > 0 )
00304 break;
00305
00306 else
00307 return 0;
00308
00309 n = n * base + d;
00310 }
00311
00312 if ( neg )
00313 result = -n;
00314 else
00315 result = n;
00316
00317 if ( end )
00318 *end = s + i;
00319
00320 return 1;
00321 }
|
|
|
Definition at line 590 of file util.cc. Referenced by search_for_file(), and usage().
00591 {
00592 const char* path = getenv("BROPATH");
00593 if ( ! path )
00594 path = ".:policy:policy/local:policy/sigs:" LIBDEST;
00595
00596 return path;
00597 }
|
|
|
Definition at line 599 of file util.cc. References loop_over_list, and prefixes. Referenced by usage().
00600 {
00601 int len = 1; // room for \0
00602 loop_over_list(prefixes, i)
00603 len += strlen(prefixes[i]) + 1;
00604
00605 char* p = new char[len];
00606
00607 loop_over_list(prefixes, j)
00608 if ( j == 0 )
00609 strcpy(p, prefixes[j]);
00610 else
00611 {
00612 strcat(p, ":");
00613 strcat(p, prefixes[j]);
00614 }
00615
00616 return p;
00617 }
|
|
|
|
Definition at line 667 of file util.cc. References double, and internal_error(). Referenced by ChunkedIOFd::ChunkedIOFd(), dispatch_next_packet(), ChunkedIOFd::FlushWriteBuffer(), DNS_Mapping::Init(), init_builtin_funcs(), StatsLogger::Log(), ChunkedIOFd::OptionalFlush(), and watchdog().
00668 {
00669 struct timeval tv;
00670 if ( gettimeofday(&tv, 0) < 0 )
00671 internal_error("gettimeofday failed in current_time()");
00672
00673 return double(tv.tv_sec) + double(tv.tv_usec) / 1e6;
00674 }
|
|
|
Definition at line 216 of file util.cc. Referenced by MIME_Entity::DecodeQuotedPrintable(), and unescape_URI().
00217 {
00218 if ( ch >= '0' && ch <= '9' )
00219 return ch - '0';
00220
00221 if ( ch >= 'A' && ch <= 'F' )
00222 return ch - 'A' + 10;
00223
00224 if ( ch >= 'a' && ch <= 'f' )
00225 return ch - 'a' + 10;
00226
00227 return -1;
00228 }
|
|
|
Definition at line 230 of file util.cc. References internal_error(). Referenced by escape_URI_char().
00231 {
00232 static const char hex[16] = {
00233 '0', '1', '2', '3', '4', '5', '6', '7', '8',
00234 '9', 'A', 'B', 'C', 'D', 'E', 'F'
00235 };
00236
00237 if ( h < 0 || h >= 16 )
00238 {
00239 internal_error("illegal value for encode_hex: %d", h);
00240 return 'X';
00241 }
00242
00243 return hex[h];
00244 }
|
|
||||||||||||
|
Definition at line 510 of file util.cc. References nerr, pinpoint(), and uint32. Referenced by AddrVal::AddrVal(), BroFile::BroFile(), ChunkedIOFd::ChunkedIOFd(), dotted_to_addr(), RecordType::Init(), RemoteSerializer::Init(), BroFile::InitEncrypt(), is_assignable(), load_mapping_table(), lookup_ID(), mask_addr(), NetVal::NetVal(), parse_dotted(), BinarySerializationFormat::Read(), SerializationFormat::ReadData(), RuleMatcher::ReadFiles(), same_stmt(), same_type(), and TypeDecl::TypeDecl().
|
|
||||||||||||
|
Definition at line 503 of file util.cc. References nerr, and pinpoint().
|
|
|
Definition at line 496 of file util.cc. References nerr, and pinpoint().
|
|
|
Definition at line 62 of file util.cc. References warn().
00063 {
00064 switch ( *(s++) ) {
00065 case 'b': return '\b';
00066 case 'f': return '\f';
00067 case 'n': return '\n';
00068 case 'r': return '\r';
00069 case 't': return '\t';
00070 case 'a': return '\a';
00071 case 'v': return '\v';
00072
00073 case '0': case '1': case '2': case '3': case '4':
00074 case '5': case '6': case '7':
00075 { // <octal>{1,3}
00076 --s; // put back the first octal digit
00077 const char* start = s;
00078
00079 // Don't increment inside loop control
00080 // because if isdigit() is a macro it might
00081 // expand into multiple increments ...
00082
00083 // Here we define a maximum length for escape sequence
00084 // to allow easy handling of string like: "^H0" as
00085 // "\0100".
00086
00087 for ( int len = 0; len < 3 && isascii(*s) && isdigit(*s);
00088 ++s, ++len)
00089 ;
00090
00091 int result;
00092 if ( sscanf(start, "%3o", &result) != 1 )
00093 {
00094 warn("bad octal escape: ", start);
00095 result = 0;
00096 }
00097
00098 return result;
00099 }
00100
00101 case 'x':
00102 { /* \x<hex> */
00103 const char* start = s;
00104
00105 // Look at most 2 characters, so that "\x0ddir" -> "^Mdir".
00106 for ( int len = 0; len < 2 && isascii(*s) && isxdigit(*s);
00107 ++s, ++len);
00108
00109 int result;
00110 if ( sscanf(start, "%2x", &result) != 1 )
00111 {
00112 warn("bad hexadecimal escape: ", start);
00113 result = 0;
00114 }
00115
00116 return result;
00117 }
00118
00119 default:
00120 return s[-1];
00121 }
00122 }
|
|
||||||||||||||||||||
|
||||||||||||
|
|
|
|
Definition at line 366 of file util.cc. Referenced by Stmt::AccessStats(), and TableVal::Describe().
00367 {
00368 static char buf[256];
00369 time_t time = (time_t) t;
00370 strftime(buf, sizeof(buf), "%d/%m-%H:%M", localtime(&time));
00371 return buf;
00372 }
|
|
||||||||||||||||
|
|
|
||||||||||||||||
|
Definition at line 323 of file util.cc. Referenced by cryptcat(), doone(), main(), Rule::PrintDebug(), and Serializer::Warning().
00324 {
00325 for ( int i = 0; i < len; ++i )
00326 if ( fputc(s[i], fp) == EOF )
00327 return EOF;
00328 return 0;
00329 }
|
|
||||||||||||||||||||
|
Definition at line 174 of file util.cc. Referenced by FTP_Conn::NewLine().
00175 {
00176 pw = s;
00177
00178 int len = 0;
00179 while ( len < length && *s && ! isspace(*s) )
00180 {
00181 ++s;
00182 ++len;
00183 }
00184
00185 pwlen = len;
00186 }
|
|
|
Definition at line 159 of file util.cc. References skip_whitespace().
00160 {
00161 char* w = s;
00162 while ( *s && ! isspace(*s) )
00163 ++s;
00164
00165 if ( *s )
00166 {
00167 *s = '\0'; // terminate the word
00168 s = skip_whitespace(s+1);
00169 }
00170
00171 return w;
00172 }
|
|
||||||||||||||||
|
Definition at line 374 of file util.cc. References md5_append(), md5_finish(), and md5_init(). Referenced by hmac_md5(), init_random_seed(), and main().
00375 {
00376 md5_state_s h;
00377 md5_init(&h);
00378 md5_append(&h, bytes, size);
00379 md5_finish(&h, digest);
00380 }
|
|
||||||||||||||||
|
Definition at line 399 of file util.cc. References hash_md5(), hmac_key_set, internal_error(), and shared_hmac_md5_key. Referenced by AnonymizeIPAddr_PrefixMD5::anonymize(), AnonymizeIPAddr_RandomMD5::anonymize(), HashKey::HashBytes(), and init_random_seed().
00400 {
00401 if ( ! hmac_key_set )
00402 internal_error("HMAC-MD5 invoked before the HMAC key is set");
00403
00404 hash_md5(size, bytes, digest);
00405
00406 for ( int i = 0; i < 16; ++i )
00407 digest[i] ^= shared_hmac_md5_key[i];
00408
00409 hash_md5(16, digest, digest);
00410 }
|
|
|
Definition at line 412 of file util.cc. References hash_md5(), hmac_key_set, hmac_md5(), internal_error(), shared_hmac_md5_key, and uint32. Referenced by main().
00413 {
00414 static const int bufsiz = 16;
00415 uint32 buf[bufsiz];
00416 int pos = 0; // accumulates entropy
00417
00418 // Gather up some entropy.
00419 gettimeofday((struct timeval *)(buf + pos), 0);
00420 pos += sizeof(struct timeval) / sizeof(uint32);
00421
00422 #if defined(O_NONBLOCK)
00423 int fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
00424 #elif defined(O_NDELAY)
00425 int fd = open("/dev/random", O_RDONLY | O_NDELAY);
00426 #else
00427 int fd = open("/dev/random", O_RDONLY);
00428 #endif
00429
00430 if ( fd >= 0 )
00431 {
00432 int amt = read(fd, buf + pos, sizeof(uint32) * (bufsiz - pos));
00433 close(fd);
00434
00435 if ( amt > 0 )
00436 pos += (amt / sizeof(uint32));
00437 }
00438
00439 if ( pos < bufsiz )
00440 {
00441 buf[pos++] = getpid();
00442
00443 if ( pos < bufsiz )
00444 buf[pos++] = getuid();
00445 }
00446
00447 uint32 result = 0;
00448 for ( int i = 0; i < pos; ++i )
00449 {
00450 result ^= buf[i];
00451 result = (result << 1) | (result >> 31);
00452 }
00453 srandom(result);
00454
00455 if ( ! hmac_key_set )
00456 {
00457 hash_md5(sizeof(buf), (u_char*) buf, shared_hmac_md5_key);
00458 hmac_key_set = 1;
00459 }
00460
00461 #ifdef USE_UHASH
00462 if ( UHASH_KEY_SIZE % 16 != 0 )
00463 internal_error("uhash key size must be multiply of 16");
00464
00465 uint32 buf2[5];
00466 hash_md5(sizeof(buf), (const u_char*) buf, (u_char*) buf2);
00467
00468 for ( int i = 0; i < UHASH_KEY_SIZE; i += 16 )
00469 {
00470 buf2[4] = i;
00471 hmac_md5(sizeof(buf2), (const unsigned char*) buf2, uhash_key + i);
00472 }
00473 #endif
00474 }
|
|
||||||||||||
|
Definition at line 577 of file util.cc. Referenced by CCL::Sort(), and DFA_Machine::StateSetToDFA_State().
00578 {
00579 int i1 = *(int *) v1;
00580 int i2 = *(int *) v2;
00581
00582 if ( i1 < i2 )
00583 return -1;
00584 else if ( i1 == i2 )
00585 return 0;
00586 else
00587 return 1;
00588 }
|
|
||||||||||||
|
||||||||||||
|
Definition at line 331 of file util.cc. Referenced by Connection::UnparsedVersionFoundEvent(), and Connection::VersionFoundEvent().
00332 {
00333 while ( --len >= 0 )
00334 if ( ! isprint(*s++) )
00335 return false;
00336 return true;
00337 }
|
|
||||||||||||
|
Definition at line 145 of file util.h. Referenced by Discarder::BuildData(), and Reassembler::TrimToSeq().
00146 {
00147 return a > b ? a : b;
00148 }
|
|
|
Definition at line 382 of file util.cc. Referenced by main().
00383 {
00384 static char digest_print[256];
00385
00386 for ( int i = 0; i < 16; ++i )
00387 snprintf(digest_print + i * 2, 3, "%02x", digest[i]);
00388
00389 return digest_print;
00390 }
|
|
|
Definition at line 476 of file util.cc. References pinpoint(). Referenced by termination_signal().
00477 {
00478 pinpoint();
00479 fprintf(stderr, "%s\n", msg);
00480 }
|
|
||||||||||||
|
Definition at line 140 of file util.h. Referenced by Reassembler::AddAndCheck(), Bstr_cmp(), Discarder::BuildData(), NetSessions::DoNextPacket(), DNS_Mgr::Resolve(), GnutellaConn::SendEvents(), BroFile::Write(), and ChunkedIOFd::Write().
00141 {
00142 return a < b ? a : b;
00143 }
|
|
|
Definition at line 676 of file util.cc. Referenced by bro_new_handler(), safe_malloc(), and safe_realloc().
00677 {
00678 fprintf( stderr, "bro: out of memory in %s.\n", where );
00679 abort();
00680 }
|
|
|
Definition at line 172 of file util.h. Referenced by DataBlock::DataBlock(), DFA_State_Cache::GetStats(), AddrVal::MemoryAllocation(), NetSessions::MemoryAllocation(), Specific_RE_Matcher::MemoryAllocation(), BaseList::MemoryAllocation(), HashKey::MemoryAllocation(), Dictionary::MemoryAllocation(), CompositeHash::MemoryAllocation(), BroString::MemoryAllocation(), EquivClass::Size(), DFA_State::Size(), and DataBlock::~DataBlock().
00173 {
00174 // We emulate glibc here (values measured on Linux i386).
00175 // FIXME: We should better copy the portable value definitions from glibc.
00176 if ( size == 0 )
00177 return 0; // glibc allocated 16 bytes anyway.
00178
00179 const int pad = 8;
00180 if ( size < 12 )
00181 return 2 * pad;
00182
00183 return ((size+3) / pad + 1) * pad;
00184 }
|
|
|
Definition at line 565 of file util.cc. References filename, line_number, and network_time. Referenced by error(), internal_error(), message(), run_time(), and warn().
00566 {
00567 if ( network_time > 0.0 )
00568 fprintf(stderr, "%.6f ", network_time);
00569 else
00570 {
00571 if ( filename )
00572 fprintf(stderr, "%s, ", filename);
00573 fprintf(stderr, "line %d: ", line_number);
00574 }
00575 }
|
|
||||||||||||||||
|
||||||||||||
|
Definition at line 531 of file util.cc. References fmt, nruntime, and pinpoint().
|
|
||||||||||||
|
Definition at line 524 of file util.cc. References BroObj::Describe(), ODesc::Description(), fmt, and run_time().
00525 {
00526 ODesc d;
00527 obj->Describe(&d);
00528 run_time(fmt, d.Description());
00529 }
|
|
|
Definition at line 517 of file util.cc. References nruntime, and pinpoint().
|
|
|
Definition at line 202 of file util.h. References out_of_memory(). Referenced by BaseList::BaseList(), dbg_handle_debug_input(), ODesc::ODesc(), and BaseList::operator=().
00203 {
00204 void* ptr = malloc(size);
00205 if ( ! ptr )
00206 out_of_memory("malloc");
00207
00208 return ptr;
00209 }
|
|
||||||||||||
|
Definition at line 190 of file util.h. References out_of_memory(). Referenced by ODesc::Grow(), and BaseList::resize().
00191 {
00192 if ( ! size )
00193 return 0;
00194
00195 ptr = realloc(ptr, size);
00196 if ( ! ptr )
00197 out_of_memory("realloc");
00198
00199 return ptr;
00200 }
|
|
||||||||||||||||
|
Definition at line 627 of file util.cc. References bro_path(), copy_string(), filename, and open_file(). Referenced by parse_location_string(), and RuleMatcher::ReadFiles().
00629 {
00630 if ( filename[0] == '/' || filename[0] == '.' )
00631 return open_file(filename, full_filename);
00632
00633 char path[1024], full_filename_buf[1024];
00634 strcpy(path, bro_path());
00635
00636 char* dir_beginning = path;
00637 char* dir_ending = path;
00638 int more = *dir_beginning != '\0';
00639
00640 while ( more )
00641 {
00642 while ( *dir_ending && *dir_ending != ':' )
00643 ++dir_ending;
00644
00645 if ( *dir_ending == ':' )
00646 *dir_ending = '\0';
00647 else
00648 more = 0;
00649
00650 sprintf(full_filename_buf, "%s/%s", dir_beginning, filename);
00651 if ( access(full_filename_buf, R_OK) == 0 )
00652 return open_file(full_filename_buf, full_filename);
00653
00654 sprintf(full_filename_buf, "%s/%s.%s", dir_beginning, filename, ext);
00655 if ( access(full_filename_buf, R_OK) == 0 )
00656 return open_file(full_filename_buf, full_filename);
00657
00658 dir_beginning = ++dir_ending;
00659 }
00660
00661 if ( full_filename )
00662 *full_filename = copy_string(filename);
00663
00664 return 0;
00665 }
|
|
|
Definition at line 152 of file util.cc.
00153 {
00154 while ( *s && isdigit(*s) )
00155 ++s;
00156 return s;
00157 }
|
|
||||||||||||
|
Definition at line 145 of file util.cc. Referenced by get_word(), HTTP_Conn::HTTP_ReplyLine(), IdentConn::NewLine(), FTP_Conn::NewLine(), FingerConn::NewLine(), IdentConn::ParseInt(), HTTP_Conn::ParseRequest(), and HTTP_Conn::PrefixWordMatch().
00146 {
00147 while ( s < end_of_s && (*s == ' ' || *s == '\t') )
00148 ++s;
00149 return s;
00150 }
|
|
||||||||||||
|
Definition at line 138 of file util.cc.
00139 {
00140 while ( s < end_of_s && (*s == ' ' || *s == '\t') )
00141 ++s;
00142 return s;
00143 }
|
|
|
Definition at line 131 of file util.cc.
00132 {
00133 while ( *s == ' ' || *s == '\t' )
00134 ++s;
00135 return s;
00136 }
|
|
|
Definition at line 124 of file util.cc.
00125 {
00126 while ( *s == ' ' || *s == '\t' )
00127 ++s;
00128 return s;
00129 }
|
|
||||||||||||||||
|
Definition at line 257 of file util.cc. Referenced by HTTP_Conn::ExpectReplyMessageBody(), HTTP_Conn::HTTP_Header(), MIME_Entity::LookupMIMEHeaderName(), MIME_Entity::ParseContentEncoding(), MIME_Entity::ParseContentType(), MIME_Entity::ParseParameter(), strcasecmp_n(), and HTTP_Entity::SubmitHeader().
00258 {
00259 if ( ! b )
00260 return -1;
00261
00262 int i;
00263 for ( i = 0; i < b_len; ++i )
00264 {
00265 char c1 = islower(b[i]) ? toupper(b[i]) : b[i];
00266 char c2 = islower(t[i]) ? toupper(t[i]) : t[i];
00267
00268 if ( c1 < c2 )
00269 return -1;
00270
00271 if ( c1 > c2 )
00272 return 1;
00273 }
00274
00275 return t[i] != '\0';
00276 }
|
|
||||||||||||||||
|
Definition at line 198 of file util.cc. Referenced by IdentConn::NewLine(), and FingerConn::NewLine().
00199 {
00200 for ( ; s < end_of_s; ++s )
00201 if ( *s == ch )
00202 return s;
00203
00204 return 0;
00205 }
|
|
||||||||||||
|
Definition at line 57 of file util.cc. Referenced by dbg_execute_command(), PktSrc::DumpCurrentPacket(), FieldExpr::FieldExpr(), RecordType::FieldOffset(), load_mapping_table(), lookup_global_symbols_regex(), make_full_var_name(), LoginConn::MatchesTypeahead(), merge_types(), same_type(), LoginConn::SetEnv(), BroObj::SetLocationInfo(), and TraceState::SetTraceFile().
00058 {
00059 return ! strcmp(s1, s2);
00060 }
|
|
||||||||||||||||
|
Definition at line 248 of file util.cc.
00249 {
00250 for ( const char* p = s; p < s + len; ++p )
00251 if ( strchr(charset, *p) )
00252 return p;
00253
00254 return 0;
00255 }
|
|
||||||||||||||||
|
Definition at line 207 of file util.cc.
00208 {
00209 for ( --end_of_s; end_of_s >= s; --end_of_s )
00210 if ( *end_of_s == ch )
00211 return end_of_s;
00212
00213 return 0;
00214 }
|
|
|
Definition at line 188 of file util.cc.
00189 {
00190 while ( *s )
00191 {
00192 if ( islower(*s) )
00193 *s = toupper(*s);
00194 ++s;
00195 }
00196 }
|
|
||||||||||||
|
|
Definition at line 482 of file util.cc. References nwarn, and pinpoint().
|
|
|
Definition at line 97 of file util.h. Referenced by hmac_md5(), init_random_seed(), and main(). |
|
|
|
Definition at line 93 of file util.h. Referenced by hmac_md5(), init_random_seed(), and main(). |
1.3.6