Fixed network-to-host byte order problems when storing ports.

This commit is contained in:
Philip Rosedale 2013-02-12 10:58:30 -08:00
parent b19fce1b46
commit 6f9d223454
3 changed files with 11 additions and 12 deletions

View file

@ -186,9 +186,9 @@ int main(int argc, const char * argv[])
//<< " " << packet_data << "\n";
float x,y,z;
sscanf(packet_data, "%f,%f,%f", &x, &y, &z);
if (addAgent(dest_address.sin_addr.s_addr, dest_address.sin_port, x, y, z)) {
if (addAgent(dest_address.sin_addr.s_addr, ntohs(dest_address.sin_port), x, y, z)) {
std::cout << "Added Agent from " <<
inet_ntoa(dest_address.sin_addr) << ":" << dest_address.sin_port << "\n";
inet_ntoa(dest_address.sin_addr) << ":" << ntohs(dest_address.sin_port) << "\n";
}
// Reply with packet listing nearby active agents
send_agent_list(handle, &dest_address);

View file

@ -84,15 +84,15 @@ int add_agent(char * address, unsigned short port) {
//std::cout << "Checking for " << IP->c_str() << " ";
for (int i = 0; i < num_agents; i++) {
if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) {
//std::cout << "Found agent!\n";
std::cout << "Found agent!\n";
return 0;
}
}
if (num_agents < MAX_AGENTS) {
strcpy(agents[num_agents].address, address);
agents[num_agents].port = port;
//std::cout << "Added Agent # " << num_agents << " with Address " <<
//agents[num_agents].address << ":" << agents[num_agents].port << "\n";
std::cout << "Added Agent # " << num_agents << " with Address " <<
agents[num_agents].address << ":" << agents[num_agents].port << "\n";
num_agents++;
return 1;
} else {
@ -113,10 +113,10 @@ int broadcast_to_agents(UDPSocket *handle, char * data, int length) {
// STUPID HACK: For some reason on OSX with NAT translation packets sent to localhost are
// received as from the NAT translated port but have to be sent to the local port number.
//
if (strcmp("127.0.0.1",agents[i].address) == 0)
sent_bytes = handle->send(agents[i].address, 40103, data, length);
else
sent_bytes = handle->send(agents[i].address, agents[i].port, data, length);
//if (1) //(strcmp("192.168.1.53",agents[i].address) == 0)
// sent_bytes = handle->send(agents[i].address, 40103, data, length);
//else
sent_bytes = handle->send(agents[i].address, agents[i].port, data, length);
if (sent_bytes != length) {
std::cout << "Broadcast packet fail!\n";

View file

@ -56,7 +56,7 @@ int simulate_on = 1;
const int MAX_PACKET_SIZE = 1500;
const int AGENT_UDP_PORT = 40103;
char DOMAINSERVER_IP[] = "127.0.0.1";
char DOMAINSERVER_IP[] = "192.168.1.53";
const int DOMAINSERVER_PORT = 40102;
UDPSocket agentSocket(AGENT_UDP_PORT);
@ -797,8 +797,7 @@ void read_network()
//
// Broadcast packet from another agent
//
//printf("Got broadcast!\n");
update_agent(inet_ntoa(senderAddress.sin_addr), senderAddress.sin_port, &incoming_packet[1], bytes_recvd - 1);
update_agent(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), &incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'T') {
printf("Got test! From port %d\n", senderAddress.sin_port);
}