Diffstat (limited to 'noncore/settings/networksettings2/lancard/lancardrun.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/lancard/lancardrun.cpp | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/lancard/lancardrun.cpp b/noncore/settings/networksettings2/lancard/lancardrun.cpp new file mode 100644 index 0000000..b3b592b --- a/dev/null +++ b/noncore/settings/networksettings2/lancard/lancardrun.cpp | |||
@@ -0,0 +1,158 @@ | |||
1 | #include <qfile.h> | ||
2 | #include <qtextstream.h> | ||
3 | #include <resources.h> | ||
4 | #include "lancardrun.h" | ||
5 | |||
6 | void LanCardRun::detectState( NodeCollection * NC ) { | ||
7 | |||
8 | // unavailable : no card found | ||
9 | // available : card found and assigned to us or free | ||
10 | // up : card found and assigned to us and up | ||
11 | |||
12 | QString S = QString( "/tmp/profile-%1.up" ).arg(NC->number()); | ||
13 | System & Sys = NSResources->system(); | ||
14 | InterfaceInfo * Run; | ||
15 | |||
16 | QFile F( S ); | ||
17 | |||
18 | if( F.open( IO_ReadOnly ) ) { | ||
19 | // could open file -> read interface and assign | ||
20 | QString X; | ||
21 | QTextStream TS(&F); | ||
22 | X = TS.readLine(); | ||
23 | // find interface | ||
24 | if( handlesInterface( X ) ) { | ||
25 | for( QDictIterator<InterfaceInfo> It(Sys.interfaces()); | ||
26 | It.current(); | ||
27 | ++It ) { | ||
28 | Run = It.current(); | ||
29 | if( X == Run->Name ) { | ||
30 | Run->assignNode( netNode() ); | ||
31 | assignInterface( Run ); | ||
32 | NC->setCurrentState( IsUp ); | ||
33 | return; | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | } | ||
38 | |||
39 | // we are certainly not UP | ||
40 | assignInterface( 0 ); | ||
41 | |||
42 | // find possible interface | ||
43 | for( QDictIterator<InterfaceInfo> It(Sys.interfaces()); | ||
44 | It.current(); | ||
45 | ++It ) { | ||
46 | Run = It.current(); | ||
47 | if( handlesInterface( Run->Name ) && | ||
48 | Run->CardType == ARPHRD_ETHER && | ||
49 | ! Run->IsUp | ||
50 | ) { | ||
51 | // proper type, and Not UP -> free | ||
52 | NC->setCurrentState( Off ); | ||
53 | return; | ||
54 | } | ||
55 | } | ||
56 | // no free found | ||
57 | |||
58 | NC->setCurrentState( Unavailable ); | ||
59 | |||
60 | } | ||
61 | |||
62 | bool LanCardRun::setState( NodeCollection * NC, Action_t A ) { | ||
63 | |||
64 | // we only handle activate and deactivate | ||
65 | switch( A ) { | ||
66 | case Activate : | ||
67 | { | ||
68 | if( NC->currentState() != Off ) { | ||
69 | return 0; | ||
70 | } | ||
71 | InterfaceInfo * N = getInterface(); | ||
72 | if( ! N ) { | ||
73 | // no interface available | ||
74 | NC->setCurrentState( Unavailable ); | ||
75 | return 0; | ||
76 | } | ||
77 | // because we were OFF the interface | ||
78 | // we get back is NOT assigned | ||
79 | N->assignNode( netNode() ); | ||
80 | assignInterface( N ); | ||
81 | NC->setCurrentState( Available ); | ||
82 | return 1; | ||
83 | } | ||
84 | case Deactivate : | ||
85 | if( NC->currentState() == IsUp ) { | ||
86 | // bring down first | ||
87 | if( ! connection()->setState( Down ) ) | ||
88 | // could not ... | ||
89 | return 0; | ||
90 | } | ||
91 | if( NC->currentState() != Available ) { | ||
92 | return 1; | ||
93 | } | ||
94 | assignedInterface()->assignNode( 0 ); // release | ||
95 | assignInterface( 0 ); | ||
96 | NC->setCurrentState( Off ); | ||
97 | return 1; | ||
98 | default : | ||
99 | // FT | ||
100 | break; | ||
101 | } | ||
102 | return 0; | ||
103 | } | ||
104 | |||
105 | bool LanCardRun::canSetState( State_t Curr, Action_t A ) { | ||
106 | // we only handle up down activate and deactivate | ||
107 | switch( A ) { | ||
108 | case Activate : | ||
109 | { // at least available | ||
110 | if( Curr == Available ) { | ||
111 | return 1; | ||
112 | } | ||
113 | // or we can make one available | ||
114 | InterfaceInfo * N = getInterface(); | ||
115 | if( ! N || N->assignedNode() != 0 ) { | ||
116 | // non available or assigned | ||
117 | return 0; | ||
118 | } | ||
119 | return 1; | ||
120 | } | ||
121 | case Deactivate : | ||
122 | return ( Curr >= Available ); | ||
123 | default : | ||
124 | // FT | ||
125 | break; | ||
126 | } | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | // get interface that is free or assigned to us | ||
131 | InterfaceInfo * LanCardRun::getInterface( void ) { | ||
132 | |||
133 | System & S = NSResources->system(); | ||
134 | InterfaceInfo * best = 0, * Run; | ||
135 | |||
136 | for( QDictIterator<InterfaceInfo> It(S.interfaces()); | ||
137 | It.current(); | ||
138 | ++It ) { | ||
139 | Run = It.current(); | ||
140 | if( handlesInterface( Run->Name ) && | ||
141 | Run->CardType == ARPHRD_ETHER | ||
142 | ) { | ||
143 | // this is a LAN card | ||
144 | if( Run->assignedNode() == netNode() ) { | ||
145 | // assigned to us | ||
146 | return Run; | ||
147 | } else if( Run->assignedNode() == 0 ) { | ||
148 | // free | ||
149 | best = Run; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | return best; // can be 0 | ||
154 | } | ||
155 | |||
156 | bool LanCardRun::handlesInterface( const QString & S ) { | ||
157 | return Pat.match( S ) >= 0; | ||
158 | } | ||