[ovs-dev] [PATCH] lib: Rename lib/pcap.h to avoid inclusion conflicts.
Stephane A. Sezer
sas at cd80.net
Fri Mar 15 09:19:31 UTC 2013
From: "Stephane A. Sezer" <sas at cd80.net>
lib/pcap.h has a name that conflicts with /usr/include/pcap.h. When one
wants to include pcap.h from libpcap (i.e.: the one from /usr/include), one
may end up with pcap.h from openvswitch.
This change renames this header to pcap-file.h and updates all
references to this file.
This change was tested with `make distcheck`.
Signed-off-by: Stephane A. Sezer <sas at cd80.net>
---
AUTHORS | 1 +
lib/automake.mk | 4 +-
lib/ofp-print.c | 1 -
lib/pcap-file.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/pcap-file.h | 30 ++++++++++
lib/pcap.c | 163 -----------------------------------------------------
lib/pcap.h | 30 ----------
tests/test-flows.c | 2 +-
8 files changed, 197 insertions(+), 197 deletions(-)
create mode 100644 lib/pcap-file.c
create mode 100644 lib/pcap-file.h
delete mode 100644 lib/pcap.c
delete mode 100644 lib/pcap.h
diff --git a/AUTHORS b/AUTHORS
index 9613142..bd0f499 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -71,6 +71,7 @@ Sanjay Sane ssane at nicira.com
Shan Wei davidshan at tencent.com
Shih-Hao Li shli at nicira.com
Simon Horman horms at verge.net.au
+Stephane A. Sezer sas at cd80.net
SUGYO Kazushi sugyo.org at gmail.com
Tadaaki Nagao nagao at stratosphere.co.jp
Tetsuo NAKAGAWA nakagawa at mxc.nes.nec.co.jp
diff --git a/lib/automake.mk b/lib/automake.mk
index ce3edc3..1d58604 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -130,8 +130,8 @@ lib_libopenvswitch_a_SOURCES = \
lib/ovsdb-types.h \
lib/packets.c \
lib/packets.h \
- lib/pcap.c \
- lib/pcap.h \
+ lib/pcap-file.c \
+ lib/pcap-file.h \
lib/poll-loop.c \
lib/poll-loop.h \
lib/process.c \
diff --git a/lib/ofp-print.c b/lib/ofp-print.c
index f7872cb..95d9b73 100644
--- a/lib/ofp-print.c
+++ b/lib/ofp-print.c
@@ -44,7 +44,6 @@
#include "openflow/openflow.h"
#include "openflow/nicira-ext.h"
#include "packets.h"
-#include "pcap.h"
#include "type-props.h"
#include "unaligned.h"
#include "util.h"
diff --git a/lib/pcap-file.c b/lib/pcap-file.c
new file mode 100644
index 0000000..d137be8
--- /dev/null
+++ b/lib/pcap-file.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2009, 2010, 2012 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include "pcap-file.h"
+#include <errno.h>
+#include <inttypes.h>
+#include <string.h>
+#include "compiler.h"
+#include "ofpbuf.h"
+#include "vlog.h"
+
+VLOG_DEFINE_THIS_MODULE(pcap);
+
+struct pcap_hdr {
+ uint32_t magic_number; /* magic number */
+ uint16_t version_major; /* major version number */
+ uint16_t version_minor; /* minor version number */
+ int32_t thiszone; /* GMT to local correction */
+ uint32_t sigfigs; /* accuracy of timestamps */
+ uint32_t snaplen; /* max length of captured packets */
+ uint32_t network; /* data link type */
+};
+BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
+
+struct pcaprec_hdr {
+ uint32_t ts_sec; /* timestamp seconds */
+ uint32_t ts_usec; /* timestamp microseconds */
+ uint32_t incl_len; /* number of octets of packet saved in file */
+ uint32_t orig_len; /* actual length of packet */
+};
+BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
+
+FILE *
+pcap_open(const char *file_name, const char *mode)
+{
+ FILE *file;
+
+ ovs_assert(!strcmp(mode, "rb") || !strcmp(mode, "wb"));
+
+ file = fopen(file_name, mode);
+ if (file == NULL) {
+ VLOG_WARN("%s: failed to open pcap file for %s",
+ file_name, mode[0] == 'r' ? "reading" : "writing");
+ return NULL;
+ }
+
+ if (mode[0] == 'r') {
+ if (!pcap_read_header(file)) {
+ fclose(file);
+ return NULL;
+ }
+ } else {
+ pcap_write_header(file);
+ }
+ return file;
+}
+
+int
+pcap_read_header(FILE *file)
+{
+ struct pcap_hdr ph;
+ if (fread(&ph, sizeof ph, 1, file) != 1) {
+ int error = ferror(file) ? errno : EOF;
+ VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
+ return error;
+ }
+ if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
+ VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
+ "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
+ return EPROTO;
+ }
+ return 0;
+}
+
+void
+pcap_write_header(FILE *file)
+{
+ /* The pcap reader is responsible for figuring out endianness based on the
+ * magic number, so the lack of htonX calls here is intentional. */
+ struct pcap_hdr ph;
+ ph.magic_number = 0xa1b2c3d4;
+ ph.version_major = 2;
+ ph.version_minor = 4;
+ ph.thiszone = 0;
+ ph.sigfigs = 0;
+ ph.snaplen = 1518;
+ ph.network = 1; /* Ethernet */
+ ignore(fwrite(&ph, sizeof ph, 1, file));
+}
+
+int
+pcap_read(FILE *file, struct ofpbuf **bufp)
+{
+ struct pcaprec_hdr prh;
+ struct ofpbuf *buf;
+ void *data;
+ size_t len;
+
+ *bufp = NULL;
+
+ /* Read header. */
+ if (fread(&prh, sizeof prh, 1, file) != 1) {
+ int error = ferror(file) ? errno : EOF;
+ VLOG_WARN("failed to read pcap record header: %s",
+ ovs_retval_to_string(error));
+ return error;
+ }
+
+ /* Calculate length. */
+ len = prh.incl_len;
+ if (len > 0xffff) {
+ uint32_t swapped_len = (((len & 0xff000000) >> 24) |
+ ((len & 0x00ff0000) >> 8) |
+ ((len & 0x0000ff00) << 8) |
+ ((len & 0x000000ff) << 24));
+ if (swapped_len > 0xffff) {
+ VLOG_WARN("bad packet length %zu or %"PRIu32" "
+ "reading pcap file",
+ len, swapped_len);
+ return EPROTO;
+ }
+ len = swapped_len;
+ }
+
+ /* Read packet. */
+ buf = ofpbuf_new(len);
+ data = ofpbuf_put_uninit(buf, len);
+ if (fread(data, len, 1, file) != 1) {
+ int error = ferror(file) ? errno : EOF;
+ VLOG_WARN("failed to read pcap packet: %s",
+ ovs_retval_to_string(error));
+ ofpbuf_delete(buf);
+ return error;
+ }
+ *bufp = buf;
+ return 0;
+}
+
+void
+pcap_write(FILE *file, struct ofpbuf *buf)
+{
+ struct pcaprec_hdr prh;
+ prh.ts_sec = 0;
+ prh.ts_usec = 0;
+ prh.incl_len = buf->size;
+ prh.orig_len = buf->size;
+ ignore(fwrite(&prh, sizeof prh, 1, file));
+ ignore(fwrite(buf->data, buf->size, 1, file));
+}
diff --git a/lib/pcap-file.h b/lib/pcap-file.h
new file mode 100644
index 0000000..46625c3
--- /dev/null
+++ b/lib/pcap-file.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (c) 2009 Nicira, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PCAP_FILE_H
+#define PCAP_FILE_H 1
+
+#include <stdio.h>
+
+struct ofpbuf;
+
+FILE *pcap_open(const char *file_name, const char *mode);
+int pcap_read_header(FILE *);
+void pcap_write_header(FILE *);
+int pcap_read(FILE *, struct ofpbuf **);
+void pcap_write(FILE *, struct ofpbuf *);
+
+#endif /* pcap-file.h */
diff --git a/lib/pcap.c b/lib/pcap.c
deleted file mode 100644
index 1033ba9..0000000
--- a/lib/pcap.c
+++ /dev/null
@@ -1,163 +0,0 @@
-/*
- * Copyright (c) 2009, 2010, 2012 Nicira, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <config.h>
-#include "pcap.h"
-#include <errno.h>
-#include <inttypes.h>
-#include <string.h>
-#include "compiler.h"
-#include "ofpbuf.h"
-#include "vlog.h"
-
-VLOG_DEFINE_THIS_MODULE(pcap);
-
-struct pcap_hdr {
- uint32_t magic_number; /* magic number */
- uint16_t version_major; /* major version number */
- uint16_t version_minor; /* minor version number */
- int32_t thiszone; /* GMT to local correction */
- uint32_t sigfigs; /* accuracy of timestamps */
- uint32_t snaplen; /* max length of captured packets */
- uint32_t network; /* data link type */
-};
-BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
-
-struct pcaprec_hdr {
- uint32_t ts_sec; /* timestamp seconds */
- uint32_t ts_usec; /* timestamp microseconds */
- uint32_t incl_len; /* number of octets of packet saved in file */
- uint32_t orig_len; /* actual length of packet */
-};
-BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
-
-FILE *
-pcap_open(const char *file_name, const char *mode)
-{
- FILE *file;
-
- ovs_assert(!strcmp(mode, "rb") || !strcmp(mode, "wb"));
-
- file = fopen(file_name, mode);
- if (file == NULL) {
- VLOG_WARN("%s: failed to open pcap file for %s",
- file_name, mode[0] == 'r' ? "reading" : "writing");
- return NULL;
- }
-
- if (mode[0] == 'r') {
- if (!pcap_read_header(file)) {
- fclose(file);
- return NULL;
- }
- } else {
- pcap_write_header(file);
- }
- return file;
-}
-
-int
-pcap_read_header(FILE *file)
-{
- struct pcap_hdr ph;
- if (fread(&ph, sizeof ph, 1, file) != 1) {
- int error = ferror(file) ? errno : EOF;
- VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
- return error;
- }
- if (ph.magic_number != 0xa1b2c3d4 && ph.magic_number != 0xd4c3b2a1) {
- VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
- "(expected 0xa1b2c3d4 or 0xd4c3b2a1)", ph.magic_number);
- return EPROTO;
- }
- return 0;
-}
-
-void
-pcap_write_header(FILE *file)
-{
- /* The pcap reader is responsible for figuring out endianness based on the
- * magic number, so the lack of htonX calls here is intentional. */
- struct pcap_hdr ph;
- ph.magic_number = 0xa1b2c3d4;
- ph.version_major = 2;
- ph.version_minor = 4;
- ph.thiszone = 0;
- ph.sigfigs = 0;
- ph.snaplen = 1518;
- ph.network = 1; /* Ethernet */
- ignore(fwrite(&ph, sizeof ph, 1, file));
-}
-
-int
-pcap_read(FILE *file, struct ofpbuf **bufp)
-{
- struct pcaprec_hdr prh;
- struct ofpbuf *buf;
- void *data;
- size_t len;
-
- *bufp = NULL;
-
- /* Read header. */
- if (fread(&prh, sizeof prh, 1, file) != 1) {
- int error = ferror(file) ? errno : EOF;
- VLOG_WARN("failed to read pcap record header: %s",
- ovs_retval_to_string(error));
- return error;
- }
-
- /* Calculate length. */
- len = prh.incl_len;
- if (len > 0xffff) {
- uint32_t swapped_len = (((len & 0xff000000) >> 24) |
- ((len & 0x00ff0000) >> 8) |
- ((len & 0x0000ff00) << 8) |
- ((len & 0x000000ff) << 24));
- if (swapped_len > 0xffff) {
- VLOG_WARN("bad packet length %zu or %"PRIu32" "
- "reading pcap file",
- len, swapped_len);
- return EPROTO;
- }
- len = swapped_len;
- }
-
- /* Read packet. */
- buf = ofpbuf_new(len);
- data = ofpbuf_put_uninit(buf, len);
- if (fread(data, len, 1, file) != 1) {
- int error = ferror(file) ? errno : EOF;
- VLOG_WARN("failed to read pcap packet: %s",
- ovs_retval_to_string(error));
- ofpbuf_delete(buf);
- return error;
- }
- *bufp = buf;
- return 0;
-}
-
-void
-pcap_write(FILE *file, struct ofpbuf *buf)
-{
- struct pcaprec_hdr prh;
- prh.ts_sec = 0;
- prh.ts_usec = 0;
- prh.incl_len = buf->size;
- prh.orig_len = buf->size;
- ignore(fwrite(&prh, sizeof prh, 1, file));
- ignore(fwrite(buf->data, buf->size, 1, file));
-}
diff --git a/lib/pcap.h b/lib/pcap.h
deleted file mode 100644
index 43a4306..0000000
--- a/lib/pcap.h
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * Copyright (c) 2009 Nicira, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef PCAP_H
-#define PCAP_H 1
-
-#include <stdio.h>
-
-struct ofpbuf;
-
-FILE *pcap_open(const char *file_name, const char *mode);
-int pcap_read_header(FILE *);
-void pcap_write_header(FILE *);
-int pcap_read(FILE *, struct ofpbuf **);
-void pcap_write(FILE *, struct ofpbuf *);
-
-#endif /* dhcp.h */
diff --git a/tests/test-flows.c b/tests/test-flows.c
index b4dedee..c77372f 100644
--- a/tests/test-flows.c
+++ b/tests/test-flows.c
@@ -25,7 +25,7 @@
#include "ofpbuf.h"
#include "ofp-print.h"
#include "ofp-util.h"
-#include "pcap.h"
+#include "pcap-file.h"
#include "util.h"
#include "vlog.h"
--
Stephane A. Sezer
More information about the dev
mailing list