#ifndef lint
#ifndef SABER
static char *rcsid_foo_c = "$Header: /afs/net.mit.edu/dapps/project/techinfodev/src/srv_ti/RCS/inet.c,v 1.3 92/08/28 16:09:10 ark Exp Locker: ark $";
#endif SABER
#endif lint
/*
* inet.c: Code for internet socket manipulations.
*/
/*
Copyright (C) 1989 by the Massachusetts Institute of Technology
Export of this software from the United States of America is assumed
to require a specific license from the United States Government.
It is the responsibility of any person or organization contemplating
export to obtain such a license before exporting.
WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
distribute this software and its documentation for any purpose and
without fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright notice and
this permission notice appear in supporting documentation, and that
the name of M.I.T. not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission. M.I.T. makes no representations about the suitability of
this software for any purpose. It is provided "as is" without express
or implied warranty.
/*
* Create a socket and establish a connection over it to the given host and
* port. Returns the (connected) socket created, or -1 in case of error.
*/
static struct sockaddr_in lsname; /* the listener */
/* ARGSUSED */
int
inet_establish_connection(char *hostname, char *portname, int quiet)
{
struct sockaddr_in sname;
struct hostent *hp;
struct servent *sp;
int sock, portnum;
/*
* resolve hostname
*/
hp = gethostbyname(hostname);
if (hp == NULL) {
fprintf(stderr, "unknown host %s\n", hostname);
return -1;
}
listen(sock, 5);
/*
printf("Listening at local address %s, port %d, over socket #%d.\n\n",
inet_ntoa(lsname.sin_addr), ntohs(lsname.sin_port),
sock);
*/
/*****/
return sock;
}
/*
* Accept a connection coming in over the given listen_sock (a socket
* that is being listend on, perhaps created by make_listner()).
* Return a new connected socket, and the contacting hostname and
* portnumber if desired and available.
*/
int
inet_accept_connection(int listen_sock, char **hname, int *portnum)
{
struct sockaddr_in sname;
struct hostent *host;
int socket, length;
/*
* get name of other end of this connection; returns zero if call
* successful. &sname is altered by call, &length is space pointed
* to by name
*/
length = sizeof(sname);
if (getpeername(socket, &sname, &length))
perror("getpeername");
if (portnum)
*portnum = ntohs(sname.sin_port);
/*
* return a pointer to the structure hostent, which contains host
* information ie. name of host, host address type (AF_INET), length
* of address
*/