- Initial DCI transformation support on server side
[public/netxms.git] / src / server / core / interface.cpp
CommitLineData
282349dd
VK
1/*
2** Project X - Network Management System
3** Copyright (C) 2003 Victor Kirhenshtein
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18**
19** $module: interface.cpp
20**
21**/
22
23#include "nms_core.h"
24
25
26//
27// Default constructor for Interface object
28//
29
30Interface::Interface()
31 : NetObj()
32{
33 m_dwIpNetMask = 0;
34 m_dwIfIndex = 0;
35 m_dwIfType = IFTYPE_OTHER;
36}
37
38
39//
40// Constructor for "fake" interface object
41//
42
43Interface::Interface(DWORD dwAddr, DWORD dwNetMask)
44 : NetObj()
45{
46 strcpy(m_szName, "lan0");
47 m_dwIpAddr = dwAddr;
48 m_dwIpNetMask = dwNetMask;
49 m_dwIfIndex = 1;
50 m_dwIfType = IFTYPE_OTHER;
51}
52
53
54//
55// Constructor for normal interface object
56//
57
58Interface::Interface(char *szName, DWORD dwIndex, DWORD dwAddr, DWORD dwNetMask, DWORD dwType)
59 : NetObj()
60{
61 strncpy(m_szName, szName, MAX_OBJECT_NAME);
62 m_dwIfIndex = dwIndex;
63 m_dwIfType = dwType;
64 m_dwIpAddr = dwAddr;
65 m_dwIpNetMask = dwNetMask;
66}
67
68
69//
70// Interfacet class destructor
71//
72
73Interface::~Interface()
74{
75}
76
77
cc140cce
VK
78//
79// Create object from database data
80//
81
82BOOL Interface::CreateFromDB(DWORD dwId)
83{
84 char szQuery[256];
85 DB_RESULT hResult;
86 DWORD dwNodeId;
87 NetObj *pObject;
88 BOOL bResult = FALSE;
89
333ece94
VK
90 sprintf(szQuery, "SELECT id,name,status,ip_addr,ip_netmask,if_type,if_index,node_id,"
91 "image_id,is_deleted FROM interfaces WHERE id=%d", dwId);
cc140cce 92 hResult = DBSelect(g_hCoreDB, szQuery);
333ece94 93 if (hResult == NULL)
cc140cce
VK
94 return FALSE; // Query failed
95
96 if (DBGetNumRows(hResult) != 0)
97 {
98 m_dwId = dwId;
99 strncpy(m_szName, DBGetField(hResult, 0, 1), MAX_OBJECT_NAME);
100 m_iStatus = DBGetFieldLong(hResult, 0, 2);
101 m_dwIpAddr = DBGetFieldULong(hResult, 0, 3);
102 m_dwIpNetMask = DBGetFieldULong(hResult, 0, 4);
103 m_dwIfType = DBGetFieldULong(hResult, 0, 5);
104 m_dwIfIndex = DBGetFieldULong(hResult, 0, 6);
105 dwNodeId = DBGetFieldULong(hResult, 0, 7);
2a41a4b5 106 m_dwImageId = DBGetFieldULong(hResult, 0, 8);
333ece94 107 m_bIsDeleted = DBGetFieldLong(hResult, 0, 9);
cc140cce
VK
108
109 // Link interface to node
333ece94 110 if (!m_bIsDeleted)
cc140cce 111 {
333ece94
VK
112 pObject = FindObjectById(dwNodeId);
113 if (pObject == NULL)
114 {
115 WriteLog(MSG_INVALID_NODE_ID, EVENTLOG_ERROR_TYPE, "dd", dwId, dwNodeId);
116 }
117 else if (pObject->Type() != OBJECT_NODE)
118 {
119 WriteLog(MSG_NODE_NOT_NODE, EVENTLOG_ERROR_TYPE, "dd", dwId, dwNodeId);
120 }
121 else
122 {
123 pObject->AddChild(this);
124 AddParent(pObject);
125 bResult = TRUE;
126 }
cc140cce
VK
127 }
128 else
129 {
cc140cce
VK
130 bResult = TRUE;
131 }
132 }
133
134 DBFreeResult(hResult);
de78f964
VK
135
136 // Load access list
137 LoadACLFromDB();
138
cc140cce
VK
139 return bResult;
140}
141
142
282349dd
VK
143//
144// Save interface object to database
145//
146
147BOOL Interface::SaveToDB(void)
148{
149 char szQuery[1024];
150 BOOL bNewObject = TRUE;
151 DWORD dwNodeId;
152 DB_RESULT hResult;
153
154 // Lock object's access
155 Lock();
156
157 // Check for object's existence in database
158 sprintf(szQuery, "SELECT id FROM interfaces WHERE id=%ld", m_dwId);
159 hResult = DBSelect(g_hCoreDB, szQuery);
160 if (hResult != 0)
161 {
162 if (DBGetNumRows(hResult) > 0)
163 bNewObject = FALSE;
164 DBFreeResult(hResult);
165 }
166
167 // Determine owning node's ID
168 if (m_dwParentCount > 0) // Always should be
169 dwNodeId = m_pParentList[0]->Id();
170 else
171 dwNodeId = 0;
172
173 // Form and execute INSERT or UPDATE query
174 if (bNewObject)
2a41a4b5
VK
175 sprintf(szQuery, "INSERT INTO interfaces (id,name,status,is_deleted,ip_addr,"
176 "ip_netmask,node_id,if_type,if_index,image_id) "
177 "VALUES (%ld,'%s',%d,%d,%ld,%ld,%ld,%ld,%ld,%ld)",
178 m_dwId, m_szName, m_iStatus, m_bIsDeleted, m_dwIpAddr, m_dwIpNetMask, dwNodeId,
179 m_dwIfType, m_dwIfIndex, m_dwImageId);
282349dd 180 else
2a41a4b5
VK
181 sprintf(szQuery, "UPDATE interfaces SET name='%s',status=%d,is_deleted=%d,"
182 "ip_addr=%ld,ip_netmask=%ld,node_id=%ld,if_type=%ld,"
183 "if_index=%ld,image_id=%ld WHERE id=%ld",
184 m_szName, m_iStatus, m_bIsDeleted, m_dwIpAddr, m_dwIpNetMask, dwNodeId,
185 m_dwIfType, m_dwIfIndex, m_dwImageId, m_dwId);
282349dd
VK
186 DBQuery(g_hCoreDB, szQuery);
187
de78f964
VK
188 // Save access list
189 SaveACLToDB();
190
282349dd
VK
191 // Clear modifications flag and unlock object
192 m_bIsModified = FALSE;
193 Unlock();
194
195 return TRUE;
196}
197
198
199//
200// Delete interface object from database
201//
202
203BOOL Interface::DeleteFromDB(void)
204{
205 char szQuery[128];
ef44d5ea 206 BOOL bSuccess;
282349dd 207
ef44d5ea
VK
208 bSuccess = NetObj::DeleteFromDB();
209 if (bSuccess)
210 {
211 sprintf(szQuery, "DELETE FROM interfaces WHERE id=%ld", m_dwId);
212 QueueSQLRequest(szQuery);
213 }
214 return bSuccess;
282349dd 215}
f00307b7
VK
216
217
218//
219// Perform status poll on interface
220//
221
222void Interface::StatusPoll(void)
223{
224 int iOldStatus = m_iStatus;
66c04cc7 225 DWORD dwPingStatus;
f00307b7
VK
226
227 if (m_dwIpAddr == 0)
228 {
229 m_iStatus = STATUS_UNKNOWN;
230 return; // Interface has no IP address, we cannot check it
231 }
232
b7271ccb 233 dwPingStatus = IcmpPing(m_dwIpAddr, 3, 1500, NULL);
66c04cc7
VK
234 if (dwPingStatus == ICMP_RAW_SOCK_FAILED)
235 WriteLog(MSG_RAW_SOCK_FAILED, EVENTLOG_WARNING_TYPE, NULL);
236 m_iStatus = (dwPingStatus == ICMP_SUCCESS) ? STATUS_NORMAL : STATUS_CRITICAL;
f00307b7
VK
237 if (m_iStatus != iOldStatus)
238 {
239 PostEvent(m_iStatus == STATUS_NORMAL ? EVENT_INTERFACE_UP : EVENT_INTERFACE_DOWN,
240 GetParent()->Id(), "dsaad", m_dwId, m_szName, m_dwIpAddr, m_dwIpNetMask,
241 m_dwIfIndex);
53512272 242 Modify();
f00307b7
VK
243 }
244}
b81249b7
VK
245
246
247//
248// Create CSCP message with object's data
249//
250
251void Interface::CreateMessage(CSCPMessage *pMsg)
252{
253 NetObj::CreateMessage(pMsg);
a5f8dbb8
VK
254 pMsg->SetVariable(VID_IF_INDEX, m_dwIfIndex);
255 pMsg->SetVariable(VID_IF_TYPE, m_dwIfType);
256 pMsg->SetVariable(VID_IP_NETMASK, m_dwIpNetMask);
b81249b7 257}