4 * $Id: util.c,v 1.48 2000/12/23 19:50:36 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
49 #ifdef NEED_SNPRINTF_H
50 # include "snprintf.h"
65 typedef int mode_t; /* for win32 */
70 #ifdef HAVE_SYS_SOCKET_H
71 #include <sys/socket.h>
74 #ifdef HAVE_SYS_IOCTL_H
75 #include <sys/ioctl.h>
82 #ifdef HAVE_SYS_SOCKIO_H
83 # include <sys/sockio.h>
91 * Given a pathname, return:
93 * the errno, if an attempt to "stat()" the file fails;
95 * EISDIR, if the attempt succeeded and the file turned out
98 * 0, if the attempt succeeded and the file turned out not
103 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
104 * define them either.)
106 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
109 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
112 #define S_IFIFO _S_IFIFO
115 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
118 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
122 test_for_directory(const char *path)
126 if (stat(path, &statb) < 0)
129 if (S_ISDIR(statb.st_mode))
136 * Given a pathname, return a pointer to the last pathname separator
137 * character in the pathname, or NULL if the pathname contains no
141 find_last_pathname_separator(char *path)
149 * We have to scan for '\' or '/'.
150 * Get to the end of the string.
152 separator = path + strlen(path); /* points to ending '\0' */
153 while (separator > path) {
155 if (c == '\\' || c == '/')
156 return separator; /* found it */
160 * OK, we didn't find any, so no directories - but there might
161 * be a drive letter....
163 return strchr(path, ':');
165 separator = strrchr(path, '/');
171 * Given a pathname, return the last component.
174 get_basename(char *path)
178 filename = find_last_pathname_separator(path);
179 if (filename == NULL) {
181 * There're no directories, drive letters, etc. in the
182 * name; the pathname *is* the file name.
187 * Skip past the pathname or drive letter separator.
195 * Given a pathname, return a string containing everything but the
196 * last component. NOTE: this overwrites the pathname handed into
200 get_dirname(char *path)
204 separator = find_last_pathname_separator(path);
205 if (separator == NULL) {
207 * There're no directories, drive letters, etc. in the
208 * name; there is no directory path to return.
214 * Get rid of the last pathname separator and the final file
220 * "path" now contains the pathname of the directory containing
221 * the file/directory to which it referred.
227 * Collect command-line arguments as a string consisting of the arguments,
228 * separated by spaces.
231 get_args_as_string(int argc, char **argv, int optind)
238 * Find out how long the string will be.
241 for (i = optind; i < argc; i++) {
242 len += strlen(argv[i]);
243 len++; /* space, or '\0' if this is the last argument */
247 * Allocate the buffer for the string.
249 argstring = g_malloc(len);
252 * Now construct the string.
254 strcpy(argstring, "");
257 strcat(argstring, argv[i]);
261 strcat(argstring, " ");
267 setup_tmpdir(char *dir)
269 int len = strlen(dir);
272 /* Append slash if necessary */
273 if (dir[len - 1] == '/') {
277 newdir = g_malloc(len + 2);
285 try_tempfile(char *namebuf, int namebuflen, const char *dir, const char *pfx)
287 static const char suffix[] = "XXXXXXXXXX";
288 int namelen = strlen(dir) + strlen(pfx) + sizeof suffix;
292 if (namebuflen < namelen) {
293 /* Stick in a truncated name, so that if this error is
294 reported with the file name, you at least get
296 snprintf(namebuf, namebuflen, "%s%s%s", dir, pfx, suffix);
297 errno = ENAMETOOLONG;
300 strcpy(namebuf, dir);
301 strcat(namebuf, pfx);
302 strcat(namebuf, suffix);
304 /* The Single UNIX Specification doesn't say that "mkstemp()"
305 creates the temporary file with mode rw-------, so we
306 won't assume that all UNIXes will do so; instead, we set
307 the umask to 0077 to take away all group and other
308 permissions, attempt to create the file, and then put
310 old_umask = umask(0077);
311 tmp_fd = mkstemp(namebuf);
316 static char *tmpdir = NULL;
318 static char *temp = NULL;
320 static char *E_tmpdir;
323 #define P_tmpdir "/var/tmp"
327 create_tempfile(char *namebuf, int namebuflen, const char *pfx)
331 static gboolean initialized;
334 if ((dir = getenv("TMPDIR")) != NULL)
335 tmpdir = setup_tmpdir(dir);
337 if ((dir = getenv("TEMP")) != NULL)
338 temp = setup_tmpdir(dir);
341 E_tmpdir = setup_tmpdir(P_tmpdir);
345 if (tmpdir != NULL) {
346 fd = try_tempfile(namebuf, namebuflen, tmpdir, pfx);
353 fd = try_tempfile(namebuf, namebuflen, temp, pfx);
359 fd = try_tempfile(namebuf, namebuflen, E_tmpdir, pfx);
363 return try_tempfile(namebuf, namebuflen, "/tmp", pfx);
366 /* ASCII/EBCDIC conversion tables from
367 * http://www.room42.com/store/computer_center/code_tables.shtml
369 static guint8 ASCII_translate_EBCDIC [ 256 ] = {
370 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
371 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
372 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
373 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
374 0x40, 0x5A, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, 0x4D,
375 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61,
376 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
377 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F,
378 0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
379 0xC9, 0xD1, 0xD2, 0xD3, 0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 0x4B, 0x4B, 0x4B, 0x4B, 0x4B,
401 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B, 0x4B
405 ASCII_to_EBCDIC(guint8 *buf, guint bytes)
412 for (i = 0; i < bytes; i++, bufptr++) {
413 *bufptr = ASCII_translate_EBCDIC[*bufptr];
418 ASCII_to_EBCDIC1(guint8 c)
420 return ASCII_translate_EBCDIC[c];
423 static guint8 EBCDIC_translate_ASCII [ 256 ] = {
424 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
425 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
426 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
427 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
428 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
429 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
430 0x2E, 0x2E, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
431 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x2E, 0x3F,
432 0x20, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
433 0x2E, 0x2E, 0x2E, 0x3C, 0x28, 0x2B, 0x7C,
434 0x26, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
435 0x2E, 0x21, 0x24, 0x2A, 0x29, 0x3B, 0x5E,
436 0x2D, 0x2F, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
437 0x2E, 0x7C, 0x2C, 0x25, 0x5F, 0x3E, 0x3F,
438 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
439 0x2E, 0x3A, 0x23, 0x40, 0x27, 0x3D, 0x22,
440 0x2E, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
441 0x69, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
442 0x2E, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71,
443 0x72, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
444 0x2E, 0x7E, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
445 0x7A, 0x2E, 0x2E, 0x2E, 0x5B, 0x2E, 0x2E,
446 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
447 0x2E, 0x2E, 0x2E, 0x2E, 0x5D, 0x2E, 0x2E,
448 0x7B, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
449 0x49, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
450 0x7D, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51,
451 0x52, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
452 0x5C, 0x2E, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
453 0x5A, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
454 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
455 0x39, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E
459 EBCDIC_to_ASCII(guint8 *buf, guint bytes)
466 for (i = 0; i < bytes; i++, bufptr++) {
467 *bufptr = EBCDIC_translate_ASCII[*bufptr];
472 EBCDIC_to_ASCII1(guint8 c)
474 return EBCDIC_translate_ASCII[c];
480 * If the ability to capture packets is added to Wiretap, these
481 * routines should be moved to the Wiretap source (with
482 * "get_interface_list()" and "free_interface_list()" renamed to
483 * "wtap_get_interface_list()" and "wtap_free_interface_list()",
484 * and modified to use Wiretap routines to attempt to open the
488 struct search_user_data {
494 search_for_if_cb(gpointer data, gpointer user_data);
497 free_if_cb(gpointer data, gpointer user_data);
501 get_interface_list(int *err, char *err_str)
504 gint nonloopback_pos = 0;
505 struct ifreq *ifr, *last;
507 struct ifreq ifrflags;
508 int sock = socket(AF_INET, SOCK_DGRAM, 0);
509 struct search_user_data user_data;
515 sprintf(err_str, "Error opening socket: %s",
521 * This code came from: W. Richard Stevens: "UNIX Network Programming",
522 * Networking APIs: Sockets and XTI, Vol 1, page 434.
525 len = 100 * sizeof(struct ifreq);
530 memset (buf, 0, len);
531 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
532 if (errno != EINVAL || lastlen != 0) {
534 "SIOCGIFCONF ioctl error getting list of interfaces: %s",
539 if (ifc.ifc_len < sizeof(struct ifreq)) {
541 "SIOCGIFCONF ioctl gave too small return buffer");
544 if (ifc.ifc_len == lastlen)
545 break; /* success, len has not changed */
546 lastlen = ifc.ifc_len;
548 len += 10 * sizeof(struct ifreq); /* increment */
551 ifr = (struct ifreq *) ifc.ifc_req;
552 last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
555 * Skip addresses that begin with "dummy", or that include
556 * a ":" (the latter are Solaris virtuals).
558 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
559 strchr(ifr->ifr_name, ':') != NULL)
563 * If we already have this interface name on the list,
564 * don't add it (SIOCGIFCONF returns, at least on
565 * BSD-flavored systems, one entry per interface *address*;
566 * if an interface has multiple addresses, we get multiple
569 user_data.name = ifr->ifr_name;
570 user_data.found = FALSE;
571 g_list_foreach(il, search_for_if_cb, &user_data);
576 * Get the interface flags.
578 memset(&ifrflags, 0, sizeof ifrflags);
579 strncpy(ifrflags.ifr_name, ifr->ifr_name,
580 sizeof ifrflags.ifr_name);
581 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
584 sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
585 ifr->ifr_name, strerror(errno));
590 * Skip interfaces that aren't up.
592 if (!(ifrflags.ifr_flags & IFF_UP))
596 * Skip interfaces that we can't open with "libpcap".
597 * Open with the minimum packet size - it appears that the
598 * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
599 * supplied is too large, rather than just truncating it.
601 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
608 * If it's a loopback interface, add it at the end of the
609 * list, otherwise add it after the last non-loopback
610 * interface, so all loopback interfaces go at the end - we
611 * don't want a loopback interface to be the default capture
612 * device unless there are no non-loopback devices.
614 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
615 strncmp(ifr->ifr_name, "lo", 2) == 0)
616 il = g_list_insert(il, g_strdup(ifr->ifr_name), -1);
618 il = g_list_insert(il, g_strdup(ifr->ifr_name),
621 * Insert the next non-loopback interface after this
629 ifr = (struct ifreq *) ((char *) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ);
631 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
637 * OK, maybe we have support for the "any" device, to do a cooked
638 * capture on all interfaces at once.
639 * Try opening it and, if that succeeds, add it to the end of
640 * the list of interfaces.
642 pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, err_str);
645 * It worked; we can use the "any" device.
647 il = g_list_insert(il, g_strdup("any"), -1);
657 * No interfaces found.
659 *err = NO_INTERFACES_FOUND;
665 g_list_foreach(il, free_if_cb, NULL);
670 *err = CANT_GET_INTERFACE_LIST;
675 search_for_if_cb(gpointer data, gpointer user_data)
677 struct search_user_data *search_user_data = user_data;
679 if (strcmp((char *)data, search_user_data->name) == 0)
680 search_user_data->found = TRUE;
684 get_interface_list(int *err, char *err_str) {
691 names = (wchar_t *)pcap_lookupdev(err_str);
696 /* If names[0] is less than 256 it means the first byte is 0
697 This implies that we are using unicode characters */
701 while (names[i] != 0)
702 newname[j++] = names[i++];
707 il = g_list_append(il, g_strdup(newname));
711 /* Otherwise we are in Windows 95/98 and using ascii(8 bit)
717 while (win95names[i] != 0)
718 newname[j++] = win95names[i++];
720 if (win95names[i] == 0)
723 il = g_list_append(il, g_strdup(newname));
732 free_if_cb(gpointer data, gpointer user_data)
738 free_interface_list(GList *if_list)
740 while (if_list != NULL) {
741 g_free(if_list->data);
742 if_list = g_list_remove_link(if_list, if_list);
746 #endif /* HAVE_LIBPCAP */
749 /* Compute the difference between two seconds/microseconds time stamps. */
751 compute_timestamp_diff(gint *diffsec, gint *diffusec,
752 guint32 sec1, guint32 usec1, guint32 sec2, guint32 usec2)
755 /* The seconds part of the first time is the same as the seconds
756 part of the second time, so if the microseconds part of the first
757 time is less than the microseconds part of the second time, the
758 first time is before the second time. The microseconds part of
759 the delta should just be the difference between the microseconds
760 part of the first time and the microseconds part of the second
761 time; don't adjust the seconds part of the delta, as it's OK if
762 the microseconds part is negative. */
764 *diffsec = sec1 - sec2;
765 *diffusec = usec1 - usec2;
766 } else if (sec1 <= sec2) {
767 /* The seconds part of the first time is less than the seconds part
768 of the second time, so the first time is before the second time.
770 Both the "seconds" and "microseconds" value of the delta
771 should have the same sign, so if the difference between the
772 microseconds values would be *positive*, subtract 1,000,000
773 from it, and add one to the seconds value. */
774 *diffsec = sec1 - sec2;
775 if (usec2 >= usec1) {
776 *diffusec = usec1 - usec2;
778 *diffusec = (usec1 - 1000000) - usec2;
782 /* Oh, good, we're not caught in a chronosynclastic infindibulum. */
783 *diffsec = sec1 - sec2;
784 if (usec2 <= usec1) {
785 *diffusec = usec1 - usec2;
787 *diffusec = (usec1 + 1000000) - usec2;