PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ui/util.c

https://github.com/labx-technologies-llc/wireshark
C | 352 lines | 172 code | 33 blank | 147 comment | 76 complexity | c68300735883c6c8e373cd1ae75ea3f7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* util.c
  2. * Utility routines
  3. *
  4. * $Id$
  5. *
  6. * Wireshark - Network traffic analyzer
  7. * By Gerald Combs <gerald@wireshark.org>
  8. * Copyright 1998 Gerald Combs
  9. *
  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.
  14. *
  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.
  19. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #include "config.h"
  25. #include <glib.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdio.h>
  29. #include <errno.h>
  30. #ifdef HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #ifdef HAVE_WINDOWS_H
  34. #include <windows.h>
  35. #endif
  36. #include <epan/address.h>
  37. #include <epan/addr_resolv.h>
  38. #include <epan/strutil.h>
  39. #include "ui/util.h"
  40. /*
  41. * Collect command-line arguments as a string consisting of the arguments,
  42. * separated by spaces.
  43. */
  44. char *
  45. get_args_as_string(int argc, char **argv, int optindex)
  46. {
  47. int len;
  48. int i;
  49. char *argstring;
  50. /*
  51. * Find out how long the string will be.
  52. */
  53. len = 0;
  54. for (i = optindex; i < argc; i++) {
  55. len += (int) strlen(argv[i]);
  56. len++; /* space, or '\0' if this is the last argument */
  57. }
  58. /*
  59. * Allocate the buffer for the string.
  60. */
  61. argstring = (char *)g_malloc(len);
  62. /*
  63. * Now construct the string.
  64. */
  65. argstring[0] = '\0';
  66. i = optindex;
  67. for (;;) {
  68. g_strlcat(argstring, argv[i], len);
  69. i++;
  70. if (i == argc)
  71. break;
  72. g_strlcat(argstring, " ", len);
  73. }
  74. return argstring;
  75. }
  76. /* Compute the difference between two seconds/microseconds time stamps. */
  77. void
  78. compute_timestamp_diff(gint *diffsec, gint *diffusec,
  79. guint32 sec1, guint32 usec1, guint32 sec2, guint32 usec2)
  80. {
  81. if (sec1 == sec2) {
  82. /* The seconds part of the first time is the same as the seconds
  83. part of the second time, so if the microseconds part of the first
  84. time is less than the microseconds part of the second time, the
  85. first time is before the second time. The microseconds part of
  86. the delta should just be the difference between the microseconds
  87. part of the first time and the microseconds part of the second
  88. time; don't adjust the seconds part of the delta, as it's OK if
  89. the microseconds part is negative. */
  90. *diffsec = sec1 - sec2;
  91. *diffusec = usec1 - usec2;
  92. } else if (sec1 <= sec2) {
  93. /* The seconds part of the first time is less than the seconds part
  94. of the second time, so the first time is before the second time.
  95. Both the "seconds" and "microseconds" value of the delta
  96. should have the same sign, so if the difference between the
  97. microseconds values would be *positive*, subtract 1,000,000
  98. from it, and add one to the seconds value. */
  99. *diffsec = sec1 - sec2;
  100. if (usec2 >= usec1) {
  101. *diffusec = usec1 - usec2;
  102. } else {
  103. *diffusec = (usec1 - 1000000) - usec2;
  104. (*diffsec)++;
  105. }
  106. } else {
  107. /* Oh, good, we're not caught in a chronosynclastic infindibulum. */
  108. *diffsec = sec1 - sec2;
  109. if (usec2 <= usec1) {
  110. *diffusec = usec1 - usec2;
  111. } else {
  112. *diffusec = (usec1 + 1000000) - usec2;
  113. (*diffsec)--;
  114. }
  115. }
  116. }
  117. /* Remove any %<interface_name> from an IP address. */
  118. static char *sanitize_filter_ip(char *hostname) {
  119. gchar *end;
  120. gchar *ret;
  121. ret = g_strdup(hostname);
  122. if (!ret)
  123. return NULL;
  124. end = strchr(ret, '%');
  125. if (end)
  126. *end = '\0';
  127. return ret;
  128. }
  129. /* Try to figure out if we're remotely connected, e.g. via ssh or
  130. Terminal Server, and create a capture filter that matches aspects of the
  131. connection. We match the following environment variables:
  132. SSH_CONNECTION (ssh): <remote IP> <remote port> <local IP> <local port>
  133. SSH_CLIENT (ssh): <remote IP> <remote port> <local port>
  134. REMOTEHOST (tcsh, others?): <remote name>
  135. DISPLAY (x11): [remote name]:<display num>
  136. SESSIONNAME (terminal server): <remote name>
  137. */
  138. const gchar *get_conn_cfilter(void) {
  139. static GString *filter_str = NULL;
  140. gchar *env, **tokens;
  141. char *lastp, *lastc, *p;
  142. char *pprotocol = NULL;
  143. char *phostname = NULL;
  144. size_t hostlen;
  145. char *remip, *locip;
  146. if (filter_str == NULL) {
  147. filter_str = g_string_new("");
  148. }
  149. if ((env = getenv("SSH_CONNECTION")) != NULL) {
  150. tokens = g_strsplit(env, " ", 4);
  151. if (tokens[3]) {
  152. remip = sanitize_filter_ip(tokens[0]);
  153. locip = sanitize_filter_ip(tokens[2]);
  154. g_string_printf(filter_str, "not (tcp port %s and %s host %s "
  155. "and tcp port %s and %s host %s)", tokens[1], host_ip_af(remip), remip,
  156. tokens[3], host_ip_af(locip), locip);
  157. g_free(remip);
  158. g_free(locip);
  159. return filter_str->str;
  160. }
  161. } else if ((env = getenv("SSH_CLIENT")) != NULL) {
  162. tokens = g_strsplit(env, " ", 3);
  163. remip = sanitize_filter_ip(tokens[2]);
  164. g_string_printf(filter_str, "not (tcp port %s and %s host %s "
  165. "and tcp port %s)", tokens[1], host_ip_af(remip), tokens[0], remip);
  166. g_free(remip);
  167. return filter_str->str;
  168. } else if ((env = getenv("REMOTEHOST")) != NULL) {
  169. /* FreeBSD 7.0 sets REMOTEHOST to an empty string */
  170. if (g_ascii_strcasecmp(env, "localhost") == 0 ||
  171. strcmp(env, "127.0.0.1") == 0 ||
  172. strcmp(env, "") == 0) {
  173. return "";
  174. }
  175. remip = sanitize_filter_ip(env);
  176. g_string_printf(filter_str, "not %s host %s", host_ip_af(remip), remip);
  177. g_free(remip);
  178. return filter_str->str;
  179. } else if ((env = getenv("DISPLAY")) != NULL) {
  180. /*
  181. * This mirrors what _X11TransConnectDisplay() does.
  182. * Note that, on some systems, the hostname can
  183. * begin with "/", which means that it's a pathname
  184. * of a UNIX domain socket to connect to.
  185. *
  186. * The comments mirror those in _X11TransConnectDisplay(),
  187. * too. :-)
  188. *
  189. * Display names may be of the following format:
  190. *
  191. * [protoco./] [hostname] : [:] displaynumber [.screennumber]
  192. *
  193. * A string with exactly two colons separating hostname
  194. * from the display indicates a DECnet style name. Colons
  195. * in the hostname may occur if an IPv6 numeric address
  196. * is used as the hostname. An IPv6 numeric address may
  197. * also end in a double colon, so three colons in a row
  198. * indicates an IPv6 address ending in :: followed by
  199. * :display. To make it easier for people to read, an
  200. * IPv6 numeric address hostname may be surrounded by []
  201. * in a similar fashion to the IPv6 numeric address URL
  202. * syntax defined by IETF RFC 2732.
  203. *
  204. * If no hostname and no protocol is specified, the string
  205. * is interpreted as the most efficient local connection
  206. * to a server on the same machine. This is usually:
  207. *
  208. * o shared memory
  209. * o local stream
  210. * o UNIX domain socket
  211. * o TCP to local host.
  212. */
  213. p = env;
  214. /*
  215. * Step 0, find the protocol. This is delimited by
  216. * the optional slash ('/').
  217. */
  218. for (lastp = p; *p != '\0' && *p != ':' && *p != '/'; p++)
  219. ;
  220. if (*p == '\0')
  221. return ""; /* must have a colon */
  222. if (p != lastp && *p != ':') { /* protocol given? */
  223. /* Yes */
  224. pprotocol = p;
  225. /* Is it TCP? */
  226. if (p - lastp != 3 || g_ascii_strncasecmp(lastp, "tcp", 3) != 0)
  227. return ""; /* not TCP */
  228. p++; /* skip the '/' */
  229. } else
  230. p = env; /* reset the pointer in
  231. case no protocol was given */
  232. /*
  233. * Step 1, find the hostname. This is delimited either by
  234. * one colon, or two colons in the case of DECnet (DECnet
  235. * Phase V allows a single colon in the hostname). (See
  236. * note above regarding IPv6 numeric addresses with
  237. * triple colons or [] brackets.)
  238. */
  239. lastp = p;
  240. lastc = NULL;
  241. for (; *p != '\0'; p++)
  242. if (*p == ':')
  243. lastc = p;
  244. if (lastc == NULL)
  245. return ""; /* must have a colon */
  246. if ((lastp != lastc) && (*(lastc - 1) == ':')
  247. && (((lastc - 1) == lastp) || (*(lastc - 2) != ':'))) {
  248. /* DECnet display specified */
  249. return "";
  250. } else
  251. hostlen = lastc - lastp;
  252. if (hostlen == 0)
  253. return ""; /* no hostname supplied */
  254. phostname = (char *)g_malloc(hostlen + 1);
  255. memcpy(phostname, lastp, hostlen);
  256. phostname[hostlen] = '\0';
  257. if (pprotocol == NULL) {
  258. /*
  259. * No protocol was explicitly specified, so it
  260. * could be a local connection over a transport
  261. * that we won't see.
  262. *
  263. * Does the host name refer to the local host?
  264. * If so, the connection would probably be a
  265. * local connection.
  266. *
  267. * XXX - compare against our host name?
  268. * _X11TransConnectDisplay() does.
  269. */
  270. if (g_ascii_strcasecmp(phostname, "localhost") == 0 ||
  271. strcmp(phostname, "127.0.0.1") == 0) {
  272. g_free(phostname);
  273. return "";
  274. }
  275. /*
  276. * A host name of "unix" (case-sensitive) also
  277. * causes a local connection.
  278. */
  279. if (strcmp(phostname, "unix") == 0) {
  280. g_free(phostname);
  281. return "";
  282. }
  283. /*
  284. * Does the host name begin with "/"? If so,
  285. * it's presumed to be the pathname of a
  286. * UNIX domain socket.
  287. */
  288. if (phostname[0] == '/') {
  289. g_free(phostname);
  290. return "";
  291. }
  292. }
  293. g_string_printf(filter_str, "not %s host %s",
  294. host_ip_af(phostname), phostname);
  295. g_free(phostname);
  296. return filter_str->str;
  297. #ifdef _WIN32
  298. } else if (GetSystemMetrics(SM_REMOTESESSION)) {
  299. /* We have a remote session: http://msdn.microsoft.com/en-us/library/aa380798%28VS.85%29.aspx */
  300. g_string_printf(filter_str, "not tcp port 3389");
  301. return filter_str->str;
  302. #endif /* _WIN32 */
  303. }
  304. return "";
  305. }
  306. /*
  307. * Editor modelines
  308. *
  309. * Local Variables:
  310. * c-basic-offset: 4
  311. * tab-width: 8
  312. * indent-tabs-mode: nil
  313. * End:
  314. *
  315. * ex: set shiftwidth=4 tabstop=8 expandtab:
  316. * :indentSize=4:tabSize=8:noTabs=true:
  317. */