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: apps/utils/net/arp/arp.c
24 * PURPOSE: view and manipulate the ARP cache
25 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
31 #define WIN32_NO_STATUS
48 const char SEPERATOR
= '-';
49 int _CRT_glob
= 0; // stop * from listing dir files in arp -d *
52 * function declerations
54 DWORD
DoFormatMessage(VOID
);
55 INT
PrintEntries(PMIB_IPNETROW pIpAddRow
);
56 INT
DisplayArpEntries(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
);
57 INT
Addhost(PTCHAR pszInetAddr
, PTCHAR pszEthAddr
, PTCHAR pszIfAddr
);
58 INT
Deletehost(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
);
62 * convert error code into meaningful message
64 DWORD
DoFormatMessage(VOID
)
69 DWORD ErrorCode
= GetLastError();
71 if (ErrorCode
!= ERROR_SUCCESS
)
73 RetVal
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
74 FORMAT_MESSAGE_FROM_SYSTEM
|
75 FORMAT_MESSAGE_IGNORE_INSERTS
,
78 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
85 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
88 /* return number of TCHAR's stored in output buffer
89 * excluding '\0' - as FormatMessage does*/
98 * Takes an ARP entry and prints the IP address,
99 * the MAC address and the entry type to screen
102 INT
PrintEntries(PMIB_IPNETROW pIpAddRow
)
107 /* print IP addresses */
108 inaddr
.S_un
.S_addr
= pIpAddRow
->dwAddr
;
109 _tprintf(_T(" %-22s"), inet_ntoa(inaddr
));
111 /* print MAC address */
112 _stprintf(cMacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
113 pIpAddRow
->bPhysAddr
[0],
114 pIpAddRow
->bPhysAddr
[1],
115 pIpAddRow
->bPhysAddr
[2],
116 pIpAddRow
->bPhysAddr
[3],
117 pIpAddRow
->bPhysAddr
[4],
118 pIpAddRow
->bPhysAddr
[5]);
119 _tprintf(_T("%-22s"), cMacAddr
);
121 /* print cache type */
122 switch (pIpAddRow
->dwType
)
124 case MIB_IPNET_TYPE_DYNAMIC
: _tprintf(_T("dynamic\n"));
126 case MIB_IPNET_TYPE_STATIC
: _tprintf(_T("static\n"));
128 case MIB_IPNET_TYPE_INVALID
: _tprintf(_T("invalid\n"));
130 case MIB_IPNET_TYPE_OTHER
: _tprintf(_T("other\n"));
138 * Takes optional parameters of an internet address and interface address.
139 * Retrieve all entries in the ARP cache. If an internet address is
140 * specified, display the ARP entry relating to that address. If an
141 * interface address is specified, display all entries relating to
145 /* FIXME: allow user to specify an interface address, via pszIfAddr */
146 INT
DisplayArpEntries(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
150 PMIB_IPNETTABLE pIpNetTable
= NULL
;
151 PMIB_IPADDRTABLE pIpAddrTable
= NULL
;
153 struct in_addr inaddr
, inaddr2
;
155 TCHAR szIntIpAddr
[20];
157 /* retrieve the IP-to-physical address mapping table */
160 GetIpNetTable(pIpNetTable
, &Size
, 0);
162 /* allocate memory for ARP address table */
163 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
164 if (pIpNetTable
== NULL
)
167 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
169 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
171 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
176 /* check there are entries in the table */
177 if (pIpNetTable
->dwNumEntries
== 0)
179 _tprintf(_T("No ARP entires found\n"));
185 /* Retrieve the interface-to-ip address mapping
186 * table to get the IP address for adapter */
190 GetIpAddrTable(pIpAddrTable
, &Size
, 0);
192 pIpAddrTable
= (MIB_IPADDRTABLE
*) HeapAlloc(GetProcessHeap(), 0, Size
);
193 if (pIpAddrTable
== NULL
)
196 ZeroMemory(pIpAddrTable
, sizeof(*pIpAddrTable
));
198 if ((iRet
= GetIpAddrTable(pIpAddrTable
, &Size
, TRUE
)) != NO_ERROR
)
200 _tprintf(_T("GetIpAddrTable failed: %d\n"), iRet
);
206 for (k
=0; k
< pIpAddrTable
->dwNumEntries
; k
++)
208 if (pIpNetTable
->table
[0].dwIndex
== pIpAddrTable
->table
[k
].dwIndex
)
210 //printf("debug print: pIpAddrTable->table[?].dwIndex = %lx\n", pIpNetTable->table[k].dwIndex);
211 inaddr2
.s_addr
= pIpAddrTable
->table
[k
].dwAddr
;
212 pszIpAddr
= inet_ntoa(inaddr2
);
213 strcpy(szIntIpAddr
, pszIpAddr
);
218 /* print header, including interface IP address and index number */
219 _tprintf(_T("\nInterface: %s --- 0x%lx \n"), szIntIpAddr
, pIpNetTable
->table
[0].dwIndex
);
220 _tprintf(_T(" Internet Address Physical Address Type\n"));
222 /* go through all ARP entries */
223 for (i
=0; i
< pIpNetTable
->dwNumEntries
; i
++)
226 /* if the user has supplied their own internet addesss *
227 * only print the arp entry which matches that */
230 inaddr
.S_un
.S_addr
= pIpNetTable
->table
[i
].dwAddr
;
231 pszIpAddr
= inet_ntoa(inaddr
);
233 /* check if it matches, print it */
234 if (strcmp(pszIpAddr
, pszInetAddr
) == 0)
235 PrintEntries(&pIpNetTable
->table
[i
]);
238 /* if an address is not supplied, print all entries */
239 PrintEntries(&pIpNetTable
->table
[i
]);
245 if (pIpNetTable
!= NULL
)
246 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
247 if (pIpAddrTable
!= NULL
)
248 HeapFree(GetProcessHeap(), 0, pIpAddrTable
);
254 * Takes an internet address, a MAC address and an optional interface
255 * address as arguments and checks their validity.
256 * Fill out an MIB_IPNETROW structure and insert the data into the
257 * ARP cache as a static entry.
260 INT
Addhost(PTCHAR pszInetAddr
, PTCHAR pszEthAddr
, PTCHAR pszIfAddr
)
262 PMIB_IPNETROW pAddHost
= NULL
;
263 PMIB_IPNETTABLE pIpNetTable
= NULL
;
270 /* check IP address */
271 if (pszInetAddr
!= NULL
)
273 if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
275 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
285 /* check MAC address */
286 if (strlen(pszEthAddr
) != 17)
288 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
293 if (pszEthAddr
[i
] == SEPERATOR
)
296 if (!isxdigit(pszEthAddr
[i
]))
298 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
303 /* We need the IpNetTable to get the adapter index */
304 /* Return required buffer size */
305 GetIpNetTable(pIpNetTable
, &Size
, 0);
307 /* allocate memory for ARP address table */
308 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
309 if (pIpNetTable
== NULL
)
312 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
314 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
316 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
322 /* reserve memory on heap and zero */
323 pAddHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
324 if (pAddHost
== NULL
)
327 ZeroMemory(pAddHost
, sizeof(MIB_IPNETROW
));
329 /* set dwIndex field to the index of a local IP address to
330 * indicate the network on which the ARP entry applies */
333 if (sscanf(pszIfAddr
, "%lx", &pAddHost
->dwIndex
) == EOF
)
340 //printf("debug print: pIpNetTable->table[0].dwIndex = %lx\n", pIpNetTable->table[0].dwIndex);
341 /* needs testing. I get the correct index on my machine, but need others
342 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
343 pAddHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
346 /* Set MAC address to 6 bytes (typical) */
347 pAddHost
->dwPhysAddrLen
= 6;
350 /* Encode bPhysAddr into correct byte array */
354 c
= toupper(pszEthAddr
[i
*3]);
355 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
358 c
= toupper(pszEthAddr
[i
*3 + 1]);
359 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
361 pAddHost
->bPhysAddr
[i
] = (BYTE
)val
;
365 /* copy converted IP address */
366 pAddHost
->dwAddr
= dwIpAddr
;
369 /* set type to static */
370 pAddHost
->dwType
= MIB_IPNET_TYPE_STATIC
;
373 /* Add the ARP entry */
374 if (SetIpNetEntry(pAddHost
) != NO_ERROR
)
380 HeapFree(GetProcessHeap(), 0, pAddHost
);
385 if (pIpNetTable
!= NULL
)
386 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
387 if (pAddHost
!= NULL
)
388 HeapFree(GetProcessHeap(), 0, pAddHost
);
394 * Takes an internet address and an optional interface address as
395 * arguments and checks their validity.
396 * Add the interface number and IP to an MIB_IPNETROW structure
397 * and remove the entry from the ARP cache.
400 INT
Deletehost(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
402 PMIB_IPNETROW pDelHost
= NULL
;
403 PMIB_IPNETTABLE pIpNetTable
= NULL
;
406 BOOL bFlushTable
= FALSE
;
410 /* check IP address */
411 if (pszInetAddr
!= NULL
)
413 /* if wildcard is given, set flag to delete all hosts */
414 if (strncmp(pszInetAddr
, "*", 1) == 0)
416 else if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
418 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
428 /* We need the IpNetTable to get the adapter index */
429 /* Return required buffer size */
430 GetIpNetTable(NULL
, &Size
, 0);
432 /* allocate memory for ARP address table */
433 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
434 if (pIpNetTable
== NULL
)
437 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
439 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
441 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
446 /* reserve memory on heap and zero */
447 pDelHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
448 if (pDelHost
== NULL
)
451 ZeroMemory(pDelHost
, sizeof(MIB_IPNETROW
));
454 /* set dwIndex field to the index of a local IP address to
455 * indicate the network on which the ARP entry applies */
458 if (sscanf(pszIfAddr
, "%lx", &pDelHost
->dwIndex
) == EOF
)
465 /* needs testing. I get the correct index on my machine, but need others
466 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
467 pDelHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
470 if (bFlushTable
== TRUE
)
472 /* delete arp cache */
473 if (FlushIpNetTable(pDelHost
->dwIndex
) != NO_ERROR
)
480 HeapFree(GetProcessHeap(), 0, pDelHost
);
485 /* copy converted IP address */
486 pDelHost
->dwAddr
= dwIpAddr
;
488 /* Add the ARP entry */
489 if (DeleteIpNetEntry(pDelHost
) != NO_ERROR
)
495 HeapFree(GetProcessHeap(), 0, pDelHost
);
500 if (pIpNetTable
!= NULL
)
501 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
502 if (pDelHost
!= NULL
)
503 HeapFree(GetProcessHeap(), 0, pDelHost
);
509 * print program usage to screen
514 _tprintf(_T("\nDisplays and modifies the IP-to-Physical address translation tables used by\n"
515 "address resolution protocol (ARP).\n"
517 "ARP -s inet_addr eth_addr [if_addr]\n"
518 "ARP -d inet_addr [if_addr]\n"
519 "ARP -a [inet_addr] [-N if_addr]\n"
521 " -a Displays current ARP entries by interrogating the current\n"
522 " protocol data. If inet_addr is specified, the IP and Physical\n"
523 " addresses for only the specified computer are displayed. If\n"
524 " more than one network interface uses ARP, entries for each ARP\n"
525 " table are displayed.\n"
527 " inet_addr Specifies an internet address.\n"
528 " -N if_addr Displays the ARP entries for the network interface specified\n"
530 " -d Deletes the host specified by inet_addr. inet_addr may be\n"
531 " wildcarded with * to delete all hosts.\n"
532 " -s Adds the host and associates the Internet address inet_addr\n"
533 " with the Physical address eth_addr. The Physical address is\n"
534 " given as 6 hexadecimal bytes separated by hyphens. The entry\n"
536 " eth_addr Specifies a physical address.\n"
537 " if_addr If present, this specifies the Internet address of the\n"
538 " interface whose address translation table should be modified.\n"
539 " If not present, the first applicable interface will be used.\n"
541 " > arp -s 157.55.85.212 00-aa-00-62-c6-09 .... Adds a static entry.\n"
542 " > arp -a .... Displays the arp table.\n\n"));
548 * Parse command line and call the required function
551 INT
main(int argc
, char* argv
[])
553 if ((argc
< 2) || (argc
> 5))
559 if (argv
[1][0] == '-')
563 case 'a': /* fall through */
566 DisplayArpEntries(NULL
, NULL
);
568 DisplayArpEntries(argv
[2], NULL
);
569 else if ((argc
== 4) && ((strcmp(argv
[2], "-N")) == 0))
570 DisplayArpEntries(NULL
, argv
[3]);
571 else if ((argc
== 5) && ((strcmp(argv
[3], "-N")) == 0))
572 DisplayArpEntries(argv
[2], argv
[4]);
577 case 'd': if (argc
== 3)
578 Deletehost(argv
[2], NULL
);
580 Deletehost(argv
[2], argv
[3]);
585 case 's': if (argc
== 4)
586 Addhost(argv
[2], argv
[3], NULL
);
588 Addhost(argv
[2], argv
[3], argv
[4]);