]> The Tcpdump Group git mirrors - libpcap/commitdiff
Don't have pcap_create_common() set opt.device.
authorGuy Harris <[email protected]>
Thu, 30 Jun 2016 08:42:49 +0000 (01:42 -0700)
committerGuy Harris <[email protected]>
Thu, 30 Jun 2016 08:42:49 +0000 (01:42 -0700)
Instead, have pcap_create() do so.

Also have pcap_create() on Windows handle converting a little-endian
UCS-2/UTF-16 string to ASCII.

14 files changed:
pcap-bpf.c
pcap-dlpi.c
pcap-dos.c
pcap-int.h
pcap-libdlpi.c
pcap-linux.c
pcap-nit.c
pcap-null.c
pcap-pf.c
pcap-sita.c
pcap-snit.c
pcap-snoop.c
pcap-win32.c
pcap.c

index 07700299f88db70e143e9d914ed3e038afabd106..b612b04e1f2faf76301ebc56a3bd9ad2226dfbff 100644 (file)
@@ -429,11 +429,11 @@ pcap_ack_zbuf(pcap_t *p)
 #endif /* HAVE_ZEROCOPY_BPF */
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_bpf));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_bpf));
        if (p == NULL)
                return (NULL);
 
index 38f01a8e2f070c00ca3c45cf447214b99cb373ba..623c5c21854cf0544730ae2ecb7a963730814ceb 100644 (file)
@@ -1812,14 +1812,14 @@ dlpi_kread(register int fd, register off_t addr,
 #endif
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 #ifdef DL_HP_RAWDLS
        struct pcap_dlpi *pd;
 #endif
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
        if (p == NULL)
                return (NULL);
 
index c852817ca0190a7a7eeed6c4661f94a220fb7532..f42b394b06c1ed2835d6b7a27b45807b5fef065b 100644 (file)
@@ -149,11 +149,11 @@ struct pcap_dos {
        struct pcap_stat stat;
 };
 
-pcap_t *pcap_create_interface (const char *device, char *ebuf)
+pcap_t *pcap_create_interface (char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_dos));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_dos));
        if (p == NULL)
                return (NULL);
 
index 628152792647f2d3b49946c6667ececcc04f228e..d88331dfe97072f3258d9b1b8fb7c4ebf2422999 100644 (file)
@@ -446,8 +446,8 @@ int pcap_setnonblock_fd(pcap_t *p, int, char *);
  * "pcap_create_common()" allocates and fills in a pcap_t, for use
  * by pcap_create routines.
  */
-pcap_t *pcap_create_interface(const char *, char *);
-pcap_t *pcap_create_common(const char *, char *, size_t);
+pcap_t *pcap_create_interface(char *);
+pcap_t *pcap_create_common(char *, size_t);
 int    pcap_do_addexit(pcap_t *);
 void   pcap_add_to_pcaps_to_close(pcap_t *);
 void   pcap_remove_from_pcaps_to_close(pcap_t *);
index 745c0c779879f76ed7ea7c43e25b492df4f744b2..0bbc96cc0aee49809fbc314eae0b580a47b79732 100644 (file)
@@ -426,11 +426,11 @@ pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
        if (p == NULL)
                return (NULL);
 
index cae2e21a6b348342dd6d240bd7ee7cb71c89b977..ea38fff50ac1bf2dcae4087fab4c96cd41ddc8f3 100644 (file)
@@ -454,11 +454,11 @@ static struct sock_fprog  total_fcode
 #endif /* SO_ATTACH_FILTER */
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *handle;
 
-       handle = pcap_create_common(device, ebuf, sizeof (struct pcap_linux));
+       handle = pcap_create_common(ebuf, sizeof (struct pcap_linux));
        if (handle == NULL)
                return NULL;
 
index aedd12e5d9dba51b5bc5f8b2ddcbb911cd57a5b1..ce3e52a586c98c3b4d2cb0ee1a7377a044c8c917 100644 (file)
@@ -355,11 +355,11 @@ pcap_activate_nit(pcap_t *p)
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_nit));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_nit));
        if (p == NULL)
                return (NULL);
 
index 20f6f3eeed7d5a66ebb30304a0b0151d9624757b..af46b8c7a280e14533d5297b8766b58fa436b0f5 100644 (file)
@@ -36,7 +36,7 @@
 static char nosup[] = "live packet capture not supported on this system";
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        (void)strlcpy(ebuf, nosup, PCAP_ERRBUF_SIZE);
        return (NULL);
index f7e7f51f6694b76f4e93751f86efac21e75b6df6..00e9107e5f6ac7cb7d1a2532b098ef7b5bf89634 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -506,11 +506,11 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_pf));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_pf));
        if (p == NULL)
                return (NULL);
 
index 6ceb38d93235e4a03611edf92fb7b23c3e521066..123700d3e0881d025497a551a736647803473698 100644 (file)
@@ -1014,10 +1014,10 @@ static int pcap_activate_sita(pcap_t *handle) {
        return 0;
 }
 
-pcap_t *pcap_create_interface(const char *device, char *ebuf) {
+pcap_t *pcap_create_interface(char *ebuf) {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, 0);
+       p = pcap_create_common(ebuf, 0);
        if (p == NULL)
                return (NULL);
 
index f7e70ed7133c66e983a4f7c9808d93497c8669de..ae8dd8bde2a853880d7a5b55f27e79922598097c 100644 (file)
@@ -431,11 +431,11 @@ pcap_activate_snit(pcap_t *p)
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_snit));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_snit));
        if (p == NULL)
                return (NULL);
 
index 7128f0b8dfd53a44d1762964aafe93e54a3199ca..cdee48b5091bf0ea0c32b0d83dc77a4104f9f725 100644 (file)
@@ -406,11 +406,11 @@ pcap_activate_snoop(pcap_t *p)
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       p = pcap_create_common(device, ebuf, sizeof (struct pcap_snoop));
+       p = pcap_create_common(ebuf, sizeof (struct pcap_snoop));
        if (p == NULL)
                return (NULL);
 
index 2ab6e2aff88ddd0c4ce24e459f8f64ef0ccfa80f..0e380ea60c8e3fc177a2626a6fe72e8432cd68f9 100644 (file)
@@ -1062,42 +1062,11 @@ bad:
 }
 
 pcap_t *
-pcap_create_interface(const char *device, char *ebuf)
+pcap_create_interface(char *ebuf)
 {
        pcap_t *p;
 
-       if (strlen(device) == 1)
-       {
-               /*
-                * It's probably a unicode string
-                * Convert to ascii and pass it to pcap_create_common
-                *
-                * This wonderful hack is needed because pcap_lookupdev still returns
-                * unicode strings, and it's used by windump when no device is specified
-                * in the command line
-                */
-               size_t length;
-               char* deviceAscii;
-
-               length = wcslen((wchar_t*)device);
-
-               deviceAscii = (char*)malloc(length + 1);
-
-               if (deviceAscii == NULL)
-               {
-                       pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "Malloc failed");
-                       return (NULL);
-               }
-
-               pcap_snprintf(deviceAscii, length + 1, "%ws", (wchar_t*)device);
-               p = pcap_create_common(deviceAscii, ebuf, sizeof (struct pcap_win));
-               free(deviceAscii);
-       }
-       else
-       {
-               p = pcap_create_common(device, ebuf, sizeof (struct pcap_win));
-       }
-
+       p = pcap_create_common(ebuf, sizeof (struct pcap_win));
        if (p == NULL)
                return (NULL);
 
diff --git a/pcap.c b/pcap.c
index 9db58cdc249ec31adf49f27417555d3e9752bf39..701f9acee6b347c296cdf5a3cf51c2bdfcddb1ba 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -428,6 +428,7 @@ pcap_create(const char *device, char *errbuf)
        size_t i;
        int is_theirs;
        pcap_t *p;
+       char *device_str;
 
        /*
         * A null device name is equivalent to the "any" device -
@@ -437,7 +438,39 @@ pcap_create(const char *device, char *errbuf)
         * the null pointer.
         */
        if (device == NULL)
-               device = "any";
+               device_str = strdup("any");
+       else {
+#ifdef _WIN32
+               /*
+                * If the string appears to be little-endian UCS-2/UTF-16,
+                * convert it to ASCII.
+                *
+                * XXX - to UTF-8 instead?  Or report an error if any
+                * character isn't ASCII?
+                */
+               if (device[0] != '\0' && device[1] == '\0') {
+                       size_t length;
+                       char *device_ascii;
+
+                       length = wcslen((wchar_t *)device);
+                       device_str = (char *)malloc(length + 1);
+                       if (device_str == NULL) {
+                               pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE,
+                                   "malloc: %s", pcap_strerror(errno));
+                               return (NULL);
+                       }
+
+                       pcap_snprintf(device, length + 1, "%ws",
+                           (wchar_t *)device);
+               } else
+#endif
+                       device_str = strdup(device);
+       }
+       if (device_str == NULL) {
+               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                   "malloc: %s", pcap_strerror(errno));
+               return (NULL);
+       }
 
        /*
         * Try each of the non-local-network-interface capture
@@ -446,7 +479,8 @@ pcap_create(const char *device, char *errbuf)
         */
        for (i = 0; capture_source_types[i].create_op != NULL; i++) {
                is_theirs = 0;
-               p = capture_source_types[i].create_op(device, errbuf, &is_theirs);
+               p = capture_source_types[i].create_op(device_str, errbuf,
+                   &is_theirs);
                if (is_theirs) {
                        /*
                         * The device name refers to a device of the
@@ -457,6 +491,14 @@ pcap_create(const char *device, char *errbuf)
                         * should return that to report the failure
                         * to create.
                         */
+                       if (p == NULL) {
+                               /*
+                                * We assume the caller filled in errbuf.
+                                */
+                               free(device_str);
+                               return (NULL);
+                       }
+                       p->opt.device = device_str;
                        return (p);
                }
        }
@@ -464,7 +506,16 @@ pcap_create(const char *device, char *errbuf)
        /*
         * OK, try it as a regular network interface.
         */
-       return (pcap_create_interface(device, errbuf));
+       p = pcap_create_interface(errbuf);
+       if (p == NULL) {
+               /*
+                * We assume the caller filled in errbuf.
+                */
+               free(device_str);
+               return (NULL);
+       }
+       p->opt.device = device_str;
+       return (p);
 }
 #endif
 
@@ -558,7 +609,7 @@ pcap_alloc_pcap_t(char *ebuf, size_t size)
 }
 
 pcap_t *
-pcap_create_common(const char *device, char *ebuf, size_t size)
+pcap_create_common(char *ebuf, size_t size)
 {
        pcap_t *p;
 
@@ -566,14 +617,6 @@ pcap_create_common(const char *device, char *ebuf, size_t size)
        if (p == NULL)
                return (NULL);
 
-       p->opt.device = strdup(device);
-       if (p->opt.device == NULL) {
-               pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
-                   pcap_strerror(errno));
-               free(p);
-               return (NULL);
-       }
-
        /*
         * Default to "can't set rfmon mode"; if it's supported by
         * a platform, the create routine that called us can set
@@ -850,13 +893,6 @@ pcap_open_offline_common(char *ebuf, size_t size)
                return (NULL);
 
        p->opt.tstamp_precision = PCAP_TSTAMP_PRECISION_MICRO;
-       p->opt.device = strdup("(savefile)");
-       if (p->opt.device == NULL) {
-               pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
-                   pcap_strerror(errno));
-               free(p);
-               return (NULL);
-       }
 
        return (p);
 }