14. FAQ o Win API

Q> How to learn ip the address (machines (in a text type)? 
A> the Piece of a source code from a plug-in to BackOrifice: 

void MachineIP (char *result) 
{ 
      WSADATA WSAData; 

      WSAStartup (MAKEWORD (1,1), &WSAData); 

 char dot [6]; 
 int iResult; 
 int i = 0; 
 u_long *ppIpNO; 
 u_long *pIpNO; 
 HOSTENT FAR *lphostent; 
 u_long ipHO; 
 unsigned char binIp [4]; 
 int iterations = 0; 

 //Get local host name and crudely validate 
 char szHostName [100]; 
 *result = 0; 

 iResult = gethostname (szHostName, sizeof (szHostName)); 
//printf ("%d %s", iResult, szHostName); 
 if ((iResult! = 0) || (lstrcmp (szHostName, "") == 0)) 
  return; 

 //Lok up this host info via supplied name 
 lphostent = gethostbyname (szHostName); 
 if (lphostent == NULL) 
  return; 
 //Retreive first entry (might have multiple connects) 
 do 
 { 
  iterations ++; 
  ppIpNO = (u_long *) lphostent-> h_addr_list; 
  if (ppIpNO+i == NULL) 
   return; 
  pIpNO = ((u_long *) * (ppIpNO+i)); 
  if (pIpNO == NULL) 
   return; 

 //convert back to host order, since SOCKADDR_IN expects that 
  ipHO = ntohl (*pIpNO); 

  binIp [0] = (BYTE) ((ipHO AND 0xff000000)>> 24); 
  itoa (binIp [0], dot, 10); 
  strcat (result, dot); 
  binIp [1] = (BYTE) ((ipHO AND 0x00ff0000)>> 16); 
  itoa (binIp [1], dot, 10); 
  strcat (result, "."); strcat (result, dot); 
  binIp [2] = (BYTE) ((ipHO AND 0x0000ff00)>> 8); 
  itoa (binIp [2], dot, 10); 
  strcat (result, "."); strcat (result, dot); 
  binIp [3] = (BYTE) (ipHO AND 0x000000ff); 
  itoa (binIp [3], dot, 10); 
  strcat (result, "."); strcat (result, dot); 
  strcat (result, "\r\n"); 
  i ++; 
 } while ((pIpNO! = NULL) && (iterations <6)); 
 WSACleanup (); 
      PostQuitMessage (0); 
 return; 
}

2000 (c) DM