Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Getting invalid IP address.............

Getting invalid IP address.............

Scheduled Pinned Locked Moved C#
csharphardwareperformance
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    3bood ghzawi
    wrote on last edited by
    #1

    Hi all... As calling an 'ipAddress' from class 'Sockaddr' to find the local IP address of my PC, it always return an IP address 255.255.255.255 for IPv4, and ffff.ffff.ffff.ffff for IPv6. the class 'Sockaddr' belongs to 'SharpPcap-2.1.1', the implementation of the class is as follwo: using System; using System.Runtime.InteropServices; using System.Net.NetworkInformation; namespace SharpPcap.Containers { public class Sockaddr { public enum Type { AF_INET_AF_INET6, HARDWARE, UNKNOWN } public Type type; static public System.Net.IPAddress ipAddress; // if type == AF_INET_AF_INET6 public PhysicalAddress hardwareAddress; // if type == HARDWARE private int _sa_family; public int sa_family { get { return _sa_family; } } public Sockaddr(PhysicalAddress hardwareAddress) { this.type = Type.HARDWARE; this.hardwareAddress = hardwareAddress; } public Sockaddr(IntPtr sockaddrPtr) { // A sockaddr struct. We use this to determine the address family PcapUnmanagedStructures.sockaddr saddr; // Marshal memory pointer into a struct saddr = (PcapUnmanagedStructures.sockaddr)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr)); // record the sa_family for informational purposes _sa_family = saddr.sa_family; byte[] addressBytes; if(saddr.sa_family == Pcap.AF_INET) { type = Type.AF_INET_AF_INET6; PcapUnmanagedStructures.sockaddr_in saddr_in = (PcapUnmanagedStructures.sockaddr_in)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr_in)); ipAddress = new System.Net.IPAddress(saddr_in.sin_addr.s_addr); } else if(saddr.sa_family == Pcap.AF_INET6) { type = Type.AF_INET_AF_INET6; addressBytes = new byte[16]; PcapUnmanagedStructures.sockaddr_in6 sin6 = (PcapUnmanagedStructures.sockaddr_in6)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr_in6));

    S 1 Reply Last reply
    0
    • 3 3bood ghzawi

      Hi all... As calling an 'ipAddress' from class 'Sockaddr' to find the local IP address of my PC, it always return an IP address 255.255.255.255 for IPv4, and ffff.ffff.ffff.ffff for IPv6. the class 'Sockaddr' belongs to 'SharpPcap-2.1.1', the implementation of the class is as follwo: using System; using System.Runtime.InteropServices; using System.Net.NetworkInformation; namespace SharpPcap.Containers { public class Sockaddr { public enum Type { AF_INET_AF_INET6, HARDWARE, UNKNOWN } public Type type; static public System.Net.IPAddress ipAddress; // if type == AF_INET_AF_INET6 public PhysicalAddress hardwareAddress; // if type == HARDWARE private int _sa_family; public int sa_family { get { return _sa_family; } } public Sockaddr(PhysicalAddress hardwareAddress) { this.type = Type.HARDWARE; this.hardwareAddress = hardwareAddress; } public Sockaddr(IntPtr sockaddrPtr) { // A sockaddr struct. We use this to determine the address family PcapUnmanagedStructures.sockaddr saddr; // Marshal memory pointer into a struct saddr = (PcapUnmanagedStructures.sockaddr)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr)); // record the sa_family for informational purposes _sa_family = saddr.sa_family; byte[] addressBytes; if(saddr.sa_family == Pcap.AF_INET) { type = Type.AF_INET_AF_INET6; PcapUnmanagedStructures.sockaddr_in saddr_in = (PcapUnmanagedStructures.sockaddr_in)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr_in)); ipAddress = new System.Net.IPAddress(saddr_in.sin_addr.s_addr); } else if(saddr.sa_family == Pcap.AF_INET6) { type = Type.AF_INET_AF_INET6; addressBytes = new byte[16]; PcapUnmanagedStructures.sockaddr_in6 sin6 = (PcapUnmanagedStructures.sockaddr_in6)Marshal.PtrToStructure(sockaddrPtr, typeof(PcapUnmanagedStructures.sockaddr_in6));

      S Offline
      S Offline
      SeMartens
      wrote on last edited by
      #2

      Hi, your code seems a bit complicated. Did you know that there is some framework implementation to obtain the local ip-adress? Check out this article: How To Get IP Address Of A Machine[^] Hope this helps. Regards Sebastian

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      L 1 Reply Last reply
      0
      • S SeMartens

        Hi, your code seems a bit complicated. Did you know that there is some framework implementation to obtain the local ip-adress? Check out this article: How To Get IP Address Of A Machine[^] Hope this helps. Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi Sebastian, such techniques return the local IP address, i.e. the address is valid on the LAN, but in general not on the internet. To get the "external" IP address, one really needs an external web service, and I happen to offer one here: http://www.perceler.com/tools1.php[^]. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Happy New Year to all.
        We hope 2010 soon brings us automatic PRE tags!
        Until then, please insert them manually.


        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups