Ejemplo n.º 1
0
int main (int argc, char **argv)
{
	const char *infile, *outfile;
	FILE *out, *in;
	int opt;
	poptContext pc;
	char buffer[4096];
	long data_offset, data_length;
	long data_bytes_read;
	int in_packet = 0;
	struct poptOption long_options[] = {
		POPT_AUTOHELP
		{ "quiet", 'q', POPT_ARG_NONE, &quiet, 0, "Be quiet, don't output warnings" },
		{ "hex", 'h', POPT_ARG_NONE, &hexformat, 0, "Output format readable by text2pcap" },
		POPT_TABLEEND
	};
	
	pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
			    POPT_CONTEXT_KEEP_FIRST);
	poptSetOtherOptionHelp(pc, "[<infile> [<outfile>]]");
	
	
	while((opt = poptGetNextOpt(pc)) != -1) {
		switch (opt) {
		}
	}

	poptGetArg(pc); /* Drop argv[0], the program name */

	infile = poptGetArg(pc);

	if(infile) {
		in  = fopen(infile, "r");
		if(!in) {
			perror("fopen");
			return 1;
		}
	} else in = stdin;
	
	outfile = poptGetArg(pc);

	if(outfile) {
		out = fopen(outfile, "w+");
		if(!out) { 
			perror("fopen"); 
			fprintf(stderr, "Can't find %s, using stdout...\n", outfile);
		}
	}

	if(!outfile) out = stdout;

	if(!hexformat)print_pcap_header(out);

	while(!feof(in)) {
		fgets(buffer, sizeof(buffer), in);
		if(buffer[0] == '[') { /* Header */
			if(strstr(buffer, "show_msg")) {
				in_packet++;
				if(in_packet == 1)continue;
				read_log_msg(in, &curpacket, &curpacket_len, &data_offset, &data_length);
			} else if(in_packet && strstr(buffer, "dump_data")) {
				data_bytes_read = read_log_data(in, curpacket+data_offset, data_length);
			}  else { 
				if(in_packet){ 
					if(hexformat) print_hex_packet(out, curpacket, curpacket_len); 
					else print_netbios_packet(out, curpacket, curpacket_len, data_bytes_read+data_offset);
					free(curpacket); 
				}
				in_packet = 0;
			}
		} 
	}

	return 0;
}
Ejemplo n.º 2
0
static void logging_format(void)
{
    const char *log_file_path = "/tmp/Xorg-logging-test.log";
    const char *str = "%s %d %u %% %p %i";
    char buf[1024];
    int i;
    unsigned int ui;
    long li;
    unsigned long lui;
    FILE *f;
    char read_buf[2048];
    char *logmsg;
    uintptr_t ptr;

    /* set up buf to contain ".....end" */
    memset(buf, '.', sizeof(buf));
    strcpy(&buf[sizeof(buf) - 4], "end");

    LogInit(log_file_path, NULL);
    assert(f = fopen(log_file_path, "r"));

#define read_log_msg(msg) \
    fgets(read_buf, sizeof(read_buf), f); \
    msg = strchr(read_buf, ']') + 2; /* advance past [time.stamp] */

    /* boring test message */
    LogMessageVerbSigSafe(X_ERROR, -1, "test message\n");
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) test message\n") == 0);

    /* long buf is truncated to "....en\n" */
    LogMessageVerbSigSafe(X_ERROR, -1, buf);
    read_log_msg(logmsg);
    assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);

    /* same thing, this time as string substitution */
    LogMessageVerbSigSafe(X_ERROR, -1, "%s", buf);
    read_log_msg(logmsg);
    assert(strcmp(&logmsg[strlen(logmsg) - 3], "en\n") == 0);

    /* strings containing placeholders should just work */
    LogMessageVerbSigSafe(X_ERROR, -1, "%s\n", str);
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) %s %d %u %% %p %i\n") == 0);

    /* literal % */
    LogMessageVerbSigSafe(X_ERROR, -1, "test %%\n");
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) test %\n") == 0);

    /* character */
    LogMessageVerbSigSafe(X_ERROR, -1, "test %c\n", 'a');
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) test a\n") == 0);

    /* something unsupported % */
    LogMessageVerbSigSafe(X_ERROR, -1, "test %Q\n");
    read_log_msg(logmsg);
    assert(strstr(logmsg, "BUG") != NULL);
    LogMessageVerbSigSafe(X_ERROR, -1, "\n");
    fseek(f, 0, SEEK_END);

    /* string substitution */
    LogMessageVerbSigSafe(X_ERROR, -1, "%s\n", "substituted string");
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) substituted string\n") == 0);

    /* Invalid format */
    LogMessageVerbSigSafe(X_ERROR, -1, "%4", 4);
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) ") == 0);
    LogMessageVerbSigSafe(X_ERROR, -1, "\n");
    fseek(f, 0, SEEK_END);

    /* %hld is bogus */
    LogMessageVerbSigSafe(X_ERROR, -1, "%hld\n", 4);
    read_log_msg(logmsg);
    assert(strstr(logmsg, "BUG") != NULL);
    LogMessageVerbSigSafe(X_ERROR, -1, "\n");
    fseek(f, 0, SEEK_END);

    /* number substitution */
    ui = 0;
    do {
        char expected[30];
        sprintf(expected, "(EE) %u\n", ui);
        LogMessageVerbSigSafe(X_ERROR, -1, "%u\n", ui);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %x\n", ui);
        LogMessageVerbSigSafe(X_ERROR, -1, "%x\n", ui);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        if (ui == 0)
            ui = 1;
        else
            ui <<= 1;
    } while(ui);

    lui = 0;
    do {
        char expected[30];
        sprintf(expected, "(EE) %lu\n", lui);
        LogMessageVerbSigSafe(X_ERROR, -1, "%lu\n", lui);
        read_log_msg(logmsg);

        sprintf(expected, "(EE) %lld\n", (unsigned long long)ui);
        LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (unsigned long long)ui);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %lx\n", lui);
        printf("%s\n", expected);
        LogMessageVerbSigSafe(X_ERROR, -1, "%lx\n", lui);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %llx\n", (unsigned long long)ui);
        LogMessageVerbSigSafe(X_ERROR, -1, "%llx\n", (unsigned long long)ui);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        if (lui == 0)
            lui = 1;
        else
            lui <<= 1;
    } while(lui);

    /* signed number substitution */
    i = 0;
    do {
        char expected[30];
        sprintf(expected, "(EE) %d\n", i);
        LogMessageVerbSigSafe(X_ERROR, -1, "%d\n", i);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %d\n", i | INT_MIN);
        LogMessageVerbSigSafe(X_ERROR, -1, "%d\n", i | INT_MIN);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        if (i == 0)
            i = 1;
        else
            i <<= 1;
    } while(i > INT_MIN);

    li = 0;
    do {
        char expected[30];
        sprintf(expected, "(EE) %ld\n", li);
        LogMessageVerbSigSafe(X_ERROR, -1, "%ld\n", li);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %ld\n", li | LONG_MIN);
        LogMessageVerbSigSafe(X_ERROR, -1, "%ld\n", li | LONG_MIN);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %lld\n", (long long)li);
        LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (long long)li);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        sprintf(expected, "(EE) %lld\n", (long long)(li | LONG_MIN));
        LogMessageVerbSigSafe(X_ERROR, -1, "%lld\n", (long long)(li | LONG_MIN));
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        if (li == 0)
            li = 1;
        else
            li <<= 1;
    } while(li > LONG_MIN);


    /* pointer substitution */
    /* we print a null-pointer differently to printf */
    LogMessageVerbSigSafe(X_ERROR, -1, "%p\n", NULL);
    read_log_msg(logmsg);
    assert(strcmp(logmsg, "(EE) 0x0\n") == 0);

    ptr = 1;
    do {
        char expected[30];
#ifdef __sun /* Solaris doesn't autoadd "0x" to %p format */
        sprintf(expected, "(EE) 0x%p\n", (void*)ptr);
#else
        sprintf(expected, "(EE) %p\n", (void*)ptr);
#endif
        LogMessageVerbSigSafe(X_ERROR, -1, "%p\n", (void*)ptr);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);
        ptr <<= 1;
    } while(ptr);


    for (i = 0; i < sizeof(float_tests)/sizeof(float_tests[0]); i++) {
        double d = float_tests[i];
        char expected[30];
        sprintf(expected, "(EE) %.2f\n", d);
        LogMessageVerbSigSafe(X_ERROR, -1, "%f\n", d);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        /* test for length modifiers, we just ignore them atm */
        LogMessageVerbSigSafe(X_ERROR, -1, "%.3f\n", d);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        LogMessageVerbSigSafe(X_ERROR, -1, "%3f\n", d);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);

        LogMessageVerbSigSafe(X_ERROR, -1, "%.0f\n", d);
        read_log_msg(logmsg);
        assert(strcmp(logmsg, expected) == 0);
    }


    LogClose(EXIT_NO_ERROR);
    unlink(log_file_path);

#undef read_log_msg
}