예제 #1
0
파일: spxabort.c 프로젝트: coyizumi/cs111
int
main(int argc, char *argv[])
{
	struct sockaddr_ipx sipx;
	int sock_listen, sock;

	sock_listen = socket(PF_IPX, SOCK_STREAM, 0);
	if (sock_listen < 0)
		err(-1, "sock_listen = socket(PF_IPX, SOCK_STREAM, 0)");

	bzero(&sipx, sizeof(sipx));
	sipx.sipx_len = sizeof(sipx);
	sipx.sipx_family = AF_IPX;
	sipx.sipx_addr = ipx_addr(IPX_ENDPOINT);

	if (bind(sock_listen, (struct sockaddr *)&sipx, sizeof(sipx)) < 0)
		err(-1, "bind(sock_listen)");

	if (listen(sock_listen, -1) < 0)
		err(-1, "listen(sock_listen)");

	sock = socket(PF_IPX, SOCK_STREAM, 0);
	if (sock < 0)
		err(-1, "sock = socket(PF_IPX, SOCK_STREAM, 0)");

	bzero(&sipx, sizeof(sipx));
	sipx.sipx_len = sizeof(sipx);
	sipx.sipx_family = AF_IPX;
	sipx.sipx_addr = ipx_addr(IPX_ENDPOINT);

	if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
		err(-1, "fcntl(sock, F_SETFL, O_NONBLOCKING)");

	if (connect(sock, (struct sockaddr *)&sipx, sizeof(sipx)) < 0) {
		if (errno != EINPROGRESS)
			err(-1, "sock = socket(PF_IPX, SOCK_STREAM, 0)");
	}

	sleep(1);	/* Arbitrary. */

	close(sock_listen);

	return (0);
};
예제 #2
0
static void
ipx_getaddr(const char *addr, int which)
{
    struct sockaddr_ipx *sipx = sipxtab[which];

    sipx->sipx_family = AF_IPX;
    sipx->sipx_len = sizeof(*sipx);
    sipx->sipx_addr = ipx_addr(addr);
    if (which == MASK)
        printf("Attempt to set IPX netmask will be ineffectual\n");
}
예제 #3
0
int
main(int argc, char *argv[])
{
	int error, sock_listen, sock_recv, sock_send;
	struct sockaddr_ipx sipx_listen, sipx_send;
	pid_t childpid, parentpid;

	/*
	 * Socket to receive with.
	 */
	sock_listen = socket(PF_IPX, SOCK_STREAM, 0);
	if (sock_listen < 0)
		err(-1, "sock_listen = socket(PF_IPX, SOCK_STREAM, 0)");

	bzero(&sipx_listen, sizeof(sipx_listen));
	sipx_listen.sipx_len = sizeof(sipx_listen);
	sipx_listen.sipx_family = AF_IPX;
	sipx_listen.sipx_addr = ipx_addr(IPX_ENDPOINT);

	if (bind(sock_listen, (struct sockaddr *)&sipx_listen,
	    sizeof(sipx_listen)) < 0)
		err(-1, "bind(sock_listen)");

	if (listen(sock_listen, -1) < 0)
		err(-1, "listen(sock_listen)");

	parentpid = getpid();

	childpid = fork();
	if (childpid < 0)
		err(-1, "fork()");

	if (childpid == 0) {
		/*
		 * The child: accept connections and process data on them.
		 */
		while (1) {
			sock_recv = accept(sock_listen, NULL, NULL);
			if (sock_recv < 0) {
				warn("accept()");
				continue;
			}

			my_recv(sock_recv, "listener", parentpid);
			my_send(sock_recv, "listener", parentpid);

			close(sock_recv);
		}
	} else {
		/*
		 * The parent: connect, send data, receive it back, and exit;
		 * build two connections, once using a full connect() API
		 * call, and the second using sendto().
		 */

		/*
		 * Socket to send with.
		 */
		sock_send = socket(PF_IPX, SOCK_STREAM, 0);
		if (sock_send < 0) {
			error = errno;
			(void)kill(childpid, SIGTERM);
			errno = error;
			err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
		}

		bzero(&sipx_send, sizeof(sipx_send));
		sipx_send.sipx_len = sizeof(sipx_send);
		sipx_send.sipx_family = AF_IPX;
		sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);

		if (connect(sock_send, (struct sockaddr *)&sipx_send,
		    sizeof(sipx_send)) < 0) {
			error = errno;
			(void)kill(childpid, SIGTERM);
			errno = error;
			err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
		}

		my_send(sock_send, "connector", childpid);
		my_recv(sock_send, "connector", childpid);

		close(sock_send);

#ifdef SPX_SUPPORTS_SENDTO_WITH_CONNECT
		sock_send = socket(PF_IPX, SOCK_STREAM, 0);
		if (sock_send < 0) {
			error = errno;
			(void)kill(childpid, SIGTERM);
			errno = error;
			err(-1, "sock_send = socket(PF_IPX, SOCK_STREAM, 0)");
		}

		bzero(&sipx_send, sizeof(sipx_send));
		sipx_send.sipx_len = sizeof(sipx_send);
		sipx_send.sipx_family = AF_IPX;
		sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);

		my_sendto(sock_send, "connector", childpid,
		    (struct sockaddr *)&sipx_send, sizeof(sipx_send));
		my_recv(sock_send, "connector", childpid);

		close(sock_send);
#endif

		(void)kill(childpid, SIGTERM);
	}

	return (0);
}
예제 #4
0
int
main(int argc, char *argv[])
{
	struct sockaddr_ipx sipx_recv, sipx_send;
	u_char packet[PACKETLEN];
	int i, sock_recv, sock_send;
	ssize_t len;

	/*
	 * Socket to receive with.
	 */
	sock_recv = socket(PF_IPX, SOCK_DGRAM, 0);
	if (sock_recv < 0)
		err(-1, "sock_recv = socket(PF_IPX, SOCK_DGRAM, 0)");

	bzero(&sipx_recv, sizeof(sipx_recv));
	sipx_recv.sipx_len = sizeof(sipx_recv);
	sipx_recv.sipx_family = AF_IPX;
	sipx_recv.sipx_addr = ipx_addr(IPX_ENDPOINT);

	if (bind(sock_recv, (struct sockaddr *)&sipx_recv, sizeof(sipx_recv))
	    < 0)
		err(-1, "bind(sock_recv)");

	/*
	 * Set non-blocking to try to avoid blocking indefinitely if the
	 * packet doesn't end up in the right place.
	 */
	if (fcntl(sock_recv, F_SETFL, O_NONBLOCK) < 0)
		err(-1, "fcntl(O_NONBLOCK, sock_recv)");

	/*
	 * Socket to send with.
	 */
	sock_send = socket(PF_IPX, SOCK_DGRAM, 0);
	if (sock_send < 0)
		err(-1, "sock_send = socket(PF_IPX, SOCK_DGRAM, 0)");

	bzero(&sipx_send, sizeof(sipx_send));
	sipx_send.sipx_len = sizeof(sipx_send);
	sipx_send.sipx_family = AF_IPX;
	sipx_send.sipx_addr = ipx_addr(IPX_ENDPOINT);

	for (i = 0; i < PACKETLEN; i++)
		packet[i] = (i & 0xff);

	len = sendto(sock_send, packet, sizeof(packet), 0,
	    (struct sockaddr *)&sipx_send, sizeof(sipx_send));
	if (len < 0)
		err(-1, "sendto()");
	if (len != sizeof(packet))
		errx(-1, "sendto(): short send (%zu length, %zd sent)",
		    sizeof(packet), len);

	sleep(1);	/* Arbitrary non-zero amount. */

	bzero(packet, sizeof(packet));
	len = recv(sock_recv, packet, sizeof(packet), 0);
	if (len < 0)
		err(-1, "recv()");
	if (len != sizeof(packet))
		errx(-1, "recv(): short receive (%zu length, %zd received)",
		    sizeof(packet), len);

	for (i = 0; i < PACKETLEN; i++) {
		if (packet[i] != (i & 0xff))
			errx(-1, "recv(): byte %d wrong (%d instead of %d)",
			    i, packet[i], i & 0xff);
	}

	return (0);
}