www.pudn.com > vxWorks_Lab.rar > sockHelp.c


/* sockHelp - handy tools to use at the shell prompt */ 
 
/* Copyright 1984-1993 Wind River Systems, Inc. */ 
 
/* 
modification history 
-------------------- 
01c,17feb98,dlk  flushed input before reading response. 
01a,24apr95,bss  changed printf() to printErr() in sockAddrPrompt(). 
01b,06dec94,bss  cleaned up. 
01a,???????,???  written. 
*/ 
 
/* 
DESCRIPTION 
This module provides several routines which allow students to 
manipulate sockets from the VxWorks shell. 
*/ 
 
/* includes */ 
 
#include "vxWorks.h" 
#include "fioLib.h" 
#include "hostLib.h" 
#include "inetLib.h" 
#include "netinet/in.h" 
#include "stdio.h" 
#include "string.h" 
#include "sys/socket.h" 
#include "sys/types.h" 
#include "ioLib.h" 
 
/******************************************************************************* 
 * 
 * sockAddrPrompt - prompt user for values to be stored in a  
 * struct sockaddr_in. 
 */ 
 
void sockAddrPrompt ( struct sockaddr_in * pSockAddr) 
	{ 
	char	string [100]; 
	u_long  inet; 
	int		portNum; 
	char	inetAddr [INET_ADDR_LEN]; 
 
 
	/* make sure address pointer was specified */ 
 
    if (pSockAddr == NULL) 
		{ 
		printErr ("usage: sockAddrPrompt addr\n"); 
		return; 
		} 
 
 
	printf ("Type  to leave field unchanged:\n\n"); 
 
	/* prompt for internet address */ 
 
	ioctl (STD_IN, FIORFLUSH, 0); 
 
	do 
		{ 
		inet_ntoa_b (pSockAddr->sin_addr, inetAddr); 
		printf ("sin_addr -- Enter inet address or host name [%s]: ", 
			inetAddr); 
		fioRdString (STD_IN, string, sizeof (string)); 
 
		if (strlen (string) == 0) 
			inet = pSockAddr->sin_addr.s_addr; 
		else 
			{ 
			inet = hostGetByName (string); 
 
			if (inet == ERROR) 
				{ 
				inet = inet_addr (string); 
 
				if (inet == ERROR) 
					printErr ("invalid inet address: %s\n", string); 
				} 
			} 
		} 
	while (inet == ERROR); 
 
 
	/* prompt for port number */ 
 
	printf ("sin_port -- Enter port number [%d]: ", ntohs(pSockAddr->sin_port)); 
	fioRdString (STD_IN, string, sizeof (string)); 
 
	if (strlen (string) == 0) 
		portNum = pSockAddr->sin_port; 
	else 
		sscanf (string, "%d", &portNum); 
 
 
	pSockAddr->sin_family 		= AF_INET; 
	pSockAddr->sin_port   		= htons (portNum); 
	pSockAddr->sin_addr.s_addr  = inet; 
	bzero (pSockAddr->sin_zero, sizeof (pSockAddr->sin_zero)); 
	} 
 
/******************************************************************************* 
 * 
 * sockAddrShow - display contents of a struct sockaddr_in. 
 */ 
 
void sockAddrShow (struct sockaddr_in *pSockAddr) 
	{ 
	char inetAddr [INET_ADDR_LEN]; 
 
    inet_ntoa_b (pSockAddr->sin_addr, inetAddr); 
 
	printf ("sin_family  = %d\n", pSockAddr->sin_family); 
	printf ("sin_addr    = %s\n", inetAddr); 
	printf ("sin_port    = %d\n", ntohs (pSockAddr->sin_port)); 
	} 
 
 
/************************************************************** 
*  inetAddrInit - Initialize a sockaddr_in structure 
* 
* 
*  RETURNS: OK, or ERROR if host name is invalid. 
*/ 
  
  
STATUS inetAddrInit  
	( 
    struct sockaddr_in * pAddr, 
    char * pHost, 
    int port 
    ) 
    { 
    u_long inet; 
      
    bzero ((char *) pAddr, sizeof (struct sockaddr_in)); 
  
    if ((inet = hostGetByName (pHost)) == ERROR) 
        { 
        if ((inet = inet_addr (pHost)) == ERROR) 
            return (ERROR); 
        } 
  
    pAddr->sin_family 		= AF_INET; 
    pAddr->sin_port 		= htons (port); 
    pAddr->sin_addr.s_addr 	= inet; 
  
    return (OK); 
    }