our current experimental Neutrino branch

git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@27 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
mrcolor
2009-12-08 11:05:11 +00:00
commit bc5bd4154e
876 changed files with 193775 additions and 0 deletions

5
lib/libnet/Makefile.am Normal file
View File

@@ -0,0 +1,5 @@
noinst_LIBRARIES = libtuxbox-net.a
AM_CPPFLAGS = -fno-rtti -fno-exceptions
libtuxbox_net_a_SOURCES = libnet.c network_interfaces.cpp

260
lib/libnet/libnet.c Normal file
View File

@@ -0,0 +1,260 @@
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <linux/route.h>
static void scanip( char *str, unsigned char *to )
{
int val;
int c;
char *sp;
int b=4;
for( sp=str; b; b-- )
{
val=0;
for(; (*sp != 0) && (*sp != '.'); sp++)
{
c = *sp - '0';
if (( c < 0 ) || ( c>=10))
break;
val=(val*10)+c;
}
*to=val;
to++;
if ( !*sp )
break;
sp++;
}
}
int netSetIP( char *dev, char *ip, char *mask, char *brdcast )
{
int fd;
struct ifreq req;
int rc=-1;
unsigned char adr_ip[4];
unsigned char adr_mask[4];
unsigned char adr_brdcast[4];
struct sockaddr_in addr;
fd=socket(AF_INET,SOCK_DGRAM,0);
if ( !fd )
return -1;
scanip( ip, adr_ip );
scanip( mask, adr_mask );
scanip( brdcast, adr_brdcast );
/* init structures */
bzero(&req,sizeof(req));
strcpy(req.ifr_name,dev);
bzero(&addr,sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *((unsigned long *) adr_ip);
memcpy(&req.ifr_addr,&addr,sizeof(addr));
if( ioctl(fd,SIOCSIFADDR,&req) < 0 )
goto abbruch;
addr.sin_addr.s_addr = *((unsigned long *) adr_mask);
memcpy(&req.ifr_addr,&addr,sizeof(addr));
if( ioctl(fd,SIOCSIFNETMASK,&req) < 0 )
goto abbruch;
addr.sin_addr.s_addr = *((unsigned long *) adr_brdcast);
memcpy(&req.ifr_addr,&addr,sizeof(addr));
if( ioctl(fd,SIOCSIFBRDADDR,&req) < 0 )
goto abbruch;
rc = 0;
abbruch:
close(fd);
return rc;
}
void netGetIP( char *dev, char *ip, char *mask, char *brdcast )
{
int fd;
struct ifreq req;
struct sockaddr_in *saddr;
unsigned char *addr;
*ip=0;
*mask=0;
*brdcast=0;
fd=socket(AF_INET,SOCK_DGRAM,0);
if ( !fd )
return;
bzero(&req,sizeof(req));
strcpy(req.ifr_name,dev);
saddr = (struct sockaddr_in *) &req.ifr_addr;
addr= (unsigned char*) &saddr->sin_addr.s_addr;
if( ioctl(fd,SIOCGIFADDR,&req) == 0 )
sprintf(ip,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
if( ioctl(fd,SIOCGIFNETMASK,&req) == 0 )
sprintf(mask,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
if( ioctl(fd,SIOCGIFBRDADDR,&req) == 0 )
sprintf(brdcast,"%d.%d.%d.%d",addr[0],addr[1],addr[2],addr[3]);
close(fd);
return;
}
void netSetDefaultRoute( char *gw )
{
struct rtentry re;
struct sockaddr_in *in_addr;
unsigned char *addr;
int fd;
unsigned char adr_gw[4];
scanip( gw, adr_gw );
memset(&re,0,sizeof(struct rtentry));
in_addr = (struct sockaddr_in *)&re.rt_dst;
in_addr->sin_family = AF_INET;
in_addr = (struct sockaddr_in *)&re.rt_gateway;
in_addr->sin_family = AF_INET;
addr=(unsigned char*)&in_addr->sin_addr.s_addr;
in_addr = (struct sockaddr_in *)&re.rt_genmask;
in_addr->sin_family = AF_INET;
fd=socket(AF_INET,SOCK_DGRAM,0);
if ( fd < 0 )
return;
re.rt_flags = RTF_GATEWAY | RTF_UP;
memcpy(addr,adr_gw,4);
ioctl(fd,SIOCADDRT,&re);
close(fd);
return;
}
void netGetDefaultRoute( char *ip )
{
FILE *fp;
char interface[9];
unsigned char destination[4];
unsigned char gateway[4];
char zeile[256];
*ip = 0 ;
fp = fopen("/proc/net/route","r");
if (fp == NULL)
return;
fgets(zeile,sizeof(zeile),fp);
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;
}
}
fclose(fp);
}
static char dombuf[256];
static char hostbuf[256];
static char domis=0;
static char hostis=0;
char *netGetDomainname( void )
{
if (!domis)
getdomainname( dombuf, 256 );
domis=1;
return dombuf;
}
void netSetDomainname( char *dom )
{
strcpy(dombuf,dom);
domis=1;
setdomainname(dombuf,strlen(dombuf)+1);
}
char *netGetHostname( void )
{
if (!hostis)
gethostname( hostbuf, 256 );
hostis=1;
return hostbuf;
}
void netSetHostname( char *host )
{
strcpy(hostbuf,host);
hostis=1;
sethostname(hostbuf,strlen(hostbuf)+1);
}
void netSetNameserver(const char * const ip)
{
FILE *fp;
fp = fopen("/etc/resolv.conf","w");
if (!fp)
return;
#if 0
char *dom;
dom=netGetDomainname();
if (dom && strlen(dom)>2)
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);
fclose(fp);
}
void netGetNameserver( char *ip )
{
FILE *fp;
char zeile[256];
char *index;
unsigned zaehler;
*ip = 0;
fp = fopen("/etc/resolv.conf","r");
if (!fp)
return;
while (fgets(zeile,sizeof(zeile),fp))
{
if (!strncasecmp(zeile,"nameserver",10))
{
index = zeile + 10;
while ( (*index == ' ') || (*index == '\t') )
index++;
zaehler = 0;
while ( (zaehler < 15) && ( ((*index >= '0') && (*index <= '9')) || (*index == '.')))
ip[zaehler++] = *(index++);
ip[zaehler] = 0;
break;
}
}
fclose(fp);
}

27
lib/libnet/libnet.h Normal file
View File

@@ -0,0 +1,27 @@
#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 );
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,373 @@
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libnet/network_interfaces.cpp,v 1.6 2003/03/20 15:32:52 thegoodguy Exp $
*
* (C) 2003 by thegoodguy <thegoodguy@berlios.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <fstream>
#include <list>
#include <map>
#include <string>
#include <sstream>
/*
* Known bugs:
* -----------
*
* - extension of lines across multiple lines using \ is not supported
*
*/
bool read_file(const std::string filename, std::list<std::string> &line)
{
std::string s;
std::ifstream in(filename.c_str());
line.clear();
if (!in.is_open())
return false;
while (getline(in, s))
line.push_back(s);
return true;
}
bool write_file(const std::string filename, const std::list<std::string> line)
{
std::ofstream out(filename.c_str());
if (!out.is_open())
return false;
for (std::list<std::string>::const_iterator it = line.begin(); it != line.end(); it++)
out << (*it) << std::endl;
return true;
}
std::list<std::string>::iterator add_attributes(const std::map<std::string, std::string> attribute, std::list<std::string> &line, std::list<std::string>::iterator here)
{
for (std::map<std::string, std::string>::const_iterator it = attribute.begin(); it != attribute.end(); it++)
{
std::ostringstream out;
out << '\t' << (it -> first) << ' ' << (it -> second);
line.insert(here, out.str());
}
return here;
}
std::string remove_interface_from_line(const std::string interface, const std::string line)
{
std::string s;
std::istringstream in(line.c_str());
std::ostringstream out;
bool contains_at_least_one_interface = false;
if (in >> s)
{
out << s; /* auto */
while (in >> s)
{
if (s != interface)
{
out << ' ' << s;
contains_at_least_one_interface = true;
}
}
}
return (contains_at_least_one_interface ? out.str() : "");
}
bool write_interface(const std::string filename, const std::string name, const bool automatic_start, const std::string family, const std::string method, const std::map<std::string, std::string> attribute)
{
std::string s;
std::list<std::string> line;
bool found = false;
read_file(filename, line); /* ignore return value */
for (std::list<std::string>::iterator it = line.begin(); it != line.end(); )
{
{
std::istringstream in((*it).c_str());
if (!(in >> s))
{
it++;
continue;
}
if (s != std::string("iface"))
{
if (s == std::string("auto"))
{
bool advance = true;
while (in >> s)
{
if (s == std::string(name))
{
*it = remove_interface_from_line(name, *it);
if ((*it).empty())
{
it = line.erase(it); /* erase advances it */
advance = false;
}
break;
}
}
if (advance)
it++;
}
else
it++;
continue;
}
if (!(in >> s))
{
it++;
continue;
}
if (s != std::string(name))
{
it++;
continue;
}
}
found = true;
/* replace line */
std::ostringstream out;
out << "iface " << name << ' ' << family << ' ' << method;
(*it) = out.str();
if (automatic_start)
line.insert(it, "auto " + name);
/* add attributes */
it++;
it = add_attributes(attribute, line, it);
/* remove current attributes */
while (it != line.end())
{
std::istringstream in((*it).c_str());
if (!(in >> s)) /* retain empty lines */
{
it++;
continue;
}
if (s[0] == '#') /* retain comments */
{
it++;
continue;
}
if (s == std::string("iface"))
break;
if (s == std::string("auto"))
break;
if (s == std::string("mapping"))
break;
it = line.erase(it);
}
}
if (!found)
{
std::ostringstream out;
if (automatic_start)
line.push_back("auto " + name);
out << "iface " << name << ' ' << family << ' ' << method;
line.push_back(out.str());
add_attributes(attribute, line, line.end());
}
return write_file(filename, line);
}
bool read_interface(const std::string filename, const std::string name, bool &automatic_start, std::string &family, std::string &method, std::map<std::string, std::string> &attribute)
{
std::string s;
std::string t;
std::ifstream in(filename.c_str());
bool advance = true;
automatic_start = false;
attribute.clear();
if (!in.is_open())
return false;
while (true)
{
if (advance)
{
if (!getline(in, s))
break;
}
else
advance = true;
{
std::istringstream in(s.c_str());
if (!(in >> s))
continue;
if (s != std::string("iface"))
{
if (s == std::string("auto"))
{
while (in >> s)
{
if (s == std::string(name))
{
automatic_start = true;
break;
}
}
}
continue;
}
if (!(in >> s))
continue;
if (s != std::string(name))
continue;
if (!(in >> s))
continue;
if (!(in >> t))
continue;
family = s;
method = t;
}
while (true)
{
if (!getline(in, s))
return true;
std::istringstream in(s.c_str());
if (!(in >> t)) /* ignore empty lines */
continue;
if (t[0] == '#') /* ignore comments */
continue;
if (t == std::string("iface"))
break;
if (t == std::string("auto"))
break;
if (t == std::string("mapping"))
break;
if (!(in >> s))
continue;
attribute[t] = s;
}
advance = false;
}
return true;
}
bool getInetAttributes(const std::string name, bool &automatic_start, std::string &address, std::string &netmask, std::string &broadcast, std::string &gateway)
{
std::string family;
std::string method;
std::map<std::string, std::string> attribute;
if (!read_interface("/etc/network/interfaces", name, automatic_start, family, method, attribute))
return false;
if (family != "inet")
return false;
if (method != "static")
return false;
address = "";
netmask = "";
broadcast = "";
gateway = "";
for (std::map<std::string, std::string>::const_iterator it = attribute.begin(); it != attribute.end(); it++)
{
if ((*it).first == "address")
address = (*it).second;
if ((*it).first == "netmask")
netmask = (*it).second;
if ((*it).first == "broadcast")
broadcast = (*it).second;
if ((*it).first == "gateway")
gateway = (*it).second;
}
return true;
}
bool addLoopbackDevice(const std::string name, const bool automatic_start)
{
std::map<std::string, std::string> attribute;
return write_interface("/etc/network/interfaces", name, automatic_start, "inet", "loopback", attribute);
}
bool setStaticAttributes(const std::string name, const bool automatic_start, const std::string address, const std::string netmask, const std::string broadcast, const std::string gateway)
{
std::map<std::string, std::string> attribute;
attribute["address"] = address;
attribute["netmask"] = netmask;
if (!broadcast.empty())
attribute["broadcast"] = broadcast;
if (!gateway.empty())
attribute["gateway"] = gateway;
return write_interface("/etc/network/interfaces", name, automatic_start, "inet", "static", attribute);
}
bool setDhcpAttributes(const std::string name, const bool automatic_start)
{
std::map<std::string, std::string> attribute;
return write_interface("/etc/network/interfaces", name, automatic_start, "inet", "dhcp", attribute);
}

View File

@@ -0,0 +1,35 @@
#ifndef __network_interfaces_h__
#define __network_interfaces_h__
/*
* $Header: /cvs/tuxbox/apps/misc/libs/libnet/network_interfaces.h,v 1.4 2003/03/20 14:07:32 thegoodguy Exp $
*
* (C) 2003 by thegoodguy <thegoodguy@berlios.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <string>
bool getInetAttributes(const std::string name, bool &automatic_start, std::string &address, std::string &netmask, std::string &broadcast, std::string &gateway);
bool addLoopbackDevice(const std::string name, const bool automatic_start);
bool setStaticAttributes(const std::string name, const bool automatic_start, const std::string address, const std::string netmask, const std::string broadcast, const std::string gateway);
bool setDhcpAttributes(const std::string name, const bool automatic_start);
#endif /* __network_interfaces_h__ */