I was trying to stream process the command using popen.
avahi-browse -trp _timeserver._udp
The output the of the command looks as below.
avahi-browse -trp _timeserver._udp
+;enp0s3;IPv4;ubuntu2;_timeserver._udp;local
+;enp0s3;IPv4;ubuntu1;_timeserver._udp;local
=;enp0s3;IPv4;ubuntu2;_timeserver._udp;local;ubuntu2.local;192.168.1.7;123;"type=configured" "source=ntp"
=;enp0s3;IPv4;ubuntu1;_timeserver._udp;local;ubuntu1.local;192.168.1.105;123;"type=configured" "source=ntp"
The code I am trying to run is below
#include <iostream>
#include <regex>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void getDNSSDServices(string aServiceType) {
//#[ operation getDNSSDServices(String)
string myCommand = "avahi-browse -trp _timeserver._udp";
cout<<"My Command is :"<< myCommand << endl;
FILE* myFile = popen(myCommand.c_str(), "r");
if(myFile == nullptr)
{
cout << "Failed to execute" << myCommand << endl;
}
else
{
char myline[100];
while(std::fgets(myline,200,myFile)){ //read data from file object and put it into string.
std::string s = std::string(myline);
std::cout << "myline1 is :"<< s << std::endl; //print the data of the string
}
}
::pclose(myFile);
}
int main() {
getDNSSDServices("_beatstime._udp");
return 0;
}
I am facing the following issue.
My Command is :avahi-browse -rtp _timeserver._udp
myline1 is :+;enp0s3;IPv4;ubuntu2;_timeserver._udp;local
myline1 is :+;enp0s3;IPv4;ubuntu1;_timeserver._udp;local
myline1 is :=;enp0s3;IPv4;ubuntu2;_timeserver._udp;local;ubuntu2.local;192.168.1.7;123;"type=configured" "source=ntp"
myline1 is :=;enp0s3;IPv4;ubuntu1;_timeserver._udp;local;ubuntu1.local;192.168.1.105;123;"type=configured" "source=ntp"
*** stack smashing detected ***: terminated
Aborted (core dumped)
How do I avoid this issue ? (I have seen alternative of using flag -fnostackprotect, But I want to solve it without that flag)