// Phar's snoop_shell listener /str0ke

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <fcntl.h>

#include <linux/soundcard.h>

#define HELLO_PORT 8193
#define MSGBUFSIZE 1024

#define RATE 8192 //sample rate
#define SIZE 16  //bits per sample
#define CHANNELS 1 //channels of audio


main(int argc, char *argv[]){
struct sockaddr_in addr;
int fd,dsp, nbytes,addrlen;
struct ip_mreq mreq;
char msgbuf[MSGBUFSIZE];
int arg;

	if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
	  perror("socket");
	  exit(1);
     }


        dsp = open("/dev/dsp", O_RDWR);
        /* set sampling parameters */
        arg = SIZE;      /* sample size */
        ioctl(dsp, SOUND_PCM_WRITE_BITS, &arg);

        arg = CHANNELS;  /* mono or stereo */
        ioctl(dsp, SOUND_PCM_WRITE_CHANNELS, &arg);

        arg = RATE;      /* sampling rate */
        ioctl(dsp, SOUND_PCM_WRITE_RATE, &arg);


     /* set up destination address */
     memset(&addr,0,sizeof(addr));
     addr.sin_family=AF_INET;
     addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
     addr.sin_port=htons(8193);
     
     /* bind to receive address */
     if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
	  perror("bind");
	  exit(1);
     }
     

     /* now just enter a read-print loop */
     while (1) {
	  addrlen=sizeof(addr);
	  if ((nbytes=recvfrom(fd,msgbuf,MSGBUFSIZE,0,(struct sockaddr *) &addr,&addrlen)) < 0) {
		perror("recvfrom");
	  }else{
		write(dsp,msgbuf,nbytes);
	  }
//	  puts(msgbuf);
	printf("%u bytes\n",nbytes);
     }
}

// milw0rm.com [2005-11-04]