⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u3util.c

📁 wireshark 0.99.7 最新源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  strncat(dir_spec, "\\*.pid", 7);  hFind = FindFirstFile(dir_spec, &find_file_data);  if(hFind != INVALID_HANDLE_VALUE) {    do {      pid = (DWORD)atoi(find_file_data.cFileName);      if(pid)	TerminateApp(pid, timeOut);      strncpy(file_name, u3_host_exec_path, strlen(u3_host_exec_path) + 1);      strncat(file_name, "\\", 2);      strncat(file_name, find_file_data.cFileName, strlen(find_file_data.cFileName) + 1);      DeleteFile(TEXT(file_name));    } while(FindNextFile(hFind, &find_file_data) != 0);    FindClose(hFind);  }}/* associateAssociate an filetype (extension) with the U3 Wireshark if it doesn't already have an association*/void associate(char *extension){  HKEY key;  DWORD disposition;  char buffer[BUFSIZ];  int  buflen = BUFSIZ;  buffer[0] = '\0';  /* open the HKCR  extension  key*/  if(RegCreateKeyEx(HKEY_CLASSES_ROOT, extension, 0, NULL, 0, (KEY_READ | KEY_WRITE), NULL, &key, &disposition) == ERROR_SUCCESS) {    /* we could look at the disposition - but we don't bother */    if((RegQueryValueEx(key, "", NULL, NULL, buffer, &buflen) != ERROR_SUCCESS) || (buffer[0] == '\0')) {      (void)RegSetValueEx(key, "", 0, REG_SZ, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);    }    RegCloseKey(key);  }}BOOL save_environment(){  char **envptr;  char *envval;  HANDLE *file;  char buffer[BUFSIZ+1];  DWORD  buflen;  DWORD  numWritten;  BOOL   retval = FALSE;  envval = getenv("U3_HOST_EXEC_PATH");  buffer[0] = '\0';  strncat(buffer, envval, strlen(envval) + 1);  strncat(buffer, ENV_FILENAME, strlen(ENV_FILENAME) + 1);  /* open the file */  if((file = CreateFile(buffer, FILE_WRITE_DATA, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) != INVALID_HANDLE_VALUE) {    for(envptr = environmentvars; *envptr; envptr++) {      if(envval = getenv(*envptr)) {	/* write it out */	buffer[0] = '\0';	strncat(buffer, *envptr, strlen(*envptr) + 1);	strncat(buffer, "=", 2);	strncat(buffer, envval, strlen(envval) + 1);	strncat(buffer, "\n", 2);	buflen = strlen(buffer);	WriteFile(file, buffer, buflen, &numWritten, NULL);      }    }    /* close the file */    CloseHandle(file);    retval = TRUE;  }  return retval;}/* disassociateRemove any file types that are associated with the U3 Wireshark (which is being removed)*/void disassociate(char *extension){  HKEY key;  DWORD disposition;  char buffer[BUFSIZ];  int  buflen = BUFSIZ;  boolean delete_key = FALSE;  buffer[0] = '\0';  /* open the HKCR  extension  key*/  if(RegOpenKeyEx(HKEY_CLASSES_ROOT, extension, 0, (KEY_READ | KEY_WRITE), &key) == ERROR_SUCCESS) {    if(RegQueryValueEx(key, "", NULL, NULL, buffer, &buflen) == ERROR_SUCCESS) {      if(!strncmp(buffer, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1))	delete_key = TRUE;    }    RegCloseKey(key);  }  if(delete_key)    RegDeleteKey(HKEY_CLASSES_ROOT, extension);}/* host_configureConfigure the host for the U3 Wireshark. This involves:1) registering the U3 Wireshark with capture file types2) installing WinPcap if not already installed3) create a "My Captures" folder on the U3 device if it doesn't already exist*/void host_configure(void){  char **pext;  HKEY  key;  DWORD disposition;  char *u3_host_exec_path;  char *u3_device_exec_path;  char *u3_device_serial;  char *u3_device_document_path;  char wireshark_path[MAX_PATH+1];  char winpcap_path[MAX_PATH+1];  char my_captures_path[MAX_PATH+1];  char reg_key[BUFSIZ];  char buffer[BUFSIZ];  int  buflen = BUFSIZ;  boolean hasWinPcap = FALSE;  /* CREATE THE U3 Wireshark TYPE */  if(RegCreateKeyEx(HKEY_CLASSES_ROOT, WIRESHARK_ASSOC, 0, NULL, 0,		    (KEY_READ | KEY_WRITE), NULL, &key, &disposition) == ERROR_SUCCESS) {    (void)RegSetValueEx(key, "", 0, REG_SZ, WIRESHARK_DESC, strlen(WIRESHARK_DESC) + 1);    RegCloseKey(key);  }  /* compute the U3 path to wireshark */  u3_host_exec_path = getenv("U3_HOST_EXEC_PATH");  strncpy(wireshark_path, u3_host_exec_path, strlen(u3_host_exec_path) + 1);  strncat(wireshark_path, U3UTIL_APPSTART, strlen(U3UTIL_APPSTART) + 1);  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, SHELL_OPEN_COMMAND, strlen(SHELL_OPEN_COMMAND) + 1);  /* associate the application */  if(RegCreateKeyEx(HKEY_CLASSES_ROOT, reg_key, 0, NULL, 0,		    (KEY_READ | KEY_WRITE), NULL, &key, &disposition) == ERROR_SUCCESS) {    (void)RegSetValueEx(key, "", 0, REG_SZ, wireshark_path, strlen(wireshark_path) + 1);    RegCloseKey(key);  }  /* associate the icon */  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, DEFAULT_ICON, strlen(DEFAULT_ICON) + 1);  /* the icon is in the exe */  strncpy(wireshark_path, u3_host_exec_path, strlen(u3_host_exec_path) + 1);  strncat(wireshark_path, WIRESHARK_EXE, strlen(WIRESHARK_EXE) + 1);  strncat(wireshark_path, ",1", 3);  /* associate the application */  if(RegCreateKeyEx(HKEY_CLASSES_ROOT, reg_key, 0, NULL, 0,		    (KEY_READ | KEY_WRITE), NULL, &key, &disposition) == ERROR_SUCCESS) {    (void)RegSetValueEx(key, "", 0, REG_SZ, wireshark_path, strlen(wireshark_path) + 1);    RegCloseKey(key);  }  /* CREATE THE FILE ASSOCIATIONS */  for(pext = extensions; *pext; pext++)    associate(*pext);  /* update icons */  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);  /* START WINPCAP INSTALLATION IF NOT ALREADY INSTALLED */  if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, WINPCAP_KEY, 0, (KEY_READ), &key) == ERROR_SUCCESS) {    if(RegQueryValueEx(key, WINPCAP_UNINSTALL, NULL, NULL, buffer, &buflen) == ERROR_SUCCESS) {      if(buffer[0] != '\0')	hasWinPcap = TRUE;    }    RegCloseKey(key);  }  if(!hasWinPcap &&     (MessageBox(NULL,		 TEXT("If you want to capture packets from the network you will need to install WinPcap.\nIt will be uninstalled when you remove your U3 device.\n\nDo you want to install WinPcap?"),		 TEXT("U3 Wireshark: Install WinPcap?"),		 MB_YESNO|MB_TOPMOST|MB_ICONQUESTION) == IDYES)) {    /* compute the U3 path to the WinPcap installation package - it stays on the device */    u3_device_exec_path = getenv("U3_DEVICE_EXEC_PATH");    strncpy(winpcap_path, "\"", 2);    strncat(winpcap_path, u3_device_exec_path, strlen(u3_device_exec_path) + 1);    strncat(winpcap_path, WINPCAP_PACKAGE, strlen(WINPCAP_PACKAGE) + 1);    strncat(winpcap_path, "\"", 2);    ExecuteAndWait(winpcap_path);    /* if installation was successful this key will now exist */    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, WINPCAP_KEY, 0, (KEY_READ | KEY_WRITE), &key) == ERROR_SUCCESS) {      u3_device_serial = getenv("U3_DEVICE_SERIAL");      (void)RegSetValueEx(key, WINPCAP_U3INSTALLED, 0, REG_SZ, u3_device_serial, strlen(u3_device_serial) + 1);    }  }  /* CREATE THE "My Captures" FOLDER IF IT DOESN'T ALREADY EXIST */  u3_device_document_path = getenv("U3_DEVICE_DOCUMENT_PATH");  strncpy(my_captures_path, u3_device_document_path, strlen(u3_device_document_path) + 1);  strncat(my_captures_path, MY_CAPTURES, strlen(MY_CAPTURES) + 1);  /* don't care if it succeeds or fails */  (void) CreateDirectory(my_captures_path, NULL);  /* Save the environment so we can use it in the file assocation */  save_environment();}/* host_cleanupRemove any references to the U3 Wireshark from the host. This involves:1) Removing the U3 Wireshark file type associations2) Uninstalling WinPcap if we installed it.   If the user cancels the uninstallation of WinPcap, we will not try and remove it again.*/void host_clean_up(void){  HKEY  key;  DWORD disposition;  char **pext;  char *u3_device_serial;  char buffer[BUFSIZ];  int buflen = BUFSIZ;  char reg_key[BUFSIZ];  /* the device has been removed -     just close the application as quickly as possible */  app_stop(0);  /* DELETE THE FILE ASSOCIATIONS */  for(pext = extensions; *pext; pext++)    disassociate(*pext);  /* update icons */  SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);  /* DELETE THE U3 Wireshark TYPE */  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, SHELL_OPEN_COMMAND, strlen(SHELL_OPEN_COMMAND) + 1);  RegDeleteKey(HKEY_CLASSES_ROOT, reg_key);  /* delete the open key */  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, SHELL_OPEN, strlen(SHELL_OPEN) + 1);  RegDeleteKey(HKEY_CLASSES_ROOT, reg_key);  /* delete the shell key */  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, SHELL, strlen(SHELL) + 1);  RegDeleteKey(HKEY_CLASSES_ROOT, reg_key);  /* delete the icon key */  strncpy(reg_key, WIRESHARK_ASSOC, strlen(WIRESHARK_ASSOC) + 1);  strncat(reg_key, DEFAULT_ICON, strlen(DEFAULT_ICON) + 1);  RegDeleteKey(HKEY_CLASSES_ROOT, reg_key);  /* finally delete the toplevel key */  RegDeleteKey(HKEY_CLASSES_ROOT, WIRESHARK_ASSOC);  /* UNINSTALL WINPCAP ONLY IF WE INSTALLED IT */  buffer[0] = '\0';  /* see if WinPcap is installed */  if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, WINPCAP_KEY, 0, (KEY_READ | KEY_WRITE), &key) == ERROR_SUCCESS) {    /* see if a U3 device installed the package */    if(RegQueryValueEx(key, WINPCAP_U3INSTALLED, NULL, NULL, buffer, &buflen) == ERROR_SUCCESS) {      u3_device_serial = getenv("U3_DEVICE_SERIAL");      /* see if this U3 device installed the package */      if(!strncmp(buffer, u3_device_serial, strlen(u3_device_serial) + 1)) {	buffer[0] = '"';	buflen = BUFSIZ-1;	/* we installed WinPcap - we should now uninstall it - read the uninstall string */	(void) RegQueryValueEx(key, WINPCAP_UNINSTALL, NULL, NULL, &buffer[1], &buflen);	strncat(buffer, "\"", 2); /* close the quotes */	/* delete our value */	RegDeleteValue(key, WINPCAP_U3INSTALLED);      } else {	/* empty the buffer */	buffer[0] = '\0';      }    }    RegCloseKey(key);  }  if(*buffer) {    /* we have an uninstall string */    ExecuteAndWait(buffer);  }}main(int argc, char *argv[]){  DWORD time_out = 0;  char *u3_is_device_available;  u3_is_device_available = getenv("U3_IS_DEVICE_AVAILABLE");  if(u3_is_device_available && !strncmp(u3_is_device_available, "true", 5))    /* the device is available - wait 5 seconds for user to respond to       any dialogs */    time_out = 5000; /* 5 seconds */  if(argc > 1) {    if(!strncmp(argv[1], "hostConfigure", 13))      host_configure();    else if(!strncmp(argv[1], "appStart", 9))      app_start(argc, argv);    else if(!strncmp(argv[1], "appStop", 8))      app_stop(time_out);    else if(!strncmp(argv[1], "hostCleanUp", 11))      host_clean_up();  }  exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -