Exemple #1
0
WSLUA_CONSTRUCTOR Dir_make(lua_State* L) {
    /* Creates a directory.

       The created directory is set for permission mode 0755 (octal), meaning it is
       read+write+execute by owner, but only read+execute by group and others.

       IF the directory was created successfully, a boolean `true` is returned.
       If the directory cannot be made because it already exists, `false` is returned.
       If the directory cannot be made because an error occurred, `nil` is returned.

       @since 1.11.3
    */
#define WSLUA_ARG_Dir_make_NAME 1 /* The name of the directory, possibly including path. */

    const char *dir_path = luaL_checkstring(L, WSLUA_ARG_Dir_make_NAME);
    ws_statb64 s_buf;
    int ret;

    if (ws_stat64(dir_path, &s_buf) != 0 && errno == ENOENT) {
        ret = ws_mkdir(dir_path, 0755);
        if (ret == -1) {
            lua_pushnil(L);
        } else {
            lua_pushboolean(L, 1);
        }
    } else {
        lua_pushboolean(L, 0);
    }

    WSLUA_RETURN(1); /* Boolean `true` on success, `false` if already exists, `nil` on error. */
}
Exemple #2
0
/* copied from filesystem.c */
static int capture_opts_test_for_fifo(const char *path)
{
  ws_statb64 statb;

  if (ws_stat64(path, &statb) < 0)
    return errno;

  if (S_ISFIFO(statb.st_mode))
    return ESPIPE;
  else
    return 0;
}
/*
 * Given the pathname of the file we just closed with ftap_fdclose(), attempt
 * to reopen that file and assign the new file descriptor(s) to the sequential
 * stream and, if do_random is TRUE, to the random stream.  Used on Windows
 * after the rename of a file we had open was done or if the rename of a
 * file on top of a file we had open failed.
 *
 * This is only required by Wireshark, not TShark, and, at the point that
 * Wireshark is doing this, the sequential stream is closed, and the
 * random stream is open, so this refuses to open pipes, and only
 * reopens the random stream.
 */
gboolean
ftap_fdreopen(ftap *fth, const char *filename, int *err)
{
	ws_statb64 statb;

	/*
	 * We need two independent descriptors for random access, so
	 * they have different file positions.  If we're opening the
	 * standard input, we can only dup it to get additional
	 * descriptors, so we can't have two independent descriptors,
	 * and thus can't do random access.
	 */
	if (strcmp(filename, "-") == 0) {
		*err = FTAP_ERR_RANDOM_OPEN_STDIN;
		return FALSE;
	}

	/* First, make sure the file is valid */
	if (ws_stat64(filename, &statb) < 0) {
		*err = errno;
		return FALSE;
	}
	if (S_ISFIFO(statb.st_mode)) {
		/*
		 * Opens of FIFOs are not allowed; see above.
		 */
		*err = FTAP_ERR_RANDOM_OPEN_PIPE;
		return FALSE;
	} else if (S_ISDIR(statb.st_mode)) {
		/*
		 * Return different errors for "this is a directory"
		 * and "this is some random special file type", so
		 * the user can get a potentially more helpful error.
		 */
		*err = EISDIR;
		return FALSE;
	} else if (! S_ISREG(statb.st_mode)) {
		*err = FTAP_ERR_NOT_REGULAR_FILE;
		return FALSE;
	}

	/* Open the file */
	errno = FTAP_ERR_CANT_OPEN;
	if (!file_fdreopen(fth->random_fh, filename)) {
		*err = errno;
		return FALSE;
	}
	return TRUE;
}
Exemple #4
0
guint
get_interface_type(gchar *name, gchar *description)
{
#if defined(__linux__)
    ws_statb64 statb;
    char *wireless_path;
#endif
#if defined(_WIN32)
    /*
     * Much digging failed to reveal any obvious way to get something such
     * as the SNMP MIB-II ifType value for an interface:
     *
     *    http://www.iana.org/assignments/ianaiftype-mib
     *
     * by making some NDIS request.
     */
    if (description && (strstr(description,"generic dialup") != NULL ||
            strstr(description,"PPP/SLIP") != NULL )) {
        return IF_DIALUP;
    } else if (description && (strstr(description,"Wireless") != NULL ||
            strstr(description,"802.11") != NULL)) {
        return IF_WIRELESS;
    } else if (description && strstr(description,"AirPcap") != NULL ||
            strstr(name,"airpcap")) {
        return IF_AIRPCAP;
    } else if (description && strstr(description, "Bluetooth") != NULL ) {
        return IF_BLUETOOTH;
    }
#elif defined(__APPLE__)
    /*
     * XXX - yes, fetching all the network addresses for an interface
     * gets you an AF_LINK address, of type "struct sockaddr_dl", and,
     * yes, that includes an SNMP MIB-II ifType value.
     *
     * However, it's IFT_ETHER, i.e. Ethernet, for AirPort interfaces,
     * not IFT_IEEE80211 (which isn't defined in OS X in any case).
     *
     * Perhaps some other BSD-flavored OSes won't make this mistake;
     * however, FreeBSD 7.0 and OpenBSD 4.2, at least, appear to have
     * made the same mistake, at least for my Belkin ZyDAS stick.
     *
     * XXX - this is wrong on a MacBook Air, as en0 is the AirPort
     * interface, and it's also wrong on a Mac that has no AirPort
     * interfaces and has multiple Ethernet interfaces.
     *
     * The SystemConfiguration framework is your friend here.
     * SCNetworkInterfaceGetInterfaceType() will get the interface
     * type.  SCNetworkInterfaceCopyAll() gets all network-capable
     * interfaces on the system; SCNetworkInterfaceGetBSDName()
     * gets the "BSD name" of the interface, so we look for
     * an interface with the specified "BSD name" and get its
     * interface type.  The interface type is a CFString, and:
     *
     *    kSCNetworkInterfaceTypeIEEE80211 means IF_WIRELESS;
     *    kSCNetworkInterfaceTypeBluetooth means IF_BLUETOOTH;
     *    kSCNetworkInterfaceTypeModem or
     *    kSCNetworkInterfaceTypePPP or
     *    maybe kSCNetworkInterfaceTypeWWAN means IF_DIALUP
     */
    if (strcmp(name, "en1") == 0) {
        return IF_WIRELESS;
    }
    /*
     * XXX - PPP devices have names beginning with "ppp" and an IFT_ of
     * IFT_PPP, but they could be dial-up, or PPPoE, or mobile phone modem,
     * or VPN, or... devices.  One might have to dive into the bowels of
     * IOKit to find out.
     */

    /*
     * XXX - there's currently no support for raw Bluetooth capture,
     * and IP-over-Bluetooth devices just look like fake Ethernet
     * devices.  There's also Bluetooth modem support, but that'll
     * probably just give you a device that looks like a PPP device.
     */
#elif defined(__linux__)
    /*
     * Look for /sys/class/net/{device}/wireless.
     */
    wireless_path = g_strdup_printf("/sys/class/net/%s/wireless", name);
    if (wireless_path != NULL) {
        if (ws_stat64(wireless_path, &statb) == 0) {
            g_free(wireless_path);
            return IF_WIRELESS;
        }
        g_free(wireless_path);
    }
    /*
     * Bluetooth devices.
     *
     * XXX - this is for raw Bluetooth capture; what about IP-over-Bluetooth
     * devices?
     */
    if ( strstr(name,"bluetooth") != NULL) {
        return IF_BLUETOOTH;
    }

    /*
     * USB devices.
     */
    if ( strstr(name,"usbmon") != NULL ) {
        return IF_USB;
    }
#endif
    /*
     * Bridge, NAT, or host-only interfaces on VMWare hosts have the name
     * vmnet[0-9]+ or VMnet[0-9+ on Windows. Guests might use a native
     * (LANCE or E1000) driver or the vmxnet driver. These devices have an
     * IFT_ of IFT_ETHER, so we have to check the name.
     */
    if ( g_ascii_strncasecmp(name, "vmnet", 5) == 0) {
        return IF_VIRTUAL;
    }

    if ( g_ascii_strncasecmp(name, "vmxnet", 6) == 0) {
        return IF_VIRTUAL;
    }

    if (description && strstr(description, "VMware") != NULL ) {
        return IF_VIRTUAL;
    }

    return IF_WIRED;
}
/* Opens a file and prepares a ftap struct.
   If "do_random" is TRUE, it opens the file twice; the second open
   allows the application to do random-access I/O without moving
   the seek offset for sequential I/O, which is used by Wireshark
   so that it can do sequential I/O to a capture file that's being
   written to as new packets arrive independently of random I/O done
   to display protocol trees for packets when they're selected. */
ftap* ftap_open_offline(const char *filename, int *err, char **err_info,
			gboolean do_random)
{
	int	fd;
	ws_statb64 statb;
	ftap	*fth;
	unsigned int	i;
	gboolean use_stdin = FALSE;
	gchar *extension;

	/* open standard input if filename is '-' */
	if (strcmp(filename, "-") == 0)
		use_stdin = TRUE;

	/* First, make sure the file is valid */
	if (use_stdin) {
		if (ws_fstat64(0, &statb) < 0) {
			*err = errno;
			return NULL;
		}
	} else {
		if (ws_stat64(filename, &statb) < 0) {
			*err = errno;
			return NULL;
		}
	}
	if (S_ISFIFO(statb.st_mode)) {
		/*
		 * Opens of FIFOs are allowed only when not opening
		 * for random access.
		 *
		 * XXX - currently, we do seeking when trying to find
		 * out the file type, so we don't actually support
		 * opening FIFOs.  However, we may eventually
		 * do buffering that allows us to do at least some
		 * file type determination even on pipes, so we
		 * allow FIFO opens and let things fail later when
		 * we try to seek.
		 */
		if (do_random) {
			*err = FTAP_ERR_RANDOM_OPEN_PIPE;
			return NULL;
		}
	} else if (S_ISDIR(statb.st_mode)) {
		/*
		 * Return different errors for "this is a directory"
		 * and "this is some random special file type", so
		 * the user can get a potentially more helpful error.
		 */
		*err = EISDIR;
		return NULL;
	} else if (! S_ISREG(statb.st_mode)) {
		*err = FTAP_ERR_NOT_REGULAR_FILE;
		return NULL;
	}

	/*
	 * We need two independent descriptors for random access, so
	 * they have different file positions.  If we're opening the
	 * standard input, we can only dup it to get additional
	 * descriptors, so we can't have two independent descriptors,
	 * and thus can't do random access.
	 */
	if (use_stdin && do_random) {
		*err = FTAP_ERR_RANDOM_OPEN_STDIN;
		return NULL;
	}

	errno = ENOMEM;
	fth = (ftap *)g_malloc0(sizeof(ftap));

	/* Open the file */
	errno = FTAP_ERR_CANT_OPEN;
	if (use_stdin) {
		/*
		 * We dup FD 0, so that we don't have to worry about
		 * a file_close of wth->fh closing the standard
		 * input of the process.
		 */
		fd = ws_dup(0);
		if (fd < 0) {
			*err = errno;
			g_free(fth);
			return NULL;
		}
#ifdef _WIN32
		if (_setmode(fd, O_BINARY) == -1) {
			/* "Shouldn't happen" */
			*err = errno;
			g_free(fth);
			return NULL;
		}
#endif
		if (!(fth->fh = file_fdopen(fd))) {
			*err = errno;
			ws_close(fd);
			g_free(fth);
			return NULL;
		}
	} else {
		if (!(fth->fh = file_open(filename))) {
			*err = errno;
			g_free(fth);
			return NULL;
		}
	}

	if (do_random) {
		if (!(fth->random_fh = file_open(filename))) {
			*err = errno;
			file_close(fth->fh);
			g_free(fth);
			return NULL;
		}
	} else
		fth->random_fh = NULL;

	/* initialization */
	fth->file_encap = FTAP_ENCAP_UNKNOWN;
	fth->subtype_sequential_close = NULL;
	fth->subtype_close = NULL;
    fth->priv = NULL;

    init_magic_number_open_routines();
	init_heuristic_open_info();
	if (fth->random_fh) {
		fth->fast_seek = g_ptr_array_new();

		file_set_random_access(fth->fh, FALSE, fth->fast_seek);
		file_set_random_access(fth->random_fh, TRUE, fth->fast_seek);
	}

	/* Try all file types that support magic numbers */
	for (i = 0; i < magic_number_open_routines_arr->len; i++) {
		/* Seek back to the beginning of the file; the open routine
		   for the previous file type may have left the file
		   position somewhere other than the beginning, and the
		   open routine for this file type will probably want
		   to start reading at the beginning.

		   Initialize the data offset while we're at it. */
		if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
			/* I/O error - give up */
			ftap_close(fth);
			return NULL;
		}

		switch ((*magic_number_open_routines[i])(fth, err, err_info)) {

		case -1:
			/* I/O error - give up */
			ftap_close(fth);
			return NULL;

		case 0:
			/* No I/O error, but not that type of file */
			break;

		case 1:
			/* We found the file type */
			goto success;
		}
	}

	/* Does this file's name have an extension? */
	extension = get_file_extension(filename);
	if (extension != NULL) {
		/* Yes - try the heuristic types that use that extension first. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			/* Does this type use that extension? */
			if (heuristic_uses_extension(i, extension)) {
				/* Yes. */
				if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;
				}

				switch ((*heuristic_open_info[i].open_routine)(fth,
				    err, err_info)) {

				case -1:
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;

				case 0:
					/* No I/O error, but not that type of file */
					break;

				case 1:
					/* We found the file type */
					g_free(extension);
					goto success;
				}
			}
		}

		/* Now try the ones that don't use it. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			/* Does this type use that extension? */
			if (!heuristic_uses_extension(i, extension)) {
				/* No. */
				if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;
				}

				switch ((*heuristic_open_info[i].open_routine)(fth,
				    err, err_info)) {

				case -1:
					/* I/O error - give up */
					g_free(extension);
					ftap_close(fth);
					return NULL;

				case 0:
					/* No I/O error, but not that type of file */
					break;

				case 1:
					/* We found the file type */
					g_free(extension);
					goto success;
				}
			}
		}
		g_free(extension);
	} else {
		/* No - try all the heuristics types in order. */
		for (i = 0; i < heuristic_open_info_arr->len; i++) {
			if (file_seek(fth->fh, 0, SEEK_SET, err) == -1) {
				/* I/O error - give up */
				ftap_close(fth);
				return NULL;
			}

			switch ((*heuristic_open_info[i].open_routine)(fth,
			    err, err_info)) {

			case -1:
				/* I/O error - give up */
				ftap_close(fth);
				return NULL;

			case 0:
				/* No I/O error, but not that type of file */
				break;

			case 1:
				/* We found the file type */
				goto success;
			}
		}
	}

    /* Well, it's not one of the types of file we know about. */
	ftap_close(fth);
	*err = FTAP_ERR_FILE_UNKNOWN_FORMAT;
	return NULL;

success:
	fth->frame_buffer = (struct Buffer *)g_malloc(sizeof(struct Buffer));
	buffer_init(fth->frame_buffer, 1500);

	return fth;
}