void process_server_info(struct sockaddr_in *sockaddr, struct buffer_t *buffer) { struct queried_server_t *cserver = queried_servers; struct found_server_t new_server_info; uint16_t servers; struct sockaddr_in s; time_t t; int proto_ver; while(cserver) { if(cserver->ip == sockaddr->sin_addr.s_addr && cserver->port == sockaddr->sin_port) { proto_ver = buffer_read_uint8(buffer); if(proto_ver != EM_PROTO_VER) { if(proto_ver > EM_PROTO_VER) num_servers_new_proto++; break; } new_server_info.ip = sockaddr->sin_addr.s_addr; new_server_info.port = sockaddr->sin_port; new_server_info.ping = get_wall_time() - cserver->stamp; new_server_info.host_name = buffer_read_string(buffer); new_server_info.map_name = buffer_read_string(buffer); new_server_info.num_players = buffer_read_uint8(buffer); new_server_info.max_players = buffer_read_uint8(buffer); new_server_info.authenticating = buffer_read_uint8(buffer); add_new_found_server(&new_server_info); add_new_server(&rumoured_servers, sockaddr, time(NULL)); servers = buffer_read_uint16(buffer); while(servers--) { s.sin_addr.s_addr = buffer_read_uint32(buffer); s.sin_port = buffer_read_uint16(buffer); t = buffer_read_uint32(buffer); if(!find_queried_server(s.sin_addr.s_addr, s.sin_port)) { if(add_new_server(&unqueried_servers, &s, t)) num_servers_unqueried++; } } break; } LL_NEXT(cserver); } }
bool unabto_buffer_test(void) { char * raw_test_string = "asjdhc#21?(!?(92814skzjbcasa"; uint8_t data[7 + 2 + strlen(raw_test_string)]; unabto_buffer buf, raw_string_buf; buffer_write_t w_buf; buffer_read_t r_buf; uint32_t t32; uint16_t t16; uint8_t t8; uint8_t raw_string_data[strlen(raw_test_string) + 1]; buffer_init(&raw_string_buf, (uint8_t*)raw_test_string, strlen(raw_test_string)); buffer_init(&buf, data, sizeof(data)); buffer_write_init(&w_buf, &buf); if (! (buffer_write_uint32(&w_buf, 0x12345678) && buffer_write_uint16(&w_buf, 0x1234) && buffer_write_uint8(&w_buf, 0x12) && buffer_write_raw(&w_buf, &raw_string_buf) ) ) { NABTO_LOG_ERROR(("Buffer write test failed")); return false; } buffer_read_init(&r_buf, &buf); memset(raw_string_data, 0, sizeof(raw_string_data) + 1); buffer_init(&raw_string_buf, raw_string_data, sizeof(raw_string_data)); bool t = false; if (! ( buffer_read_uint32(&r_buf, &t32) && t32 == 0x12345678 && buffer_read_uint16(&r_buf, &t16) && t16 == 0x1234 && buffer_read_uint8(&r_buf, &t8) && t8 == 0x12 && (t = buffer_read_raw(&r_buf, &raw_string_buf)) && buffer_get_size(&raw_string_buf) == strlen(raw_test_string) && 0 == strncmp((char*)buffer_get_data(&raw_string_buf), raw_test_string, strlen(raw_test_string)) )) { NABTO_LOG_ERROR(("Failed read test failed")); return false; } if (buffer_read_uint32(&r_buf, &t32) || buffer_read_uint16(&r_buf, &t16) || buffer_read_uint8(&r_buf, &t8) || buffer_read_raw(&w_buf, &raw_string_buf) || buffer_write_uint32(&w_buf, 0x12345678) || buffer_write_uint16(&w_buf, 0x1234) || buffer_write_uint8(&w_buf, 0x12) || buffer_write_raw(&w_buf, &raw_string_buf) ) { NABTO_LOG_ERROR(("Some function should have returned false but returned true")); return false; } return true; }
application_event_result application_event(application_request* request, buffer_read_t* read_buffer, buffer_write_t* write_buffer) { switch(request->queryId) { case 1: { // <query name="light_write.json" description="Turn light on and off" id="1"> // <request> // <parameter name="light_id" type="uint8"/> // <parameter name="light_on" type="uint8"/> // </request> // <response> // <parameter name="light_state" type="uint8"/> // </response> // </query> uint8_t light_id; uint8_t light_on; uint8_t light_state; // Read parameters in request if (!buffer_read_uint8(read_buffer, &light_id)) { NABTO_LOG_ERROR(("Can't read light_id, the buffer is too small")); return AER_REQ_TOO_SMALL; } if (!buffer_read_uint8(read_buffer, &light_on)) { NABTO_LOG_ERROR(("Can't read light_state, the buffer is too small")); return AER_REQ_TOO_SMALL; } // Set light according to request light_state = setLight(light_id, light_on); // Write back led state if (!buffer_write_uint8(write_buffer, light_state)) { return AER_REQ_RSP_TOO_LARGE; } return AER_REQ_RESPONSE_READY; } case 2: { // <query name="light_read.json" description="Read light status" id="2"> // <request> // <parameter name="light_id" type="uint8"/> // </request> // <response> // <parameter name="light_state" type="uint8"/> // </response> // </query> uint8_t light_id; uint8_t light_state; // Read parameters in request if (!buffer_read_uint8(read_buffer, &light_id)) return AER_REQ_TOO_SMALL; // Read light state light_state = readLight(light_id); // Write back led state if (!buffer_write_uint8(write_buffer, light_state)) return AER_REQ_RSP_TOO_LARGE; return AER_REQ_RESPONSE_READY; } } return AER_REQ_INV_QUERY_ID; }