/// <summary> /// Modifie une entrée ARP existante. /// </summary> /// <param name="entryToSet">Entrée à modifier.</param> /// <param name="IPInterface">Nouvelle interface.</param> /// <param name="PhysicalAddress">Nouvelle adresse physique (MAC).</param> /// <param name="Address">Nouvelle adresse IP.</param> /// <param name="Flags">Flag ARP.</param> /// <returns></returns> public int ChangeArpEntry(ref ArpEntry entryToSet, int IPInterface, byte[] PhysicalAddress, IPAddress Address, ArpFlags Flags) { int ret = -1; try { MIB_IPNETROW ipArp = entryToSet._ipArpNative; ipArp.dwIndex = IPInterface; ipArp.dwType = Flags; ipArp.dwAddr = (uint)BitConverter.ToInt32(Address.GetAddressBytes(), 0); ipArp.bPhysAddr = PhysicalAddress; ipArp.dwPhysAddrLen = PhysicalAddress.Length; //Appel api pour modifier l'entrée. ret = NativeMethods.SetIpNetEntry(ref ipArp); if (ret == 0) { entryToSet.Index = ipArp.dwIndex; entryToSet.Address = Address; entryToSet.Flags = Flags; entryToSet.MacAddress = PhysicalAddress; entryToSet.RelatedInterface = _adapters.GetAdapter(IPInterface); } } catch (Exception) { } return(ret); }
/// <summary> /// Liste des entrées de la table ARP. /// </summary> public IEnumerable <ArpEntry> GetArpEntries() { List <ArpEntry> arpEntries = new List <ArpEntry>(); IntPtr pTable = IntPtr.Zero; int iBufLen = 0; int iRet = 0; //1ier appel pour déterminer la taille de la table ARP. iRet = NativeMethods.GetIpNetTable(IntPtr.Zero, ref iBufLen, true); try { pTable = Marshal.AllocHGlobal(iBufLen); //Lecture de la table. iRet = NativeMethods.GetIpNetTable(pTable, ref iBufLen, true); //Retour OK. if (iRet == 0) { //Nombre d'entrées dans la table. int iEntries = Marshal.ReadInt32(pTable); //Pointeur sur 1ière entrée. IntPtr pEntry = new IntPtr(pTable.ToInt32() + 4); //Parcours chaque entrée. for (int i = 0; i < iEntries; i++) { //Lit l'entrée. MIB_IPNETROW entry = (MIB_IPNETROW)Marshal.PtrToStructure(pEntry, typeof(MIB_IPNETROW)); ArpEntry arpEntry = new ArpEntry(_adapters.GetAdapter(entry.dwIndex), entry.bPhysAddr, entry.dwAddr, entry.dwType); arpEntry._ipArpNative = entry; //Extrait les infos. arpEntries.Add(arpEntry); //Pointeur sur entrée suivante. pEntry = new IntPtr(pEntry.ToInt32() + Marshal.SizeOf(typeof(MIB_IPNETROW))); } } } catch (Exception) { } finally { Marshal.FreeHGlobal(pTable); } return(arpEntries); }
/// <summary> /// Supprime l'entrée <paramref name="entry"/> dans la table ARP. /// </summary> /// <param name="entry">Entrée ARP.</param> /// <returns></returns> public bool DeleteArpEntry(ArpEntry entry) { int ret = 0; try { if (ret == 0) { ret = NativeMethods.DeleteIpNetEntry(ref entry._ipArpNative); } } catch (Exception) { } return(ret == 0 ? true : false); }