3 #include <sys/socket.h>
8 #include <netinet/in.h>
16 #include "../flight_gear.h"
23 struct sockaddr_in addr;
25 unsigned int initial_time;
26 unsigned int time_offset;
52 void nps_flightgear_init(
const char *host,
unsigned int port,
unsigned int port_in,
unsigned int time_offset)
55 struct protoent *pte = getprotobyname(
"UDP");
56 flightgear.socket = socket(PF_INET, SOCK_DGRAM, pte->p_proto);
58 perror(
"nps_flightgear_init flightgear.socket socket()");
61 if ( setsockopt(
flightgear.socket, SOL_SOCKET, SO_REUSEADDR,
62 &so_reuseaddr,
sizeof(so_reuseaddr)) == -1) {
63 perror(
"nps_flightgear_init flightgear.socket setsockopt()");
69 flightgear.addr.sin_addr.s_addr = inet_addr(host);
74 struct sockaddr_in addr_in;
75 flightgear.socket_in = socket(PF_INET, SOCK_DGRAM, pte->p_proto);
77 perror(
"nps_flightgear_init flightgear.socket_in socket()");
80 if ( setsockopt(
flightgear.socket_in, SOL_SOCKET, SO_REUSEADDR,
81 &so_reuseaddr,
sizeof(so_reuseaddr)) == -1) {
82 perror(
"nps_flightgear_init flightgear.socket_in setsockopt()");
85 addr_in.sin_family = PF_INET;
86 addr_in.sin_port = htons(port_in);
87 addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
89 if (bind(
flightgear.socket_in, (
struct sockaddr*)&addr_in,
sizeof(addr_in)) == -1) {
90 perror(
"nps_flightgear_init bind()");
94 printf(
"Bount to port %u to receive from\n", port_in);
102 time_t t = time(NULL);
119 memset(&fgfdm, 0,
sizeof(fgfdm));
165 if (sendto(
flightgear.socket, (
char *)(&fgfdm),
sizeof(fgfdm), 0,
167 fprintf(stderr,
"error sending to FlightGear\n");
219 if (sendto(
flightgear.socket, (
char *)(&gui),
sizeof(gui), 0,
221 fprintf(stderr,
"error sending to FlightGear\n");
238 size_t s_env =
sizeof(env);
244 memset(&env, 0, s_env);
245 bytes_read = recvfrom(
flightgear.socket_in, (
char*)(&env), s_env, MSG_WAITALL, NULL, NULL);
246 while (bytes_read != -1) {
247 if (bytes_read == (
int)s_env){
258 printf(
"WARNING : ignoring packet with size %d (%d expected)", bytes_read, (
int)s_env);
262 memset(&env, 0, s_env);
263 bytes_read = recvfrom(
flightgear.socket_in, (
char*)(&env), s_env, MSG_WAITALL, NULL, NULL);
266 if ((errno & (EAGAIN | EWOULDBLOCK)) == 0) {
267 perror(
"nps_flightgear_receive recvfrom()");