How to use a specific DNS server/nameserver for name resolve queries in C++ boost::asio? - dns

I would like to bypass system configured nameserver and use my own nameserver (or list of them) configured in application. I can do in nslookup in windows. How can I do it in C++ preferably by using boost::asio? I would like to avoid using std::system("nslookup ...> output.txt") and reading the file.
I cannot see where I can specify nameserver to use for lookup in boost::asio.
#include <boost/asio.hpp>
#include <string>
#include <iostream>
using namespace boost;
int main()
{
asio::io_service io_service;
asio::ip::tcp::resolver resolver(io_service);//how to pass specific nameserver?
asio::ip::tcp::resolver::iterator itr = resolver.resolve("bbc.co.uk","");
asio::ip::tcp::resolver::iterator end;
for (int i = 1; itr != end; itr++, i++)
std::cout << "hostname #" << i << ": " << itr->host_name() << " " << itr->endpoint() << '\n';
return 0;
}

You can't. The DNS resvoling is handled by the sockets api and you can't specify the DNS servers.
You will have to directly use either a OS specific API to resolve names like DnsQueryEx in win32 or using a library like LDNS.

/*
* DNSResolver.h
*
* Created on: Jan 12, 2023
* Author: marian
*/
#ifndef DNSRESOLVER_H_
#define DNSRESOLVER_H_
#include <cstdint>
#include <cstddef>
#include <vector>
#include <string_view>
#include <string>
#include <ostream>
#include <iostream>
#include <thread>
#include <chrono>
#include <boost/asio.hpp>
namespace tools
{
template <typename Func>
std::string_view find_first_success(const std::string_view& str,const Func& func, size_t pos0=0, char delim=',')
{
size_t pos=0;
do
{
pos=str.find_first_of(delim,pos0);
std::string_view sv=str.substr(pos0,pos-pos0);
if (func(sv))
return sv;
pos0=pos+1;
} while (pos!=std::string_view::npos);
return {};
}
template<int unused=0>
uint16_t be2uint16(const uint8_t* hi)
{
uint16_t hi1=*(hi+1);
return (((uint16_t)*hi)<<8)|hi1;
}
template<int unused=0>
uint32_t be2uint32(const uint8_t* hi)
{
uint32_t v3=*(hi+3);
uint32_t v2=*(hi+2);
uint32_t v1=*(hi+1);
uint32_t v0=*hi;
return (v0<<24)|(v1<<16)|(v2<<8)|v3;
}
template<int unused=0>
void out_IP4(std::ostream& os,uint32_t IP4)
{
os << (IP4>>24) << '.' << ((IP4>>16)&0xFF) << '.' << ((IP4>>8)&0xFF) << '.' << (IP4&0xFF);
}
}
namespace DNS
{
const char *rcode_msg[]={"No error","Format error","Server failure","Name error","Not implemented","Refused"};
const char *opcode_msg[]={"Standard query","Inverse query","Status"};
const char *type_msg[]={"0","A","NS","MD","MF","CNAME","SOA","MB","MG","MR","NULL","WKS","PTR","HINFO","MINFO","MX","TX"};
/*
A 1 a host address
NS 2 an authoritative name server
MD 3 a mail destination (Obsolete - use MX)
MF 4 a mail forwarder (Obsolete - use MX)
CNAME 5 the canonical name for an alias
SOA 6 marks the start of a zone of authority
MB 7 a mailbox domain name (EXPERIMENTAL)
MG 8 a mail group member (EXPERIMENTAL)
MR 9 a mail rename domain name (EXPERIMENTAL)
NULL 10 a null RR (EXPERIMENTAL)
WKS 11 a well known service description
PTR 12 a domain name pointer
HINFO 13 host information
MINFO 14 mailbox or mail list information
MX 15 mail exchange
TXT 16 text strings
*/
template<int unused=0>
void disp_type(std::ostream&os,uint16_t type)
{
os << (int)type;
if (type<=16)
os << " " << type_msg[type];
}
typedef boost::asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO> rcv_timeout_option; //somewhere in your headers to be used everywhere you need it
template<int unused=0>
class resolver
{
public:
resolver(const std::string_view& nameservers="127.0.0.53",boost::asio::chrono::milliseconds timeout=boost::asio::chrono::milliseconds{3000})
:nameservers_{nameservers},timeout_(timeout)
{}
std::vector<uint32_t> resolve(
const std::string_view& hostname,
std::string_view& nameserver_used,
std::vector<uint32_t>* pTTLs=nullptr)
{
std::vector<uint8_t> result;
result.resize(1024);
nameserver_used=resolve(hostname,result);
if (nameserver_used.empty())
return {};
std::string qname;
std::vector<uint32_t> IP4s;
int cnt=answer_A(result,qname,IP4s,pTTLs);
(void)cnt;
if (hostname!=qname)
return {};
return IP4s;
}
void resolve_ostream(
const std::string_view& hostname,
std::string_view& nameserver_used,
std::ostream& os)
{
std::vector<uint8_t> result;
result.resize(1024);
nameserver_used=resolve(hostname,result);
if (nameserver_used.empty())
return;
os << "nameserver:" << nameserver_used << std::endl;
disp_result(os,result);
}
private:
static void out_name(std::ostream&os,const std::vector<uint8_t>& result,uint16_t& pos)
{
uint8_t sz=result[pos++];
for (;sz;)
{
if (sz>=64)
{
if ((sz>>6)!=3)
throw "label size exceed 64 character and not pointer, see RFC 1035";
uint16_t offset=(((uint16_t)(sz&63))<<8)|result[pos++];
out_name(os,result,offset);//recursion, hope not too deep
return;
}
uint16_t epos=pos+sz;
while (pos<epos)
os << result[pos++];
sz=result[pos++];
if (sz)
os << ".";
}
}
static void disp_rr(std::ostream&os,const std::vector<uint8_t>& result,uint16_t& pos)
{
if (pos>=result.size())
return;
if (result[pos]==0)
return;
out_name(os,result,pos);
if (((size_t)pos+10)>=(size_t)result.size())
{
throw "wrong packet size1";
}
uint16_t rr_type=tools::be2uint16(&result[pos]);
os << " (" ;
os << "type:" ; disp_type(os,rr_type) ;
pos+=2;
os << " class:" << tools::be2uint16(&result[pos]) ;
pos+=2;
os << " TTL:" << tools::be2uint32(&result[pos]) << " secs) ";
pos+=4;
uint16_t len=tools::be2uint16(&result[pos]);
pos+=2;
if (pos+len>result.size())
throw "wrong packet size2";
if (rr_type==1)
{
if (len!=4)
{
throw "unknown data size";
}
os << (int)result[pos] << "." << (int)result[pos+1] << "." << (int)result[pos+2] << "." << (int)result[pos+3];
} else if (rr_type==2)
{
uint16_t pos2=pos;
out_name(os,result,pos2);
} else
{
throw "unimplement type";
}
pos+=len;
}
static void split_rr(const std::vector<uint8_t>& result,
uint16_t& pos,
std::string& name,
uint16_t& rr_type,
uint16_t& rr_class,
uint32_t& TTL,
uint32_t& IP4,
std::string* pname2=nullptr
)
{
if (pos>=result.size())
return;
if (result[pos]==0)
return;
std::stringstream ss;
out_name(ss,result,pos);
name=ss.str();
if (((size_t)pos+10)>=(size_t)result.size())
{
throw "wrong packet size1";
}
rr_type=tools::be2uint16(&result[pos]);
pos+=2;
rr_class=tools::be2uint16(&result[pos]) ;
pos+=2;
TTL=tools::be2uint32(&result[pos]);
pos+=4;
uint16_t len=tools::be2uint16(&result[pos]);
pos+=2;
if (pos+len>result.size())
throw "wrong packet size2";
if (rr_type==1)
{
if (len!=4)
{
throw "unknown data size";
}
IP4=tools::be2uint32(&result[pos]); //*(uint32_t*)&result[pos];
} else if (rr_type==2)
{
uint16_t pos2=pos;
std::stringstream ss;
out_name(ss,result,pos2);
if (pname2)
*pname2=ss.str();
} else
{
throw "unimplement type";
}
pos+=len;
}
static int check_A(const std::vector<uint8_t>& result)
{
if (result.size()<12)
return -1;
uint8_t response=(result[2]>>7);
if (response!=1)
return -2;
uint8_t rcode=result[3]&0xf;
if (rcode!=0)
return -3;
uint8_t opcode=(result[2]>>3)&0xF;
if (opcode!=0)
return -4;
return 0;
}
static int answer_A(const std::vector<uint8_t>& result,
std::string& q_name,
std::vector<uint32_t>& IP4s,
std::vector<uint32_t>* pTTLs=nullptr)
{
int ret=check_A(result);
if (ret<0)
return ret;
std::string name;
uint16_t q_type=0;
uint16_t q_class=0;
uint16_t rr_type=0;
uint16_t rr_class=0;
uint32_t TTL=0;
uint32_t IP4=0;
uint16_t qc=tools::be2uint16(&result[4]);
uint16_t pos=12;
for (int i=0;i<qc;i++)//qc =1 usually
{
std::stringstream ss;
out_name(ss,result,pos);
q_name=ss.str();
q_type=tools::be2uint16(&result[pos]);
pos+=2;
q_class=tools::be2uint16(&result[pos]);
pos+=2;
}
uint16_t ac=tools::be2uint16(&result[6]);
IP4s.resize(ac);
if (pTTLs)
pTTLs->resize(ac);
for (int i=0;i<ac;i++)
{
split_rr(result,pos,name,rr_type,rr_class,TTL,IP4);
if (name!=q_name)
break;
if (rr_type!=q_type)
break;
if (rr_class!=q_class)
break;
IP4s[i]=IP4;
if (pTTLs)
(*pTTLs)[i]=TTL;
}
return ac;
}
static void disp_result(std::ostream&os,const std::vector<uint8_t>& result)
{
if (result.size()<12)
return;
os << std::string((result[2]&0x80) ? "response" : "query") << std::endl;
os << "size=" << result.size() << " bytes" << std::endl;
uint8_t opcode=(result[2]>>3)&0xF;
os << "Opcode:" << (int)opcode;
if (opcode<=2)
os << " " << opcode_msg[opcode];
os << std::endl;
os << ((result[2]&1) ? "recursion asked" : "recursion NOT asked") << std::endl;
if (result[2]&2)
os << "response truncated" << std::endl;
if (result[2]&4)
os << "Authoritative answer" << std::endl;
os << ((result[3]&0x80) ? "recursion available" : "recursion NOT available") << std::endl;
uint8_t rcode=result[3]&0xf;
os << "rcode:" << (int)rcode;
if (rcode<=5)
os << " " << rcode_msg[rcode];
os << std::endl;
uint16_t qc=tools::be2uint16(&result[4]);
os << "Query Count:" << qc << std::endl;
uint16_t ac=tools::be2uint16(&result[6]);
os << "Answer Count:" << ac << std::endl;
uint16_t nc=tools::be2uint16(&result[8]);
os << "Authoritative Name Server Count:" << nc << std::endl;
uint16_t arc=tools::be2uint16(&result[10]);
os << "Additional resource records Count:" << arc << std::endl;
uint16_t pos=12;
os << "_____________" << std::endl;
os << "Query:" << std::endl;
for (int i=0;i<qc;i++)//qc =1 usually
{
out_name(os,result,pos);
os << " (";
os << "type:"; disp_type(os,tools::be2uint16(&result[pos]));
pos+=2;
os << " class:" << tools::be2uint16(&result[pos]) << " ) ";
pos+=2;
os << std::endl;
}
//answer
if (ac)
{
os << "_________________________" << std::endl;
os << "Answer:" << std::endl;
}
for (int i=0;i<ac;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
if (nc)
{
os << "_________________________" << std::endl;
os << "Authoritative nameservers:" << std::endl;
}
for (int i=0;i<nc;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
if (arc)
{
os << "_________________________" << std::endl;
os << "Additional resource records:" << std::endl;
}
for (int i=0;i<arc;i++)
{
disp_rr(os,result,pos);
os << std::endl;
}
}
size_t udp_request(const boost::asio::const_buffer& request,
const boost::asio::mutable_buffer& response,
const std::string_view& destination_ip,
const unsigned short port,
boost::system::error_code& ec)
{
using namespace boost;
asio::ip::udp::socket socket(io_context_);
auto remote = asio::ip::udp::endpoint(asio::ip::make_address(destination_ip), port);
socket.open(boost::asio::ip::udp::v4());
size_t sent=socket.send_to(request, remote);
if (request.size()!=sent)
return 0;
return receive_from(socket,response,timeout_,ec);
}
std::size_t receive_from(
boost::asio::ip::udp::socket& sock,
const boost::asio::mutable_buffer& buffer,
boost::asio::chrono::steady_clock::duration timeout,
boost::system::error_code& ec)
{
std::size_t length = 0;
sock.async_receive(boost::asio::buffer(buffer),
[&](const boost::system::error_code& ec1,std::size_t sz){
ec=ec1;
length=sz;
}
);
run(sock,timeout);
return length;
}
void run(boost::asio::ip::udp::socket& sock,boost::asio::chrono::steady_clock::duration timeout)
{
// Restart the io_context, as it may have been left in the "stopped" state
// by a previous operation.
io_context_.restart();
// Block until the asynchronous operation has completed, or timed out. If
// the pending asynchronous operation is a composed operation, the deadline
// applies to the entire operation, rather than individual operations on
// the socket.
io_context_.run_for(timeout);
// If the asynchronous operation completed successfully then the io_context
// would have been stopped due to running out of work. If it was not
// stopped, then the io_context::run_for call must have timed out.
if (!io_context_.stopped())
{
// Cancel the outstanding asynchronous operation.
sock.cancel();
// Run the io_context again until the operation completes.
io_context_.run();
}
}
static std::vector<uint8_t> make_dns_request(const std::string_view& hostname)
{
static uint16_t id=257;
id++;
std::vector<uint8_t> req;
uint16_t epos=12+1+hostname.size();
req.resize(epos+1+4);//plus null plus type (2 bytes) plus class (2 bytes)
*((uint16_t*)&req[0])=id;
req[2]=1;//recursive
req[3]=32;//??, linux has 32, but 0 works too
req[5]=1;//one query
req[epos]=0;//end of string
req[epos+2]=1;//type A - host address
req[epos+4]=1;//class INT
uint16_t cnt=0;
for(int i=hostname.size()-1;i>=0;i--)
{
req[13+i]=(hostname[i]=='.') ? cnt : hostname[i];
cnt=(hostname[i]=='.') ? 0 : cnt+1;
}
req[12]=cnt;
return req;
}
std::string_view resolve(const std::string_view& hostname,std::vector<uint8_t>& result)
{
auto myrequest=make_dns_request(hostname);
return tools::find_first_success(nameservers_,
[&](const std::string_view& nameserver)
{
size_t sz=0;
try
{
boost::system::error_code ec;
sz=udp_request(boost::asio::buffer(myrequest),
boost::asio::mutable_buffer(&result[0],result.size()),
nameserver,53,ec);
if (ec.value()!=boost::system::errc::success)
{
std::cerr << "skipping due to error code not a success : error code=" << ec.value() << " " << ec.message() << std::endl;
return false;
}
if (sz<35)//min packet , 12 header+3 name+4 type/class+ 16 (rr type 1)
{
std::cerr << "skipping because packet size < 35" << std::endl;
return false;
}
if ((myrequest[0]!=result[0])||(myrequest[1]!=result[1]))
{
std::cerr << "skipping because ID respond not matching ID request " << std::endl;
return false;
}
if (check_A(result)<0)
{
std::cerr << "skipping because packet check failed " << std::endl;
return false;
}
}
catch(std::exception& ex)
{
std::cerr << "Skipping because of exception:" << ex.what() << std::endl;
return false;
}
catch(...)
{
std::cerr << "Skipping because of unknown exception:" << std::endl;
return false;
}
result.resize(sz);
return true;
});
}
private:
boost::asio::io_context io_context_;
std::string nameservers_;
boost::asio::chrono::milliseconds timeout_;
};
}
#endif /* DNSRESOLVER_H_ */

/*
* main.cpp
*
* Created on: Jan 12, 2023
* Author: marian
*/
#include <iostream>
#include <vector>
#include "DNSResolver.h"
#define check_offset if (offset==argc) { print_error(); return 1;}
int main (int argc, char **argv) {
auto print_error=[&]()
{
std::cerr << "Usage: " << argv[0] << " [-v] [-t timeout_msecs] [--nottl] [--nons] <query> [csv_ip_nameserver_list] \n";
std::cerr << "Example: " << argv[0] << " bbc.co.uk 127.0.0.53,8.8.8.8" << std::endl;
std::cerr << "Example: " << argv[0] << " -v bbc.co.uk" << std::endl;
};
if (argc < 2) {
print_error();
return 1;
}
int offset=1;
bool verbose= (std::string(argv[offset])=="-v");
if (verbose)
offset++;
check_offset;
int millisecs=3000;
if (std::string(argv[offset])=="-t")
{
offset++;
check_offset;
millisecs=std::stoi(argv[offset++]);
}
check_offset;
bool ttl=true;
if (std::string(argv[offset])=="--nottl")
{
ttl=false;
offset++;
}
check_offset;
bool ns=true;
if (std::string(argv[offset])=="--nons")
{
ns=false;
offset++;
}
check_offset;
const char *dname=argv[offset];
const char *nameservers= (offset+1<argc) ? argv[offset+1] : "127.0.0.53,8.8.8.8";
if (ns)
std::cout << "nameservers:" << nameservers << std::endl;
DNS::resolver resolver{nameservers,boost::asio::chrono::milliseconds{millisecs}};
if (verbose)
{
std::string_view nameserver_used;
resolver.resolve_ostream(dname,nameserver_used,std::cout);
return 0;
}
std::vector<uint32_t> TTLs;
std::string_view nameserver_used;
std::vector<uint32_t> IP4s=resolver.resolve(dname,nameserver_used,&TTLs);
if (ns&&(!nameserver_used.empty()))
std::cout << "nameserver:" << nameserver_used << std::endl;
int n=IP4s.size();
if (!n)
{
std::cerr << "Error: No IPs found" << std::endl;
return 2;
}
for (int i=0;i<n;i++)
{
tools::out_IP4(std::cout,IP4s[i]);
if (ttl)
std::cout << " (TTL:" << TTLs[i] << " sec)" ;
std::cout << std::endl;
}
return 0;
}

Related

Why does glReadPixels seem to return garbage?

I'm trying to draw offscreen with OpenGL. For this I use EGL to initialize a pbuffer surface, and then draw to it, reading the results back with glReadPixels. But the following program gives me garbage on different (Mesa-based Intel on Linux) GPUs. Namely, on Atom N550 I get zeros, while on Xeon E3-1200 v3 I have 70 00 07 44 instead of the expected 40 80 bf ff.
With LIBGL_ALWAYS_SOFTWARE=1 environment variable set, I get the expected results. Also, if I comment out the line with eglBindAPI, I get good result on Xeon, but still zeros on Atom.
Here's my program:
#include <EGL/egl.h>
#include <GL/gl.h>
#include <iostream>
#include <iomanip>
#include <cstring>
int eglPrintError(std::string const& context)
{
const GLint error=eglGetError();
std::cerr << context << ": error 0x" << std::hex << int(error) << "\n";
return 1;
}
bool checkError(std::string const& funcName)
{
const GLenum error=glGetError();
if(error!=GL_NO_ERROR)
{
std::cerr << funcName << ": error 0x" << std::hex << int(error) << "\n";
return true;
}
return false;
}
constexpr int fbW=1, fbH=1;
bool initGL()
{
if(!eglBindAPI(EGL_OPENGL_API)) return !eglPrintError("eglBindAPI");
const EGLDisplay dpy=eglGetDisplay(EGL_DEFAULT_DISPLAY);
if(!dpy) return !eglPrintError("eglGetDisplay");
if(!eglInitialize(dpy,nullptr,nullptr)) return !eglPrintError("eglInitialize");
static const EGLint cfgAttribs[]={EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_NONE};
EGLConfig cfg;
EGLint cfgCount;
if(!eglChooseConfig(dpy,cfgAttribs,&cfg,1,&cfgCount))
return !eglPrintError("eglChooseConfig");
if(cfgCount==0)
{
std::cerr << "Failed to get any usable EGL configs\n";
return false;
}
const EGLContext context=eglCreateContext(dpy,cfg,EGL_NO_CONTEXT,NULL);
if(!context) return !eglPrintError("eglCreateContext");
const EGLint surfaceAttribs[]={EGL_WIDTH, fbW, EGL_HEIGHT, fbH, EGL_NONE};
const EGLSurface surface=eglCreatePbufferSurface(dpy,cfg,surfaceAttribs);
if(!surface) return eglPrintError("eglCreatePbufferSurface");
if(!eglMakeCurrent(dpy,surface,surface,context))
return !eglPrintError("eglMakeCurrent");
return true;
}
int main(int argc, char** argv)
{
if(!initGL()) return 1;
glViewport(0,0,fbW,fbH);
glClearColor(0.25,0.5,0.75,1.);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
unsigned char data[4*fbW*fbH];
std::memset(data,0xb7,sizeof data); // to see unchanged values
glReadPixels(0,0,fbW,fbH,GL_RGBA,GL_UNSIGNED_BYTE,data);
if(checkError("glReadPixels")) return 1;
std::cout << "Data read: " << std::hex << std::setfill('0');
for(auto datum : data)
std::cout << std::setw(2) << +datum << " ";
std::cout << "\n";
return 0;
}
My question is, is there anything wrong in the above code which could lead to such behavior, or are my drivers just simply buggy?

How to set end condition of string iterator in C++?

i'm new in C++. I met a problem when doing exercise in C++ Primer 5th edition(Ex 9.43). The for loop can't stop in my function find_and_replace. Here is the code:
#include <iostream>
#include <string>
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace);
int main(int argc, char const *argv[])
{
std::string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
std::cout << find_times << std::endl;
std::cout << str << std::endl;
return 0;
}
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace)
{
int find_times = 0;
if (org_str.size() < str4find.size())
{
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
else if (org_str == str4find)
{
org_str.assign(str4replace);
++find_times;
return find_times;
}
for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); )
{
std::string temp(i, j);
// std::cout << temp << std::endl;
if (temp == str4find)
{
j = org_str.erase(i, j);
org_str.insert(i, str4replace.begin(), str4replace.end());
// std::cout << org_str << std::endl;
// std::cout << *i << std::endl;
j = i + str4find.size();
// std::cout << *j << std::endl;
++find_times;
}
else
{
++i;
++j;
}
}
if (org_str.substr(org_str.size() - str4find.size()) == str4find)
{
org_str.erase(org_str.size() - str4find.size(), str4find.size());
org_str.insert(org_str.end(), str4replace.begin(), str4replace.end());
++find_times;
}
return find_times;
}
I update iterator j and i after erase and insert operation, so i think, i and j are not invalid.
Anyone can tell me why the end condition: j != org_str.end() is not work?
SOLVED!
Although the cout output is correct, the i iterator is already invalid. Here is the new code. Just change few lines in for-loop.
#include <iostream>
#include <string>
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace);
int main(int argc, char const *argv[])
{
std::string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
std::cout << find_times << std::endl;
std::cout << str << std::endl;
return 0;
}
int find_and_replace(std::string& org_str, const std::string& str4find, const std::string& str4replace)
{
int find_times = 0;
if (org_str.size() < str4find.size())
{
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
else if (org_str == str4find)
{
org_str.assign(str4replace);
++find_times;
return find_times;
}
for (auto i = org_str.begin(), j = i + str4find.size(); j != org_str.end(); )
{
std::string temp(i, j);
// std::cout << temp << std::endl;
if (temp == str4find)
{
i = org_str.erase(i, j);
//CHANGE BEGIN
//org_str.insert(i, str4replace.begin(), str4replace.end());
for (auto k = str4replace.begin(); k != str4replace.end(); ++k, ++i)
{
i = org_str.insert(i, *k);
}
//CHANGE END
// std::cout << org_str << std::endl;
// std::cout << *i << std::endl;
j = i + str4find.size();
// std::cout << *j << std::endl;
++find_times;
}
else
{
++i;
++j;
}
}
if (org_str.substr(org_str.size() - str4find.size()) == str4find)
{
org_str.erase(org_str.size() - str4find.size(), str4find.size());
org_str.insert(org_str.end(), str4replace.begin(), str4replace.end());
++find_times;
The i iterator become invalid after calling erase and insert, you should use the return values of that functions or recalculate the position starting from s.begin() again.
#include <iostream>
#include <string>
using std::cout;
using std::string;
int find_and_replace( string &s, const string &oldVal, const string &newVal ) {
int find_times = 0;
if ( s.size() < oldVal.size() ) {
std::cout << "Error: The original string is too short!" << std::endl;
return find_times;
}
if ( s == oldVal ) {
s.assign(newVal);
return ++find_times;
}
for ( auto i = s.begin(); i != s.end(); ++i ) {
// compare using iterators
bool is_equal = true;
auto k = i;
for ( auto j = oldVal.begin(); j != oldVal.end(); ++j, ++k ) {
if ( k == s.end() || *j != *k ) {
is_equal = false;
break;
}
}
if ( is_equal ) {
auto ie = s.erase(i, i + oldVal.size());
// This may not work with older version of g++
auto ii = s.insert(ie, std::begin(newVal), std::end(newVal));
i = ii + newVal.size() - 1;
++find_times;
}
}
return find_times;
}
int main() {
string str("I am a very loooooong string to be process!");
int find_times;
find_times = find_and_replace(str, "a", "###");
cout << find_times << '\n';
cout << str << '\n';
return 0;
}
The output (as you can see here) is:
2
I ###m ### very loooooong string to be process!

async_ read_until doesn't work as expected

So I am trying to write a program that reads and writes data through a tcp socket. I can successfully accept a connection, write data to it (though the write handler doesn't work as expected?). I also want to read data through the same socket - which doesnt seem to be working.
The class that handles all this is as follows:
using namespace boost::asio;
using namespace boost::asio::ip;
TcpServer::TcpServer(unsigned short port = 1700)
: ipPort(port){
tcp::acceptor acc(svc, tcp::endpoint(tcp::v4(), ipPort));
acc.listen();
acc.async_accept(socket, boost::bind(&TcpServer::Accept_Handler,this, placeholders::error));
SAY("Waiting for a New connection");
svc.run();
}
void TcpServer::Write_Handler(const boost::system::error_code& ec,
std::size_t bytes_transferred){
std::cout << ec.message() << std::endl;
if (!ec)
{
std::cout << "Just sent " << yawData << std::endl;
}
}
void TcpServer::Read_Handler(const boost::system::error_code& ec,
std::size_t bytes_transferred){
if (!ec)
{
std::string line;
std::istream is(&input_buffer_);
std::string test;
is >> test;
std::cout << "test" << test << std::endl;
std::getline(is, line);
if (!line.empty())
{
std::cout << "Recieved: " << line << std::endl;
}
}
else
std::cout << "Error reading:" << ec.message() << std::endl;
}
void TcpServer::Accept_Handler(const boost::system::error_code& ec){
if (!ec)
{
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
connectMode = 1;
}
}
void TcpServer::Write_Data(){
if (connectMode){
SAY("Sent data");
std::ostringstream ss;
std::string sendBuffer;
ss << std::fixed << std::setprecision(2);
ss << yawData;
sendBuffer = ss.str() + "\r";
async_write(socket, buffer(sendBuffer), boost::bind(&TcpServer::Write_Handler, this,
placeholders::error,
placeholders::bytes_transferred));
}
}
void TcpServer::UpdateYaw(double data) {
yawData = data;
}
void TcpServer::Read_Data(){
if (connectMode){
async_read_until(socket, input_buffer_, "\n" , boost::bind(&TcpServer::Read_Handler, this,
placeholders::error,
placeholders::bytes_transferred));
}
}
TcpServer::~TcpServer(){
svc.stop();
}
The class header goes as:
class TcpServer {
private:
io_service svc;
tcp::socket socket{svc};
double yawData = 0;
unsigned short ipPort;
bool connectMode = 0;
streambuf input_buffer_;
void Write_Handler(const boost::system::error_code&,
std::size_t);
void Read_Handler(const boost::system::error_code&,
std::size_t);
void Accept_Handler(const boost::system::error_code&);
public:
TcpServer(unsigned short );
void Write_Data();
void Read_Data();
void UpdateYaw(double);
~TcpServer();
};
To use this, I call Write_Data(), followed by Read_Data(). Write_Data works, but the write handler isn't called - I can recieve data on the client side.
Read_Data() doesn't work at all. I know for sure that Data is being sent through the socket in teh format needed (ends with "\n")
Any ideas on what could possibly be wrong or, any debugging tips?
Thanks
EDIT
I plan to run the write_data and read_data functions from my main function as follows:
TcpServer *socketObj = new TcpServer(1700);
while ( i < 100 && trackObj->ReadTrackingState() != 0) {
SAY("Current Yaw - %.02f", trackObj->CurrentYaw());
socketObj->UpdateYaw(trackObj->CurrentYaw());
socketObj->Write_Data();
socketObj->Read_Data();
Platform::sleepMillis(1000);
i++;
}
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
connectMode = 1;
}
}
This function ends the async processing. It doesn't schedule any more async work and therefore io_service::run() ends, as documented.
You want to chain directly or use io_service::work to keep the service running. I suggest the chaining:
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
Write_Data();
}
}
But...
HOLD ON
You'll want to carefully review all the code.
void TcpServer::Write_Data() {
SAY("Sent data");
std::ostringstream ss;
std::string sendBuffer;
ss << std::fixed << std::setprecision(2);
ss << yawData;
sendBuffer = ss.str() + "\r";
async_write(socket, buffer(sendBuffer),
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
What's going on here? First you create a temporary stream, fail to use it to append the carriage-return, and then pass the reference to a local string to async_write... That can't work. It's Undefined Behaviour.
A fix:
void TcpServer::Write_Data() {
SAY("Send data");
std::ostream ss(&output_buffer_);
ss << std::fixed << std::setprecision(2) << yawData << "\r";
async_write(socket, output_buffer_,
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
DEMO
Live On Coliru
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <iomanip>
using namespace boost::asio;
using namespace boost::asio::ip;
template <typename T>
static inline void SAY(T&& v) { std::cout << std::forward<T>(v) << "\n"; }
class TcpServer {
private:
io_service svc;
tcp::socket socket{ svc };
double yawData = 0;
unsigned short ipPort;
streambuf input_buffer_, output_buffer_;
void Write_Handler(const boost::system::error_code &, std::size_t);
void Read_Handler(const boost::system::error_code &, std::size_t);
void Accept_Handler(const boost::system::error_code &);
public:
TcpServer(unsigned short = 1700);
void Write_Data();
void Read_Data();
void UpdateYaw(double);
~TcpServer();
};
TcpServer::TcpServer(unsigned short port) : ipPort(port) {
tcp::acceptor acc(svc, tcp::endpoint(tcp::v4(), ipPort));
acc.listen();
acc.async_accept(socket, boost::bind(&TcpServer::Accept_Handler, this, placeholders::error));
SAY("Waiting for a New connection");
svc.run();
}
void TcpServer::Write_Handler(const boost::system::error_code &ec, std::size_t /*bytes_transferred*/) {
std::cout << ec.message() << std::endl;
if (!ec) {
std::cout << "Just sent " << yawData << std::endl;
Read_Data();
}
}
void TcpServer::Read_Handler(const boost::system::error_code &ec, std::size_t /*bytes_transferred*/) {
if (!ec) {
std::cout << "Recieved: " << &input_buffer_ << std::endl;
} else
std::cout << "Error reading:" << ec.message() << std::endl;
}
void TcpServer::Accept_Handler(const boost::system::error_code &ec) {
if (!ec) {
std::cout << "Accepted a connection! - Now switching to write mode " << std::endl;
Write_Data();
}
}
void TcpServer::Write_Data() {
SAY("Send data");
std::ostream ss(&output_buffer_);
ss << std::fixed << std::setprecision(2) << yawData << "\r";
async_write(socket, output_buffer_,
boost::bind(&TcpServer::Write_Handler, this, placeholders::error, placeholders::bytes_transferred));
}
void TcpServer::UpdateYaw(double data) { yawData = data; }
void TcpServer::Read_Data() {
async_read_until(socket, input_buffer_, "\n", boost::bind(&TcpServer::Read_Handler, this, placeholders::error,
placeholders::bytes_transferred));
}
TcpServer::~TcpServer() { svc.stop(); }
int main() {
TcpServer server;
}

std::thread context of execution (c++14)

The problem appear when an in/out variable of a function called by std::thread changes the value during the execution...
Function:
static int func(stThread_t *&pStThread)
Parameters
pStThread: It´s a struct that has a pointer to std::thread and other variables (some flags)
typedef struct stThread {
stThread() noexcept {...};
stThread(const stThread &cRigth) noexcept {...};
stThread & operator = (const stThread &cRigth) noexcept {...};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
The function func print the address of the std::thread of the parameter
pStThread and the thread id
func before 1785280 this_id 21968
after making an this_thread::sleep for 2 seconds, it print it again
func afer ... this_id 21968
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
The address of the pointer to std::thread it´s changed (corrutped, deleted..?)
The pStThread is pushing_back of a list of pointer struct stThread_t
std::list<stThread_t*> listOfThreads;
listOfThreads.push_back(pStThread);
I read about std::move, but does not work with pointers
At the end there is a thread "garbage collector" that it is trying to erase all the threads pending of execution.
Full code here
#include <string>
#include <list>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>
typedef struct stThread {
stThread() noexcept {
pThread = NULL;
bBegin = false;
bEnd = false;
};
stThread(const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
};
stThread & operator = (const stThread &cRigth) noexcept {
this->pThread = cRigth.pThread;
this->bBegin = (bool)cRigth.bBegin;
this->bEnd = (bool)cRigth.bEnd;
return *this;
};
std::thread *pThread;
volatile bool bBegin;
volatile bool bEnd;
} stThread_t;
class CMain
{
public:
typedef std::list<stThread_t*> MyList_threads;
MyList_threads listOfThreads;
public:
CMain() {
std::cout << std::boolalpha << "Ex1 is move-constructible? "
<< std::is_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex1 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is trivially move-constructible? "
<< std::is_trivially_move_constructible<stThread_t>::value << '\n'
<< "Ex2 is nothrow move-constructible? "
<< std::is_nothrow_move_constructible<stThread_t>::value << '\n';
};
static int func(stThread_t *&pStThread) {
std::thread::id this_id = std::this_thread::get_id();
long long p_begin = (long long)pStThread;
std::cout << "func before " << std::to_string(p_begin) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bBegin = true;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
this_id = std::this_thread::get_id();
long long p_end = (long long)pStThread;
std::cout << "func afer " << std::to_string(p_end) << " this_id " << this_id << "\n";
std::cout.flush();
pStThread->bEnd = true;
return 1;
};
int _createThreads() {
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
pStThread->pThread = new std::thread(&CMain::func,
std::ref(pStThread));
if (pStThread) {
do {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
} while (!pStThread->bBegin);
listOfThreads.push_back(pStThread);
std::string sLog;
sLog = "\nlistOfThreads.push_back " + std::to_string((long long)pStThread) + "\n";
std::cout << sLog;
std::cout.flush();
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return 1;
};
int _main() {
_createThreads();
std::thread thread_collector([=]() {
bool bEnd = false;
MyList_threads::iterator it;
it = listOfThreads.end();
do {
stThread_t *pStThread = NULL;
if (it == listOfThreads.end()) {
it = listOfThreads.begin();
if (it == listOfThreads.end()) bEnd = true;
}
else it++;
if (it != listOfThreads.end()) {
if ((*it)->bEnd) {
pStThread = *it;
listOfThreads.erase(it);
it = listOfThreads.begin();
}
}
if (pStThread) {
if (pStThread->pThread) {
if (pStThread->pThread->joinable()) {
pStThread->pThread->join();
std::cout << " element deleted " << std::to_string((long long)pStThread) << "\n";
std::cout.flush();
}
delete pStThread->pThread;
pStThread->pThread = NULL;
}
delete pStThread;
}
pStThread = NULL;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!bEnd);
});
if (thread_collector.joinable()) {
thread_collector.join();
}
return 1;
};
};
int main()
{
CMain _main;
_main._main();
return 0;
}
You have a rather straightforward bug, that is mostly unrelated to threading:
(1) func takes a reference to an stThread_t*.
static int func(stThread_t *&pStThread);
(2) You pass in a reference to pStThread
std::thread(&CMain::func,std::ref(pStThread));
(3) Which is a local variable whose lifetime ends as soon as the loop iteration is finished
for (int iIdx = 0; (iIdx < 5); iIdx++) {
stThread_t *pStThread = new stThread_t;
//...
}
(4) And consequently, you get undefined behavior when your function tries to access the object after it is destroyed. (be careful! here, "the object" refers to the pointer in question, not the object the pointer is pointing to)
It's unclear why you insist on passing pStThread by reference; your function doesn't actually modify the pointer (just what's pointed to), and you don't seem to intend to do any of the things that such a device is actually good for.

socket() returning file descriptor 1 inside std::thread

When I call socket() inside a std::thread() it returns a socket descriptor of 1. Calls to std::cout than send the text meant for the terminal to the server. When I added cout << ""; prior to the call to socket(), descriptors for stdin/out/err for are created and socket() returns 3.
From http://www.cplusplus.com/reference/iostream/cout/:
In terms of static initialization order, cout is guaranteed to be properly constructed and initialized no later than the first time an object of type ios_base::Init is constructed, with the inclusion of <iostream> counting as at least one initialization of such objects with static duration.
By default, cout is synchronized with stdout (see ios_base::sync_with_stdio).
std::cout should already be initialized and synchronized to stdout which explains why std::cout is sending the message to the server and not the terminal.
I'm wondering if calling std::thread() closes the stdin/out/err descriptors in the new thread or if these descriptors don't exist in the thread since the thread wasn't created by the terminal or initd?
I'm running on RHEL 6.4 using GCC 4.8.2. I've included my client code below with the additional cout << ""; commented out for completeness.
Client.cpp:
#include "Client.hpp"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <sstream>
#include <functional>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
using namespace std;
Client::Client( const string& hostname, const string& port ) {
this->hostname = hostname;
this->port = port;
}
Client::~Client() { close( fd ); }
void Client::operator()() {
struct addrinfo hints;
memset( &hints, 0, sizeof( struct addrinfo ) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
struct addrinfo *result;
int ret = getaddrinfo( hostname.c_str(), port.c_str(), &hints, &result );
if( ret != 0) {
cerr << "getaddrinfo failed: " << gai_strerror( ret ) << endl;
return;
}
// cout << ""; // prevents socket() from returning 1 and redefining cout
struct addrinfo *rp = NULL;
for( rp = result; rp != NULL; rp = rp->ai_next ) {
fd = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol );
if( fd == -1 ) {
continue; /* Error */
}
if( connect( fd, rp->ai_addr, rp->ai_addrlen ) != -1) {
break; /* Success */
}
close( fd ); /* Try again */
}
if( rp == NULL ) {
cerr << "Failed to connect to " << hostname << ":" << port << endl;
return;
}
freeaddrinfo( result );
cout << "Starting echo client" << endl;
int i = 0;
do {
stringstream ss;
ss << "Thread " << this_thread::get_id() << ": Message #" << ++i;
string str = ss.str();
_send( str.c_str() );
string msg = _recv();
//cout << "Thread " << this_thread::get_id() << " received message: " << msg << endl;
} while ( i < 10 );
cout << "Stopping echo client" << endl;
close( fd );
}
string Client::_recv( ) const {
const int BUF_SIZE = 1024;
char* buf = ( char * ) malloc( sizeof( char) * BUF_SIZE );
memset( buf, '\0', sizeof( char ) * BUF_SIZE );
int bytes = recv( fd, buf, BUF_SIZE, 0 );
if( bytes < 0 ) {
perror( "recv failed" );
}
string msg = string( buf );
free( buf );
return msg;
}
void Client::_send( const string buf ) const {
if( send( fd, buf.c_str(), buf.length(), 0 ) < 0 ) {
perror( "send failed" );
}
}
void usage() {
cerr << "Usage: client <hostname> <port>" << endl;
cerr << " hostname - server name listening for incoming connctions" << endl;
cerr << " port - internet port to listen for connections from" << endl;
}
int main( int argc, char* argv[] ) {
if( argc < 3 ) {
cerr << "Not enough arguments!" << endl;
usage();
return EXIT_FAILURE;
}
vector<thread> clients;
for( int i = 0; i < 1; i++ ) {
clients.push_back( thread( ( Client( argv[1], argv[2] ) ) ) );
}
for_each( clients.begin(), clients.end(), mem_fn( &thread::join ) );
return EXIT_SUCCESS;
}
Client.hpp:
#ifndef __CLIENT_HPP__
#define __CLIENT_HPP__
#include <string>
class Client {
private:
int fd;
std::string hostname;
std::string port;
std::string _recv( ) const;
void _send( const std::string buf ) const;
public:
Client( const std::string& hostname, const std::string& port);
~Client();
void operator()();
};
#endif
This normally happens if you accidentally close your stdout, i.e. there is a spurious close(1);. Then the FD will legitimately be number 1. This may well be elsewhere in the program.
I've had this several times, and normally find it with gdb and setting a breakpoint on close().
Your destructor looks suspicious:
Client::~Client() { close( fd ); }
Shouldn't you be setting fd to -1 in the constructor, and carefully setting fd to -1 wherever else you close it, and not doing this close if fd==-1? Currently creating a Client and destroying it will close a random fd.

Resources