6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include "file_util.h"
42 #include <epan/address.h>
43 #include <epan/addr_resolv.h>
45 #if GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6)
46 #include <glib/gstdio.h> /* available since GLib 2.6 only! */
48 /* GLib2.6 or above, using new wrapper functions */
49 #define eth_mkstemp g_mkstemp
51 #define eth_mkstemp mkstemp
55 * This has to come after the include of <pcap.h>, as the include of
56 * <pcap.h> might cause <winsock2.h> to be included, and if we've
57 * already included <winsock.h> as a result of including <windows.h>,
58 * we get a bunch of redefinitions.
67 * Collect command-line arguments as a string consisting of the arguments,
68 * separated by spaces.
71 get_args_as_string(int argc, char **argv, int optind)
78 * Find out how long the string will be.
81 for (i = optind; i < argc; i++) {
82 len += strlen(argv[i]);
83 len++; /* space, or '\0' if this is the last argument */
87 * Allocate the buffer for the string.
89 argstring = g_malloc(len);
92 * Now construct the string.
94 strcpy(argstring, "");
97 strcat(argstring, argv[i]);
101 strcat(argstring, " ");
107 setup_tmpdir(const char *dir)
109 size_t len = strlen(dir);
112 /* Append path separator if necessary */
113 if (len != 0 && dir[len - 1] == G_DIR_SEPARATOR) {
117 newdir = g_malloc(len + 2);
119 strcat(newdir, G_DIR_SEPARATOR_S);
125 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
127 static const char suffix[] = "XXXXXXXXXX";
128 int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
132 if (namebuflen < namelen) {
133 /* Stick in a truncated name, so that if this error is
134 reported with the file name, you at least get
136 g_snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix);
137 errno = ENAMETOOLONG;
140 strcpy(namebuf, dir);
141 strcat(namebuf, pfx);
142 strcat(namebuf, suffix);
144 /* The Single UNIX Specification doesn't say that "mkstemp()"
145 creates the temporary file with mode rw-------, so we
146 won't assume that all UNIXes will do so; instead, we set
147 the umask to 0077 to take away all group and other
148 permissions, attempt to create the file, and then put
150 old_umask = umask(0077);
151 tmp_fd = eth_mkstemp(namebuf);
156 static const char *tmpdir = NULL;
158 static const char *temp = NULL;
160 static const char *E_tmpdir;
163 #define P_tmpdir "/var/tmp"
167 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
171 static gboolean initialized;
174 if ((dir = getenv("TMPDIR")) != NULL)
175 tmpdir = setup_tmpdir(dir);
177 if ((dir = getenv("TEMP")) != NULL)
178 temp = setup_tmpdir(dir);
181 E_tmpdir = setup_tmpdir(P_tmpdir);
185 if (tmpdir != NULL) {
186 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
193 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
199 fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
203 return try_tempfile(namebuf, namebuflen, G_DIR_SEPARATOR_S "tmp", pfx);
206 /* Compute the difference between two seconds/microseconds time stamps. */
208 compute_timestamp_diff(gint *diffsec, gint *diffusec,
209 guint32 sec1, guint32 usec1, guint32 sec2, guint32 usec2)
212 /* The seconds part of the first time is the same as the seconds
213 part of the second time, so if the microseconds part of the first
214 time is less than the microseconds part of the second time, the
215 first time is before the second time. The microseconds part of
216 the delta should just be the difference between the microseconds
217 part of the first time and the microseconds part of the second
218 time; don't adjust the seconds part of the delta, as it's OK if
219 the microseconds part is negative. */
221 *diffsec = sec1 - sec2;
222 *diffusec = usec1 - usec2;
223 } else if (sec1 <= sec2) {
224 /* The seconds part of the first time is less than the seconds part
225 of the second time, so the first time is before the second time.
227 Both the "seconds" and "microseconds" value of the delta
228 should have the same sign, so if the difference between the
229 microseconds values would be *positive*, subtract 1,000,000
230 from it, and add one to the seconds value. */
231 *diffsec = sec1 - sec2;
232 if (usec2 >= usec1) {
233 *diffusec = usec1 - usec2;
235 *diffusec = (usec1 - 1000000) - usec2;
239 /* Oh, good, we're not caught in a chronosynclastic infindibulum. */
240 *diffsec = sec1 - sec2;
241 if (usec2 <= usec1) {
242 *diffusec = usec1 - usec2;
244 *diffusec = (usec1 + 1000000) - usec2;
250 /* Try to figure out if we're remotely connected, e.g. via ssh or
251 Terminal Server, and create a capture filter that matches aspects of the
252 connection. We match the following environment variables:
254 SSH_CONNECTION (ssh): <remote IP> <remote port> <local IP> <local port>
255 SSH_CLIENT (ssh): <remote IP> <remote port> <local port>
256 REMOTEHOST (tcsh, others?): <remote name>
257 DISPLAY (x11): [remote name]:<display num>
258 CLIENTNAME (terminal server): <remote name>
261 const gchar *get_conn_cfilter(void) {
262 static GString *filter_str = NULL;
263 gchar *env, **tokens;
265 if (filter_str == NULL) {
266 filter_str = g_string_new("");
268 if ((env = getenv("SSH_CONNECTION")) != NULL) {
269 tokens = g_strsplit(env, " ", 4);
271 g_string_sprintf(filter_str, "not (tcp port %s and %s host %s "
272 "and tcp port %s and %s host %s)", tokens[1], host_ip_af(tokens[0]), tokens[0],
273 tokens[3], host_ip_af(tokens[2]), tokens[2]);
274 return filter_str->str;
276 } else if ((env = getenv("SSH_CLIENT")) != NULL) {
277 tokens = g_strsplit(env, " ", 3);
278 g_string_sprintf(filter_str, "not (tcp port %s and %s host %s "
279 "and tcp port %s)", tokens[1], host_ip_af(tokens[0]), tokens[0], tokens[2]);
280 return filter_str->str;
281 } else if ((env = getenv("REMOTEHOST")) != NULL) {
282 if (strcasecmp(env, "localhost") == 0 || strcmp(env, "127.0.0.1") == 0) {
285 g_string_sprintf(filter_str, "not %s host %s", host_ip_af(env), env);
286 return filter_str->str;
287 } else if ((env = getenv("DISPLAY")) != NULL) {
288 tokens = g_strsplit(env, ":", 2);
289 if (tokens[0] && tokens[0][0] != 0) {
290 if (strcasecmp(tokens[0], "localhost") == 0 ||
291 strcmp(tokens[0], "127.0.0.1") == 0) {
294 g_string_sprintf(filter_str, "not %s host %s",
295 host_ip_af(tokens[0]), tokens[0]);
296 return filter_str->str;
298 } else if ((env = getenv("CLIENTNAME")) != NULL) {
299 g_string_sprintf(filter_str, "not tcp port 3389");
300 return filter_str->str;