📄 frontenddlg.cpp
字号:
/* * The olsr.org Optimized Link-State Routing daemon (olsrd) * Copyright (c) 2004, Thomas Lopatic (thomas@lopatic.de) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of olsr.org, olsrd nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Visit http://www.olsr.org for more information. * * If you find this software useful feel free to make a donation * to the project. For more information see the website or contact * the copyright holders. * * $Id: FrontendDlg.cpp,v 1.11 2007/03/27 03:01:06 tlopatic Exp $ */#include "stdafx.h"#include "Frontend.h"#include "FrontendDlg.h"#include "TrayIcon.h"#include "Ipc.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifCFrontendDlg::CFrontendDlg(CWnd* pParent) : CDialog(CFrontendDlg::IDD, pParent){ //{{AFX_DATA_INIT(CFrontendDlg) //}}AFX_DATA_INIT Event = CreateEvent(NULL, FALSE, FALSE, "TheOlsrdShimEvent"); LogThread = NULL; NetThread = NULL;}void CFrontendDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CFrontendDlg) DDX_Control(pDX, IDC_BUTTON2, m_StopButton); DDX_Control(pDX, IDC_BUTTON1, m_StartButton); DDX_Control(pDX, IDC_TAB1, m_TabCtrl); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CFrontendDlg, CDialog) //{{AFX_MSG_MAP(CFrontendDlg) ON_BN_CLICKED(IDC_BUTTON1, OnStartButton) ON_BN_CLICKED(IDC_BUTTON2, OnStopButton) ON_BN_CLICKED(IDC_BUTTON3, OnExitButton) ON_BN_CLICKED(IDOK, OnOK) ON_BN_CLICKED(IDCANCEL, OnCancel) //}}AFX_MSG_MAPEND_MESSAGE_MAP()#if 0static void HexDump(unsigned char *Mem, int Len){ char Buff[10000]; int i, k; char *Walker = Buff; for (i = 0; i < Len; i += k) { Walker += sprintf(Walker, "%08x:", i); for (k = 0; i + k < Len && k < 16; k++) Walker += sprintf(Walker, " %02x", Mem[i + k]); while (k < 16) { Walker += sprintf(Walker, " "); k++; } Walker += sprintf(Walker, " "); for (k = 0; i + k < Len && k < 16; k++) if (Mem[i + k] < 32 || Mem[i + k] > 126) Walker += sprintf(Walker, "."); else Walker += sprintf(Walker, "%c", Mem[i + k]); Walker += sprintf(Walker, "\r\n"); } ::MessageBox(NULL, Buff, "HEXDUMP", MB_OK);}#endif// XXX - pretty inefficientvoid CFrontendDlg::Timeout(void){ POSITION Pos, Pos2; class NodeEntry Entry; class MprEntry MprEntry; class MidEntry MidEntry; class HnaEntry HnaEntry;Restart0: Pos = NodeList.GetHeadPosition(); while (Pos != NULL) { Entry = NodeList.GetAt(Pos); if (Entry.Timeout < Now) { NodeList.RemoveAt(Pos); goto Restart0; }Restart1: Pos2 = Entry.MprList.GetHeadPosition(); while (Pos2 != NULL) { MprEntry = Entry.MprList.GetAt(Pos2); if (MprEntry.Timeout < Now) { Entry.MprList.RemoveAt(Pos2); goto Restart1; } Entry.MprList.GetNext(Pos2); }Restart2: Pos2 = Entry.MidList.GetHeadPosition(); while (Pos2 != NULL) { MidEntry = Entry.MidList.GetAt(Pos2); if (MidEntry.Timeout < Now) { Entry.MidList.RemoveAt(Pos2); goto Restart2; } Entry.MidList.GetNext(Pos2); }Restart3: Pos2 = Entry.HnaList.GetHeadPosition(); while (Pos2 != NULL) { HnaEntry = Entry.HnaList.GetAt(Pos2); if (HnaEntry.Timeout < Now) { Entry.HnaList.RemoveAt(Pos2); goto Restart3; } Entry.HnaList.GetNext(Pos2); } NodeList.GetNext(Pos); } if( NodeList.IsEmpty() ) TrayIcon::getInstance()->setStatus( TrayIcon::ON, "No nodes found" ); else TrayIcon::getInstance()->setStatus( TrayIcon::CONNECTED, "Nodes available" ); m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);}unsigned int VTimeToInt(unsigned int VTime){ return ((0x10 | ((VTime & 0xf0) >> 4)) << (VTime & 0x0f)) >> 8;}void CFrontendDlg::AddMpr(unsigned int MprAddr, unsigned int NodeAddr, unsigned int VTime){ class NodeEntry NewEntry; POSITION Pos; unsigned __int64 Timeout; Timeout = Now + (unsigned __int64)VTimeToInt(VTime) * (unsigned __int64)10000000; AddNode(NodeAddr, VTime); AddNode(MprAddr, VTime); NewEntry.Addr = NodeAddr; Pos = NodeList.Find(NewEntry); if (Pos == NULL) return; class NodeEntry &OldEntry = NodeList.GetAt(Pos); OldEntry.AddMpr(MprAddr, Timeout); m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);}void CFrontendDlg::AddMid(unsigned int IntAddr, unsigned int NodeAddr, unsigned int VTime){ class NodeEntry NewEntry; POSITION Pos; unsigned __int64 Timeout; Timeout = Now + (unsigned __int64)VTimeToInt(VTime) * (unsigned __int64)10000000; AddNode(NodeAddr, VTime); NewEntry.Addr = NodeAddr; Pos = NodeList.Find(NewEntry); if (Pos == NULL) return; class NodeEntry &OldEntry = NodeList.GetAt(Pos); OldEntry.AddMid(IntAddr, Timeout); m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);}void CFrontendDlg::AddHna(unsigned int NetAddr, unsigned int NetMask, unsigned int NodeAddr, unsigned int VTime){ class NodeEntry NewEntry; POSITION Pos; unsigned __int64 Timeout; Timeout = Now + (unsigned __int64)VTimeToInt(VTime) * (unsigned __int64)10000000; AddNode(NodeAddr, VTime); NewEntry.Addr = NodeAddr; Pos = NodeList.Find(NewEntry); if (Pos == NULL) return; class NodeEntry &OldEntry = NodeList.GetAt(Pos); OldEntry.AddHna(NetAddr, NetMask, Timeout); m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);}void CFrontendDlg::AddNode(unsigned int NodeAddr, unsigned int VTime){ class NodeEntry NewEntry; POSITION Pos; unsigned __int64 Timeout; if (NodeAddr == LocalHost) return; Timeout = Now + (unsigned __int64)VTimeToInt(VTime) * (unsigned __int64)10000000; NewEntry.Addr = NodeAddr; Pos = NodeList.Find(NewEntry); if (Pos != NULL) { class NodeEntry &OldEntry = NodeList.GetAt(Pos); OldEntry.Timeout = Timeout; } else { NewEntry.Timeout = Timeout; NodeList.AddTail(NewEntry); } m_TabCtrl.m_Dialog3.UpdateNodeInfo(NodeList);}void CFrontendDlg::HandleOlsrTc(struct OlsrTc *Msg, int UseLq){ int Size; unsigned int *Addr; Msg->Header.SeqNo = ::ntohs(Msg->Header.SeqNo); Msg->Ansn = ::ntohs(Msg->Ansn); AddNode(Msg->Header.Orig, Msg->Header.VTime); Size = Msg->Header.Size; Size -= sizeof (struct OlsrTc); Addr = (unsigned int *)(Msg + 1); while (Size > 0) { Size -= 4; AddMpr(*Addr, Msg->Header.Orig, Msg->Header.VTime); Addr++; if (UseLq != 0) { Size -= 4; Addr++; } }}void CFrontendDlg::HandleOlsrMid(struct OlsrHeader *Msg){ int Size; unsigned int *Addr; Msg->SeqNo = ::ntohs(Msg->SeqNo); AddNode(Msg->Orig, Msg->VTime); Size = Msg->Size; Size -= sizeof (struct OlsrHeader); Addr = (unsigned int *)(Msg + 1); while (Size > 0) { Size -= 4; AddMid(*Addr, Msg->Orig, Msg->VTime); Addr++; }}void CFrontendDlg::HandleOlsrHna(struct OlsrHeader *Msg){ int Size; unsigned int *Addr; Msg->SeqNo = ::ntohs(Msg->SeqNo); AddNode(Msg->Orig, Msg->VTime); Size = Msg->Size; Size -= sizeof (struct OlsrHeader); Addr = (unsigned int *)(Msg + 1); while (Size > 0) { Size -= 8; AddHna(Addr[0], Addr[1], Msg->Orig, Msg->VTime); Addr += 2; }}void CFrontendDlg::HandleOlsrHello(struct OlsrHello *Msg, int UseLq){ int Size, LinkSize; struct OlsrHelloLink *Link; unsigned int *Addr; Msg->Header.SeqNo = ::ntohs(Msg->Header.SeqNo); AddNode(Msg->Header.Orig, Msg->Header.VTime); Size = Msg->Header.Size; Size -= sizeof (struct OlsrHello); Link = (struct OlsrHelloLink *)(Msg + 1); while (Size > 0) { Link->Size = ::ntohs(Link->Size); LinkSize = Link->Size; Size -= LinkSize; LinkSize -= sizeof (struct OlsrHelloLink); Addr = (unsigned int *)(Link + 1); while (LinkSize > 0) { LinkSize -= 4; AddNode(*Addr, Msg->Header.VTime); if ((Link->LinkCode & 0x0c) == 0x08) AddMpr(*Addr, Msg->Header.Orig, Msg->Header.VTime); Addr++; if (UseLq != 0) { LinkSize -= 4; Addr++; } } Link = (struct OlsrHelloLink *)Addr; }}void CFrontendDlg::HandleIpcRoute(struct IpcRoute *Msg){ if (Msg->Header.Size != sizeof (struct IpcRoute)) return; if (Msg->Add == 0) m_TabCtrl.m_Dialog4.DeleteRoute(Msg->Dest.v4); else m_TabCtrl.m_Dialog4.AddRoute(Msg->Dest.v4, Msg->Gate.v4, Msg->Metric, Msg->Int);}void CFrontendDlg::HandleIpcConfig(struct IpcConfig *Msg){ if (Msg->Header.Size != sizeof (struct IpcConfig)) return; Msg->HelloInt = ::ntohs(Msg->HelloInt); Msg->WiredHelloInt = ::ntohs(Msg->WiredHelloInt); Msg->TcInt = ::ntohs(Msg->TcInt); Msg->HelloHold = ::ntohs(Msg->HelloHold); Msg->TcHold = ::ntohs(Msg->TcHold); LocalHost = Msg->MainAddr.v4;}static int FullRead(SOCKET SockHand, char *Buff, int Len){ int Res; do { Res = ::recv(SockHand, Buff, Len, 0); if (Res <= 0) return -1; Len -= Res; Buff += Res; } while (Len > 0); return 0;}// XXX - we should have access to olsrd's internal data structures insteadunsigned int CFrontendDlg::NetThreadFunc(void){ struct IpcHeader Header; int Res;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -