aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Vijay Pai <vpai@google.com>2015-08-03 10:43:24 -0700
committerGravatar Vijay Pai <vpai@google.com>2015-08-03 10:43:24 -0700
commit0a9ca86c35ddea533c5a0d889ca78c1f623015fd (patch)
treea98cf1484902197e9d91a253da013785612a7b6a
parente2e8eacb3e93f64abdfbf234679caea2ef400103 (diff)
parentb803313b2c39d6d0f2ed64253db89c910a8c39e2 (diff)
Merge pull request #2702 from yang-g/peeraddr
Add accessor peer() in ClientContext and ServerContext
-rw-r--r--include/grpc++/client_context.h6
-rw-r--r--include/grpc++/server_context.h6
-rw-r--r--src/cpp/client/client_context.cc11
-rw-r--r--src/cpp/server/server_context.cc11
-rw-r--r--test/cpp/end2end/end2end_test.cc29
-rw-r--r--test/cpp/util/messages.proto2
6 files changed, 64 insertions, 1 deletions
diff --git a/include/grpc++/client_context.h b/include/grpc++/client_context.h
index 5cf4d3328a..10c967d85b 100644
--- a/include/grpc++/client_context.h
+++ b/include/grpc++/client_context.h
@@ -118,6 +118,12 @@ class ClientContext {
std::shared_ptr<const AuthContext> auth_context() const;
+ // Return the peer uri in a string.
+ // WARNING: this value is never authenticated or subject to any security
+ // related code. It must not be used for any authentication related
+ // functionality. Instead, use auth_context.
+ grpc::string peer() const;
+
// Get and set census context
void set_census_context(struct census_context* ccp) { census_context_ = ccp; }
struct census_context* census_context() const { return census_context_; }
diff --git a/include/grpc++/server_context.h b/include/grpc++/server_context.h
index e204d66456..cf2732b33d 100644
--- a/include/grpc++/server_context.h
+++ b/include/grpc++/server_context.h
@@ -117,6 +117,12 @@ class ServerContext {
std::shared_ptr<const AuthContext> auth_context() const;
+ // Return the peer uri in a string.
+ // WARNING: this value is never authenticated or subject to any security
+ // related code. It must not be used for any authentication related
+ // functionality. Instead, use auth_context.
+ grpc::string peer() const;
+
const struct census_context* census_context() const;
private:
diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc
index 14ab772e50..c38d0c1df6 100644
--- a/src/cpp/client/client_context.cc
+++ b/src/cpp/client/client_context.cc
@@ -34,6 +34,7 @@
#include <grpc++/client_context.h>
#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/string_util.h>
#include <grpc++/credentials.h>
#include <grpc++/time.h>
@@ -104,4 +105,14 @@ void ClientContext::TryCancel() {
}
}
+grpc::string ClientContext::peer() const {
+ grpc::string peer;
+ if (call_) {
+ char* c_peer = grpc_call_get_peer(call_);
+ peer = c_peer;
+ gpr_free(c_peer);
+ }
+ return peer;
+}
+
} // namespace grpc
diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc
index f6c073040b..cf19556e7a 100644
--- a/src/cpp/server/server_context.cc
+++ b/src/cpp/server/server_context.cc
@@ -34,6 +34,7 @@
#include <grpc++/server_context.h>
#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc++/impl/call.h>
#include <grpc++/impl/sync.h>
@@ -179,6 +180,16 @@ std::shared_ptr<const AuthContext> ServerContext::auth_context() const {
return auth_context_;
}
+grpc::string ServerContext::peer() const {
+ grpc::string peer;
+ if (call_) {
+ char* c_peer = grpc_call_get_peer(call_);
+ peer = c_peer;
+ gpr_free(c_peer);
+ }
+ return peer;
+}
+
const struct census_context* ServerContext::census_context() const {
return grpc_census_call_get_context(call_);
}
diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc
index 5b351c169e..f39c6cf82a 100644
--- a/test/cpp/end2end/end2end_test.cc
+++ b/test/cpp/end2end/end2end_test.cc
@@ -93,6 +93,15 @@ void CheckServerAuthContext(const ServerContext* context) {
EXPECT_TRUE(auth_ctx->GetPeerIdentity().empty());
}
+bool CheckIsLocalhost(const grpc::string& addr) {
+ const grpc::string kIpv6("ipv6:[::1]:");
+ const grpc::string kIpv4MappedIpv6("ipv6:[::ffff:127.0.0.1]:");
+ const grpc::string kIpv4("ipv4:127.0.0.1:");
+ return addr.substr(0, kIpv4.size()) == kIpv4 ||
+ addr.substr(0, kIpv4MappedIpv6.size()) == kIpv4MappedIpv6 ||
+ addr.substr(0, kIpv6.size()) == kIpv6;
+}
+
} // namespace
class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
@@ -148,6 +157,9 @@ class TestServiceImpl : public ::grpc::cpp::test::util::TestService::Service {
response->set_message(
grpc::string(request->param().response_message_length(), '\0'));
}
+ if (request->has_param() && request->param().echo_peer()) {
+ response->mutable_param()->set_peer(context->peer());
+ }
return Status::OK;
}
@@ -236,7 +248,7 @@ class End2endTest : public ::testing::Test {
void SetUp() GRPC_OVERRIDE {
int port = grpc_pick_unused_port_or_die();
- server_address_ << "localhost:" << port;
+ server_address_ << "127.0.0.1:" << port;
// Setup server
ServerBuilder builder;
SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
@@ -818,6 +830,21 @@ TEST_F(End2endTest, HugeResponse) {
EXPECT_TRUE(s.ok());
}
+TEST_F(End2endTest, Peer) {
+ ResetStub();
+ EchoRequest request;
+ EchoResponse response;
+ request.set_message("hello");
+ request.mutable_param()->set_echo_peer(true);
+
+ ClientContext context;
+ Status s = stub_->Echo(&context, request, &response);
+ EXPECT_EQ(response.message(), request.message());
+ EXPECT_TRUE(s.ok());
+ EXPECT_TRUE(CheckIsLocalhost(response.param().peer()));
+ EXPECT_TRUE(CheckIsLocalhost(context.peer()));
+}
+
} // namespace testing
} // namespace grpc
diff --git a/test/cpp/util/messages.proto b/test/cpp/util/messages.proto
index 2fad8b42a2..24e199b809 100644
--- a/test/cpp/util/messages.proto
+++ b/test/cpp/util/messages.proto
@@ -39,6 +39,7 @@ message RequestParams {
optional bool echo_metadata = 4;
optional bool check_auth_context = 5;
optional int32 response_message_length = 6;
+ optional bool echo_peer = 7;
}
message EchoRequest {
@@ -49,6 +50,7 @@ message EchoRequest {
message ResponseParams {
optional int64 request_deadline = 1;
optional string host = 2;
+ optional string peer = 3;
}
message EchoResponse {