Exemple #1
0
MetaServerPacket::MetaServerPacket(const std::array<char,MAX_PACKET_BYTES>& pl, std::size_t bytes )
		: m_packetType(NMT_NULL),
		  m_AddressInt(0),
		  m_Port(0),
		  m_Bytes(bytes),
		  m_packetPayload(pl),
		  m_needFree(false),
		  m_outBound(false),
		  m_Sequence(0),
		  m_TimeOffset(0)
{
		m_readPtr  = m_packetPayload.data();
		m_headPtr  = m_packetPayload.data();
		m_writePtr = m_packetPayload.data();

		if ( bytes > 0 )
		{
			// if we have data ... parse out type
			parsePacketType();
		}
		else
		{
			// otherwise assume a construction and zero it out
			m_packetPayload.fill(0);
		}
}
Exemple #2
0
int main(int argc, char **argv) {
	char* port = NULL;
	int index;
	int c;
	packet_type type = INVALID_PACKET_TYPE;
	int retstatus = -1;

	// Turn off default error handling for getopt, return '?' instead
	opterr = 0;

	if (argc == 1) {
		fprintf(stderr, "Must specify port and packet type.\n");
		return 1;
	}

	while ((c = getopt(argc, argv, "p:t:")) != -1) {
		switch (c) {
			case 'p':
				if (parsePortNumber(LOWER_IP, UPPER_IP, optarg) == NULL) {
					fprintf(stderr, "Invalid port %s. Must use ephemeral or dynamic port within range of 49152-65535 so as to preven stepping on registered ports.\n", optarg);
					return 1;
				} else {
					port = optarg;
				}
				break;
			case 't':
				//Value ucp or tcp
				type = parsePacketType(optarg);
				break;
			case '?':
				if (optopt == 't') {
					fprintf(stderr, "Option -%c requires an argument. (tcp | udp)\n", optopt);
				} else if (optopt == 'p') {
					fprintf(stderr, "Option -%c requires an argument (-p <port number from 49152-65535>)\n", optopt);
				} else if (isprint(optopt)) {
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				} else {
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				}
				return 1;
			default:
				abort();
		}
	}

	for (index = optind; index < argc; index++) {
		printf("Non-option argument %s, ignoring.\n", argv[index]);
	}

	if (type == UDP) {
		retstatus = start_udp_server(port);
	} else if (type == TCP) {
		retstatus = start_tcp_server(port);
	} else {
		retstatus = 2;
		fprintf(stderr, "Invalid type, must be tcp or udp.");
	}

	return retstatus;
}
Exemple #3
0
void
MetaServerPacket::setPacketType(const NetMsgType& nmt)
{

	/**
	 *
	 */
	m_packetPayload.fill(0);
	m_readPtr = m_packetPayload.data();
	m_writePtr = m_packetPayload.data();
	m_Bytes = 0;

	// write must occur prior to read
	m_writePtr = pack_uint32(nmt, m_writePtr);
	parsePacketType();

}
Exemple #4
0
int main(int argc, char **argv) {
	char* port = NULL;
	char* host = NULL;
	int index;
	int c;
	int test;
	unsigned int msg = 0;
	packet_type type = INVALID_PACKET_TYPE;


	opterr = 0;

	if (argc == 1) {
		fprintf(stderr, "Must specify port, packet type, server, and data\n");
		return 1;
	}

	while ((c = getopt(argc, argv, "p:t:x:s:")) != -1) {
		switch (c) {
			case 'p':
				if (parsePortNumber(LOWER_IP, UPPER_IP, optarg) == NULL) {
					fprintf(stderr, "Invalid port %s. Must use ephemeral or dynamic port within range of 49152-65535 so as to prevent stepping on registered ports.\n", optarg);
				} else {
					port = optarg;
				}
				break;
			case 't':
				//Value ucp or tcp
				type = parsePacketType(optarg);
				break;
			case 'x':
				// Data to send
				test = atoi(optarg);

				if ((*optarg != '0' && test == 0) ||
				    //(test == INT_MAX || test == INT_MIN) || Need to find these consts
				    (test < 0)) {

					fprintf(stderr, "Invalid data. Data must be a positive 32 bit integer.");
					return 1;
				}
				msg = (unsigned int)test;
				break;
			case 's':
				if (gethostbyname(optarg) == NULL) {
					fprintf(stderr, "Invalid hostname.\n");
					return 2;
				}
				host = optarg;
				break;
			case '?':
				if (optopt == 't') {
					fprintf(stderr, "Option -%c requires an argument. (tcp | udp)\n", optopt);
				} else if (optopt == 'p') {
					fprintf(stderr, "Option -%c requires an argument (-p <port number from 1-65535>)\n", optopt);
				} else if (optopt == 's') {
					fprintf(stderr, "Option -%c requires an argument (-s <valid hostname>)\n", optopt);
				} else if (optopt == 'x') {
					fprintf(stderr, "Option -%c requires an argument (-x <some positive 32 bit int>)\n", optopt);
				} else if (isprint(optopt)) {
					fprintf(stderr, "Unknown option `-%c'.\n", optopt);
				} else {
					fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
				}
				return 1;
			default:
				abort();
		}
	}

	for (index = optind; index < argc; index++) {
		printf("Non-option argument %s, ignoring.\n", argv[index]);
	}

	if (type == UDP) {
		start_udp_client(host, port, msg);
	} else if (type == TCP) {
		start_tcp_client(host, port, msg);
	} else {
		fprintf(stderr, "Invalid packet type, must be tcp or udp.");
	}
	return 0;
}