Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

WIN32: TCP related functions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Scott Turchin
    replied
    Dave posted those 5 years ago...

    See the other forum (Programming the internet), and FYI - NO that would not work for you.....


    You'll need to do a traceroute.


    ------------------
    Scott Turchin
    MCSE, MCP+I
    Computer Creations Software
    http://www.tngbbs.com/ccs

    Leave a comment:


  • Noel Orden
    Guest replied
    Hi Dave,

    Does your code work on a router? I tried using your function to get my external IP address, and my code to get a valid socket handle is as follows:

    Local s AS LONG, IP as long

    s=FreeFile
    Tcp Open "http" At "www.powerbasic.com" AS #s

    If Err Then
    ' "Error"
    Else
    IP=TcpAddr(s)
    End If

    Got no errors but the IP address retrieved was that of my network and not the WAN IP address of the router.

    Any help is much appreciated.

    Thanks and Kind Regards

    Noel

    ------------------
    NHO

    Leave a comment:


  • Dave Navarro
    replied
    Code:
    UNION in_addr
      s_addr AS LONG
      s AS STRING * 4
    END UNION
     
    TYPE sockaddr_in
      sin_family AS WORD
      sin_port AS WORD
      sin_addr AS in_addr
      sin_zero AS STRING * 8
    END TYPE
     
    DECLARE FUNCTION getsockname LIB "wsock32.dll" ALIAS "getsockname" _
       (BYVAL s AS LONG, sname AS sockaddr_in, namelen AS LONG) AS LONG
     
    FUNCTION TcpAddr(BYVAL s AS LONG) AS LONG
     
      LOCAL sa AS sockaddr_in
      LOCAL l  AS LONG
     
      s = FILEATTR(s,2) ' get winsock socket handle
      l = SIZEOF(sa)
     
      IF getsockname(s, sa, l) = 0 THEN
        FUNCTION = sa.sin_addr.s_addr   'return IP address of connection
      END IF
     
    END FUNCTION
    -------------
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Dave Navarro
    replied
    If your computer is connected to a network using a network card, and it has a modem in it. It's possible for your computer to have two IP addresses at the same time.

    One IP address for the network connection and one IP address for your dialup connection.

    When your computer boots up, the first IP address assigned to it is for the network card. So the following code will return the IP address for your network connection:
    Code:
      HOST ADDR TO ip&
    When you connect to the internet using your modem, a second IP address is assigned to your computer (by your ISP). But the preceding code will still return the address of your network card, not the new IP address for your internet connection.

    In that situation you can use the following to get the IP address for your modem connection:
    Code:
      HOST ADDR(2) TO ip&
    The reason that you would want to is because some mail servers are set up so that they will only allow connections from authorized IP addresses. Most ISP's won't let anyone who is not dialed up through them connect to their mail server to send a message. This prevents non-users from sending spam through their servers.

    In most cases, the SMTP server will simply look at the IP address of the incoming connection to make that determination. However, sometimes the SMTP server looks at the IP address specified in the "HELO ..." command to determine if you can connect to it. In this case, sending the IP address of your network card will cause the connection to fail.

    On Windows 95 and 98 machines, you can only have one IP address per network connection. (If you have two network cards in your computer, they'll each get their own IP address). So it's technically possible to have more than two IP addresses on your machine. Particularly with Windows NT where you can actually assign more than one IP address to a single network card.

    Trying to use "HOST ADDR(n)" in those situations is practically useless because there is no way to know which IP address in the sequence is being used for the connection to the SMTP server.

    What should you do? Use the WINSOCK API!

    The trick is to use "TCP OPEN" to connect to the server. This causes PowerBASIC to create a valid socket for connection. Once that's done, you can use the socket handle to get the IP address of the connection.

    TCP OPEN uses the PowerBASIC file system, so the socket handle used in your code is not a valid WINSOCK handle. You need to use the FILEATTR() function to obtain the actual WINSOCK handle.

    Once you've done that, you can use the API call getsockname() to get the IP address that was used for the connection. In the next reply, I'll post a function that does this.

    --Dave

    -------------
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Dave Navarro
    started a topic WIN32: TCP related functions

    WIN32: TCP related functions

    I think I've posted this before, but it's related to the function following it:
    Code:
    ' Convert a long integer representing an IP address to a displayable string
    '
    FUNCTION IpToString(BYVAL ip AS LONG) AS STRING
     
      LOCAL b AS BYTE PTR
     
      b = VARPTR(ip)
      FUNCTION = FORMAT$(@b[0]) & "." & FORMAT$(@b[1]) & "." & _
                 FORMAT$(@b[2]) & "." & FORMAT$(@b[3])
     
    END FUNCTION
    --Dave

    -------------
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
Working...
X