convert most char[...] configuration values to std::string

Signed-off-by: Jacek Jendrzej <crashdvb@googlemail.com>
This commit is contained in:
martii
2013-06-11 13:32:19 +02:00
committed by Jacek Jendrzej
parent fce161b70b
commit cbc9299df8
40 changed files with 483 additions and 470 deletions

View File

@@ -2,5 +2,5 @@ noinst_LIBRARIES = libtuxbox-net.a
AM_CXXFLAGS = -fno-rtti -fno-exceptions
libtuxbox_net_a_SOURCES = libnet.c network_interfaces.cpp
libtuxbox_net_a_SOURCES = libnet.cpp network_interfaces.cpp

View File

@@ -9,7 +9,9 @@
#include <netdb.h>
#include <linux/route.h>
#if 0
#include "libnet.h"
#if 0
//never used
static void scanip( char *str, unsigned char *to )
{
@@ -83,16 +85,16 @@ abbruch:
return rc;
}
#endif
void netGetIP( char *dev, char *ip, char *mask, char *brdcast )
void netGetIP(std::string &dev, std::string &ip, std::string &mask, std::string &brdcast)
{
int fd;
struct ifreq req;
struct sockaddr_in *saddr;
unsigned char *addr;
*ip=0;
*mask=0;
*brdcast=0;
ip = "";
mask = "";
brdcast = "";
fd=socket(AF_INET,SOCK_DGRAM,0);
if ( !fd )
@@ -100,23 +102,28 @@ void netGetIP( char *dev, char *ip, char *mask, char *brdcast )
memset(&req,0,sizeof(req));
strcpy(req.ifr_name,dev);
strncpy(req.ifr_name, dev.c_str(), sizeof(req.ifr_name));
saddr = (struct sockaddr_in *) &req.ifr_addr;
addr= (unsigned char*) &saddr->sin_addr.s_addr;
char tmp[80];
if( ioctl(fd,SIOCGIFADDR,&req) == 0 )
sprintf(ip,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
snprintf(tmp, sizeof(tmp),"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
ip = std::string(tmp);
if( ioctl(fd,SIOCGIFNETMASK,&req) == 0 )
sprintf(mask,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
snprintf(tmp, sizeof(tmp),"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
mask = std::string(tmp);
if( ioctl(fd,SIOCGIFBRDADDR,&req) == 0 )
sprintf(brdcast,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
snprintf(tmp, sizeof(tmp),"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
brdcast = std::string(tmp);
close(fd);
return;
}
#if 0
#if 0
//never used
void netSetDefaultRoute( char *gw )
{
@@ -153,34 +160,37 @@ void netSetDefaultRoute( char *gw )
return;
}
#endif
void netGetDefaultRoute( char *ip )
void netGetDefaultRoute( std::string &ip )
{
FILE *fp;
char interface[9];
unsigned char destination[4];
unsigned char gateway[4];
uint32_t destination;
uint32_t gw;
uint8_t gateway[4];
char zeile[256];
*ip = 0 ;
ip = "";
fp = fopen("/proc/net/route","r");
if (fp == NULL)
return;
fgets(zeile,sizeof(zeile),fp);
fgets(zeile,sizeof(zeile),fp); /* skip header */
while(fgets(zeile,sizeof(zeile),fp))
{
sscanf(zeile,"%8s %x %x",interface,(unsigned *) destination,(unsigned *) gateway);
if (*(unsigned *)destination == 0)
{
sprintf(ip,"%d.%d.%d.%d",gateway[0],gateway[1],gateway[2],gateway[3]);
break;
}
destination = 1; /* in case sscanf fails */
sscanf(zeile,"%8s %x %x", interface, &destination, &gw);
if (destination)
continue;
/* big/little endian kernels have reversed entries, so this is correct */
memcpy(gateway, &gw, 4);
char tmp[80];
snprintf(tmp, sizeof(tmp), "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
ip = std::string(tmp);
break;
}
fclose(fp);
}
static char hostbuf[256];
static char hostis=0;
#if 0
#if 0
static char dombuf[256];
static char domis=0;
//never used
@@ -199,29 +209,27 @@ void netSetDomainname( char *dom )
setdomainname(dombuf,strlen(dombuf)+1);
}
#endif
char *netGetHostname( void )
void netGetHostname( std::string &host )
{
if (!hostis)
gethostname( hostbuf, 256 );
hostis=1;
return hostbuf;
host = "";
char hostbuf[256];
if (!gethostname(hostbuf, sizeof(hostbuf)))
host = std::string(hostbuf);
}
void netSetHostname( char *host )
void netSetHostname( std::string &host )
{
FILE * fp;
strcpy(hostbuf,host);
hostis=1;
sethostname(hostbuf,strlen(hostbuf)+1);
sethostname(host.c_str(), host.length());
fp = fopen("/etc/hostname", "w");
if(fp != NULL) {
fprintf(fp, "%s\n", hostbuf);
fprintf(fp, "%s\n", host.c_str());
fclose(fp);
}
}
void netSetNameserver(const char * const ip)
void netSetNameserver(std::string &ip)
{
FILE *fp;
@@ -236,19 +244,19 @@ void netSetNameserver(const char * const ip)
fprintf(fp,"search %s\n",dom);
#endif
fprintf(fp, "# generated by neutrino\n");
if ((ip != NULL) && (strlen(ip) > 0))
fprintf(fp,"nameserver %s\n",ip);
if (!ip.empty())
fprintf(fp,"nameserver %s\n",ip.c_str());
fclose(fp);
}
void netGetNameserver( char *ip )
void netGetNameserver( std::string &ip )
{
FILE *fp;
char zeile[256];
char *indexLocal;
unsigned zaehler;
*ip = 0;
ip = "";
fp = fopen("/etc/resolv.conf","r");
if (!fp)
return;
@@ -257,32 +265,33 @@ void netGetNameserver( char *ip )
{
if (!strncasecmp(zeile,"nameserver",10))
{
char tmp[20];
indexLocal = zeile + 10;
while ( (*indexLocal == ' ') || (*indexLocal == '\t') )
indexLocal++;
zaehler = 0;
while ( (zaehler < 15) && ( ((*indexLocal >= '0') && (*indexLocal <= '9')) || (*indexLocal == '.')))
ip[zaehler++] = *(indexLocal++);
ip[zaehler] = 0;
tmp[zaehler++] = *(indexLocal++);
tmp[zaehler] = 0;
ip = std::string(tmp);
break;
}
}
fclose(fp);
}
void netGetMacAddr(char * ifname, unsigned char * mac)
void netGetMacAddr(std::string &ifname, unsigned char *mac)
{
int fd;
struct ifreq ifr;
memset(mac, 0, 6);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0)
return;
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, ifname, IFNAMSIZ-1);
strncpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name));
if(ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
return;

View File

@@ -1,28 +1,18 @@
#ifndef __libnet__
#define __libnet__
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
extern int netSetIP( char *dev, char *ip, char *mask, char *brdcast );
extern void netGetIP( char *dev, char *ip, char *mask, char *brdcast );
extern void netSetDefaultRoute( char *gw );
extern void netGetDefaultRoute( char *ip );
extern char *netGetDomainname( void );
extern void netSetDomainname( char *dom );
extern char *netGetHostname( void );
extern void netSetHostname( char *host );
extern void netSetNameserver(const char *ip);
extern void netGetNameserver( char *ip );
extern void netGetMacAddr(char * ifname, unsigned char * mac);
#ifdef __cplusplus
}
#endif
#include <string>
int netSetIP(std::string &dev, std::string &ip, std::string &mask, std::string &brdcast );
void netGetIP(std::string &dev, std::string &ip, std::string &mask, std::string &brdcast );
void netSetDefaultRoute( std::string &gw );
void netGetDefaultRoute( std::string &ip );
void netGetDomainname( std::string &dom );
void netSetDomainname( std::string &dom );
void netGetHostname( std::string &host );
void netSetHostname( std::string &host );
void netSetNameserver(std::string &ip);
void netGetNameserver( std::string &ip );
void netGetMacAddr(std::string & ifname, unsigned char *);
#endif