desperately seeking RAW SOCKET help (4.2BSD+BRL)

henry strickland strick at gatech.CSNET
Wed May 8 06:18:49 AEST 1985


I'm having a very tough time using RAW sockets on our vaxen
750s and 780.  I've been stuck at the same place for days and
seem to be getting nowhere.  The machines are on the same
ethernet, and use DEC's DEUNA ethernet interface.

Does anyone have any example programs they can mail me?
Has anyone ever used RAW sockets successfully?  Have any hints?
I need raw sockets to eventually network with non-UNIX machines.

The story so far:

I open a RAW socket(), bind() an address, define a connect()ion
address, and try either to send() or recv().

The recv blocks inside recv, like it should.  Send returns
an error 0 [ unused error code ].

I've read /usr/doc/ipc [the IPC primer], tried to read /usr/doc/net
[ network implementation manual ], and have looked through the
code for the rwho daemon.  Nothing shows anything that looks wrong
in my program.

I think next I'll put printf's in the kernel to try to
figure out where this error 0 is coming from.  [ I'm using
DEC's DEUNA ethernet interface, with driver if_de.c 6.7  84/08/29,
by Lou Salkind, New York University ].

Nothing really talks about port numbers for RAW sockets.  It seems
that it must be zero.  There's no place in the ip header for any
number to go.  So I suppose you should only have one RAW reader
per interface.

Here's my program in case you want to try this yourself.

	thanks ..... strick at gatech

#############################################  net.c follows:


/* net.c
 * usage:
 *	net local foreign    -- to recieve
 *	wet local foreign    -- to send
 * example:  on stratus use:  wet stratus nimbus
 *           on nimbus  use:  net nimbus stratus
 *    to send from stratus to nimbus
 *
 *  gatech!strick
 *  henry strickland -- 1may85 -- the clouds project -- ga tech
 *
 *  test raw ip sockets on 4.2 unix machines
 *
 *  $Header: net.c,v 1.3 85/05/05 16:08:08 strick Exp $
 *
 * $Log:	net.c,v $
 * Revision 1.3  85/05/05  16:08:08  strick
 * 
 *
 */


#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>


#define SOCK_TYPE SOCK_RAW
#define PORT 0
#define BROAD

int flags = 0, len = 100, count;
int s, errno, cc, writer;
int on = 1;

struct sockaddr_in sin;
struct hostent *h;
int i;

char buf[300];

main(argc,argv)
char *argv[];
{
	
	setlinebuf(stdout);

	if ( argc != 3 )  { printf("argc not two\n"); exit(1); }
	
	/* first char of $0 determines if we read or write */
	if ( argv[0][0]=='w'  )  writer=1;

	printf ( "Up and running -- %s\n", writer? "writer" : "reader" );

/* open socket */
	s = socket(AF_INET, SOCK_TYPE, 0 );
	if ( s<0 ) { 
		perror("socket");exit(1);
	} 
	printf("Socket opened\n");

#ifdef BROAD
/* set BROADCAST option */
	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof on) <0 ) {
		perror("setsock broadcast");  exit(1);
	}
#endif BROAD


#ifdef fancy
/* set DEBUG option */
	if (setsockopt(s, SOL_SOCKET, SO_DEBUG, &on, sizeof on) <0) {
		perror("setsock debug"); exit(1);
	}

/* set DONTROUTE option */
	if (setsockopt(s, SOL_SOCKET, SO_DONTROUTE, &on, sizeof on)<0) {
		perror("setsock dontroute"); exit(1);
	}
#endif fancy


/* bind local address */
	bzero((char*)&sin,sizeof(sin));
	h = gethostbyname( argv[1] );
	if ( h == 0 ) { printf("cant get src addr: %s\n", argv[1]);
				exit(1);	
	}
	bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
	sin.sin_port =  PORT ;
	sin.sin_family = h->h_addrtype ;
	printsin( &sin, "bound to", argv[1] );
	if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0 ) {
		perror("cant bind");
	} else printf("bind ok.\n");

/* bind foreign address */
	h = gethostbyname( argv[2] );
	if ( h == 0 ) { printf("can't get dest addr: %s\n",argv[2]);
				exit(1);
	}
	bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
	sin.sin_port = PORT;
	sin.sin_family = h->h_addrtype;
	printsin( &sin, "connected to", argv[2]);
#ifdef BROAD	
   	sin.sin_addr.s_imp = INADDR_ANY; 
#endif BROAD

/* assertions */
	if ( h->h_length!=4 || s!=3 ) {
		printf("%d not four or %d not three\n",
			h->h_length, s );  exit(1);
	}

/* print all of destination address */
	for (i=0; i<16; i++)  printf("%x ", ((char*)&sin)[i]);
	printf("\n");

	if (connect ( s, (struct sockaddr*)&sin, h->h_length ) == -1 ) {
			perror("connection bad");  exit(1); 
	} else {
			printf("connection successful\n");
	}



	if ( writer ) {

	    for ( count=0; count<60; count++  ) {

		(void) sprintf( buf, "wrambling wreck %d ==========", count );

		cc = send( s, buf, len, flags );

		if ( cc= -1 ) {
			perror("send err");
		} else {
			printf("Sent %d!\n", count);
		}

		sleep(3);
	    }

	} else /* if reader */ {

	    while (1) {
		printf("...\n");
		cc = recv( s, buf, len, flags );
		if ( cc= -1) {
			perror("recv err" );
		} else {
			buf[25] = '\0';
			printf("got: <%s>\n", buf);
		}

	    }
	} /* endif */


}


printsin( sin, m1, m2 )
struct sockaddr_in *sin;
char *m1, *m2;
{
	printf("%s %s:\n", m1, m2);
	printf("family %d addr %x port %d\n", sin->sin_family,
		sin->sin_addr.s_addr,  sin->sin_port);
}

###############################   makefile follows:
# use 'net' to recieve
# use 'wet' to send
# usage:
#        [nw]et  local  foriegn
# use ascii string hostnames
#
all : net wet
net : net.c
	cc -o net net.c
wet : net.c
	ln net wet
###############################
-- 
 --  henry strickland  
  --  the clouds project            { akgua allegra hplabs inhp4 }
   --   school of ics / ga tech                         !gatech!strick
    --    atlanta ga 30332



More information about the Comp.unix.wizards mailing list