00001 #include <system.h>
00002 #include <netnode.h>
00003 #include "networkedit.h"
00004 #include "network_NNI.h"
00005 #include "network_NN.h"
00006
00007 ANetwork::ANetwork( NetworkNetNode * PNN ) : ANetNodeInstance( PNN ) {
00008 Data.UseDHCP = 1;
00009 Data.IPAddress = "";
00010 Data.NetMask = "";
00011 Data.Broadcast = "";
00012 Data.Gateway = "";
00013 Data.DNS1 = "";
00014 Data.DNS2 = "";
00015 Data.SendHostname = 0;
00016 Data.Hostname = "";
00017 Data.PreUp_SL.clear();
00018 Data.PreDown_SL.clear();
00019 Data.PostUp_SL.clear();
00020 Data.PostDown_SL.clear();
00021 GUI = 0;
00022 RT = 0;
00023 }
00024
00025 void ANetwork::setSpecificAttribute( QString & A, QString & V ) {
00026 if( A == "usedhcp" ) {
00027 Data.UseDHCP = (V == "yes");
00028 } else if( A == "sendhostname" ) {
00029 Data.SendHostname = (V=="yes");
00030 } else if( A == "hostname" ) {
00031 Data.Hostname = V;
00032 } else if( A == "ipaddress" ) {
00033 Data.IPAddress = V;
00034 } else if( A == "netmask" ) {
00035 Data.NetMask = V;
00036 } else if( A == "broadcast" ) {
00037 Data.Broadcast = V;
00038 } else if( A == "gateway" ) {
00039 Data.Gateway = V;
00040 } else if( A == "dns1" ) {
00041 Data.DNS1 = V;
00042 } else if( A == "dns2" ) {
00043 Data.DNS2 = V;
00044 } else if( A == "preup" ) {
00045 Data.PreUp_SL.append( V );
00046 } else if( A == "predown" ) {
00047 Data.PreDown_SL.append( V );
00048 } else if( A == "postup" ) {
00049 Data.PostUp_SL.append( V );
00050 } else if( A == "postdown" ) {
00051 Data.PostDown_SL.append( V );
00052 }
00053 }
00054
00055 void ANetwork::saveSpecificAttribute( QTextStream & TS ) {
00056 TS << "usedhcp=" << ((Data.UseDHCP) ? "yes" : "no") << endl;
00057 TS << "sendhostname=" << ((Data.SendHostname) ? "yes" : "no") << endl;
00058 TS << "hostname=" << Data.Hostname << endl;
00059 TS << "ipaddress=" << Data.IPAddress << endl;
00060 TS << "netmask=" << Data.NetMask << endl;
00061 TS << "broadcast=" << Data.Broadcast << endl;
00062 TS << "gateway=" << Data.Gateway << endl;
00063 TS << "dns1=" << Data.DNS1 << endl;
00064 TS << "dns2=" << Data.DNS2 << endl;
00065 for ( QStringList::Iterator it = Data.PreUp_SL.begin();
00066 it != Data.PreUp_SL.end();
00067 ++it ) {
00068 TS << "preup=" << quote(*it) << endl;
00069 }
00070 for ( QStringList::Iterator it = Data.PreDown_SL.begin();
00071 it != Data.PreDown_SL.end();
00072 ++it ) {
00073 TS << "predown=" << quote(*it) << endl;
00074 }
00075 for ( QStringList::Iterator it = Data.PostUp_SL.begin();
00076 it != Data.PostUp_SL.end();
00077 ++it ) {
00078 TS << "postup=" << quote(*it) << endl;
00079 }
00080 for ( QStringList::Iterator it = Data.PostDown_SL.begin();
00081 it != Data.PostDown_SL.end();
00082 ++it ) {
00083 TS << "postdown=" << quote(*it) << endl;
00084 }
00085 }
00086
00087 QWidget * ANetwork::edit( QWidget * parent ) {
00088 GUI = new NetworkEdit( parent );
00089 GUI->showData( Data );
00090 return GUI;
00091 }
00092
00093 QString ANetwork::acceptable( void ) {
00094 return ( GUI ) ? GUI->acceptable( ) : QString();
00095 }
00096
00097 void ANetwork::commit( void ) {
00098 if( GUI && GUI->commit( Data ) )
00099 setModified( 1 );
00100 }
00101
00102 bool ANetwork::hasDataForFile( SystemFile & S ) {
00103 return S.name() == "interfaces";
00104 }
00105
00106 short ANetwork::generateFile( SystemFile &SF,
00107 long DevNr
00108 ) {
00109
00110 short rvl, rvd ;
00111 QString NIC = runtime()->device()->netNode()->nodeClass()->genNic( DevNr );
00112
00113 rvl = 1;
00114 if( SF.name() == "interfaces" ) {
00115 Log(("Generate Network for %s\n", SF.name().latin1() ));
00116
00117 if( Data.UseDHCP ) {
00118 SF << "iface "
00119 << "A"
00120 << networkSetup()->number()
00121 << NIC
00122 << " inet dhcp"
00123 << endl;
00124 SF << " up echo \""
00125 << NIC
00126 << "\" > /tmp/profile-"
00127 << networkSetup()->number()
00128 << ".up"
00129 << endl;
00130 if( Data.SendHostname ) {
00131 SF << " hostname "
00132 << Data.Hostname
00133 << endl;
00134 }
00135
00136 SF << " down rm -f /tmp/profile-"
00137 << networkSetup()->number()
00138 << ".up"
00139 << endl;
00140 } else {
00141 SF << "iface "
00142 << "A"
00143 << networkSetup()->number()
00144 << NIC
00145 << " inet static"
00146 << endl;
00147 SF << " up echo \""
00148 << NIC
00149 << "\" > /tmp/profile-"
00150 << networkSetup()->number()
00151 << ".up"
00152 << endl;
00153 SF << " down rm -f /tmp/profile-"
00154 << networkSetup()->number()
00155 << ".up"
00156 << endl;
00157 SF << " address "
00158 << Data.IPAddress
00159 << endl;
00160 SF << " broadcast "
00161 << Data.Broadcast
00162 << endl;
00163 SF << " netmask "
00164 << Data.NetMask
00165 << endl;
00166
00167
00168 { QString NW;
00169 QStringList ipal = QStringList::split( '.', Data.IPAddress );
00170 QStringList nmal = QStringList::split( '.', Data.NetMask );
00171
00172 NW = QString( "%1.%2.%3.%4" ).
00173 arg( ipal[0].toShort() & nmal[0].toShort() ).
00174 arg( ipal[1].toShort() & nmal[1].toShort() ).
00175 arg( ipal[2].toShort() & nmal[2].toShort() ).
00176 arg( ipal[3].toShort() & nmal[3].toShort() );
00177 SF << " network "
00178 << NW
00179 << endl;
00180 }
00181 }
00182 for ( QStringList::Iterator it = Data.PreUp_SL.begin();
00183 it != Data.PreUp_SL.end();
00184 ++it ) {
00185 SF << " pre-up "
00186 << (*it)
00187 << endl;
00188 }
00189 for ( QStringList::Iterator it = Data.PostUp_SL.begin();
00190 it != Data.PostUp_SL.end();
00191 ++it ) {
00192 SF << " up "
00193 << (*it)
00194 << endl;
00195 }
00196 for ( QStringList::Iterator it = Data.PreDown_SL.begin();
00197 it != Data.PreDown_SL.end();
00198 ++it ) {
00199 SF << " down "
00200 << (*it)
00201 << endl;
00202 }
00203 for ( QStringList::Iterator it = Data.PostDown_SL.begin();
00204 it != Data.PostDown_SL.end();
00205 ++it ) {
00206 SF << " post-down "
00207 << (*it)
00208 << endl;
00209 }
00210 rvl = 0;
00211 }
00212
00213
00214 rvd = networkSetup()->getToplevel()->generateFileEmbedded( SF, DevNr );
00215
00216 return (rvd == 2 || rvl == 2 ) ? 2 :
00217 (rvd == 0 || rvl == 0 ) ? 0 : 1;
00218 }