OSDN Git Service

Wrap NLMSG_ERROR handling into |SendMessageAndGetSingleResponse|
authorNingyuan Wang <nywang@google.com>
Tue, 20 Sep 2016 20:37:10 +0000 (13:37 -0700)
committerNingyuan Wang <nywang@google.com>
Wed, 21 Sep 2016 20:14:32 +0000 (13:14 -0700)
This also fixes a typo.

Bug: 31111084
Test: compile, unit tests, manual tests

Change-Id: I802487deedc219a6ad1ed78ea1a6b94e876e4d67

net/netlink_manager.cpp
net/netlink_manager.h
net/netlink_utils.cpp

index e17f25c..34d21bc 100644 (file)
@@ -294,12 +294,30 @@ bool NetlinkManager::SendMessageAndGetResponses(
 bool NetlinkManager::SendMessageAndGetSingleResponse(
     const NL80211Packet& packet,
     unique_ptr<const NL80211Packet>* response) {
+  unique_ptr<const NL80211Packet> response_or_error;
+  if (!SendMessageAndGetSingleResponseOrError(packet, &response_or_error)) {
+    return false;
+  }
+  if (response_or_error->GetMessageType() == NLMSG_ERROR) {
+    // We use ERROR because we are not expecting to receive a ACK here.
+    // In that case the caller should use |SendMessageAndGetAckOrError|.
+    LOG(ERROR) << "Received error message: "
+               << strerror(response_or_error->GetErrorCode());
+    return false;
+  }
+  *response = std::move(response_or_error);
+  return true;
+}
+
+bool NetlinkManager::SendMessageAndGetSingleResponseOrError(
+    const NL80211Packet& packet,
+    unique_ptr<const NL80211Packet>* response) {
   vector<unique_ptr<const NL80211Packet>> response_vec;
   if (!SendMessageAndGetResponses(packet, &response_vec)) {
     return false;
   }
   if (response_vec.size() != 1) {
-    LOG(ERROR) << "Unexpected repsonse size: " << response_vec.size();
+    LOG(ERROR) << "Unexpected response size: " << response_vec.size();
     return false;
   }
 
@@ -310,7 +328,7 @@ bool NetlinkManager::SendMessageAndGetSingleResponse(
 bool NetlinkManager::SendMessageAndGetAckOrError(const NL80211Packet& packet,
                                                  int* error_code) {
   unique_ptr<const NL80211Packet> response;
-  if (!SendMessageAndGetSingleResponse(packet, &response)) {
+  if (!SendMessageAndGetSingleResponseOrError(packet, &response)) {
     return false;
   }
   uint16_t type = response->GetMessageType();
@@ -407,11 +425,6 @@ bool NetlinkManager::DiscoverFamilyId() {
     LOG(ERROR) << "Failed to get NL80211 family info";
     return false;
   }
-  if (response->GetMessageType() == NLMSG_ERROR) {
-      LOG(ERROR) << "Receive ERROR message: "
-                 << strerror(response->GetErrorCode());
-      return false;
-  }
   OnNewFamily(std::move(response));
   if (message_types_.find(NL80211_GENL_NAME) == message_types_.end()) {
     LOG(ERROR) << "Failed to get NL80211 family id";
index c61fb6d..64a489a 100644 (file)
@@ -90,10 +90,22 @@ class NetlinkManager {
   // Wrapper of |SendMessageAndGetResponses| for messages with a single
   // response.
   // Returns true on successfully receiving an valid reply.
+  // This will returns false if a NLMSG_ERROR is received.
   // Reply packet will be stored in |*response|.
   virtual bool SendMessageAndGetSingleResponse(
       const NL80211Packet& packet,
       std::unique_ptr<const NL80211Packet>* response);
+
+  // Wrapper of |SendMessageAndGetResponses| for messages with a single
+  // response.
+  // Returns true on successfully receiving an valid reply.
+  // This will returns true if a NLMSG_ERROR is received.
+  // This is useful when the caller needs the error code from kernel.
+  // Reply packet will be stored in |*response|.
+  virtual bool SendMessageAndGetSingleResponseOrError(
+      const NL80211Packet& packet,
+      std::unique_ptr<const NL80211Packet>* response);
+
   // Wrapper of |SendMessageAndGetResponses| for messages that trigger
   // only a NLMSG_ERROR response
   // Returns true if the message is successfully sent and a NLMSG_ERROR response
index 6534f17..674da73 100644 (file)
@@ -189,12 +189,6 @@ bool NetlinkUtils::GetWiphyInfo(
     LOG(ERROR) << "Failed to get scan capabilities";
     return false;
   }
-  if (response->GetMessageType() == NLMSG_ERROR) {
-    LOG(ERROR) << "Receive ERROR message: "
-               << strerror(response->GetErrorCode())
-               << "in response to a GetWiphy request";
-    return false;
-  }
   if (response->GetCommand() != NL80211_CMD_NEW_WIPHY) {
     LOG(ERROR) << "Wrong command in response to a get wiphy request: "
                << static_cast<int>(response->GetCommand());
@@ -319,13 +313,6 @@ bool NetlinkUtils::GetStationInfo(uint32_t interface_index,
     LOG(ERROR) << "Failed to get packet counters";
     return false;
   }
-  if (response->GetMessageType() == NLMSG_ERROR) {
-    LOG(ERROR) << "Receive ERROR message: "
-               << strerror(response->GetErrorCode())
-               << "in response to a get station request";
-    return false;
-  }
-
   if (response->GetCommand() != NL80211_CMD_NEW_STATION) {
     LOG(ERROR) << "Wrong command in response to a get station request: "
                << static_cast<int>(response->GetCommand());