Ejemplo n.º 1
0
        void send_(
          Transport & trans, CryptContext & crypt_context, int encryptionLevel
        , uint16_t userId, uint16_t channelId, uint32_t length, uint32_t flags
        , const uint8_t * chunk, size_t chunk_size) {
            write_packets(
                trans,
                [&](StreamSize<65536-1024>, OutStream & stream) {
                    stream.out_uint32_le(length);
                    stream.out_uint32_le(flags);
                    stream.out_copy_bytes(chunk, chunk_size);

                    if (enable_verbose && (((this->verbose & 128) != 0) || ((this->verbose & 16) != 0))) {
                        LOG(LOG_INFO, "Sec clear payload to send (channelId=%d):", channelId);
                        hexdump_d(stream.get_data(), stream.get_offset());
                    }
                },
                [&](StreamSize<256>, OutStream & sec_header, uint8_t * packet_data, std::size_t packet_size) {
                    SEC::Sec_Send(sec_header, packet_data, packet_size, 0, crypt_context, encryptionLevel);

                },
                [&](StreamSize<256>, OutStream & mcs_header, std::size_t packet_size) {
                    MCS_SendData(static_cast<OutPerStream&>(mcs_header), userId, channelId, 1, 3, packet_size, MCS::PER_ENCODING);
                },
                [&](StreamSize<256>, OutStream & x224_header, std::size_t packet_size) {
                    X224::DT_TPDU_Send(x224_header, packet_size);
                }
            );
        }
Ejemplo n.º 2
0
    void emit(OutTransport trans) /* TODO const*/ {
        this->buffer_stream.set_out_uint8(this->area_count,
                                          this->offset_area_count);

        this->sdata.emit_end();

        write_packets(
            trans,
            [this](StreamSize<65536+256>, OutStream & sctrl_header) {
                ShareControl_Send(sctrl_header,
                                  PDUTYPE_DATAPDU,
                                  this->userId + GCC::MCS_USERCHANNEL_BASE,
                                  this->buffer_stream.get_offset());

                sctrl_header.out_copy_bytes(this->buffer_stream.get_data(),
                                            this->buffer_stream.get_offset());
            },
            [this](StreamSize<256>, OutStream & sec_header, uint8_t * pkt_data, std::size_t pkt_size) {
                SEC::Sec_Send sec(sec_header,
                                  pkt_data,
                                  pkt_size,
                                  0,
                                  this->encrypt,
                                  this->encryptionLevel);
                (void)sec;
            },
            [this](StreamSize<256>, OutStream & mcs_header, std::size_t pkt_size) {
                MCS::SendDataRequest_Send mcs(static_cast<OutPerStream&>(mcs_header),
                                              this->userId,
                                              GCC::MCS_GLOBAL_CHANNEL,
                                              1,
                                              3,
                                              pkt_size,
                                              MCS::PER_ENCODING);
                (void)mcs;
            },
            [this](StreamSize<256>, OutStream &x224_header, std::size_t pkt_size) {
                X224::DT_TPDU_Send(x224_header, pkt_size);
            }
        );
    }
Ejemplo n.º 3
0
int
main (int argc, char **argv)
{
	struct sockaddr_in sin;
	int udp_socket;

	int dmx[argc - 3];
	int dvr;

	int i,j;

	int argp = 1, retries=0;

	#ifdef UDPSTREAMTS_DEBUG
	int min_read = READ_BUFFER_SIZE+1, max_read = -1;
	#endif

	char readbuf[READ_BUFFER_SIZE];
	char writebuf[UDP_PACKET_SIZE];
	ssize_t rsize = 0;
	ssize_t wsize = 0;

	#ifdef UDPSTREAMTS_DEBUG
		printf("udpstreamts (compiled in debug mode) starting... \n");
		printf("Size of readbuffer: %d bytes\n", READ_BUFFER_SIZE);
		printf("Size of writebuffer: %d bytes\n", UDP_PACKET_SIZE);
		printf("\n");
	#else
		printf("udpstreamts starting... \n");
		printf("\n");		
	#endif

	if (argc < 4) {
		/*
		 * Main screen turn on
		 */
		fprintf(stderr, "usage: %s [-b <logfile>] <ip> <port> <pid> [<pid> ...]\n", argv[0]);
		return EXIT_FAILURE;
	}

	/* inserted A.Wacker 19.07.2007 for starting udpstreamts in background */
	if (!strncmp(argv[1],"-b",2)) {
        	printf("Daemonizing %s and logging to %s...\n", argv[0], argv[2]);
		fflush(stdout);
        	
		/* daemonize */
		j=fork();
		if (j<0) exit(1); /* fork error */
		if (j>0) exit(0); /* parent exits */
		/* child daemon continous */
		
		setsid(); /* obtain a new process group */

		for (j=getdtablesize();j>=0;--j) close(j); /* close all descriptors */

		j = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR); /*open stdin */
		dup(j); /* stdout */
		dup(j); /* stderr */

		if (j == -1)
        	{
			perror("Cannot open logfile for writing");
        	}
		
		chdir ("/");

		signal(SIGCHLD,SIG_IGN); /* ignore child terminate signal */
		signal(SIGTERM,sig_handler); /* termination signal from kill */
		
		
		/* check again if there are enough arguments */
		if (argc < 6) {
			fprintf(stderr, "usage: %s [-b <logfile>] <ip> <port> <pid> [<pid> ...]\n", argv[0]);
		return EXIT_FAILURE;
	}

		/* finally shift the remaining arguments, so that the rest of the code remains untouched */
		argp = 3;
	}
	printf("Preparing to send the transport stream to %s:%s ...\n",argv[argp],argv[argp+1]);
    	/* end changes 19.07.2007 */


	/*
	 * It's You!!
	 */
	if ((udp_socket = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket");
		return EXIT_FAILURE;
	}

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inet_addr(argv[argp]);
	sin.sin_port = htons(strtoul(argv[argp+1], NULL, 0));

	/*
	 * How are you gentlemen!!
	 */
	if (connect(udp_socket, (struct sockaddr *) &sin, sizeof(struct sockaddr)) < 0) {
		perror("connect");
		return EXIT_FAILURE;
	}

	if ((dvr = open(DVR, O_RDONLY)) < 0) {
		perror(DVR);
		return EXIT_FAILURE;
	}

	/*
	 * All your base are belong to us
	 */
	for (i = 0; i < (argc - (argp+2)); i++)
	{
		printf("Preparing demuxer for streaming PID %i: %s ...\n",i ,argv[i + (argp + 2)]);
		dmx[i] = dmx_pid_filter_start(strtoul(argv[i + (argp + 2)], NULL, 0));
	}
	fflush(stdout);

	rsize = read(dvr, readbuf, TS_PACKET_SIZE);

	if (rsize == -1) {
		/*
		 * You are on the way to destruction
		 */
		perror("Error reading from dvr");
	}

	else {
		int read_offset = sync_byte_offset(readbuf, TS_PACKET_SIZE);
		int size_offset = 0;

		/*
		 * What you say!!
		 */
		if (read_offset == -1)
			fprintf(stderr, "%s: not a valid ts\n", __FILE__);

		else do {
			/*
			 * read as much bytes as needed to fill the buffer
			 * as much as possible and to end with a complete
			 * transport stream packet
			 */
			rsize = read(dvr, readbuf + read_offset, sizeof(readbuf) - read_offset - size_offset);


			/*
			* retry to read
			*/
			retries = 0;
			while((rsize == -1) && (retries < MAX_RETRIES) && (errno==EOVERFLOW)){
				retries++;
				#ifdef UDPSTREAMTS_DEBUG
				fprintf(stderr,"Error reading from dvr, still trying..(%i/%i)",retries,MAX_RETRIES);
				perror("; Reason");
				#endif
				rsize = read(dvr, readbuf + read_offset, sizeof(readbuf) - read_offset - size_offset);
			}


			if (rsize == -1) {
				perror("Error reading from dvr. Giving up");
				break;
			}

			else if ((rsize + read_offset) % TS_PACKET_SIZE) {
				/*
				 * You have no chance to survive make your time
				 * HA HA HA HA ...
				 */
				fprintf(stderr, "%s: bad ts packet size %d, r_off %d, s_off %d\n", __FILE__, (rsize + read_offset) % TS_PACKET_SIZE, read_offset, size_offset);
				exit(EXIT_FAILURE);
			}

			/*
			 * Take off every 'zig'
			 */
			read_offset = sync_byte_offset(readbuf, rsize - TS_PACKET_SIZE);


			#ifdef UDPSTREAMTS_DEBUG
			/* just for debugging*/
			if(rsize>max_read)
			{
				max_read = rsize;
				printf("New max read size: %d\n",max_read);
			}
			if(rsize<min_read)
			{
				min_read = rsize;
				printf("New min read size: %d\n",min_read);
			}
			if ((rsize!=max_read) && (rsize!=min_read))
			{
				printf("Current read size: %d\n",rsize);
			}
			#endif


			if (!read_offset) {
				/*
				 * All your TS are belong to us
				 */
				wsize = write_packets(udp_socket, readbuf, rsize, writebuf, wsize);
				size_offset = 0;

			}

			else if (read_offset == -1) {
				fprintf(stderr, "%s: sync now absolutely f****d up\n", __FILE__);
				/*
				 * You know what you doing
				 */
				read_offset = size_offset = 0;
			}

			else {
				fprintf(stderr, "%s: ts out of sync: r_offs %d, len: %d\n", __FILE__, read_offset, rsize);
				/*
				 * Move 'zig'
				 */
				/* FIXME size_offset = ((rsize - read_offset) % TS_PACKET_SIZE); */
				rsize -= read_offset + ((rsize - read_offset) % TS_PACKET_SIZE);

				if (rsize % TS_PACKET_SIZE) {
					/*
					 * Somebody set up us the bomb
					 */
					fprintf(stderr, "%s:%s: Internal Error!\n", __FILE__, __FUNCTION__);
					exit(EXIT_FAILURE);
				}

				wsize = write_packets(udp_socket, readbuf + read_offset, rsize, writebuf, wsize);
				read_offset = 0;
			}

		} while (wsize >= 0 && !exiting);
	}

	printf("Prepare for shutdown ");

	for (i = 0; i < (argc - (argp+2)); i++)
	{
		fprintf(stdout,".");
		dmx_pid_filter_stop(dmx[i]);
	}

	close(dvr);

	/* just to be sure, that everything is closed */
	for (j=getdtablesize();j>=3;--j) close(j); /* close all descriptors, except std dscr. */

	printf(" done. Goodby!\n");

	/*
	 * For great justice
	 */
	return EXIT_SUCCESS;
}