How to turn URL into IP address using boost::asio? - c++

So I need some way of turning given Protocol://URLorIP:Port string into string ip int port How to do such thing with boost ASIO and Boost Regex? Or is it possible - to get IP using C++ Net Lib (boost candidate) - notice - we do not need long connection - only IP.
So I currently use such code for parsing
#include <boost/regex.hpp>
#include <vector>
#include <string>
int main(int argc, char** argv)
{
if (argc < 2) return 0;
std::vector<std::string> values;
boost::regex expression(
// proto host port
"^(\?:([^:/\?#]+)://)\?(\\w+[^/\?#:]*)(\?::(\\d+))\?"
// path file parameters
"(/\?(\?:[^\?#/]*/)*)\?([^\?#]*)\?(\\\?(.*))\?"
);
std::string src(argv[1]);
if (boost::regex_split(std::back_inserter(values), src, expression))
{
const char* names[] = {"Protocol", "Host", "Port", "Path", "File",
"Parameters", NULL};
for (int i = 0; names[i]; i++)
printf("%s: %s\n", names[i], values[i].c_str());
}
return 0;
}
What shall I add to my small program to parse URL into IP?

Remember that there may be multiple IP addresses for any one hostname, boost gives you an iterator that will go through them.
The use is fairly straightforward, add this before return 0; of your program:
std::cout << "IP addresses: \n";
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query(values[1], "");
for(boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query);
i != boost::asio::ip::tcp::resolver::iterator();
++i)
{
boost::asio::ip::tcp::endpoint end = *i;
std::cout << end.address() << ' ';
}
std::cout << '\n';
and don't forget #include <boost/asio.hpp>
test run:
~ $ g++ -g -Wall -Wextra -pedantic -Wconversion -ansi -o test test.cc -lboost_regex -lboost_system -lboost_thread
~ $ ./test http://www.google.com:7777
Protocol: http
Host: www.google.com
Port: 7777
Path:
File:
Parameters:
IP addresses:
74.125.226.179 74.125.226.176 74.125.226.178 74.125.226.177 74.125.226.180
PS: For reference, I called
TCP resolver's constructor
query's host/service constructor with a don't-care service value of ""
the exception-throwing form of resolve()
dereferenced the iterator to get a resolver entry
used resolver_entry's type conversion to endpoint
used the TCP endpoint's address() accessor
used operator<< to show the address: you could use to_string() instead, if needed.

Related

fatal error: 'grpcpp/grpcpp.h' file not found C++

Context
I have been working with C++ for about the past 5-6 months and I'm beginning to learn gRPC. I have followed many tutorials online to get started, but I want to build a client-server communication app from scratch. Probably a bit too much, but I'm doing my best to understand how to get it all to work from the ground up rather than downloading, typing 'make', and then having a working product that I don't know how to implement into my own projects.
Goal: Create and run a simple C++ gRPC client-server communication
Versions
Using VSCode IDE.
Protoc = libprotoc 3.17.3
gRPC = 1.41.1
make = 3.81
Files
mathtest.proto
syntax = "proto3";
option java_package = "ex.grpc";
package mathtest;
// Defines the service
service MathTest {
// Function invoked to send the request
rpc sendRequest (MathRequest) returns (MathReply) {}
}
// The request message containing requested numbers
message MathRequest {
int32 a = 1;
int32 b = 2;
}
// The response message containing response
message MathReply {
int32 result = 1;
}
server.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathServiceImplementation final : public MathTest::Service {
Status sendRequest(
ServerContext* context,
const MathRequest* request,
MathReply* reply
) override {
int a = request->a();
int b = request->b();
reply->set_result(a * b);
return Status::OK;
}
};
void Run() {
std::string address("0.0.0.0:5000");
MathServiceImplementation service;
ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on port: " << address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
Run();
return 0;
}
client.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathTestClient {
public:
MathTestClient(std::shared_ptr<Channel> channel) : stub_(MathTest::NewStub(channel)) {}
int sendRequest(int a, int b) {
MathRequest request;
request.set_a(a);
request.set_b(b);
MathReply reply;
ClientContext context;
Status status = stub_->sendRequest(&context, request, &reply);
if(status.ok()){
return reply.result();
} else {
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return -1;
}
}
private:
std::unique_ptr<MathTest::Stub> stub_;
};
void Run() {
std::string address("0.0.0.0:5000");
MathTestClient client(
grpc::CreateChannel(
address,
grpc::InsecureChannelCredentials()
)
);
int response;
int a = 5;
int b = 10;
response = client.sendRequest(a, b);
std::cout << "Answer received: " << a << " * " << b << " = " << response << std::endl;
}
int main(int argc, char* argv[]){
Run();
return 0;
}
Steps taken for compilation
Use mathtest.proto to create the necessary files via 'protoc' (or protobuf) by executing these: protoc --grpc_out=. --plugin=protoc-gen-grpc=/opt/homebrew/bin/grpc_cpp_plugin mathtest.proto & protoc --cpp_out=. mathtest.proto
This creates the following files:
mathtest.pb.h
mathtest.pb.cc
mathtest.grpc.pb.h
mathtest.grpc.pb.cc
Compile client.cpp & server.cpp files to create executable binaries using these commands: g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client 'pkg-config --libs protobuf grpc++' (NOTE: in this post, I use a single quote in the command line, but in the actual command I use a backtick; just wanted to make that clear)
Errors
As you may notice, I can't get to compiling the server because I can't get past the client compilation first. After executing the above command in step 2 of compilation, this is my output:
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs protobuf grpc++`
client.cpp:4:10: fatal error: 'grpcpp/grpcpp.h' file not found
#include <grpcpp/grpcpp.h>
^~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.pb.cc:4:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.grpc.pb.cc:5:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [client] Error 1
Here's my real confusion...
C++ intellisense has no issues finding these files. My $PATH variables point to these folders, and my VS Code include path also point to these folders. I'm unsure where I am going wrong here...
echo $PATH returns this:
/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/include:/opt/homebrew/Cellar:/opt/homebrew/opt/libtool/libexec/gnubin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/tzeller/.local/bin
The folders in question ('google' & 'grcpp') live within /opt/homebrew/include and they hold the necessary files as well...
What am I missing??
Change your compile command to
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs --cflags protobuf grpc++`
The --cflags bit asks pkg-config to spit out the necessary parameters for setting the header search path (on my system -I/opt/homebrew/Cellar/grpc/1.41.1/include and others)

Change the metavars in boost::program_options

This is a purely aesthetic issue.
I have written a CLI program in C++ and use boost::program_options to pasrse its args.
Per default, boost names all meta variables arg. I'd like to change that.
Here's the code:
#include <iostream>
using std::cerr;
using std::cout;
#include <optional>
using std::optional;
#include <string>
using std::string;
#include <boost/program_options.hpp>
using boost::program_options::unknown_option;
#include "Net.h"
#include "GameServer.h"
using proto::GameServer;
namespace args = boost::program_options;
static auto parseArgs(int argc, const char *argv[])
{
args::options_description desc("Command line options");
desc.add_options()
("help,h", "Show this page")
("address,a", args::value<string>()->default_value(net::Defaults::HOST), "IP address to listen on")
("port,p", args::value<unsigned short>()->default_value(net::Defaults::PORT), "Port to listen on")
;
optional<args::variables_map> result;
args::variables_map varMap;
try {
args::store(args::parse_command_line(argc, argv, desc), varMap);
} catch (unknown_option const &error) {
cerr << "Invalid option: " << error.get_option_name() << "\n";
cerr << desc << "\n";
return result;
}
args::notify(varMap);
if (varMap.count("help")) {
cout << desc << "\n";
return result;
}
return result = varMap;
}
int main(int argc, const char *argv[])
{
auto parsedArgs = parseArgs(argc, argv);
if (!parsedArgs.has_value())
return 1;
auto args = parsedArgs.value();
auto address = args.at("address").as<string>();
auto port = args.at("port").as<unsigned short>();
GameServer server(address, port);
server.listen();
}
And the output generated by the help page:
Command line options:
-h [ --help ] Show this page
-a [ --address ] arg (=127.0.0.1) IP address to listen on
-p [ --port ] arg (=9000) Port to listen on
I'd like to rename arg for -a to ip_address and for -p to portnum respectively.
#prehistoricpenguin's answer brought me onto the right track. In boost, the typed_value class has a method value_name() to set the argument name, which works analogous to setting the defaults, i.e. it modifies the value object and returns it for subsequent operations:
static auto parseArgs(int argc, const char *argv[])
{
options_description desc("Command line options");
desc.add_options()
("help,h", "Show this page")
("address,a", value<string>()->default_value(HOST)->value_name("ip_address"),
"IP address to connect to")
("port,p", value<unsigned short>()->default_value(PORT)->value_name("portnum"),
"Port to connect to");
return parseArgDesc(argc, argv, desc);
}
Resulting in:
Command line options:
-h [ --help ] Show this page
-a [ --address ] ip_address (=127.0.0.1)
IP address to connect to
-p [ --port ] portnum (=9000) Port to connect to
Seems it's an undocumented part of boost::program_options, I have a quick look at the implementation of boost::program_options and find there is a global variable which is used to control the behavior, so we come up with one line code hack:
args::arg = "i_am_not_arg";
Modify a global variable is not an elegant way, but I haven't found any usable APIs to do it, you may do more research and try to find a better solution(Don't forget to notify me here!)
Insert this line in some place of your code, then we get the output for --help command line:
->./a.out --help
Command line options:
-h [ --help ] Show this page
-a [ --address ] i_am_not_arg (=1234) IP address to listen on
-p [ --port ] i_am_not_arg (=42) Port to listen on
Suggestions for asking question on SO:
provide a minimal, reproducible program. Your code doesn't compile on my machine, because you used variables from files that are not provided.
Online demo

SOAP 1.2 fault SOAP-ENV:Sender[no subcode]

Accessing IIS webservice using Gsoap.
I have similar error as in this link shows that the error was solved compiling with -lssl.
I did the same thing in build as
g++ -o client client.cpp stdsoap2.cpp soapC.cpp soapDataManagementSoapProxy.cpp -I /usr/local/ssl/include -L/home/xavier/GSOAP/lib -lgsoapssl++ -L/usr/local/ssl/lib -lssl
My GSOAP lib was build with OpenSSL.
But I still have error as
SOAP 1.2 fault SOAP-ENV:Sender[no subcode]
"OpenSSL not installed: recompile with -DWITH_OPENSSL"
Detail: [no detail]
My test code is as follow. What could be wrong?
#include "soapDataManagementSoapProxy.h"
#include "DataManagementSoap.nsmap"
const char server[] = "https://XXXXXXX.com/XXXmanagement.asmx";
int main(int argc, char **argv)
{
DataManagementSoapProxy webinf;
webinf.soap_endpoint = server;
_tempuri__ReadTestData* a;
_tempuri__ReadTestDataResponse res;
int ret = webinf.ReadTestData(a, res);
if (webinf.error){
webinf.soap_stream_fault(std::cerr);
}
else{
//printf("result = %g\n", result);
std::cout << "Success " << std::endl;
}
webinf.destroy(); /* clean up mem */
return 0;
}
Solution for this issue is
#include "calc.nsmap"
#include "soapcalcProxy.h" // generated with soapcpp2 -j calc.h
calcProxy calc("https-server-endpoint-URL");
double sum;
soap_ssl_init(); // init SSL (just need to do this once in an application)
// soap_ssl_no_init(); // or prevent init OpenSSL when already initialized elsewhere in an application
if (soap_ssl_client_context(calc.soap,
SOAP_SSL_DEFAULT,
NULL, // no keyfile
NULL, // no keyfile password
"cacerts.pem", // trusted certificates (or use self-signed cacert.pem)
NULL, // no capath to trusted certificates
NULL // no random data to seed randomness
))
{
calc.soap_stream_fault(std::cerr);
exit(EXIT_FAILURE);
}
if (calc.add(1.23, 4.56, sum) == SOAP_OK)

Compile error when trying to compile boost and openssl (When using Websocket++ or CPPRestSDK)

I am currently trying to get a working tls websocket client running in C++ (which is a pain in the ass) and I have tried CPP Rest SDK as well as Websocket++. Both spit out a bunch of compile errors (see below). When I tried compiling it using Websocket++ without tls, it compiles so the error clearly is related to SSL.
I tried different OpenSSL versions (1.0.1, 1.0.2, 1.1.0), different C++ versions (11, 14 and even 17), and I just can't get it to compile.
I googled and none of the solutions worked. I am on Ubuntu 16 and the build command I am using looks like this:
g++ source/* -o test.out -Iinclude/ -std=c++14 -L/lib64 -lcurl -lboost_system -lssl -lcrypto -l pthread
Here are some of the errors:
/usr/include/boost/asio/ssl/detail/impl/openssl_init.ipp: In constructor ‘boost::asio::ssl::detail::openssl_init_base::do_init::do_init()’:
/usr/include/boost/asio/ssl/detail/impl/openssl_init.ipp:43:23: error: expected id-expression before ‘(’ token
mutexes_.resize(::CRYPTO_num_locks());
/usr/include/boost/asio/ssl/detail/impl/engine.ipp:221:9: error: ‘SSL_R_SHORT_READ’ was not declared in this scope
ERR_PACK(ERR_LIB_SSL, 0, SSL_R_SHORT_READ),
And here is the basic source code:
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
void on_close(client* c, websocketpp::connection_hdl hdl) {
c->get_alog().write(websocketpp::log::alevel::app, "Connection Closed");
}
int main(int argc, char* argv[]) {
client c;
std::string uri = "wss://gateway.discord.gg/";
if (argc == 2) {
uri = argv[1];
}
try {
// set logging policy if needed
c.clear_access_channels(websocketpp::log::alevel::frame_header);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
//c.set_error_channels(websocketpp::log::elevel::none);
// Initialize ASIO
c.init_asio();
// Register our handlers
c.set_open_handler(bind(&on_open,&c,::_1));
c.set_fail_handler(bind(&on_fail,&c,::_1));
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
c.set_close_handler(bind(&on_close,&c,::_1));
// Create a connection to the given URI and queue it for connection once
// the event loop starts
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
c.connect(con);
// Start the ASIO io_service run loop
c.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
This was a long time ago but in case it helps, adding -lcrypto -lssl in the g++ cmd arguments solved the problem for me.

boost program_options custom parsing

Is there a way to have something like
myapp hostname:port
parsed by boost program_options? I'm also using other options and I would love to use boost program_options without having to roll my own parser for argc/argv.
I tried with some combinations of
desc.add_options()
("help", "list all available options")
(new MyCustomValue(&store_var), "")
but it didn't work
As Dan Mašek writes, this looks more fitting for a positional argument. Since you specify a specific structure for the option, though, you might want to add std::regex_match.
Say you start with
#include <string>
#include <iostream>
#include <regex>
#include <boost/program_options.hpp>
using namespace std;
using namespace boost::program_options;
int main(int argc, const char *argv[]) {
try {
options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("ip_port", value<std::string>()->required(), "ip:port");
positional_options_description pos_desc;
pos_desc.add("ip_port", -1);
command_line_parser parser{argc, argv};
parser.options(desc).positional(pos_desc).allow_unregistered();
parsed_options parsed_options = parser.run();
variables_map vm;
store(parsed_options, vm);
notify(vm);
const auto ip_port = vm["ip_port"].as<string>();
At this point, we have the user's input for ip_port. We can define a regex to match it. Note that first part can be a string (e.g., localhost), but the second part must be an integer:
const regex ip_port_re("([^:]+):([[:digit:]]+)");
smatch ip_port_match;
if(!regex_match(ip_port, ip_port_match, ip_port_re))
throw validation_error{validation_error::invalid_option_value, ip_port, "ip_port"};
cout << "ip: " << ip_port_match[1] << " port: " << ip_port_match[2] << endl;
}
catch (const error &ex) {
cerr << ex.what() << '\n';
}
}
When running it, it looks like this:
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out 127.0.0.1:30
ip: 127.0.0.1 port: 30
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30
ip: localhost port: 30
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30d
the argument for option 'localhost:30d' is invalid
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out
the option '--ip_port' is required but missing
$ g++ --std=c++11 po.cpp -lboost_program_options && ./a.out localhost:30 foo
option '--ip_port' cannot be specified more than once