#include "tcp.h"
/* set_address - wypelnienie struktury sockaddr_in */
void set_address( char *hname, char *sname,
                  struct sockaddr_in *sap, char *protocol )
/* hname    - nazwa hosta
   sname    - nazwa uslugi
   sap      - struktura adresowa do wypelnienia
   protocol - nazwa protokołu transportowego
*/

{
	struct servent *sp;
	struct hostent *hp;
	char *endptr;
	short port;

	bzero( sap, sizeof( *sap ) );

	sap->sin_family = AF_INET;

   /* Czy podany adres */
	if ( hname != NULL )
	{
      /* Czy adres podany w postaci notacji dziesiętno-kropkowej */
		if ( !inet_aton( hname, &sap->sin_addr ) )
		{
         /* adres podany w postaci nazwy */
			hp = gethostbyname( hname );
			if ( hp == NULL )
				error( 1, 0, "nieznany host: %s\n", hname );
			sap->sin_addr = *( struct in_addr * )hp->h_addr;
		}
	}
   /* adres dowolny */
	else
		sap->sin_addr.s_addr = htonl( INADDR_ANY );

   /* Wypelnienie numeru portu */
	port = strtol( sname, &endptr, 0 );
	if ( *endptr == '\0' )
		sap->sin_port = htons( port );
	else
	{
		sp = getservbyname( sname, protocol );
		if ( sp == NULL )
			error( 1, 0, "nieznana usługa: %s\n", sname );
		sap->sin_port = sp->s_port;
	}
}
