1998-09-16 02:39:15 +00:00
|
|
|
/* util.c
|
|
|
|
* Utility routines
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
1998-09-16 03:22:19 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* Ethereal - Network traffic analyzer
|
On Windows, use the directory in which the binary resides as the
directory in which global data files are stored. If an installed binary
is being run, that's the correct directory for them; if a build-tree
binary is being run, the "manuf" file will be there, and you can put
other data files there as well, if necessary.
Do the same with plugins, except that, if there's no
"plugins\\{version}" subdirectory of that directory, fall back on the
default installation directory, so you at least have a place where you
can put plugins for use by build-tree binaries. (Should we, instead,
have the Windows build procedure create a subdirectory of the "plugins"
source directory, with the plugin version number as its name, and copy
the plugins there, so you'd use the build-tree plugin binaries?)
Move "test_for_directory()" out of "util.c" and into
"epan/filesystem.c", with the other file system access portability
wrappers and convenience routines. Fix "util.h" not to declare it - or
other routines moved to "epan/filesystem.c" a while ago.
svn path=/trunk/; revision=3858
2001-08-21 06:39:18 +00:00
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
1998-09-16 02:39:15 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-16 02:39:15 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
1999-08-18 02:59:05 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1998-12-22 05:52:51 +00:00
|
|
|
#include <stdio.h>
|
1999-08-18 02:59:05 +00:00
|
|
|
#include <errno.h>
|
1998-09-16 02:39:15 +00:00
|
|
|
|
1999-04-06 16:24:50 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2005-11-06 22:43:25 +00:00
|
|
|
#include "file_util.h"
|
2000-01-10 17:33:17 +00:00
|
|
|
|
2004-12-30 02:10:24 +00:00
|
|
|
#include <epan/address.h>
|
2004-08-06 19:57:49 +00:00
|
|
|
#include <epan/addr_resolv.h>
|
2004-06-25 07:00:54 +00:00
|
|
|
|
2005-11-07 00:20:20 +00:00
|
|
|
#if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6)
|
|
|
|
#include <glib/gstdio.h> /* available since GLib 2.6 only! */
|
|
|
|
|
|
|
|
/* GLib2.6 or above, using new wrapper functions */
|
|
|
|
#define eth_mkstemp g_mkstemp
|
|
|
|
#else
|
|
|
|
#define eth_mkstemp mkstemp
|
|
|
|
#endif
|
|
|
|
|
2003-10-31 07:57:22 +00:00
|
|
|
/*
|
|
|
|
* This has to come after the include of <pcap.h>, as the include of
|
|
|
|
* <pcap.h> might cause <winsock2.h> to be included, and if we've
|
|
|
|
* already included <winsock.h> as a result of including <windows.h>,
|
|
|
|
* we get a bunch of redefinitions.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2001-11-09 07:44:51 +00:00
|
|
|
#include "util.h"
|
2003-03-08 07:00:48 +00:00
|
|
|
|
2000-02-22 07:07:55 +00:00
|
|
|
/*
|
|
|
|
* Collect command-line arguments as a string consisting of the arguments,
|
|
|
|
* separated by spaces.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
get_args_as_string(int argc, char **argv, int optind)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
char *argstring;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out how long the string will be.
|
|
|
|
*/
|
|
|
|
len = 0;
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
len += strlen(argv[i]);
|
|
|
|
len++; /* space, or '\0' if this is the last argument */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the buffer for the string.
|
|
|
|
*/
|
|
|
|
argstring = g_malloc(len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now construct the string.
|
|
|
|
*/
|
|
|
|
strcpy(argstring, "");
|
|
|
|
i = optind;
|
|
|
|
for (;;) {
|
|
|
|
strcat(argstring, argv[i]);
|
|
|
|
i++;
|
|
|
|
if (i == argc)
|
|
|
|
break;
|
|
|
|
strcat(argstring, " ");
|
|
|
|
}
|
|
|
|
return argstring;
|
|
|
|
}
|
|
|
|
|
2005-08-06 18:38:35 +00:00
|
|
|
static const char *
|
2005-08-06 14:03:14 +00:00
|
|
|
setup_tmpdir(const char *dir)
|
1999-08-18 02:59:05 +00:00
|
|
|
{
|
2005-08-06 18:38:35 +00:00
|
|
|
size_t len = strlen(dir);
|
1999-08-18 02:59:05 +00:00
|
|
|
char *newdir;
|
|
|
|
|
2004-05-22 07:50:15 +00:00
|
|
|
/* Append path separator if necessary */
|
2005-08-06 18:38:35 +00:00
|
|
|
if (len != 0 && dir[len - 1] == G_DIR_SEPARATOR) {
|
|
|
|
return dir;
|
1999-08-18 02:59:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
newdir = g_malloc(len + 2);
|
|
|
|
strcpy(newdir, dir);
|
2004-05-22 07:50:15 +00:00
|
|
|
strcat(newdir, G_DIR_SEPARATOR_S);
|
2005-08-06 18:38:35 +00:00
|
|
|
return newdir;
|
1999-08-18 02:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
|
|
|
|
{
|
|
|
|
static const char suffix[] = "XXXXXXXXXX";
|
1999-09-23 05:20:18 +00:00
|
|
|
int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
|
2005-11-06 22:43:25 +00:00
|
|
|
int old_umask;
|
1999-08-23 05:02:50 +00:00
|
|
|
int tmp_fd;
|
1999-08-18 02:59:05 +00:00
|
|
|
|
|
|
|
if (namebuflen < namelen) {
|
1999-09-24 04:14:43 +00:00
|
|
|
/* Stick in a truncated name, so that if this error is
|
|
|
|
reported with the file name, you at least get
|
|
|
|
something. */
|
2005-08-08 18:50:39 +00:00
|
|
|
g_snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix);
|
1999-08-18 02:59:05 +00:00
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strcpy(namebuf, dir);
|
|
|
|
strcat(namebuf, pfx);
|
|
|
|
strcat(namebuf, suffix);
|
1999-08-23 05:02:50 +00:00
|
|
|
|
|
|
|
/* The Single UNIX Specification doesn't say that "mkstemp()"
|
|
|
|
creates the temporary file with mode rw-------, so we
|
|
|
|
won't assume that all UNIXes will do so; instead, we set
|
|
|
|
the umask to 0077 to take away all group and other
|
|
|
|
permissions, attempt to create the file, and then put
|
|
|
|
the umask back. */
|
|
|
|
old_umask = umask(0077);
|
2005-11-06 22:43:25 +00:00
|
|
|
tmp_fd = eth_mkstemp(namebuf);
|
1999-08-23 05:02:50 +00:00
|
|
|
umask(old_umask);
|
|
|
|
return tmp_fd;
|
1999-08-18 02:59:05 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 18:38:35 +00:00
|
|
|
static const char *tmpdir = NULL;
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
1999-08-18 15:29:06 +00:00
|
|
|
static char *temp = NULL;
|
1999-08-18 02:59:05 +00:00
|
|
|
#endif
|
2005-08-06 18:38:35 +00:00
|
|
|
static const char *E_tmpdir;
|
1999-08-18 02:59:05 +00:00
|
|
|
|
1999-08-18 15:29:06 +00:00
|
|
|
#ifndef P_tmpdir
|
|
|
|
#define P_tmpdir "/var/tmp"
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 02:59:05 +00:00
|
|
|
int
|
|
|
|
create_tempfile(char *namebuf, int namebuflen, const char *pfx)
|
|
|
|
{
|
|
|
|
char *dir;
|
|
|
|
int fd;
|
|
|
|
static gboolean initialized;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
if ((dir = getenv("TMPDIR")) != NULL)
|
|
|
|
tmpdir = setup_tmpdir(dir);
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
1999-08-18 02:59:05 +00:00
|
|
|
if ((dir = getenv("TEMP")) != NULL)
|
|
|
|
temp = setup_tmpdir(dir);
|
|
|
|
#endif
|
1999-08-18 15:29:06 +00:00
|
|
|
|
1999-08-18 02:59:05 +00:00
|
|
|
E_tmpdir = setup_tmpdir(P_tmpdir);
|
|
|
|
initialized = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmpdir != NULL) {
|
|
|
|
fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
|
|
|
|
if (fd != -1)
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2004-09-11 23:03:36 +00:00
|
|
|
#ifdef _WIN32
|
1999-08-18 02:59:05 +00:00
|
|
|
if (temp != NULL) {
|
|
|
|
fd = try_tempfile(namebuf, namebuflen, temp, pfx);
|
|
|
|
if (fd != -1)
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
|
|
|
|
if (fd != -1)
|
|
|
|
return fd;
|
|
|
|
|
2004-02-07 04:25:16 +00:00
|
|
|
return try_tempfile(namebuf, namebuflen, G_DIR_SEPARATOR_S "tmp", pfx);
|
1999-08-18 02:59:05 +00:00
|
|
|
}
|
1999-10-19 04:11:23 +00:00
|
|
|
|
2000-09-10 06:44:39 +00:00
|
|
|
/* Compute the difference between two seconds/microseconds time stamps. */
|
|
|
|
void
|
|
|
|
compute_timestamp_diff(gint *diffsec, gint *diffusec,
|
|
|
|
guint32 sec1, guint32 usec1, guint32 sec2, guint32 usec2)
|
|
|
|
{
|
|
|
|
if (sec1 == sec2) {
|
|
|
|
/* The seconds part of the first time is the same as the seconds
|
|
|
|
part of the second time, so if the microseconds part of the first
|
|
|
|
time is less than the microseconds part of the second time, the
|
|
|
|
first time is before the second time. The microseconds part of
|
|
|
|
the delta should just be the difference between the microseconds
|
|
|
|
part of the first time and the microseconds part of the second
|
|
|
|
time; don't adjust the seconds part of the delta, as it's OK if
|
|
|
|
the microseconds part is negative. */
|
|
|
|
|
|
|
|
*diffsec = sec1 - sec2;
|
|
|
|
*diffusec = usec1 - usec2;
|
|
|
|
} else if (sec1 <= sec2) {
|
|
|
|
/* The seconds part of the first time is less than the seconds part
|
|
|
|
of the second time, so the first time is before the second time.
|
|
|
|
|
|
|
|
Both the "seconds" and "microseconds" value of the delta
|
|
|
|
should have the same sign, so if the difference between the
|
|
|
|
microseconds values would be *positive*, subtract 1,000,000
|
|
|
|
from it, and add one to the seconds value. */
|
|
|
|
*diffsec = sec1 - sec2;
|
|
|
|
if (usec2 >= usec1) {
|
|
|
|
*diffusec = usec1 - usec2;
|
|
|
|
} else {
|
|
|
|
*diffusec = (usec1 - 1000000) - usec2;
|
|
|
|
(*diffsec)++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Oh, good, we're not caught in a chronosynclastic infindibulum. */
|
|
|
|
*diffsec = sec1 - sec2;
|
|
|
|
if (usec2 <= usec1) {
|
|
|
|
*diffusec = usec1 - usec2;
|
|
|
|
} else {
|
|
|
|
*diffusec = (usec1 + 1000000) - usec2;
|
|
|
|
(*diffsec)--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-05-23 05:25:19 +00:00
|
|
|
|
2003-11-18 04:16:28 +00:00
|
|
|
/* Try to figure out if we're remotely connected, e.g. via ssh or
|
|
|
|
Terminal Server, and create a capture filter that matches aspects of the
|
|
|
|
connection. We match the following environment variables:
|
2004-03-12 17:23:56 +00:00
|
|
|
|
2003-11-18 04:16:28 +00:00
|
|
|
SSH_CONNECTION (ssh): <remote IP> <remote port> <local IP> <local port>
|
|
|
|
SSH_CLIENT (ssh): <remote IP> <remote port> <local port>
|
|
|
|
REMOTEHOST (tcsh, others?): <remote name>
|
|
|
|
DISPLAY (x11): [remote name]:<display num>
|
|
|
|
CLIENTNAME (terminal server): <remote name>
|
|
|
|
*/
|
|
|
|
|
2005-08-06 14:03:14 +00:00
|
|
|
const gchar *get_conn_cfilter(void) {
|
2003-11-18 04:16:28 +00:00
|
|
|
static GString *filter_str = NULL;
|
|
|
|
gchar *env, **tokens;
|
2004-03-12 17:23:56 +00:00
|
|
|
|
2003-11-18 04:16:28 +00:00
|
|
|
if (filter_str == NULL) {
|
|
|
|
filter_str = g_string_new("");
|
|
|
|
}
|
|
|
|
if ((env = getenv("SSH_CONNECTION")) != NULL) {
|
|
|
|
tokens = g_strsplit(env, " ", 4);
|
|
|
|
if (tokens[3]) {
|
2004-06-25 07:00:54 +00:00
|
|
|
g_string_sprintf(filter_str, "not (tcp port %s and %s host %s "
|
|
|
|
"and tcp port %s and %s host %s)", tokens[1], host_ip_af(tokens[0]), tokens[0],
|
|
|
|
tokens[3], host_ip_af(tokens[2]), tokens[2]);
|
2003-11-18 04:16:28 +00:00
|
|
|
return filter_str->str;
|
|
|
|
}
|
|
|
|
} else if ((env = getenv("SSH_CLIENT")) != NULL) {
|
|
|
|
tokens = g_strsplit(env, " ", 3);
|
2004-06-25 07:00:54 +00:00
|
|
|
g_string_sprintf(filter_str, "not (tcp port %s and %s host %s "
|
|
|
|
"and tcp port %s)", tokens[1], host_ip_af(tokens[0]), tokens[0], tokens[2]);
|
2003-11-18 04:16:28 +00:00
|
|
|
return filter_str->str;
|
|
|
|
} else if ((env = getenv("REMOTEHOST")) != NULL) {
|
2004-03-12 17:23:56 +00:00
|
|
|
if (strcasecmp(env, "localhost") == 0 || strcmp(env, "127.0.0.1") == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
2004-06-25 07:00:54 +00:00
|
|
|
g_string_sprintf(filter_str, "not %s host %s", host_ip_af(env), env);
|
2003-11-18 04:16:28 +00:00
|
|
|
return filter_str->str;
|
|
|
|
} else if ((env = getenv("DISPLAY")) != NULL) {
|
|
|
|
tokens = g_strsplit(env, ":", 2);
|
|
|
|
if (tokens[0] && tokens[0][0] != 0) {
|
2004-03-12 17:23:56 +00:00
|
|
|
if (strcasecmp(tokens[0], "localhost") == 0 ||
|
|
|
|
strcmp(tokens[0], "127.0.0.1") == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
2004-06-25 07:00:54 +00:00
|
|
|
g_string_sprintf(filter_str, "not %s host %s",
|
|
|
|
host_ip_af(tokens[0]), tokens[0]);
|
2003-11-18 04:16:28 +00:00
|
|
|
return filter_str->str;
|
|
|
|
}
|
|
|
|
} else if ((env = getenv("CLIENTNAME")) != NULL) {
|
2003-12-01 20:27:09 +00:00
|
|
|
if (g_strcasecmp("console", env) != 0) {
|
2004-06-25 07:00:54 +00:00
|
|
|
g_string_sprintf(filter_str, "not %s host %s", host_ip_af(env), env);
|
2003-12-01 20:27:09 +00:00
|
|
|
return filter_str->str;
|
|
|
|
}
|
2003-11-18 04:16:28 +00:00
|
|
|
}
|
|
|
|
return "";
|
2004-03-12 17:23:56 +00:00
|
|
|
}
|