Skip to content
This repository was archived by the owner on Mar 30, 2020. It is now read-only.

Commit ae23c27

Browse files
committed
Fix messy extern "C" related issues.
1 parent 9645932 commit ae23c27

File tree

8 files changed

+34
-42
lines changed

8 files changed

+34
-42
lines changed

src/connection_cache.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
extern "C" {
2323
#include <linux/inet_diag.h>
24-
#include <sys/socket.h>
24+
#include <sys/socket.h> // For AF_INET
2525
}
2626

2727
namespace mlab {

src/tcpinfo.proto

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ enum TCPState { // from tcp_states.h
8282

8383
message InetDiagMsgProto {
8484
enum AddressFamily {
85+
// NOTE: these are equivalent to AF_... in socket.h, but cannot have the
86+
// same names since those are macros and will cause collisions.
8587
// There are many other families, but for now we only care about these.
86-
AF_UNSPEC = 0;
87-
AF_INET = 2;
88-
AF_INET6 = 10;
88+
UNSPEC = 0;
89+
INET = 2;
90+
INET6 = 10;
8991
}
9092
// These are 8 bit unsigned.
9193
optional AddressFamily family = 1;

src/tcpinfo_c_adapter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "tcpinfo_c_adapter.h"
1919

2020
#include <arpa/inet.h>
21+
#include <linux/inet_diag.h> // for INET_DIAG_...
2122
#include <linux/sock_diag.h>
2223
#include <unistd.h>
2324

src/tcpinfo_c_adapter.h

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef TCPINFO_C_ADAPTER_H_
2+
#define TCPINFO_C_ADAPTER_H_
3+
14
// Copyright 2016 measurement-lab
25
//
36
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,24 +16,14 @@
1316
// limitations under the License.
1417

1518
/****************************************************************************
16-
* This file contains declarations that are used by both .c files and .cc
17-
* files. When included in .cc files, it must be enclosed in an extern "C"
18-
* block.
19-
*
20-
* fetch_tcpinfo(...) is implemented in the tcpinfo_c_adapter.c for easy
21-
* access to C libraries.
22-
* update_record(...) is implemented in tcpinfo_lib.cc, because it requires
23-
* access to C++ functions.
19+
* fetch_tcpinfo() is called from C++, but implemented in C to provide clear
20+
* linkage to C library functions.
2421
****************************************************************************/
2522

26-
#ifndef TCPINFO_C_ADAPTER_H_
27-
#define TCPINFO_C_ADAPTER_H_
28-
29-
#include <linux/inet_diag.h>
30-
#include <linux/netlink.h>
31-
#include <sys/socket.h>
32-
33-
#include "libnetlink.h"
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
#include "libnetlink.h" // For rtnl_filter_t
3427

3528
struct inet_diag_arg {
3629
int protocol;
@@ -40,9 +33,7 @@ struct inet_diag_arg {
4033
// `callback` has rtnl_filter_t signature, and is passed to rtnl_dump_filter.
4134
int fetch_tcpinfo(rtnl_filter_t callback);
4235

43-
// rtnl_filter_t function to handle each result, passed into fetch_tcpinfo()
44-
// function. Implementation in tcpinfo_lib.cc
45-
int update_record(const struct sockaddr_nl *addr,
46-
struct nlmsghdr *nlh, void *arg);
47-
36+
#ifdef __cplusplus
37+
}
38+
#endif
4839
#endif // TCPINFO_C_ADAPTER_H_

src/tcpinfo_lib.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#include <string>
1919

2020
#include "connection_cache.h" // For ConnectionTracker
21+
#include "tcpinfo_c_adapter.h" // For update_record, inet_diag_arg
2122

2223
extern "C" {
2324
#include <arpa/inet.h>
2425
#include <linux/sock_diag.h>
2526
#include <linux/tcp.h>
2627

2728
#include "libnetlink.h"
28-
#include "tcpinfo_c_adapter.h" // declaration of update_record.
2929
}
3030

3131
namespace mlab {
@@ -56,7 +56,7 @@ InetDiagMsgProto::AddressFamily GetFamily(struct inet_diag_msg* r) {
5656
auto family = r->idiag_family;
5757
if (!InetDiagMsgProto_AddressFamily_IsValid(family)) {
5858
fprintf(stderr, "Invalid family: %d\n", family);
59-
return InetDiagMsgProto_AddressFamily_AF_UNSPEC;
59+
return InetDiagMsgProto_AddressFamily_UNSPEC;
6060
} else {
6161
return InetDiagMsgProto::AddressFamily(family);
6262
}
@@ -81,15 +81,15 @@ void ParseInetDiagMsg(struct inet_diag_msg* r, InetDiagMsgProto* proto) {
8181
dest->set_port(ntohs(r->id.idiag_dport));
8282

8383
switch (proto->family()) {
84-
case InetDiagMsgProto_AddressFamily_AF_INET:
84+
case InetDiagMsgProto_AddressFamily_INET:
8585
src->set_ip(r->id.idiag_src, 4);
8686
dest->set_ip(r->id.idiag_dst, 4);
8787
break;
88-
case InetDiagMsgProto_AddressFamily_AF_INET6:
88+
case InetDiagMsgProto_AddressFamily_INET6:
8989
src->set_ip(r->id.idiag_src, 16);
9090
dest->set_ip(r->id.idiag_dst, 16);
9191
break;
92-
case InetDiagMsgProto_AddressFamily_AF_UNSPEC:
92+
case InetDiagMsgProto_AddressFamily_UNSPEC:
9393
// We don't know how to interpret the addresses, so leave them unset.
9494
// TODO(gfr) Log a warning here.
9595
break;

src/tcpinfo_lib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ extern "C" {
3636
namespace mlab {
3737
namespace netlink {
3838

39+
extern "C"
40+
// rtnl_filter_t function to handle each result, passed into fetch_tcpinfo()
41+
// function.
42+
int update_record(const struct sockaddr_nl *addr,
43+
struct nlmsghdr *nlh, void *arg);
44+
3945
class TCPInfoPoller;
4046
namespace test {
4147
// Test support function that required friend status.

src/tcpinfo_lib_test.cc

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <string>
16-
17-
#include "gtest/gtest.h"
18-
#include "connection_cache.h"
19-
// TODO(gfr) The include order matters. If tcpinfo_lib.h is included later,
20-
// we get obscure compiler errors processing tcpinfo.pb.h.
2115
#include "tcpinfo_lib.h"
2216

23-
extern "C" {
24-
#include <netinet/in.h> // IPPROTO_TCP
25-
#include <sys/socket.h> // AF_INET*
17+
#include <string>
2618

19+
#include "gtest/gtest.h"
2720
#include "tcpinfo_c_adapter.h"
28-
}
2921

3022
extern mlab::netlink::TCPInfoPoller g_poller_;
3123

src/tcpinfo_proto_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,15 @@ std::string raw19(
403403

404404
TEST(Parser, IPToString) {
405405
InetDiagMsgProto p4;
406-
p4.set_family(InetDiagMsgProto_AddressFamily_AF_INET);
406+
p4.set_family(InetDiagMsgProto_AddressFamily_INET);
407407
auto* sock_id = p4.mutable_sock_id();
408408
auto* src = sock_id->mutable_source();
409409
src->set_port(1234);
410410
src->set_ip("abcd", 4);
411411
EXPECT_EQ(ToString(p4.sock_id().source()), "97.98.99.100:1234");
412412

413413
InetDiagMsgProto p6;
414-
p6.set_family(InetDiagMsgProto_AddressFamily_AF_INET6);
414+
p6.set_family(InetDiagMsgProto_AddressFamily_INET6);
415415
auto* sock_id6 = p6.mutable_sock_id();
416416
auto* src6 = sock_id6->mutable_source();
417417
src6->set_port(5678);

0 commit comments

Comments
 (0)