線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3594
推到 Plurk!
推到 Facebook!

[BCB] 請教如何得到子網路遮罩?

答題得分者是:axsoft
ching040
一般會員


發表:6
回覆:1
積分:1
註冊:2003-08-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-28 16:18:04 IP:61.30.xxx.xxx 未訂閱
我已可以得到自己的MAC, IP, Hostname, 但在捉取區網裡的MAC 及IP時, 會將外來的訊號(封包)一併處理 所以可能會捉到非區網裡的電腦資料. 請問要如何得到子網路遮罩? 來封掉非區網裡的電腦資料及封包?
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-28 17:22:10 IP:61.218.xxx.xxx 未訂閱
ching040您好:    試試這個範例 http://www.bytesandmore.de/rad/cpp/snipp/sc08009.php    Source Code 範例下載: 原始網站:http://www.bytesandmore.de/rad/cpp/files/NicViewSrc.zip 本站下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=11423887&CC=255493    Demo執行檔下載: 原始網站:http://www.bytesandmore.de/rad/cpp/files/NicViewExe.zip 本站下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=11424200&CC=255500    
/*開心的事情別隱藏在心裡,分享給別人知道會更快樂的*/
/*得到新知識別隱藏在心裡,分享給別人了解會更清楚的*/
發表人 - axsoft 於 2003/08/28 17:24:23
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-08-28 17:29:48 IP:61.218.xxx.xxx 未訂閱

In the search for the self or how to find out your IP address and other stuff in Win95 and WinNT

Stas Khirman (staskh@rocketmail.com) Raz Galili (razgalili@hotmail.com) Definition of the problem 資料來源:http://www.caip.rutgers.edu/~arni/ws2/stas.htm What is your current IP address? You can not program for TCP/IP without finding yourself needing this piece of information sometime. There is an API function for that now, isn't it? Just call gethostbyname() for "localhost" and you would get your IP address. Well, yes you can, and no, it is not enough. First of all, this function gives you your IP address only, it does not give you any other information you would like to know, like your network mask. Also, this function would only give you ONE IP address, while you might have more then one: there are a lot of computers out there with both a modem and a network adapters. And to top all that, if anything is wrong with your TCP/IP settings, you would get the wrong IP address. Windows 95 and Windows NT contains a bunch of network statistic utilities: IPCONFIG on NT and WINIPCFG on Win95 both detect your IP addresses, network mask, even you adapter MAC address. NETSTAT utility show list of active TCP and UDP connection, and detailed transmit statistic. ROUTE utility give you read and write access to your routing table and ARP give you same access to you address resolution table. This shows that Windows have some way to access the internal TCP/IP information and we do not. We have a right to know! The need to find the IP addresses of a machine was the original motive that drove us into looking closer at some of those Windows 95 and NT programs, and trying to figure out how they do it. The solution There is one thing common to IPCONFIG, NETSTAT, ROUTE and ARP utilities. All of them use a DLL called INETMIB1.DLL . This DLL is present both on any Windows 95 and NT. Microsoft documentation describes this DLL as extension for SNMP extendable agent. This DLL - if accessed right - would give all the information we are looking for, and much, much more. All we have to do is to simulate the Windows extendible agent, and request the DLL for the right OIDs. Now we would attempt to explain what is SMNP, what is the extensible agent, what is an OID and how to parsuade inetmib1.dll to speak with us. What is SNMP SNMP stands for Simple Network Management Protocol. SNMP was developed to answer the complex problem of managing networks. Many devices are connected to network today: printers, routers, repeaters, and bridges, multi-functional servers, mainframes and desktop computers, actually everything but the coffee machine and the toaster are connected together, (and both the coffee machine and the toaster are going to be plugged into the net really soon). Each of these devices has different problems to solve. All those devices present unique problems, different status information they can present for retrieval and different settings to set. SNMP is presenting a way to handle all those devices through the net itself, a reliable way to alter and retrieve data remotely on a variety of different platforms. It is powerful and flexible enough in order to supports every different kind of data structure and request type for every network device that exists today and may exist in the future. It is the most widely adopted network management protocol. In the SNMP model there is a software agent residing on the network device. This agent collects the related information about that device. The information can be retrieved from the agent by a manger component through the net. The manger can reside anywhere on the net - usually it is on a different machine then the agent. The manager can send queries and requests to the agent that would handle them for the manger. SMNP data structures The data handled by the SNMP agent is organized into units that are called "management information bases" or MIBs for short. MIBs are described via a precise definition language called "Abstract Syntax Notation". A manager can talk to an agent, understand the agent information and handle it, if the manager has the MIB of the agent. For example, a hardware vendor might create a new device, let’s say the device is called "elephant", a pink network elephant. With the new product the manufacturer would also create a diskette with a MIB file, describing the data structure needed to handle the new device. This MIB file can be fed into the any SNMP system management tools. The elephant would have an agent running that would be able to collect all the information the network manager need to know. This information would be organized in the data structures described by the MIB, and the SMNP management tools the system manger already using, would be able to read this information from the elephant. The information described by the MIB might describe unlimited number of objects. Each object has a unique identifier, called OID. An OID is a sequence of numbers that identify an object. Every object that can be handled through SNMP has a unique OID. All the OIDs in the world are organized in one big tree-like structure. The sequence of the numbers that is the OID are the identifiers of the branches of the tree. Every subtree in the tree is assigned by the IETF to ensure that all branches are unique. Each branch has a name and a number assigned to it. All SNMP objects are somewhere under the subtree iso.org.dod.internet which is 1.3.6.1. All of TCP/IP basic objects are contained inside the sub tree called MIB-II base. The MibII definition can be obtained from RFC1213 . Reading the MibII file we can see that to obtain system description, we need to read the value from iso.org.dod.internet.mgmt.mib-2.system.sysDescr (1.3.6.1.2.1.1.1.0) The last number identifier, 0, show that to read sysDescr we would need to read a scalar value. Scalar values are easy to read. Reading elements of a table is a little bit trickier then reading simple values. For exemple, to read the current IP address of our machine, we need to read the value from: iso. org. dod. internet. mgmt. mib-2. ip. ipAddrTable. ipAddrEntry. ipAddress. IPADDRESSREALVALUE or (to make an infinite story long) 1, 3, 6, 1, 2, 1, 4 , 20, 1 ,1,?,?,?,?. Each of the question marks stands for one number of the ip. If your IP address is, say 123.45.67.89 then you would find on your machine the value 1, 3, 6, 1, 2, 1, 4 , 20, 1 ,1, 123, 45, 67, 89. Of course to read this value you would need to know it exists. IpAddress is element of table indexed by this address itself. To get access to some table object we have to concatenate it base OID with its index. In our example we meet a deadloop – we need to know ip address to retrieve it! SNMP solves this problem by supporting a command set that allows the user to search its data tree. If you have a given OID you can ask for the value of the object with next OID. In our case, if we would ask for the value of the element next to 1.3.6.1.2.1.4.20.1.1 we would receive a full OID and value of our first IP interface. With the full OID of our first interface ip we can use the "get next" request to get the IP address of the second interface and so on. To get the IP Masks we need use 1.3.6.1.2.1.4.20.1.3 as our starting OID. SMNP and Windows Windows 95 and Windows NT both give the option to install SNMP agent. (Windows NT also let you install SNMP manager). Windows SNMP service is built so it is easily extendable. The SNMP agent could be easily extended by extension DLLs. Those DLLs talk with the agent through an API of three functions. Each and every one of those DLLs exports the following 3 functions: * SnmpExtensionInit - The extension agent initialization function. * SnmpExtensionQuery - The main query function through which the agent Extension DLL. * SnmpExtensionTrap - handling of traps created by the Extension DLL. The Dll might also support the *SnmpExtensionInitEx() - This is an extended version of SnmpExtensionInit that enable the extendible agent to query the extension DLL some more. Windows SNMP services are thread a DLL called "inetmib1.dll" as an extensible agent. This DLL is present on any Windows 95 and NT. It is there even if the SNMP agent was not installed. This extension agent DLL is the one responsible to connect between the SNMP and the TCP/IP subsystems. All is left to do now, is to load the DLL, simulate an init by the agent, and then query it for the IP number and whatever else we could ask for. SNMP Command Set SNMP has three Basic Commands - Get, Set, And GetNext. In every call to SnmpExtensionQuery one command is called but it can be done on a multiple number of data elements. A structure of type RFC1157VarBindList is passed to the function. This structure is a list of VarBind elements that is defined like this:
typedef struct{ 
                RFC1157VarBind *list; 
                UINT len; 
               } RFC1157VarBindList;     typedef struct vb {
                    AsnObjectName name; 
                    AsnObjectSyntax value; 
                  } RFC1157VarBind; 
A VarBind structure contain both the name (OID) of the element, and it's value. Get and Set are used to access actual data objects and we do not need them to get the information we are interested in. GetNext is a little different then Get and Set. It is used to travel a long the tree of the OIDs that the agent support. When a call is made to SnmpExtensionQuery with GetNext the function returns the first value that the agent supports that is s lexicographically greater than the supplied OID. How to talk to inetmib1.dll To start using an SNMP extension DLL the system (and us) first has to call SnmpExtensionInit(). This function needs 3 parameters - time zero reference, a handle to a trap event and an object identifier to received the supported view. A full understanding of this function and it's big brother SnmpExtensionInitEx() is needed to fully investigate the snmp extension DLL. Since we are not going to fully investigate it here -- we would just be happy with the fact that MibII has a short version of it that pass it default values. We then call Query with GetNext request, repeating the call until there are no more ip address to return. When we start, the VarBind we pass to the function contains iso.org.dod.internet.mgmt.mib-2.ip.ipAddrTable.ipAddrEntry.ipAddress (i.e. 1,3,6,1,2,1,4,20,1,1) in each and every call, the value which we get back is different, and this value we pass to the function. If we had 3 IP addresses 205.5.3.1 205.5.3.3 and 205.5.3.6 the first time we call the function we would get in return 1,3,6,1,2,1,4,20,1,1,205.5.3.1. the second time we would receive 1,3,6,1,2,1,4,20,1,1,205.5.3.3 and the third time 1,3,6,1,2,1,4,20,1,1,205.5.3.6. the forth time would return something that begins with 1,3,6,1,2,1,4,20,1,2 or with 1,3,6,1,2,1,4,20,1,3. by this change we now that the function has failed to get any more ip numbers. The Attached code (Click here for code) In the attached code MibAccess.cpp and MibAccess.h we loaded inetmib1.dll, and got the address to it's four interface functions. The functions can be called through the methods of MibExtLoad class. During the construction of the class, the class loads the mib dll into memory and get the addresses of it's functions. The MibII class is a more specialized class then MibExtLoad. it explicitly loads inetmib1.dll, and give access to the data we are looking for - the ip address and mask. To compile and link the attached code you would need snmp.h and snmp.lib which you can find in Win95 or WinNT SDK. If you want to generate Win95 compatible code DO NOT use the files that come with MSVC , which relay on the existence of snmpapi.dll on your machine to do the simple tasks of handling the varbind lists. While using GetIpAddress please notice that it would give you the loopback ip address (127.0.0.1). This IP address always exists. Summary The original reason to write this article was to help us find something we did not know about ourselves – while Microsoft does. Our IP address. After obtaining the MIB2 file we noticed that we could get out of it not only the IP address but also the Network MAC address, TCP and UDP connections currently active and a bunch of statistics. We did not show here how to do it, but we showed enough information so you can do it yourself. After all – you do have the right to know!!!
/*開心的事情別隱藏在心裡,分享給別人知道會更快樂的*/
/*得到新知識別隱藏在心裡,分享給別人了解會更清楚的*/
發表人 - axsoft 於 2003/08/28 17:59:05
ching040
一般會員


發表:6
回覆:1
積分:1
註冊:2003-08-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-08-28 17:40:02 IP:61.30.xxx.xxx 未訂閱
axsoft您好: 感謝您的資料, 剛看了原始碼, 我想要好好用功了~~ 因為有些看不太懂@@, 研究去了, 再次感謝您
JerryKuo
版主


發表:42
回覆:571
積分:322
註冊:2003-03-10

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-09-15 17:26:41 IP:61.230.xxx.xxx 未訂閱
小弟下載了這個程式,並自行compiler及執行這個程式 可是卻得不到資料,沒有ip也沒有subnet mask。最後 自己根據他的程式重新寫一次,compiler都沒問題,也可 以執行,但還是得不到資料。我是卡在紅色這個function 好像沒有作用,得不到任何資訊

ret = Query(ASN_RFC1157_GETNEXTREQUEST, &varBindList, &errorStatus, &errorIndex);
而Query函式如下
BOOL MibExtLoad::Query(BYTE requestType, 
                       OUT RFC1157VarBindList *variableBindings,
                       AsnInteger *errorStatus, 
                       AsnInteger *errorIndex)
{
   if (m_hInst && m_Query)
      return m_Query(requestType, 
                     variableBindings, 
                     errorStatus, 
                     errorIndex);
        
   return FALSE;
}
m_Query又是指向下面函式
m_Query        = (pSnmpExtensionQuery) GetProcAddress(m_hInst ,"SnmpExtensionQuery");
不過下載網站的執行檔,就可以得到資料。我想請問一下 有人也試過這個程式的原始碼嗎?是否遇到同樣的問題 ,請教一下如何讓他真正的執行。 我的環境是win2000,BCB6.0(原始程式是5.0的)
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-09-15 23:13:19 IP:140.135.xxx.xxx 未訂閱
JerryKuo您好:    我用BCB5.0,compiler及執行這個程式,可以正常WORK耶< > 順心< > ~我也是在學習的階段,回答的不好請您多多見諒與指教~
JerryKuo
版主


發表:42
回覆:571
積分:322
註冊:2003-03-10

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-09-16 10:59:27 IP:61.230.xxx.xxx 未訂閱
引言: JerryKuo您好: 我用BCB5.0,compiler及執行這個程式,可以正常WORK耶< > 順心< > ~我也是在學習的階段,回答的不好請您多多見諒與指教~
有沒有辦法解決bcb6.0的問題?

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-09-16 16:17:30 IP:211.23.xxx.xxx 未訂閱
windows 2000 with BCB6 i could complie this example maybe you can try it again File down From website as follow http://www.bytesandmore.de/rad/cpp/files/NicViewSrc.zip but i can`t get any information even the program can`t read my adapters ...why? 發表人 - 流 於 2003/09/16 16:21:35
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-09-16 17:02:30 IP:140.135.xxx.xxx 未訂閱
JerryKuo,流您好:  我用BCB6.0和OS WinXP也是一樣可以compiler及執行這個程式耶  步驟如下:   1.用BCB6.0開啟專案檔,但FormMain不會出現   2.Open->FormMain.cpp   3.按下 F9     順心 <>~我也是在學習的階段,回答的不好請您多多見諒與指教~
JerryKuo
版主


發表:42
回覆:571
積分:322
註冊:2003-03-10

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-09-17 12:38:51 IP:61.230.xxx.xxx 未訂閱
引言: 我用BCB6.0和OS WinXP也是一樣可以compiler及執行這個程式耶 步驟如下: 1.用BCB6.0開啟專案檔,但FormMain不會出現 2.Open->FormMain.cpp 3.按下 F9
結果一樣沒有任何東西,空白資料。請再賜教^^
系統時間:2024-04-23 23:11:31
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!