I'm trying to convert a vector of string to a char ** to pass into another method that makes a hostent struct. I have been trying to figure this out, but don't know what to do. Here's what I am doing
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <netdb.h>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
static struct hostent host;
#define INADDRSZ 4
#define _UNCONST(a) ((void *)(unsigned long)(const void *)(a))
void tokenize(string const &str, const char delim, vector<std::string> &out) {
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos){
end = str.find(delim, start);
out.push_back(str.substr(start, end - start));
}
}
static struct hostent * mk_hostent (const char *name, char **aliases, char **haddress, int addrsz, int af) {
host.h_name = const_cast<char*> (name);
host.h_addr_list = haddress;
host.h_aliases = aliases;
//host.h_addr_list[index] = (char *)_UNCONST(&host_addr[index]);
host.h_length = addrsz;
host.h_addrtype = af;
//host.h_aliases = _UNCONST(aliases);
return (&host);
}
void getHostAddrList (string hostname, vector<string> ipsAndHost, vector<string> &addrs) {
cout << "I reached here" << "\n";
vector<string> tokens;
for (int i = 0; i < ipsAndHost.size(); i++) {
tokenize(ipsAndHost[i], ' ', tokens);
// comparing if hostname same then pushing ip to host addrs list
if (hostname.compare(tokens[1]) == 0) {
addrs.push_back(tokens[0]);
}
tokens.clear();
}
}
void getAliasList (string hostip, vector<string> ipsAndHost, vector<string> &aliases) {
vector<string> tokens;
for (int i = 0; i < ipsAndHost.size(); i++) {
tokenize(ipsAndHost[i], ' ', tokens);
// comparing if host ip same then pushing host alias to aliases list
if (hostip.compare(tokens[0]) == 0) {
aliases.push_back(tokens[1]);
}
tokens.clear();
}
}
char ** vector2string (vector<string> &input) {
char ** arr = new char*[input.size()];
for(size_t i = 0; i < input.size(); i++){
arr[i] = new char[input[i].size() + 1];
strcpy(arr[i], input[i].c_str());
}
return arr;
}
static struct hostent * gethostbyaddr_iri (const char *addr, int af) {
//void printLines(const char * addr){
fstream file("sampleFile");
string line;
vector<string> readLines;
while (getline(file, line)) {
// skipping empty line
if (line.empty()) {
continue;
}
// skipping comments
if (line[0] == '#') {
continue;
}
readLines.push_back(line);
}
vector<string> ips;
vector<string> hostAddresses;
vector<string> hostAliases;
for (int i = 0; i < readLines.size(); i++) {
tokenize(readLines[i], ' ', ips);
//ips[0] will have ip address to compare
if (strcmp(ips[0].c_str(), addr) == 0) {
cout << "one was same" << "\n";
getHostAddrList(ips[1], readLines, hostAddresses);
getAliasList(ips[0], readLines, hostAliases);
//return mk_hostent(ips[1],
}
// clearing vector for next loop
ips.clear();
char ** hAddr = vector2string(hostAddresses);
char ** hAlias = vector2string(hostAliases);
return mk_hostent(ips[1].c_str(), hAlias, hAddr, INADDRSZ, AF_INET);
}
}
int main() {
gethostbyaddr_iri("128.0.0.16", 2);
}
So if you see I call the vector2string method to convert my vector string into char ** that I then pass to the mk_hostent method. The memory is never being freed and I need a way to convert my vectors to char ** without causing this memory leak