'===========================================================================
' Subject: 2 IP ADDRESSES ON YOUR COMPUTER    Date: 01-20-00 (14:27)       
'  Author: Dave Navarro, Jr.                  Code: PBCC, PBDLL            
'  Origin: dave@powerbasic.com              Packet: PBCC.ABC
'===========================================================================
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:

   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:

   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.

-------------------------------[ 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
