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_LEAN_AND_MEAN
47 const char SEPERATOR
= '-';
48 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
);
63 * convert error code into meaningful message
65 DWORD
DoFormatMessage(VOID
)
70 DWORD ErrorCode
= GetLastError();
72 if (ErrorCode
!= ERROR_SUCCESS
)
74 RetVal
= FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
75 FORMAT_MESSAGE_FROM_SYSTEM
|
76 FORMAT_MESSAGE_IGNORE_INSERTS
,
79 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), /* Default language */
86 _tprintf(_T("%s"), (LPTSTR
)lpMsgBuf
);
89 /* return number of TCHAR's stored in output buffer
90 * excluding '\0' - as FormatMessage does*/
101 * Takes an ARP entry and prints the IP address,
102 * the MAC address and the entry type to screen
105 INT
PrintEntries(PMIB_IPNETROW pIpAddRow
)
110 /* print IP addresses */
111 inaddr
.S_un
.S_addr
= pIpAddRow
->dwAddr
;
112 _tprintf(_T(" %-22s"), inet_ntoa(inaddr
));
114 /* print MAC address */
115 _stprintf(cMacAddr
, _T("%02x-%02x-%02x-%02x-%02x-%02x"),
116 pIpAddRow
->bPhysAddr
[0],
117 pIpAddRow
->bPhysAddr
[1],
118 pIpAddRow
->bPhysAddr
[2],
119 pIpAddRow
->bPhysAddr
[3],
120 pIpAddRow
->bPhysAddr
[4],
121 pIpAddRow
->bPhysAddr
[5]);
122 _tprintf(_T("%-22s"), cMacAddr
);
124 /* print cache type */
125 switch (pIpAddRow
->dwType
)
127 case MIB_IPNET_TYPE_DYNAMIC
: _tprintf(_T("dynamic\n"));
129 case MIB_IPNET_TYPE_STATIC
: _tprintf(_T("static\n"));
131 case MIB_IPNET_TYPE_INVALID
: _tprintf(_T("invalid\n"));
133 case MIB_IPNET_TYPE_OTHER
: _tprintf(_T("other\n"));
142 * Takes optional parameters of an internet address and interface address.
143 * Retrieve all entries in the ARP cache. If an internet address is
144 * specified, display the ARP entry relating to that address. If an
145 * interface address is specified, display all entries relating to
149 /* FIXME: allow user to specify an interface address, via pszIfAddr */
150 INT
DisplayArpEntries(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
154 PMIB_IPNETTABLE pIpNetTable
= NULL
;
155 PMIB_IPADDRTABLE pIpAddrTable
= NULL
;
157 struct in_addr inaddr
, inaddr2
;
159 TCHAR szIntIpAddr
[20];
161 /* retrieve the IP-to-physical address mapping table */
164 GetIpNetTable(pIpNetTable
, &Size
, 0);
166 /* allocate memory for ARP address table */
167 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
168 if (pIpNetTable
== NULL
)
171 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
173 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
175 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
180 /* check there are entries in the table */
181 if (pIpNetTable
->dwNumEntries
== 0)
183 _tprintf(_T("No ARP entires found\n"));
189 /* Retrieve the interface-to-ip address mapping
190 * table to get the IP address for adapter */
194 GetIpAddrTable(pIpAddrTable
, &Size
, 0);
196 pIpAddrTable
= (MIB_IPADDRTABLE
*) HeapAlloc(GetProcessHeap(), 0, Size
);
197 if (pIpAddrTable
== NULL
)
200 ZeroMemory(pIpAddrTable
, sizeof(*pIpAddrTable
));
202 if ((iRet
= GetIpAddrTable(pIpAddrTable
, &Size
, TRUE
)) != NO_ERROR
)
204 _tprintf(_T("GetIpAddrTable failed: %d\n"), iRet
);
210 for (k
=0; k
< pIpAddrTable
->dwNumEntries
; k
++)
212 if (pIpNetTable
->table
[0].dwIndex
== pIpAddrTable
->table
[k
].dwIndex
)
214 //printf("debug print: pIpAddrTable->table[?].dwIndex = %lx\n", pIpNetTable->table[k].dwIndex);
215 inaddr2
.s_addr
= pIpAddrTable
->table
[k
].dwAddr
;
216 pszIpAddr
= inet_ntoa(inaddr2
);
217 strcpy(szIntIpAddr
, pszIpAddr
);
222 /* print header, including interface IP address and index number */
223 _tprintf(_T("\nInterface: %s --- 0x%lx \n"), szIntIpAddr
, pIpNetTable
->table
[0].dwIndex
);
224 _tprintf(_T(" Internet Address Physical Address Type\n"));
226 /* go through all ARP entries */
227 for (i
=0; i
< pIpNetTable
->dwNumEntries
; i
++)
230 /* if the user has supplied their own internet addesss *
231 * only print the arp entry which matches that */
234 inaddr
.S_un
.S_addr
= pIpNetTable
->table
[i
].dwAddr
;
235 pszIpAddr
= inet_ntoa(inaddr
);
237 /* check if it matches, print it */
238 if (strcmp(pszIpAddr
, pszInetAddr
) == 0)
239 PrintEntries(&pIpNetTable
->table
[i
]);
242 /* if an address is not supplied, print all entries */
243 PrintEntries(&pIpNetTable
->table
[i
]);
249 if (pIpNetTable
!= NULL
)
250 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
251 if (pIpAddrTable
!= NULL
)
252 HeapFree(GetProcessHeap(), 0, pIpAddrTable
);
259 * Takes an internet address, a MAC address and an optional interface
260 * address as arguments and checks their validity.
261 * Fill out an MIB_IPNETROW structure and insert the data into the
262 * ARP cache as a static entry.
265 INT
Addhost(PTCHAR pszInetAddr
, PTCHAR pszEthAddr
, PTCHAR pszIfAddr
)
267 PMIB_IPNETROW pAddHost
= NULL
;
268 PMIB_IPNETTABLE pIpNetTable
= NULL
;
275 /* check IP address */
276 if (pszInetAddr
!= NULL
)
278 if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
280 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
290 /* check MAC address */
291 if (strlen(pszEthAddr
) != 17)
293 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
298 if (pszEthAddr
[i
] == SEPERATOR
)
301 if (!isxdigit(pszEthAddr
[i
]))
303 _tprintf(_T("ARP: bad argument: %s\n"), pszEthAddr
);
308 /* We need the IpNetTable to get the adapter index */
309 /* Return required buffer size */
310 GetIpNetTable(pIpNetTable
, &Size
, 0);
312 /* allocate memory for ARP address table */
313 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
314 if (pIpNetTable
== NULL
)
317 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
319 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
321 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
327 /* reserve memory on heap and zero */
328 pAddHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
329 if (pAddHost
== NULL
)
332 ZeroMemory(pAddHost
, sizeof(MIB_IPNETROW
));
334 /* set dwIndex field to the index of a local IP address to
335 * indicate the network on which the ARP entry applies */
338 if (sscanf(pszIfAddr
, "%lx", &pAddHost
->dwIndex
) == EOF
)
345 //printf("debug print: pIpNetTable->table[0].dwIndex = %lx\n", pIpNetTable->table[0].dwIndex);
346 /* needs testing. I get the correct index on my machine, but need others
347 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
348 pAddHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
351 /* Set MAC address to 6 bytes (typical) */
352 pAddHost
->dwPhysAddrLen
= 6;
355 /* Encode bPhysAddr into correct byte array */
359 c
= toupper(pszEthAddr
[i
*3]);
360 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
363 c
= toupper(pszEthAddr
[i
*3 + 1]);
364 c
= c
- (isdigit(c
) ? '0' : ('A' - 10));
366 pAddHost
->bPhysAddr
[i
] = (BYTE
)val
;
370 /* copy converted IP address */
371 pAddHost
->dwAddr
= dwIpAddr
;
374 /* set type to static */
375 pAddHost
->dwType
= MIB_IPNET_TYPE_STATIC
;
378 /* Add the ARP entry */
379 if (SetIpNetEntry(pAddHost
) != NO_ERROR
)
385 HeapFree(GetProcessHeap(), 0, pAddHost
);
390 if (pIpNetTable
!= NULL
)
391 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
392 if (pAddHost
!= NULL
)
393 HeapFree(GetProcessHeap(), 0, pAddHost
);
400 * Takes an internet address and an optional interface address as
401 * arguments and checks their validity.
402 * Add the interface number and IP to an MIB_IPNETROW structure
403 * and remove the entry from the ARP cache.
406 INT
Deletehost(PTCHAR pszInetAddr
, PTCHAR pszIfAddr
)
408 PMIB_IPNETROW pDelHost
= NULL
;
409 PMIB_IPNETTABLE pIpNetTable
= NULL
;
412 BOOL bFlushTable
= FALSE
;
416 /* check IP address */
417 if (pszInetAddr
!= NULL
)
419 /* if wildcard is given, set flag to delete all hosts */
420 if (strncmp(pszInetAddr
, "*", 1) == 0)
422 else if ((dwIpAddr
= inet_addr(pszInetAddr
)) == INADDR_NONE
)
424 _tprintf(_T("ARP: bad IP address: %s\n"), pszInetAddr
);
434 /* We need the IpNetTable to get the adapter index */
435 /* Return required buffer size */
436 GetIpNetTable(NULL
, &Size
, 0);
438 /* allocate memory for ARP address table */
439 pIpNetTable
= (PMIB_IPNETTABLE
) HeapAlloc(GetProcessHeap(), 0, Size
);
440 if (pIpNetTable
== NULL
)
443 ZeroMemory(pIpNetTable
, sizeof(*pIpNetTable
));
445 if (GetIpNetTable(pIpNetTable
, &Size
, TRUE
) != NO_ERROR
)
447 _tprintf(_T("failed to allocate memory for GetIpNetTable\n"));
452 /* reserve memory on heap and zero */
453 pDelHost
= (MIB_IPNETROW
*) HeapAlloc(GetProcessHeap(), 0, sizeof(MIB_IPNETROW
));
454 if (pDelHost
== NULL
)
457 ZeroMemory(pDelHost
, sizeof(MIB_IPNETROW
));
460 /* set dwIndex field to the index of a local IP address to
461 * indicate the network on which the ARP entry applies */
464 if (sscanf(pszIfAddr
, "%lx", &pDelHost
->dwIndex
) == EOF
)
471 /* needs testing. I get the correct index on my machine, but need others
472 * to test their card index. Any problems and we can use GetAdaptersInfo instead */
473 pDelHost
->dwIndex
= pIpNetTable
->table
[0].dwIndex
;
476 if (bFlushTable
== TRUE
)
478 /* delete arp cache */
479 if (FlushIpNetTable(pDelHost
->dwIndex
) != NO_ERROR
)
486 HeapFree(GetProcessHeap(), 0, pDelHost
);
491 /* copy converted IP address */
492 pDelHost
->dwAddr
= dwIpAddr
;
494 /* Add the ARP entry */
495 if (DeleteIpNetEntry(pDelHost
) != NO_ERROR
)
501 HeapFree(GetProcessHeap(), 0, pDelHost
);
506 if (pIpNetTable
!= NULL
)
507 HeapFree(GetProcessHeap(), 0, pIpNetTable
);
508 if (pDelHost
!= NULL
)
509 HeapFree(GetProcessHeap(), 0, pDelHost
);
517 * print program usage to screen
522 _tprintf(_T("\nDisplays and modifies the IP-to-Physical address translation tables used by\n"
523 "address resolution protocol (ARP).\n"
525 "ARP -s inet_addr eth_addr [if_addr]\n"
526 "ARP -d inet_addr [if_addr]\n"
527 "ARP -a [inet_addr] [-N if_addr]\n"
529 " -a Displays current ARP entries by interrogating the current\n"
530 " protocol data. If inet_addr is specified, the IP and Physical\n"
531 " addresses for only the specified computer are displayed. If\n"
532 " more than one network interface uses ARP, entries for each ARP\n"
533 " table are displayed.\n"
535 " inet_addr Specifies an internet address.\n"
536 " -N if_addr Displays the ARP entries for the network interface specified\n"
538 " -d Deletes the host specified by inet_addr. inet_addr may be\n"
539 " wildcarded with * to delete all hosts.\n"
540 " -s Adds the host and associates the Internet address inet_addr\n"
541 " with the Physical address eth_addr. The Physical address is\n"
542 " given as 6 hexadecimal bytes separated by hyphens. The entry\n"
544 " eth_addr Specifies a physical address.\n"
545 " if_addr If present, this specifies the Internet address of the\n"
546 " interface whose address translation table should be modified.\n"
547 " If not present, the first applicable interface will be used.\n"
549 " > arp -s 157.55.85.212 00-aa-00-62-c6-09 .... Adds a static entry.\n"
550 " > arp -a .... Displays the arp table.\n\n"));
558 * Parse command line and call the required function
561 INT
main(int argc
, char* argv
[])
563 if ((argc
< 2) || (argc
> 5))
569 if (argv
[1][0] == '-')
573 case 'a': /* fall through */
576 DisplayArpEntries(NULL
, NULL
);
578 DisplayArpEntries(argv
[2], NULL
);
579 else if ((argc
== 4) && ((strcmp(argv
[2], "-N")) == 0))
580 DisplayArpEntries(NULL
, argv
[3]);
581 else if ((argc
== 5) && ((strcmp(argv
[3], "-N")) == 0))
582 DisplayArpEntries(argv
[2], argv
[4]);
587 case 'd': if (argc
== 3)
588 Deletehost(argv
[2], NULL
);
590 Deletehost(argv
[2], argv
[3]);
595 case 's': if (argc
== 4)
596 Addhost(argv
[2], argv
[3], NULL
);
598 Addhost(argv
[2], argv
[3], argv
[4]);