Hello All,
I am seeing this seg fault, I have modified the existing example pro…gram and below is the details of my pgm and the proto file.
Can you let me know what is the issue here? what am I doing wrong.
BT
```
Thread 1 "mytest_async_clie" received signal SIGSEGV, Segmentation fault.
__GI_getenv (name=0x7ffff570eac1 "CALDOMAIN") at getenv.c:84
84 getenv.c: No such file or directory.
(gdb) where
#0 __GI_getenv (name=0x7ffff570eac1 "CALDOMAIN") at getenv.c:84
#1 0x00007ffff5706614 in ares_init_options () from /root/grpc/install/lib/libcares.so.2
#2 0x00007ffff78a42a7 in grpc_ares_ev_driver_create_locked(grpc_ares_ev_driver**, grpc_pollset_set*, int, grpc_core::Combiner*, grpc_ares_request*) () from /root/grpc/install/lib/libgrpc.so.9
#3 0x00007ffff78a54b4 in grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked(grpc_ares_request*, char const*, char const*, char const*, grpc_pollset_set*, int, grpc_core::Combiner*) ()
from /root/grpc/install/lib/libgrpc.so.9
#4 0x00007ffff78a602f in ?? () from /root/grpc/install/lib/libgrpc.so.9
#5 0x00007ffff78a2179 in ?? () from /root/grpc/install/lib/libgrpc.so.9
#6 0x00007ffff7862f82 in grpc_core::ResolvingLoadBalancingPolicy::ResolvingLoadBalancingPolicy(grpc_core::LoadBalancingPolicy::Args, grpc_core::TraceFlag*, std::unique_ptr<char, grpc_core::DefaultDeleteChar>, bool (*)(void*, grpc_core::Resolver::Result const&, char const**, grpc_core::RefCountedPtr<grpc_core::LoadBalancingPolicy::Config>*, grpc_error**), void*) () from /root/grpc/install/lib/libgrpc.so.9
#7 0x00007ffff784f907 in ?? () from /root/grpc/install/lib/libgrpc.so.9
#8 0x00007ffff77cac36 in grpc_combiner_continue_exec_ctx() () from /root/grpc/install/lib/libgrpc.so.9
#9 0x00007ffff77d3ec3 in grpc_core::ExecCtx::Flush() () from /root/grpc/install/lib/libgrpc.so.9
#10 0x00007ffff77f4eed in grpc_call_start_batch () from /root/grpc/install/lib/libgrpc.so.9
#11 0x0000555555575f85 in grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata, grpc::internal::CallOpSendMessage, grpc::internal::CallOpClientSendClose, grpc::internal::CallOpRecvInitialMetadata, grpc::internal::CallOpRecvMessage<benu_cp_pkts::ReceiveReport>, grpc::internal::CallOpClientRecvStatus>::ContinueFillOpsAfterInterception (this=0x5555557b6828)
at /root/grpc/install/include/grpcpp/impl/codegen/call_op_set.h:938
#12 0x0000555555575d18 in grpc::internal::CallOpSet<grpc::internal::CallOpSendInitialMetadata, grpc::internal::CallOpSendMessage, grpc::internal::CallOpClientSendClose, grpc::internal::CallOpRecvInitialMetadata, grpc::internal::CallOpRecvMessage<benu_cp_pkts::ReceiveReport>, grpc::internal::CallOpClientRecvStatus>::FillOps (this=0x5555557b6828, call=0x5555557b67f0)
at /root/grpc/install/include/grpcpp/impl/codegen/call_op_set.h:868
#13 0x0000555555566411 in grpc::internal::Call::PerformOps (this=0x5555557b67f0, ops=0x5555557b6828) at /root/grpc/install/include/grpcpp/impl/codegen/call.h:69
#14 0x00005555555754ef in grpc_impl::ClientAsyncResponseReader<benu_cp_pkts::ReceiveReport>::Finish (this=0x5555557b67e0, msg=0x7fffffffd8f0, status=0x7fffffffd930, tag=0x1)
at /root/grpc/install/include/grpcpp/impl/codegen/async_unary_call_impl.h:150
#15 0x000055555557a2fb in myTestCPClient::SendCPPacket (this=0x7fffffffdbc0, request=...) at benu_async_client.cc:100
#16 0x0000555555579ab2 in main (argc=1, argv=0x7fffffffe488) at benu_async_client.cc:161
```
Code modified from greeter_async_client.cc
```
/*
*
* Copyright 2015 gRPC authors.
*
* 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 <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include <grpc/support/log.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "mytestTest.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientAsyncResponseReader;
using grpc::ClientContext;
using grpc::CompletionQueue;
using grpc::Status;
using mytest_cp_pkts::ReceiveReport;
using mytest_cp_pkts::CPProcessPkt;
using mytest_cp_pkts::CP_PACKETS_RX_TX;
using mytest_cp_pkts::PacketType;
using mytest_cp_pkts::EventType;
using mytest_cp_pkts::Report;
class myTestCPClient {
public:
explicit myTestCPClient(std::shared_ptr<Channel> channel)
: stub_(CP_PACKETS_RX_TX::NewStub(channel)) {}
void string2hexString(char* input, char* output, size_t strLen)
{
int loop;
int i;
i=0;
loop=0;
//while(input[loop] != '\0')
while(loop < strLen)
{
sprintf((char*)(output+i),"%02X", input[loop]);
loop+=1;
i+=2;
}
//insert NULL at the end of the output string
output[i++] = '\0';
}
// Assembles the client's payload, sends it and presents the response back
// from the server.
ReceiveReport * SendCPPacket(const CPProcessPkt& request) {
// Data we are sending to the server.
// Container for the data we expect from the server.
//CPProcessPkt request;
ReceiveReport reply;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext context;
// The producer-consumer queue we use to communicate asynchronously with the
// gRPC runtime.
CompletionQueue cq;
// Storage for the status of the RPC upon completion.
Status status;
// stub_->PrepareAsyncSayHello() creates an RPC object, returning
// an instance to store in "call" but does not actually start the RPC
// Because we are using the asynchronous API, we need to hold on to
// the "call" instance in order to get updates on the ongoing RPC.
std::unique_ptr<ClientAsyncResponseReader<ReceiveReport> > rpc(
stub_->PrepareAsyncPktRequest(&context, request, &cq));
// StartCall initiates the RPC call
rpc->StartCall();
// Request that, upon completion of the RPC, "reply" be updated with the
// server's response; "status" with the indication of whether the operation
// was successful. Tag the request with the integer 1.
rpc->Finish(&reply, &status, (void*)1);
void* got_tag;
bool ok = false;
// Block until the next result is available in the completion queue "cq".
// The return value of Next should always be checked. This return value
// tells us whether there is any kind of event or the cq_ is shutting down.
GPR_ASSERT(cq.Next(&got_tag, &ok));
// Verify that the result from "cq" corresponds, by its tag, our previous
// request.
GPR_ASSERT(got_tag == (void*)1);
// ... and that the request was completed successfully. Note that "ok"
// corresponds solely to the request for updates introduced by Finish().
GPR_ASSERT(ok);
// Act upon the status of the actual RPC.
if (status.ok()) {
return &reply;
} else {
return NULL;
}
}
private:
// Out of the passed in Channel comes the stub, stored here, our view of the
// server's exposed services.
std::unique_ptr<CP_PACKETS_RX_TX::Stub> stub_;
};
int main(int argc, char** argv) {
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureChannelCredentials()).
std::string server;
if (argc >= 2)
{
server = argv[1] + std::string(":50051");
}
else
{
server = "localhost:50051";
}
myTestCPClient client(grpc::CreateChannel(
server.c_str(), grpc::InsecureChannelCredentials()));
char dhcp_req[] = "0050568d52b1005056b561bc08004500017900010000402f5ef80aa000010d0c02b100006558ffffffffffff000003223341810000c808004500014f000100004011799e00000000ffffffff00440043013b00000101060001020304000000000000000000000000000000000000000000000322334100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000638253633501033604ac1c43013204ac1c430e5231011c30303a39303a38383a37373a36363a35363b4454482d535349443b6f021130303a39303a38383a37373a36363a3536ff";
int dataLen = strlen(dhcp_req);
char output[1024] = {0};
client.string2hexString(dhcp_req, output, strlen(dhcp_req));
CPProcessPkt packet;
packet.set_packet_type(PacketType::DHCP);
packet.set_event(EventType::EVENT_NONE);
packet.set_dp_id(19);
packet.set_port_id(3);
packet.set_data(output);
std::string user("world");
ReceiveReport *reply = client.SendCPPacket(packet); // The actual RPC call!
std::cout << "CP_PACKETS_RX_TX received: " << reply->processed_pkt().port_id() << " dp: " << reply->processed_pkt().dp_id() << std::endl;
return 0;
}
```
my proto file
```
// Copyright 2015 myTest
//
// 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.
syntax = "proto3";
package myTest_cp_pkts;
// Interface exported by the server.
service CP_PACKETS_RX_TX {
// A simple RPC.
//
// Sends the packet to and from CP and/or DP
rpc PktRequest(CPProcessPkt) returns (ReceiveReport) {}
}
enum PacketType {
DHCP = 0;
ARP = 1;
EVENT = 2;
};
// If PacketType is EVENT then the events can be of the following
enum EventType {
EVENT_NONE = 0;
};
enum Report {
RECEIVE_OK = 0;
RECEIVE_FAIL = 1;
};
message CPProcessPkt {
PacketType packet_type = 1;
EventType event = 2;
int32 dp_id = 3;
int32 port_id = 4;
string data = 5;
};
message ReceiveReport {
Report report = 1;
CPProcessPkt processed_pkt = 2;
};
```