raw socket query
Web Dove
dove at mit-bug.UUCP
Thu Oct 3 11:18:03 AEST 1985
Can any bsd net wizard get these two programs to work in RAW_MODE??
echo x - send.c
sed 's/^X//' >send.c <<'*-*-END-of-send.c-*-*'
X/*
X * Send to a datagram socket. host is the first arg. Protocol is determined
X * from argv[0].
X */
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <netdb.h>
X
X#define DATASIZE 128
X
Xstruct pkt
X{
X char p_data[DATASIZE];
X} pkt;
X
X/* pick a high enough number to be non-priviledged */
Xstruct sockaddr_in sinaddr = { AF_INET, 1100 };
Xint sinaddrlen;
X
Xint argc;
Xchar **argv;
X
Xvoid
Xerr(string)
X char *string;
X{
X extern errno;
X
X fprintf(stderr, "%s: errno %d ", argv[0], errno);
X perror(string);
X exit(1);
X}
X
X#define RAW_MODE 1
X#define UDP_MODE 2
Xint mode;
X
Xmain (ac, av)
Xchar **av;
X{
X int s, n;
X int pkt_num;
X struct hostent *gethostbyname(), *hostent;
X
X argc = ac, argv = av;
X setlinebuf(stderr);
X
X if(argc != 2)
X {
X fprintf(stderr, "usage: %s hostname < file\n", argv[0]);
X exit(1);
X }
X
X /* choose the mode */
X if(strcmpn(argv[0], "udp", 3) == 0) mode = UDP_MODE;
X else if (strcmpn(argv[0], "raw", 3) == 0) mode = RAW_MODE;
X else
X {
X fprintf(stderr, "%s should be installed as one of:", argv[0]);
X fprintf(stderr, " \"udp???\"");
X fprintf(stderr, " \"raw???\"");
X exit(1);
X }
X
X /* set up the peer address of sinaddr */
X hostent = gethostbyname(argv[1]);
X sinaddr.sin_addr.s_addr = *(int *)hostent->h_addr;
X
X /* build a socket */
X if((s=socket(AF_INET, mode==RAW_MODE?SOCK_RAW:SOCK_DGRAM, 0))==(-1))
X err("socket");
X
X /* connect to the receiver */
X if(connect(s, &sinaddr, sizeof(sinaddr))==(-1)) err("connect");
X
X /* display the peer's address */
X sinaddrlen = sizeof(sinaddr);
X if(getpeername(s, &sinaddr, &sinaddrlen)==(-1)) err("getpeername");
X fprintf(stderr, "sinaddrlen=%d, family=%d, port=%d, sin_addr=%s\n",
X sinaddrlen, sinaddr.sin_family, sinaddr.sin_port,
X inet_ntoa(sinaddr.sin_addr));
X
X /* send an initial pkt so the peer gets our address */
X strcpy(pkt.p_data, "hello");
X if(send(s, &pkt, sizeof("hello"), 0) != sizeof("hello")) err("send error");
X
X /* send stdin */
X pkt_num = 0;
X while((n=read(0, pkt.p_data, sizeof(pkt.p_data))) > 0)
X {
X if(send(s, &pkt, n, 0) != n) err("send data");
X write(2, "#", 1);
X if((++pkt_num)%8==0)
X {
X if((n=recv(s, pkt.p_data, sizeof(pkt.p_data), 0))!=4)
X err("bad ack %d bytes", n);
X if(((int *)pkt.p_data)[0]!= pkt_num)
X fprintf(stderr, "sender: got ack %d??\n", ((int *)pkt.p_data)[0]);
X }
X }
X write(2, "\n", 1);
X}
*-*-END-of-send.c-*-*
echo x - recv.c
sed 's/^X//' >recv.c <<'*-*-END-of-recv.c-*-*'
X/*
X * Recv from a datagram socket. Protocol is determined from argv[0].
X */
X#include <stdio.h>
X#include <sys/types.h>
X#include <sys/socket.h>
X#include <netinet/in.h>
X#include <netdb.h>
X
Xstruct sockaddr_in sinaddr = { AF_INET, 1100 };
X
Xchar *inet_ntoa();
X
X#define MAXPKTSIZ 128
X
Xu_char inbuf[MAXPKTSIZ];
X
Xint argc;
Xchar **argv;
X
Xvoid
Xerr(string)
X char *string;
X{
X extern errno;
X
X fprintf(stderr, "%s: errno %d ", argv[0], errno);
X perror(string);
X exit(1);
X}
X
X#define RAW_MODE 1
X#define UDP_MODE 2
Xint mode;
X
Xmain (ac, av)
X char **av;
X{
X int s, sinaddrlen, i, nbytes, pkt_num;
X struct hostent *gethostbyname(), *hostent;
X
X argc = ac, argv = av;
X setlinebuf(stderr);
X
X if(argc != 2)
X {
X fprintf(stderr, "usage: %s hostname < file\n", argv[0]);
X exit(1);
X }
X
X /* choose the mode */
X if(strcmpn(argv[0], "udp", 3) == 0) mode = UDP_MODE;
X else if (strcmpn(argv[0], "raw", 3) == 0) mode = RAW_MODE;
X else
X {
X fprintf(stderr, "%s should be installed as one of:", argv[0]);
X fprintf(stderr, " \"udp???\"");
X fprintf(stderr, " \"raw???\"");
X exit(1);
X }
X
X
X if((s=socket(AF_INET, mode==RAW_MODE?SOCK_RAW:SOCK_DGRAM, 0))==(-1))
X err("socket");
X
X /* establish the local address of the socket */
X hostent = gethostbyname(argv[1]);
X sinaddr.sin_addr.s_addr = *(int *)hostent->h_addr;
X if(bind(s, &sinaddr, sizeof (sinaddr)) == (-1)) err("bind");
X fprintf(stderr, "%s: done binding\n", argv[0]);
X
X /* receive a pkt labelled by sender */
X sinaddrlen = sizeof (sinaddr);
X if((nbytes=recvfrom(s, inbuf, sizeof(inbuf), 0, &sinaddr, &sinaddrlen)) <= 0)
X err("recvfrom");
X fprintf(stderr, "%s: done recving %d bytes\n", argv[0], nbytes);
X
X /* display the peer's address */
X fprintf(stderr, "sinaddrlen=%d, family=%d, port=%d, sin_addr=%s\n",
X sinaddrlen, sinaddr.sin_family, sinaddr.sin_port,
X inet_ntoa(sinaddr.sin_addr));
X fprintf(stderr, "initial pkt (as string) = \"%s\"\n", inbuf, nbytes);
X
X /* wire the other end of socket "s" to the sender */
X if(connect(s, &sinaddr, sizeof (sinaddr)) == -1) err("connect");
X
X pkt_num = 0;
X while((nbytes=recv(s, inbuf, sizeof(inbuf), 0)) != 0)
X {
X if(nbytes==(-1)) err("read");
X if(write (1, inbuf, nbytes) != nbytes) err("write");
X write(2, "!", 1);
X if((++pkt_num)%8==0)
X {
X if((nbytes=send(s, &pkt_num, sizeof(pkt_num), 0))!=4)
X err("bad send ack %d bytes", nbytes);
X }
X }
X write(2, "\n", 1);
X}
*-*-END-of-recv.c-*-*
echo x - Makefile
sed 's/^X//' >Makefile <<'*-*-END-of-Makefile-*-*'
X#
X# makefile for SIGPIPE test
X#
XCFLAGS = -O
XCC = /4.3/bin/cc
XPROGS=udp_send udp_recv raw_send raw_recv
X
Xall: $(PROGS)
X
Xudp_send raw_send: send.o
X $(CC) -o $@ send.o
X
Xudp_recv raw_recv: recv.o
X $(CC) -o $@ recv.o
*-*-END-of-Makefile-*-*
exit
More information about the Comp.unix.wizards
mailing list