Socket Addresses

Chris Torek chris at umcp-cs.UUCP
Tue Aug 5 10:58:39 AEST 1986


In article <2725 at umcp-cs.UUCP> I wrote:
>	int s;
>	struct sockaddr_un name;
>
>	s = socket(AF_UNIX, SOCK_STREAM, 0);
>	...
>	name.sun_family = AF_UNIX;
>	name.sun_name = "server";
>	if (bind(s, (struct sockaddr *) &name, strlen(name.sun_name))) ...

Ai!  This is all wrong (and will not even compile)!  Better (tested
even):

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

char	*strcpy(*);

/*ARGSUSED*/
main(argc, argv)
	int argc;
	char **argv;
{
	int s;
	struct sockaddr_un name;

	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	name.sun_family = AF_UNIX;
	(void) strcpy(name.sun_path, "server");
	(void) unlink(name.sun_path);	/* delete previous socket, if any */
	if (bind(s, (struct sockaddr *) &name,
	    sizeof (name.sun_family) + strlen(name.sun_path))) {
		perror("bind");
		exit(1);
	}

	/*
	 * Thanks to John Bruner for pointing out the missing
	 * sizeof name.sun_family.  The rest of the corrections are
	 * from the revised 4.3 IPC primer---I should have known,
	 * since I helped revise it, and we tried to ensure that all
	 * the examples worked.
	 *
	 * Incidentally, there is a hidden assumption about structure
	 * padding even in the above.  This `path name' business needs
	 * work.
	 */

	exit(0);
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list