Ejemplo n.º 1
0
int
sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
    register_t *retval)
{
	/* {
		syscallarg(int)		domain;
		syscallarg(int)		type;
		syscallarg(int)		protocol;
		syscallarg(int *)	rsv;
	} */
	file_t		*fp1, *fp2;
	struct socket	*so1, *so2;
	int		fd, error, sv[2];
	proc_t		*p = curproc;
	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
	int		domain = SCARG(uap, domain);
	int		proto = SCARG(uap, protocol);

	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
	if (error)
		return error;
	so1 = fp1->f_socket;
	sv[0] = fd;

	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
	if (error)
		goto out;
	so2 = fp2->f_socket;
	sv[1] = fd;

	solock(so1);
	error = soconnect2(so1, so2);
	if (error == 0 && type == SOCK_DGRAM) {
		/*
		 * Datagram socket connection is asymmetric.
		 */
		error = soconnect2(so2, so1);
	}
	sounlock(so1);

	if (error == 0)
		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
	if (error == 0) {
		fd_affix(p, fp2, sv[1]);
		fd_affix(p, fp1, sv[0]);
		return 0;
	}
	fd_abort(p, fp2, sv[1]);
	(void)soclose(so2);
out:
	fd_abort(p, fp1, sv[0]);
	(void)soclose(so1);
	return error;
}
Ejemplo n.º 2
0
int multireceive(int PORT, std::string group, std::string ip,  BlockingQueue<std::string> &q, BlockingQueue<std::string> &foreign){

	int pack = makesocket(PORT, group, ip);										// call function above to make socket
	int recvlen;
	char buf[BUFSIZE] = "";
	std::string foreignIP;														// string to store IP of sender

	recvlen = 0;

	struct sockaddr_in peer_address;											// makes structure for peer_address
	socklen_t peer_address_len;
	peer_address_len = sizeof(struct sockaddr_storage);

	while(true){																// while loop to keep receiving
			//std::cout << "waiting on port " << PORT << std::endl;
			recvlen = recvfrom(pack, buf, BUFSIZE, 0, (struct sockaddr *)&peer_address, &peer_address_len);	//actual receiving mechanism, returns amount of bytes received
			//std::cout << "received bytes: " << recvlen << std::endl;
			if (recvlen > 0) {													// if bytes available push them to the BlockingQueue
				foreignIP = inet_ntoa(peer_address.sin_addr);					// stores IP of sender
				foreign.push(std::string(foreignIP, foreignIP.size()));
				q.push(std::string(buf, recvlen));
			}

		}

		exit(0);
}
Ejemplo n.º 3
0
int main(int argc, char *argv[])
{
    if (argc < 4) {
       fprintf(stderr,"%s", USAGE);
       exit(0);
    }
    server_t = gethostbyname(argv[2]);
    server = gethostbyname(argv[2]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    if (server_t == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }
    filename = (char *) malloc(FILENAME_SIZE);
    strcpy(filename,argv[1]);
    portno = atoi(argv[3]);
    makesocket();
    mapfile();
    //send_file_info();
    send_file_info_tcp();
    if((error=pthread_create(&resend_thread,NULL,resend_packet,NULL))){
        fprintf(stderr, "error in creating pthread: %s\n",strerror(error));
        exit(1);
    }
    packet packet1;
    memset(packet1.payload,'\0',PAYLOAD_SIZE+1);
    if((filesize % PAYLOAD_SIZE) != 0)
        no_of_packets = (filesize/PAYLOAD_SIZE)+1;
    else
        no_of_packets = (filesize/PAYLOAD_SIZE);
	if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) { perror( "clock gettime" );}
    while(seqNum < no_of_packets){
        packet1.seq_num = seqNum;
        if((seqNum == (no_of_packets-1)) && ((filesize % PAYLOAD_SIZE) != 0)){
        	memcpy(packet1.payload,data+off,(filesize % PAYLOAD_SIZE));
        	}
        else{
        	memcpy(packet1.payload,data+off,PAYLOAD_SIZE);
        	}
        seqNum++;
        memcpy(packet1.payload,data+off,PAYLOAD_SIZE);
        off = off + PAYLOAD_SIZE;
        send_packets(packet1);
    }
    pthread_join(resend_thread,NULL);
    munmap(data, filesize);
    close(fp);
	return 1;
}
Ejemplo n.º 4
0
/** @brief Finish full autoconfiguration (set everything needed to go to partial autoconf)
 * This function is called when FULL autoconfiguration is finished
 * It fill the asked pid array
 * It open the file descriptors for the new filters, and set the filters
 * It open the new sockets
 * It free autoconfiguration memory wich will be not used anymore
 *
 * @param card the card number
 * @param number_of_channels the number of channels
 * @param channels the array of channels
 * @param fds the file descriptors
*/
int autoconf_finish_full(mumudvb_chan_and_pids_t *chan_and_pids, autoconf_parameters_t *autoconf_vars, multicast_parameters_t *multicast_vars, tuning_parameters_t *tuneparams, fds_t *fds, unicast_parameters_t *unicast_vars, int server_id)
{
  int curr_channel,curr_pid;
  //We sort the services
  autoconf_sort_services(autoconf_vars->services);
  chan_and_pids->number_of_channels=autoconf_services_to_channels(*autoconf_vars, chan_and_pids->channels, multicast_vars->common_port, tuneparams->card, tuneparams->tuner, unicast_vars, multicast_vars, server_id); //Convert the list of services into channels
  //we got the pmt pids for the channels, we open the filters
  for (curr_channel = 0; curr_channel < chan_and_pids->number_of_channels; curr_channel++)
  {
    for (curr_pid = 0; curr_pid < chan_and_pids->channels[curr_channel].num_pids; curr_pid++)
    {
      if(chan_and_pids->asked_pid[chan_and_pids->channels[curr_channel].pids[curr_pid]]==PID_NOT_ASKED)
        chan_and_pids->asked_pid[chan_and_pids->channels[curr_channel].pids[curr_pid]]=PID_ASKED;
      chan_and_pids->number_chan_asked_pid[chan_and_pids->channels[curr_channel].pids[curr_pid]]++;
    }
  }

  // we open the file descriptors
  if (create_card_fd (tuneparams->card_dev_path, tuneparams->tuner, chan_and_pids->asked_pid, fds) < 0)
  {
    log_message( log_module, MSG_ERROR,"ERROR : CANNOT open the new descriptors. Some channels will probably not work\n");
  }
  // we set the new filters
  set_filters( chan_and_pids->asked_pid, fds);


  //Networking
  for (curr_channel = 0; curr_channel < chan_and_pids->number_of_channels; curr_channel++)
  {

    /** open the unicast listening connections fo the channels */
    if(chan_and_pids->channels[curr_channel].unicast_port && unicast_vars->unicast)
    {
      log_message( log_module, MSG_INFO,"Unicast : We open the channel %d http socket address %s:%d\n",
                  curr_channel,
                  unicast_vars->ipOut,
                  chan_and_pids->channels[curr_channel].unicast_port);
      unicast_create_listening_socket(UNICAST_LISTEN_CHANNEL,
                                      curr_channel,
                                      unicast_vars->ipOut,
                                      chan_and_pids->channels[curr_channel].unicast_port,
                                      &chan_and_pids->channels[curr_channel].sIn,
                                      &chan_and_pids->channels[curr_channel].socketIn,
                                      fds,
                                      unicast_vars);
    }

    //Open the multicast socket for the new channel
    if(multicast_vars->multicast_ipv4)
      {
	if(multicast_vars->multicast && multicast_vars->auto_join) //See the README for the reason of this option
	  chan_and_pids->channels[curr_channel].socketOut4 = 
	    makeclientsocket (chan_and_pids->channels[curr_channel].ip4Out,
			      chan_and_pids->channels[curr_channel].portOut,
			      multicast_vars->ttl,
			      multicast_vars->iface4,
			      &chan_and_pids->channels[curr_channel].sOut4);
	else if(multicast_vars->multicast)
	  chan_and_pids->channels[curr_channel].socketOut4 = 
	    makesocket (chan_and_pids->channels[curr_channel].ip4Out,
			chan_and_pids->channels[curr_channel].portOut,
			multicast_vars->ttl,
			multicast_vars->iface4,
			&chan_and_pids->channels[curr_channel].sOut4);
      }
    if(multicast_vars->multicast_ipv6)
      {
	if(multicast_vars->multicast && multicast_vars->auto_join) //See the README for the reason of this option
	  chan_and_pids->channels[curr_channel].socketOut6 = 
	    makeclientsocket6 (chan_and_pids->channels[curr_channel].ip6Out,
			      chan_and_pids->channels[curr_channel].portOut,
			      multicast_vars->ttl,
			       multicast_vars->iface6,
			      &chan_and_pids->channels[curr_channel].sOut6);
	else if(multicast_vars->multicast)
	  chan_and_pids->channels[curr_channel].socketOut6 = 
	    makesocket6 (chan_and_pids->channels[curr_channel].ip6Out,
			chan_and_pids->channels[curr_channel].portOut,
			multicast_vars->ttl,
			multicast_vars->iface6,
			&chan_and_pids->channels[curr_channel].sOut6);
      }
  }

  log_message( log_module, MSG_DEBUG,"Step TWO, we get the video and audio PIDs\n");
  //We free autoconf memort
  autoconf_freeing(autoconf_vars);

  autoconf_vars->autoconfiguration=AUTOCONF_MODE_PIDS; //Next step add video and audio pids

  return 0;
}
Ejemplo n.º 5
0
int
main(int argc, char **argv)
{
    unsigned char mp3buffer[LAME_MAXMP3BUFFER];
    char    inPath[PATH_MAX + 1];
    char    outPath[PATH_MAX + 1];
    int     Buffer[2][1152];

    lame_global_flags *gf;

    int     ret;
    int     wavsamples;
    int     mp3bytes;
    FILE   *outf;

    char    ip[16];
    unsigned port = 5004;
    unsigned ttl = 2;
    char    dummy;

    int     enc_delay = -1;
    int     enc_padding = -1;

    frontend_open_console();
    if (argc <= 2) {
        console_printf("Encode (via LAME) to mp3 with RTP streaming of the output\n"
                       "\n"
                       "    mp3rtp ip[:port[:ttl]] [lame encoding options] infile outfile\n"
                       "\n"
                       "    examples:\n"
                       "      arecord -b 16 -s 22050 -w | ./mp3rtp 224.17.23.42:5004:2 -b 56 - /dev/null\n"
                       "      arecord -b 16 -s 44100 -w | ./mp3rtp 10.1.1.42 -V2 -b128 -B256 - my_mp3file.mp3\n"
                       "\n");
        frontend_close_console();
        return 1;
    }

    switch (sscanf(argv[1], "%11[.0-9]:%u:%u%c", ip, &port, &ttl, &dummy)) {
    case 1:
    case 2:
    case 3:
        break;
    default:
        error_printf("Illegal destination selector '%s', must be ip[:port[:ttl]]\n", argv[1]);
        frontend_close_console();
        return -1;
    }

    rtpsocket = makesocket(ip, port, ttl, &rtpsi);
    srand(getpid() ^ time(NULL));
    initrtp(&RTPheader);

    /* initialize encoder */
    gf = lame_init();
    if (NULL == gf) {
        error_printf("fatal error during initialization\n");
        frontend_close_console();
        return 1;
    }
    lame_set_errorf(gf, &frontend_errorf);
    lame_set_debugf(gf, &frontend_debugf);
    lame_set_msgf(gf, &frontend_msgf);

    /* Remove the argumets that are rtp related, and then 
     * parse the command line arguments, setting various flags in the
     * struct pointed to by 'gf'.  If you want to parse your own arguments,
     * or call libmp3lame from a program which uses a GUI to set arguments,
     * skip this call and set the values of interest in the gf struct.  
     * (see lame.h for documentation about these parameters)
     */

    argv[1] = argv[0];
    parse_args(gf, argc - 1, argv + 1, inPath, outPath, NULL, NULL);

    /* open the output file.  Filename parsed into gf.inPath */
    if (0 == strcmp(outPath, "-")) {
        lame_set_stream_binary_mode(outf = stdout);
    }
    else {
        if ((outf = fopen(outPath, "wb+")) == NULL) {
            error_printf("Could not create \"%s\".\n", outPath);
            frontend_close_console();
            return 1;
        }
    }


    /* open the wav/aiff/raw pcm or mp3 input file.  This call will
     * open the file with name gf.inFile, try to parse the headers and
     * set gf.samplerate, gf.num_channels, gf.num_samples.
     * if you want to do your own file input, skip this call and set
     * these values yourself.  
     */
    init_infile(gf, inPath, &enc_delay, &enc_padding);


    /* Now that all the options are set, lame needs to analyze them and
     * set some more options 
     */
    ret = lame_init_params(gf);
    if (ret < 0) {
        if (ret == -1)
            display_bitrates(stderr);
        error_printf("fatal error during initialization\n");
        frontend_close_console();
        return -1;
    }

    lame_print_config(gf); /* print useful information about options being used */

    if (update_interval < 0.)
        update_interval = 2.;

    /* encode until we hit EOF */
    while ((wavsamples = get_audio(gf, Buffer)) > 0) { /* read in 'wavsamples' samples */
        levelmessage(maxvalue(Buffer));
        mp3bytes = lame_encode_buffer_int(gf, /* encode the frame */
                                          Buffer[0], Buffer[1], wavsamples,
                                          mp3buffer, sizeof(mp3buffer));

        rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
        fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */
    }

    mp3bytes = lame_encode_flush(gf, /* may return one or more mp3 frame */
                                 mp3buffer, sizeof(mp3buffer));
    rtp_output(mp3buffer, mp3bytes); /* write MP3 output to RTP port */
    fwrite(mp3buffer, 1, mp3bytes, outf); /* write the MP3 output to file */

    lame_mp3_tags_fid(gf, outf); /* add VBR tags to mp3 file */

    lame_close(gf);
    fclose(outf);
    close_infile();     /* close the sound input file */
    frontend_close_console();
    return 0;
}
Ejemplo n.º 6
0
/*
######################################################################
# defaults
*/
int main(int argc, char **argv) {
int c;
int n;
int hasoffset = 0;

port = PORT;
strncpy(pass,DEFPASS,sizeof(pass));
host[0] = '\0';
resp[0] = resp[1] = 0;
unknown[0] = unknown[1] = 1;
strncpy(mesg,"Data retrieved OK",sizeof(mesg));

/* process arguments */
static struct option options[] = {
	{ "host", 1, 0, 'H' },
	{ "port", 1, 0, 'p' },
	{ "offset", 1, 0, 'o' },
	{ "module", 1, 0, 'c' },
	{ "command", 1, 0, 'c' },
	{ "cmd", 1, 0, 'c' },
	{ "arg", 1, 0, 'a' },
	{ "debug", 0, 0, 'd' },
	{ "timeout", 1, 0, 't' },
	{ "ratio", 0, 0, 'r' },
	{ "password", 1, 0, 'P' },
	{ "compat", 0, 0, 'C' }
};
while(1) {
	c = getopt_long(argc,argv,"CP:H:s:p:o:n:c:v:l:a:dt:rh",options,NULL);
	if(c == -1) break;
	switch(c) {
		case 'H':
		case 's':
			strncpy(host,optarg,sizeof(host)-1); break;
		case 'p':
			port = atoi(optarg); break;
		case 'P':
			strncpy(pass,optarg,sizeof(pass)-1); break;
		case 'o': /* offset */
		case 'n':
			n = 0; if(hasoffset || (cmd[1]>-1) || arg[1]) { n = 1; }
			offset[n] = atoi(optarg);
			hasoffset = 1;
			break;
		case 'c': /* command */
		case 'v':
			n = 0; if(cmd[n]>-1) { n = 1; }
			if(cmd[n]>-1) {
				sprintf(mesg,"You may only specify two commands.");
				outputresp();
				exit(1);
			}
			cmd[n] = getcmd(optarg);
			if(cmd[n]<0) {
				sprintf(mesg,"Invalid command [%s]",optarg);
				outputresp();
				exit(1);
			}
			break;
		case 'l': /* arg */
		case 'a':
			n = 0; if((cmd[1]>-1) || arg[0]) { n = 1; }
			arg[n] = optarg;
			break;
		case 'd':
			debugmode = 1; break;
		case 't':
			timeout = atoi(optarg);
			if(timeout < 1) { timeout = TIMEOUT; }	
			break;
		case 'r':
			ratiomode = 1; break;
		case 'h':
			dohelp(); exit(1);
		case 'C':
			compatmode = 1; break;
		default:
			sprintf(mesg,"Option was not recognised..."); 
			outputresp();
			exit(1);
	} /* switch */
} /* while loop */

if( !port || !host[0] ) {
	sprintf(mesg,"Must specify a valid port and hostname");
	outputresp();
	exit( 1 );
}

/* we need to run a second command only if the args have changed */
if((cmd[1]<0) && !arg[1] && !offset[1]) { offset[1]=1; } /* ? */
if((cmd[1]<0) && arg[1]) { cmd[1] = cmd[0]; }

if(cmd[0]<0) {
	sprintf(mesg,"No command was given.");
	outputresp(); exit(1);
}
/*
# Now we have one or two command to pass to the agent.
# We connect, and send, then listen for the response.
# Repeat for second argument if necessary
*/
#ifdef WITHALARM
/* timeout for program */
signal(SIGALRM,handler);
if(debugmode) { printf("Starting alarm for %d sec\n",timeout); fflush(NULL); }
alarm(timeout);
#endif

/* first, identify remote agent if necessary */
if((cmd[0]==8)||(cmd[1]==8)) {
    if(debugmode) { printf("Testing version\n"); fflush(NULL); }
    makesocket();
    verstr = (char *)malloc(256);
    n = ask(1,(char *)0,(double *)0,0,0,0,verstr);
    close(sock);
    if(! strcasestr(verstr,"nsclient++") ) {
    	if(debugmode) { printf("Setting compat mode\n"); fflush(NULL); }
	compatmode = 1;
    }
}

/* Connect */
if(debugmode) { printf("Starting queries\n"); fflush(NULL); }
makesocket();
n = ask(cmd[0],arg[0],&resp[0],offset[0],ratiomode,0,NULL);
close(sock);
if(n) { outputresp(); exit(0); }
else { unknown[0] = 0; }
if(cmd[1]>-1) {
	makesocket();
	n = ask(cmd[1],arg[1],&resp[1],offset[1],ratiomode,0,NULL);
	close(sock);
	if(n) { outputresp(); exit(0); }
	else { unknown[1] = 0; }
} else {
	makesocket();
	n = ask(-1,(char *)NULL,&resp[1],offset[1],ratiomode,1,NULL);
	close(sock);
	if(!n) { unknown[1] = 0; }
}
#ifdef WITHALARM
alarm(0);
#endif

sprintf(mesg,"Nagios query agent version %s",VERSION);

outputresp();
exit(0);
}