2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS arp utility
23 * FILE: base/applications/network/arp/arp.c
24 * PURPOSE: view and manipulate the ARP cache
25 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
31 #define WIN32_NO_STATUS
45 const char SEPARATOR
= '-';
46 int _CRT_glob
= 0; // stop * from listing dir files in arp -d *
49 * function declarations
51 DWORD
DoFormatMessage(VOID
);
52 INT
PrintEntries(PMIB_IPNETROW pIpAddRow
);
53 INT
DisplayArpEntries(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
);
54 INT
Addhost(PTCHAR pszInetAddr
, PTCHAR pszEthAddr
, PTCHAR pszIfAddr
);
55 INT
Deletehost(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
);
59 * convert error code into meaningful message
61 DWORD
DoFormatMessage(VOID
)
66 DWORD ErrorCode
= GetLastError();
68 if (ErrorCode
!= ERROR_SUCCESS
)
70 RetVal
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
71 FORMAT_MESSAGE_FROM_SYSTEM
|
72 FORMAT_MESSAGE_IGNORE_INSERTS
,
75 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
82 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
85 /* return number of TCHAR's stored in output buffer
86 * excluding '\0' - as FormatMessage does*/
95 * Takes an ARP entry and prints the IP address,
96 * the MAC address and the entry type to screen
99 INT
PrintEntries(PMIB_IPNETROW pIpAddRow
)
104 /* print IP addresses */
105 inaddr
.S_un
.S_addr
= pIpAddRow
->dwAddr
;
106 _tprintf(_T(" %-22s"), inet_ntoa(inaddr
));
108 /* print MAC address */
109 _stprintf(cMacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
110 pIpAddRow
->bPhysAddr
[0],
111 pIpAddRow
->bPhysAddr
[1],
112 pIpAddRow
->bPhysAddr
[2],
113 pIpAddRow
->bPhysAddr
[3],
114 pIpAddRow
->bPhysAddr
[4],
115 pIpAddRow
->bPhysAddr
[5]);
116 _tprintf(_T("%-22s"), cMacAddr
);
118 /* print cache type */
119 switch (pIpAddRow
->dwType
)
121 case MIB_IPNET_TYPE_DYNAMIC
: _tprintf(_T("dynamic\n"));
123 case MIB_IPNET_TYPE_STATIC
: _tprintf(_T("static\n"));
125 case MIB_IPNET_TYPE_INVALID
: _tprintf(_T("invalid\n"));
127 case MIB_IPNET_TYPE_OTHER
: _tprintf(_T("other\n"));
135 * Takes optional parameters of an internet address and interface address.
136 * Retrieve all entries in the ARP cache. If an internet address is
137 * specified, display the ARP entry relating to that address. If an
138 * interface address is specified, display all entries relating to
142 /* FIXME: allow user to specify an interface address, via pszIfAddr */
143 INT
DisplayArpEntries(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
147 PMIB_IPNETTABLE pIpNetTable
= NULL
;
148 PMIB_IPADDRTABLE pIpAddrTable
= NULL
;
150 struct in_addr inaddr
, inaddr2
;
152 TCHAR szIntIpAddr
[20];
154 /* retrieve the IP-to-physical address mapping table */
157 GetIpNetTable(pIpNetTable
, &Size
, 0);
159 /* allocate memory for ARP address table */
160 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
161 if (pIpNetTable
== NULL
)
164 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
166 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
168 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
173 /* check there are entries in the table */
174 if (pIpNetTable
->dwNumEntries
== 0)
176 _tprintf(_T("No ARP entires found\n"));
182 /* Retrieve the interface-to-ip address mapping
183 * table to get the IP address for adapter */
187 GetIpAddrTable(pIpAddrTable
, &Size
, 0);
189 pIpAddrTable
= (MIB_IPADDRTABLE
*) HeapAlloc(GetProcessHeap(), 0, Size
);
190 if (pIpAddrTable
== NULL
)
193 ZeroMemory(pIpAddrTable
, sizeof(*pIpAddrTable
));
195 if ((iRet
= GetIpAddrTable(pIpAddrTable
, &Size
, TRUE
)) != NO_ERROR
)
197 _tprintf(_T("GetIpAddrTable failed: %d\n"), iRet
);
203 for (k
=0; k
< pIpAddrTable
->dwNumEntries
; k
++)
205 if (pIpNetTable
->table
[0].dwIndex
== pIpAddrTable
->table
[k
].dwIndex
)
207 //printf("debug print: pIpAddrTable->table[?].dwIndex = %lx\n", pIpNetTable->table[k].dwIndex);
208 inaddr2
.s_addr
= pIpAddrTable
->table
[k
].dwAddr
;
209 pszIpAddr
= inet_ntoa(inaddr2
);
210 strcpy(szIntIpAddr
, pszIpAddr
);
215 /* print header, including interface IP address and index number */
216 _tprintf(_T("\nInterface: %s --- 0x%lx \n"), szIntIpAddr
, pIpNetTable
->table
[0].dwIndex
);
217 _tprintf(_T(" Internet Address Physical Address Type\n"));
219 /* go through all ARP entries */
220 for (i
=0; i
< pIpNetTable
->dwNumEntries
; i
++)
223 /* if the user has supplied their own internet address *
224 * only print the arp entry which matches that */
227 inaddr
.S_un
.S_addr
= pIpNetTable
->table
[i
].dwAddr
;
228 pszIpAddr
= inet_ntoa(inaddr
);
230 /* check if it matches, print it */
231 if (strcmp(pszIpAddr
, pszInetAddr
) == 0)
232 PrintEntries(&pIpNetTable
->table
[i
]);
235 /* if an address is not supplied, print all entries */
236 PrintEntries(&pIpNetTable
->table
[i
]);
242 if (pIpNetTable
!= NULL
)
243 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
244 if (pIpAddrTable
!= NULL
)
245 HeapFree(GetProcessHeap(), 0, pIpAddrTable
);
251 * Takes an internet address, a MAC address and an optional interface
252 * address as arguments and checks their validity.
253 * Fill out an MIB_IPNETROW structure and insert the data into the
254 * ARP cache as a static entry.
257 INT
Addhost(PTCHAR pszInetAddr
, PTCHAR pszEthAddr
, PTCHAR pszIfAddr
)
259 PMIB_IPNETROW pAddHost
= NULL
;
260 PMIB_IPNETTABLE pIpNetTable
= NULL
;
267 /* check IP address */
268 if (pszInetAddr
!= NULL
)
270 if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
272 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
282 /* check MAC address */
283 if (strlen(pszEthAddr
) != 17)
285 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
290 if (pszEthAddr
[i
] == SEPARATOR
)
293 if (!isxdigit(pszEthAddr
[i
]))
295 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
300 /* We need the IpNetTable to get the adapter index */
301 /* Return required buffer size */
302 GetIpNetTable(pIpNetTable
, &Size
, 0);
304 /* allocate memory for ARP address table */
305 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
306 if (pIpNetTable
== NULL
)
309 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
311 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
313 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
319 /* reserve memory on heap and zero */
320 pAddHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
321 if (pAddHost
== NULL
)
324 ZeroMemory(pAddHost
, sizeof(MIB_IPNETROW
));
326 /* set dwIndex field to the index of a local IP address to
327 * indicate the network on which the ARP entry applies */
330 if (sscanf(pszIfAddr
, "%lx", &pAddHost
->dwIndex
) == EOF
)
337 //printf("debug print: pIpNetTable->table[0].dwIndex = %lx\n", pIpNetTable->table[0].dwIndex);
338 /* needs testing. I get the correct index on my machine, but need others
339 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
340 pAddHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
343 /* Set MAC address to 6 bytes (typical) */
344 pAddHost
->dwPhysAddrLen
= 6;
347 /* Encode bPhysAddr into correct byte array */
351 c
= toupper(pszEthAddr
[i
*3]);
352 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
355 c
= toupper(pszEthAddr
[i
*3 + 1]);
356 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
358 pAddHost
->bPhysAddr
[i
] = (BYTE
)val
;
362 /* copy converted IP address */
363 pAddHost
->dwAddr
= dwIpAddr
;
366 /* set type to static */
367 pAddHost
->dwType
= MIB_IPNET_TYPE_STATIC
;
370 /* Add the ARP entry */
371 if (SetIpNetEntry(pAddHost
) != NO_ERROR
)
377 HeapFree(GetProcessHeap(), 0, pAddHost
);
382 if (pIpNetTable
!= NULL
)
383 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
384 if (pAddHost
!= NULL
)
385 HeapFree(GetProcessHeap(), 0, pAddHost
);
391 * Takes an internet address and an optional interface address as
392 * arguments and checks their validity.
393 * Add the interface number and IP to an MIB_IPNETROW structure
394 * and remove the entry from the ARP cache.
397 INT
Deletehost(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
399 PMIB_IPNETROW pDelHost
= NULL
;
400 PMIB_IPNETTABLE pIpNetTable
= NULL
;
403 BOOL bFlushTable
= FALSE
;
407 /* check IP address */
408 if (pszInetAddr
!= NULL
)
410 /* if wildcard is given, set flag to delete all hosts */
411 if (strncmp(pszInetAddr
, "*", 1) == 0)
413 else if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
415 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
425 /* We need the IpNetTable to get the adapter index */
426 /* Return required buffer size */
427 GetIpNetTable(NULL
, &Size
, 0);
429 /* allocate memory for ARP address table */
430 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
431 if (pIpNetTable
== NULL
)
434 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
436 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
438 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
443 /* reserve memory on heap and zero */
444 pDelHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
445 if (pDelHost
== NULL
)
448 ZeroMemory(pDelHost
, sizeof(MIB_IPNETROW
));
451 /* set dwIndex field to the index of a local IP address to
452 * indicate the network on which the ARP entry applies */
455 if (sscanf(pszIfAddr
, "%lx", &pDelHost
->dwIndex
) == EOF
)
462 /* needs testing. I get the correct index on my machine, but need others
463 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
464 pDelHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
467 if (bFlushTable
!= FALSE
)
469 /* delete arp cache */
470 if (FlushIpNetTable(pDelHost
->dwIndex
) != NO_ERROR
)
477 HeapFree(GetProcessHeap(), 0, pDelHost
);
482 /* copy converted IP address */
483 pDelHost
->dwAddr
= dwIpAddr
;
485 /* Add the ARP entry */
486 if (DeleteIpNetEntry(pDelHost
) != NO_ERROR
)
492 HeapFree(GetProcessHeap(), 0, pDelHost
);
497 if (pIpNetTable
!= NULL
)
498 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
499 if (pDelHost
!= NULL
)
500 HeapFree(GetProcessHeap(), 0, pDelHost
);
506 * print program usage to screen
511 _tprintf(_T("\nDisplays and modifies the IP-to-Physical address translation tables used by\n"
512 "address resolution protocol (ARP).\n"
514 "ARP -s inet_addr eth_addr [if_addr]\n"
515 "ARP -d inet_addr [if_addr]\n"
516 "ARP -a [inet_addr] [-N if_addr]\n"
518 " -a Displays current ARP entries by interrogating the current\n"
519 " protocol data. If inet_addr is specified, the IP and Physical\n"
520 " addresses for only the specified computer are displayed. If\n"
521 " more than one network interface uses ARP, entries for each ARP\n"
522 " table are displayed.\n"
524 " inet_addr Specifies an internet address.\n"
525 " -N if_addr Displays the ARP entries for the network interface specified\n"
527 " -d Deletes the host specified by inet_addr. inet_addr may be\n"
528 " wildcarded with * to delete all hosts.\n"
529 " -s Adds the host and associates the Internet address inet_addr\n"
530 " with the Physical address eth_addr. The Physical address is\n"
531 " given as 6 hexadecimal bytes separated by hyphens. The entry\n"
533 " eth_addr Specifies a physical address.\n"
534 " if_addr If present, this specifies the Internet address of the\n"
535 " interface whose address translation table should be modified.\n"
536 " If not present, the first applicable interface will be used.\n"
538 " > arp -s 157.55.85.212 00-aa-00-62-c6-09 .... Adds a static entry.\n"
539 " > arp -a .... Displays the arp table.\n\n"));
545 * Parse command line and call the required function
548 INT
main(int argc
, char* argv
[])
550 if ((argc
< 2) || (argc
> 5))
556 if (argv
[1][0] == '-')
560 case 'a': /* fall through */
563 DisplayArpEntries(NULL
, NULL
);
565 DisplayArpEntries(argv
[2], NULL
);
566 else if ((argc
== 4) && ((strcmp(argv
[2], "-N")) == 0))
567 DisplayArpEntries(NULL
, argv
[3]);
568 else if ((argc
== 5) && ((strcmp(argv
[3], "-N")) == 0))
569 DisplayArpEntries(argv
[2], argv
[4]);
576 case 'd': if (argc
== 3)
577 Deletehost(argv
[2], NULL
);
579 Deletehost(argv
[2], argv
[3]);
586 case 's': if (argc
== 4)
587 Addhost(argv
[2], argv
[3], NULL
);
589 Addhost(argv
[2], argv
[3], argv
[4]);