how to convert a dotted-decimal IP address into host internal IP address

Discussion in 'Windows Vista Drivers' started by Steve, May 26, 2004.

  1. Steve

    Steve Guest

    Hi,All,
    It is very convenient to use a dotted-decimal ip
    address,however,TDISendDataGram requires a ULong-typed IP address.
    Hence a convension is necessary.Is there any function in windows kernel
    mode which can assume this duty?
     
    Steve, May 26, 2004
    #1
    1. Advertisements

  2. Write inet_addr() yourself. It's trivial.
     
    Maxim S. Shatskih, May 26, 2004
    #2
    1. Advertisements

  3. I think gethostbyname() should do it.
     
    Alexander Grigoriev, May 26, 2004
    #3
  4. In the kernel?
    Well, we have the kernel-mode DNS resolver, but it is around 2000 lines of
    code or such.
     
    Maxim S. Shatskih, May 26, 2004
    #4
  5. Steve

    Guest Guest

    RtlIpv4AddressToStringA
    RtlIpv4AddressToStringExA
    RtlIpv4AddressToStringExW
    RtlIpv4AddressToStringW
    RtlIpv4StringToAddressA
    RtlIpv4StringToAddressExA
    RtlIpv4StringToAddressExW
    RtlIpv4StringToAddressW
    RtlIpv6AddressToStringA
    RtlIpv6AddressToStringExA
    RtlIpv6AddressToStringExW
    RtlIpv6AddressToStringW
    RtlIpv6StringToAddressA
    RtlIpv6StringToAddressExA
    RtlIpv6StringToAddressExW
    RtlIpv6StringToAddressW
     
    Guest, May 26, 2004
    #5
  6. Steve

    Steve Guest

    Thank you all. But I think the functions like RtlIPv* don't work.
    So I will have to write a special-purpose function like inet_addr.
     
    Steve, May 27, 2004
    #6
  7. Steve

    Tim Roberts Guest

    I love responses like this.

    What do you mean by "I think the functions like RtlIPv* don't work"? Did
    you just have a bad feeling that they were flawed in some way? Did someone
    tell you they didn't work? Or have you actually tried one and had it fail?
     
    Tim Roberts, May 28, 2004
    #7
  8. Steve

    Stephan Wolf Guest

    I thought exactly the same thing ;)

    Another, beloved, frequently posted reply is like "problem solved,
    thanks". Not that we would ever like to know *how* it got solved...

    Stephan
    ---
     
    Stephan Wolf, May 28, 2004
    #8
  9. Steve

    Steve Guest

    Hi,Stephan and Tim,
    Thank you for good advice.
    I think the functions like RtlIPv* don't work because I can't find them
    in Windows DDK Document.
    On the other hand, it is certainly trivial to write an inet_addr
    function, but fortunately, the principle of the function is clear to us.
    Hence, I would implement it rather than spend time in looking for it.

    Steve

    fail?
     
    Steve, May 29, 2004
    #9
  10. Hello Steve,

    Trivial? Did you consider

    - getting an IP as decimal value (i.e., 127.0.0.1 as "2130706433")?
    - getting an IP in octal notation (i.e., 127.0.0.1 as "0177.0.0.1")?

    As there are some possibilities which are not frequently used, you might
    miss a use.

    Kind regards,
    Spiro.
     
    Spiro Trikaliotis, May 29, 2004
    #10
  11. Steve

    Steve Guest

    I just convert a string of "10.0.0.3" into a ULong-typed number of
    0X0a000003.
    so I can parse the string and calculate its corresponding value.
     
    Steve, May 29, 2004
    #11
  12. Trivial? Did you consider
    2 or 3 screenfuls of C code even with all these things handled.
     
    Maxim S. Shatskih, May 29, 2004
    #12
  13. Hello Maxim,

    My point was that inet_addr() can process many different types of IPs,
    and one does not always consider every single way of it.

    If I know which options there are, I don't think it is hard to write
    such a function. I only wanted to tell that the "if" part can be quite
    hard. ;-)

    Kind regards,
    Spiro.
     
    Spiro Trikaliotis, May 29, 2004
    #13
  14. Hello Steve,

    I'm not sure if I understand you correctly:

    Does your routine handle

    010.0.0.3 as 0x08000003,
    0177.0.017.2 as 0x7F000F02,
    2130706435 as 0x7F000003,

    and so on? If not, you only have a limited inet_addr() function. If you
    get external inputs (i.e., from a user), this could really be a
    problem.

    Regards,
    Spiro.
     
    Spiro Trikaliotis, May 29, 2004
    #14
  15. If I know which options there are, I don't think it is hard to write
    Here is the description of IP address formats understandable by inet_addr()
    from StorageCraft KSockets
    (http://www.storagecraft.com/products/ksockets.html). It (logic, not code) was
    taken from FreeBSD IIRC.

    // Possible formats:
    // 1 digit group (a) - treat as is
    // 2 digit groups (a.b) - treat as ( a << 24 ) | b, a must be < 256, b
    must be < 16M
    // 3 digit groups (a.b.c) - treat as ( a << 24 ) | ( b << 16 ) | c, a and
    b must be < 256,
    // c must be < 64K
    // 4 digit groups (a.b.c.d) - treat as ( a << 24 ) | ( b << 16 ) | ( c <<
    8 ) | d,
    // all of them must be < 256
    // Any digit group with trailing dot must be < 256, and the last digit
    group
    // must be < ( 1 << ( 32 - ( NumberOfDigitGroups - 1 ) * 8 ) )

    And, surely, any digit group can have a radix of 8 ("0" prefix), 16 ("0x"
    prefix) or 10 (no prefix).
     
    Maxim S. Shatskih, May 29, 2004
    #15
  16. Hello Maxim,

    thanks for the info.

    *g* An xBSD would have been the place where I would have looked, too.

    Regards,
    Spiro.
     
    Spiro Trikaliotis, May 29, 2004
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.