summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/lancard/lancardrun.cpp
blob: 470b79706d7cc304ddfdfc82054978d0e5c1cc10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <qfile.h>
#include <qtextstream.h>
#include <qstringlist.h>
#include <resources.h>
#include "lancardrun.h"

State_t LanCardRun::detectState( void ) {

    // unavailable : no card found
    // available : card found and assigned to us or free
    // up : card found and assigned to us and up

    NodeCollection * NC = nodeCollection();
    QString S = QString( "/tmp/profile-%1.up" ).
                arg( NC->number());
    System & Sys = NSResources->system();
    InterfaceInfo * Run;

    QFile F( S );

    if( F.open( IO_ReadOnly ) ) {
      // could open file -> read interface and assign
      QString X;
      QTextStream TS(&F);
      X = TS.readLine();
      // find interface
      if( handlesInterface( X ) ) {
        for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
             It.current();
             ++It ) {
          Run = It.current();
          if( X == Run->Name ) {
            NC->assignInterface( Run );
            return IsUp;
          }
        }
      }
    } 

    if( ( Run = NC->assignedInterface() ) ) {
      // we already have an interface assigned -> still present ?
      if( ! Run->IsUp ) {
        // usb is still free -> keep assignment
        return Available;
      } // else interface is up but NOT us -> some other profile
    }

    // nothing (valid) assigned to us
    NC->assignInterface( 0 );

    // find possible interface
    for( QDictIterator<InterfaceInfo> It(Sys.interfaces());
         It.current();
         ++It ) {
      Run = It.current();
      if( handlesInterface( *Run ) &&
          ( Run->CardType == ARPHRD_ETHER
#ifdef ARPHRD_IEEE1394
            || Run->CardType == ARPHRD_IEEE1394
#endif
          ) &&
          ! Run->IsUp
        ) {
        // proper type, and Not UP -> free
        return Off;
      }
    }
    // no free found

    return Unavailable;
}

QString LanCardRun::setMyState( NodeCollection * NC, Action_t A, bool ) {

    if( A == Activate ) {
      InterfaceInfo * N = getInterface();

      if( ! N ) {
        // no interface available
        NC->setCurrentState( Unavailable );
        return tr("No interface found");
      }

      // because we were OFF the interface
      // we get back is NOT assigned
      NC->assignInterface( N );
      NC->setCurrentState( Available );
      return QString();
    }

    if( A == Deactivate ) {
      NC->assignInterface( 0 );
      NC->setCurrentState( Off );
    }

    return QString();
}

// get interface that is free or assigned to us
InterfaceInfo * LanCardRun::getInterface( void ) {

    System & S = NSResources->system();
    InterfaceInfo * best = 0, * Run;

    for( QDictIterator<InterfaceInfo> It(S.interfaces());
         It.current();
         ++It ) {
      Run = It.current();
      if( handlesInterface( *Run ) &&
          ( Run->CardType == ARPHRD_ETHER
#ifdef ARPHRD_IEEE1394
            || Run->CardType == ARPHRD_IEEE1394
#endif
          )
        ) {
        // this is a LAN card
        if( Run->assignedConnection() == netNode()->connection() ) {
          // assigned to us
          return Run;
        } else if( Run->assignedConnection() == 0 ) {
          // free
          best = Run;
        }
      }
    }
    return best; // can be 0
}

bool LanCardRun::handlesInterface( const QString & S ) {
    InterfaceInfo * II;
    II = NSResources->system().interface( S );
    if( ( II = NSResources->system().interface( S ) ) ) {
      return handlesInterface( *II );
    }
    return Pat.match( S ) >= 0;
}

bool LanCardRun::handlesInterface( const InterfaceInfo & II ) {
    if( Pat.match( II.Name ) < 0 )
      return 0;

    if( Data->AnyLanCard ) {
      return 1;
    }

    // must also match hardware address
    return ( Data->HWAddresses.findIndex( II.MACAddress ) >= 0 );
}