4 * $Id: util.c,v 1.41 2000/08/11 13:34:53 deniel 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, 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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;
513 sprintf(err_str, "Error opening socket: %s",
519 * Since we have to grab the interface list all at once, we'll
520 * make plenty of room.
522 ifc.ifc_len = 1024 * sizeof(struct ifreq);
523 ifc.ifc_buf = malloc(ifc.ifc_len);
525 if (ioctl(sock, SIOCGIFCONF, &ifc) < 0 ||
526 ifc.ifc_len < sizeof(struct ifreq)) {
527 sprintf(err_str, "SIOCGIFCONF error getting list of interfaces: %s",
532 ifr = (struct ifreq *) ifc.ifc_req;
533 last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
536 * Skip addresses that begin with "dummy", or that include
537 * a ":" (the latter are Solaris virtuals).
539 if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
540 strchr(ifr->ifr_name, ':') != NULL)
544 * If we already have this interface name on the list,
545 * don't add it (SIOCGIFCONF returns, at least on
546 * BSD-flavored systems, one entry per interface *address*;
547 * if an interface has multiple addresses, we get multiple
550 user_data.name = ifr->ifr_name;
551 user_data.found = FALSE;
552 g_list_foreach(il, search_for_if_cb, &user_data);
557 * Get the interface flags.
559 memset(&ifrflags, 0, sizeof ifrflags);
560 strncpy(ifrflags.ifr_name, ifr->ifr_name,
561 sizeof ifrflags.ifr_name);
562 if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
565 sprintf(err_str, "SIOCGIFFLAGS error getting flags for interface %s: %s",
566 ifr->ifr_name, strerror(errno));
571 * Skip interfaces that aren't up.
573 if (!(ifrflags.ifr_flags & IFF_UP))
577 * Skip interfaces that we can't open with "libpcap".
578 * Open with the minimum packet size - it appears that the
579 * IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
580 * supplied is too large, rather than just truncating it.
582 pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
589 * If it's a loopback interface, add it at the end of the
590 * list, otherwise add it after the last non-loopback
591 * interface, so all loopback interfaces go at the end - we
592 * don't want a loopback interface to be the default capture
593 * device unless there are no non-loopback devices.
595 if ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
596 strncmp(ifr->ifr_name, "lo", 2) == 0)
597 il = g_list_insert(il, g_strdup(ifr->ifr_name), -1);
599 il = g_list_insert(il, g_strdup(ifr->ifr_name),
602 * Insert the next non-loopback interface after this
610 ifr = (struct ifreq *) ((char *) ifr + ifr->ifr_addr.sa_len + IFNAMSIZ);
612 ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
621 * No interfaces found.
623 *err = NO_INTERFACES_FOUND;
629 g_list_foreach(il, free_if_cb, NULL);
634 *err = CANT_GET_INTERFACE_LIST;
639 search_for_if_cb(gpointer data, gpointer user_data)
641 struct search_user_data *search_user_data = user_data;
643 if (strcmp((char *)data, search_user_data->name) == 0)
644 search_user_data->found = TRUE;
648 get_interface_list(int *err, char *err_str) {
655 names = (wchar_t *)pcap_lookupdev(err_str);
660 /* If names[0] is less than 256 it means the first byte is 0
661 This implies that we are using unicode characters */
665 while (names[i] != 0)
666 newname[j++] = names[i++];
671 il = g_list_append(il, g_strdup(newname));
675 /* Otherwise we are in Windows 95/98 and using ascii(8 bit)
681 while (win95names[i] != 0)
682 newname[j++] = win95names[i++];
684 if (win95names[i] == 0)
687 il = g_list_append(il, g_strdup(newname));
696 free_if_cb(gpointer data, gpointer user_data)
702 free_interface_list(GList *if_list)
704 while (if_list != NULL) {
705 g_free(if_list->data);
706 if_list = g_list_remove_link(if_list, if_list);
710 #endif /* HAVE_LIBPCAP */
715 static const char *home = NULL;
717 char *homedrive, *homepath;
724 /* Return the cached value, if available */
729 * XXX - should we use USERPROFILE anywhere in this process?
730 * Is there a chance that it might be set but one or more of
731 * HOMEDRIVE or HOMEPATH isn't set?
733 homedrive = getenv("HOMEDRIVE");
734 if (homedrive != NULL) {
735 homepath = getenv("HOMEPATH");
736 if (homepath != NULL) {
738 * This is cached, so we don't need to worry about
739 * allocating multiple ones of them.
742 g_malloc(strlen(homedrive) + strlen(homepath) + 1);
743 strcpy(homestring, homedrive);
744 strcat(homestring, homepath);
747 * Trim off any trailing slash or backslash.
749 lastsep = find_last_pathname_separator(homestring);
750 if (lastsep != NULL && *(lastsep + 1) == '\0') {
752 * Last separator is the last character
753 * in the string. Nuke it.
767 home = getenv("HOME");
770 * Get their home directory from the password file.
771 * If we can't even find a password file entry for them,
774 pwd = getpwuid(getuid());
777 * This is cached, so we don't need to worry
778 * about allocating multiple ones of them.
780 home = g_strdup(pwd->pw_dir);