Re: [tcpdump-workers] savefile.c patch
Guy Harris [EMAIL PROTECTED] said: Also, should we save the mode returned by setmode() and restore it when we close a pcap_t or pcap_dumper_t that refers to the standard input or output? Since pcap_dump_close() doesn't have a pcap_t argument, where should the oldmode come from? Can we have two module globals; oldmode_stdin, oldmode_stdout, assuming stdin/stdout won't be opened for capture more than once? Ideally it should be pcap_dump_flush(pcap_t *p), but too late to change that now. --gv - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.
Re: [tcpdump-workers] savefile.c patch
On May 27, 2004, at 5:22 AM, Gisle Vanem wrote: Since pcap_dump_close() doesn't have a pcap_t argument, where should the oldmode come from? Can we have two module globals; oldmode_stdin, oldmode_stdout, assuming stdin/stdout won't be opened for capture more than once? If it's opened for capture or dumping more than once in sequence, that's not an issue; if it's opened for capture or dumping more than once in parallel, that's not going to work anyway. As such, the two globals would probably be the best idea. - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.
[tcpdump-workers] savefile.c patch
I feel it's high time we cleanup some of the sources. I'd start with savefile.c. Currently it doesn't work for offline data from stdin. --gv --- libpcap-2004.05.20/savefile.c Tue Mar 23 21:18:08 2004 +++ savefile.c Wed Mar 24 16:29:06 2004 @@ -52,6 +52,12 @@ #define TCPDUMP_MAGIC 0xa1b2c3d4 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34 +#if defined(WIN32) || defined(MSDOS) +#define SETMODE(file,mode) setmode(file,mode) +#else +#define SETMODE(file,mode) ((void)0) +#endif + /* * We use the receiver-makes-right approach to byte order, * because time is at a premium when we are writing the file. @@ -587,6 +593,7 @@ { if (p-sf.rfile != stdin) (void)fclose(p-sf.rfile); + elseSETMODE (fileno(stdin),O_TEXT); if (p-sf.base != NULL) free(p-sf.base); } @@ -607,15 +614,12 @@ } memset((char *)p, 0, sizeof(*p)); - - if (fname[0] == '-' fname[1] == '\0') + if (fname[0] == '-' fname[1] == '\0') { fp = stdin; + SETMODE(fileno(fp), O_BINARY); + } else { -#ifndef WIN32 - fp = fopen(fname, r); -#else fp = fopen(fname, rb); -#endif if (fp == NULL) { snprintf(errbuf, PCAP_ERRBUF_SIZE, %s: %s, fname, pcap_strerror(errno)); @@ -726,13 +730,15 @@ break; } -#ifndef WIN32 +#if !defined(WIN32) !defined(MSDOS) /* * You can do select() and poll() on plain files on most * platforms, and should be able to do so on pipes. * * You can't do select() on anything other than sockets in * Windows, so, on Win32 systems, we don't have selectable_fd. +* But one could use 'WaitForSingleObject()' on HANDLE obtained +* from '_get_osfhandle(p-selectable_fd)'. */ p-selectable_fd = fileno(fp); #endif @@ -748,8 +754,10 @@ return (p); bad: - if(fp) + if(fp fp != stdin) fclose(fp); + if (fp == stdin) + SETMODE (fileno(stdin),O_TEXT); free(p); return (NULL); } @@ -973,6 +981,7 @@ pcap_dump_open(pcap_t *p, const char *fname) { FILE *f; + pcap_dumper_t *pd; int linktype; linktype = dlt_to_linktype(p-linktype); @@ -985,26 +994,23 @@ if (fname[0] == '-' fname[1] == '\0') { f = stdout; -#ifdef WIN32 - _setmode(_fileno(f), _O_BINARY); -#endif + SETMODE(fileno(f), O_BINARY); } else { -#ifndef WIN32 - f = fopen(fname, w); -#else f = fopen(fname, wb); -#endif - if (f == NULL) { + setbuf(f, NULL);/* XXX - why? */ + } + + pd = (pcap_dumper_t*)f; + + if (!pd || sf_write_header(f, linktype, p-tzoff, p-snapshot) 0) { snprintf(p-errbuf, PCAP_ERRBUF_SIZE, %s: %s, fname, pcap_strerror(errno)); - return (NULL); + if (pd) + pcap_dump_close(pd); + pd = NULL; + f = NULL; } -#ifdef WIN32 - setbuf(f, NULL);/* XXX - why? */ -#endif - } - (void)sf_write_header(f, linktype, p-tzoff, p-snapshot); - return ((pcap_dumper_t *)f); + return (pd); } FILE * @@ -1026,11 +1032,15 @@ void pcap_dump_close(pcap_dumper_t *p) { + FILE *fil = (FILE*)p; #ifdef notyet - if (ferror((FILE *)p)) + if (ferror(fil)) return-an-error; /* XXX should check return from fclose() too */ #endif - (void)fclose((FILE *)p); + if (fil == stdin || fil == stdout) + SETMODE (fileno(fil),O_TEXT); + else + fclose (fil); } - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.
Re: [tcpdump-workers] savefile.c patch
On May 26, 2004, at 1:55 AM, Gisle Vanem wrote: I feel it's high time we cleanup some of the sources. I'd start with savefile.c. Currently it doesn't work for offline data from stdin. --gv --- libpcap-2004.05.20/savefile.c Tue Mar 23 21:18:08 2004 +++ savefile.c Wed Mar 24 16:29:06 2004 @@ -52,6 +52,12 @@ #define TCPDUMP_MAGIC 0xa1b2c3d4 #define PATCHED_TCPDUMP_MAGIC 0xa1b2cd34 +#if defined(WIN32) || defined(MSDOS) +#define SETMODE(file,mode) setmode(file,mode) +#else +#define SETMODE(file,mode) ((void)0) +#endif + Perhaps it should just define SETMODE() as nothing - that would cause SETMODE(file, mode); to expand to ; which, as a null statement, should be a valid statement. Also, is setmode() sufficient with all the compilers that could be used to compile libpcap/WinPcap on Windows (MSVC++, MinGW, etc.), or is _setmode() needed with some compilers? (The code currently uses _setmode().) -#ifndef WIN32 - fp = fopen(fname, r); -#else fp = fopen(fname, rb); -#endif Presumably there are no interesting UN*X platforms left that wouldn't ignore the b (Ethereal's library for reading capture files unconditionally uses rb), so that should be OK. - if(fp) + if(fp fp != stdin) fclose(fp); + if (fp == stdin) + SETMODE (fileno(stdin),O_TEXT); Why not just if (fp) { if (fp == stdin) SETMODE(fileno(stdin), O_TEXT); else fclose(fp); } @@ -973,6 +981,7 @@ pcap_dump_open(pcap_t *p, const char *fname) { FILE *f; + pcap_dumper_t *pd; int linktype; linktype = dlt_to_linktype(p-linktype); @@ -985,26 +994,23 @@ if (fname[0] == '-' fname[1] == '\0') { f = stdout; -#ifdef WIN32 - _setmode(_fileno(f), _O_BINARY); -#endif + SETMODE(fileno(f), O_BINARY); } else { -#ifndef WIN32 - f = fopen(fname, w); -#else f = fopen(fname, wb); -#endif - if (f == NULL) { + setbuf(f, NULL);/* XXX - why? */ + } I'm not sure why we're setting the output unbuffered on Windows; even if there's a legitimate reason to do so, I don't see any reason to do so on UN*X - we really don't want to have the standard I/O library routines make a separate write() call for every fwrite() etc. call to the file. - This is the tcpdump-workers list. Visit https://lists.sandelman.ca/ to unsubscribe.