requesting and receiving rref
but currently the value is buggy
This commit is contained in:
parent
56bb116ab9
commit
6c04cdf97f
7
Makefile
7
Makefile
@ -1,10 +1,13 @@
|
||||
BIN=bin
|
||||
|
||||
all: dirs command
|
||||
all: dirs command readref
|
||||
|
||||
command: command.c
|
||||
command:
|
||||
gcc -o ${BIN}/command network.c command.c
|
||||
|
||||
readref:
|
||||
gcc -o ${BIN}/readref network.c readref.c
|
||||
|
||||
.PHONY: clean dirs
|
||||
clean:
|
||||
rm -rf ${BIN}
|
||||
|
@ -14,13 +14,13 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
// network
|
||||
xPlaneSocket = connectXPlane(SERVER, PORT);
|
||||
xPlaneSocket = connectXPlane(DEST_SERVER, DEST_PORT, SRC_PORT);
|
||||
if (xPlaneSocket < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// send payload
|
||||
if (sendToXPlane(xPlaneSocket, "CMND", argv[1]) < 0) {
|
||||
if (sendToXPlane(xPlaneSocket, "CMND", argv[1], strlen(argv[1])) < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
60
network.c
60
network.c
@ -15,7 +15,7 @@
|
||||
const int DGRAM_MSG_START = 0;
|
||||
const int DGRAM_PAYLOAD_START = DGRAM_MSG_START + DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH;
|
||||
|
||||
int sendToXPlane(int sock, char *msg, char *payload) {
|
||||
int sendToXPlane(int sock, char *msg, void *payload, int lengthOfPayload) {
|
||||
char bytesToSend[SEND_BUFFER];
|
||||
memset(bytesToSend, 0, SEND_BUFFER);
|
||||
|
||||
@ -25,14 +25,14 @@ int sendToXPlane(int sock, char *msg, char *payload) {
|
||||
}
|
||||
|
||||
// payload shouldn't exceed buffer
|
||||
if (strlen(payload) > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH)) {
|
||||
if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH)) {
|
||||
return -11;
|
||||
}
|
||||
|
||||
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
|
||||
strcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload);
|
||||
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
|
||||
|
||||
int numBytesSend = send(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + strlen(payload), 0);
|
||||
int numBytesSend = send(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0);
|
||||
if (numBytesSend >= 0) {
|
||||
return numBytesSend;
|
||||
} else {
|
||||
@ -41,9 +41,34 @@ int sendToXPlane(int sock, char *msg, char *payload) {
|
||||
}
|
||||
}
|
||||
|
||||
int connectXPlane(char *addr, int port) {
|
||||
int recvFromXPlane(int sock, char msg[DGRAM_MSG_LENGTH], void *payload, int maxLength) {
|
||||
char buf[maxLength + DGRAM_MSG_LENGTH];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
int recvBytes = recv(sock, buf, maxLength + DGRAM_MSG_LENGTH, 0);
|
||||
if (recvBytes >= 0) {
|
||||
printf("received (%d): ", recvBytes);
|
||||
for (int i = 0; i < recvBytes; i++) {
|
||||
printf("%02x ", buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
if (recvBytes >= DGRAM_MSG_LENGTH) {
|
||||
memcpy(msg, &buf[0], DGRAM_MSG_LENGTH);
|
||||
memcpy(payload, &buf[DGRAM_MSG_LENGTH], maxLength);
|
||||
return recvBytes - DGRAM_MSG_LENGTH;
|
||||
} else {
|
||||
fprintf(stderr, "Received data without message header?!");
|
||||
return -11;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Error receiving data (%d): %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int connectXPlane(char *destAddr, int destPort, int srcPort) {
|
||||
int xPlaneSocket;
|
||||
struct sockaddr_in6 xPlaneAddress;
|
||||
struct sockaddr_in6 xPlaneAddress, localAddress;
|
||||
|
||||
int err;
|
||||
|
||||
@ -54,15 +79,28 @@ int connectXPlane(char *addr, int port) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// prepare address struct
|
||||
// prepare source address struct
|
||||
localAddress.sin6_family = AF_INET6;
|
||||
localAddress.sin6_port = htons(srcPort);
|
||||
//inet_pton(AF_INET6, in6addr_any, &localAddress.sin6_addr.s6_addr);
|
||||
localAddress.sin6_addr = in6addr_any;
|
||||
|
||||
// bind to local port
|
||||
err = bind(xPlaneSocket, (struct sockaddr *)&localAddress, sizeof(localAddress));
|
||||
if (err) {
|
||||
fprintf(stderr, "Error binding [::]:%d (%d): %s\n", srcPort, errno, gai_strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// prepare destination address struct
|
||||
xPlaneAddress.sin6_family = AF_INET6;
|
||||
xPlaneAddress.sin6_port = htons(PORT);
|
||||
inet_pton(AF_INET6, SERVER, &xPlaneAddress.sin6_addr.s6_addr);
|
||||
xPlaneAddress.sin6_port = htons(destPort);
|
||||
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
|
||||
|
||||
// connect to xPlane
|
||||
err = connect(xPlaneSocket, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
||||
//err = connect(xPlaneSocket, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
||||
if (err) {
|
||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", addr, port, err, gai_strerror(err));
|
||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", destAddr, destPort, errno, gai_strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
13
network.h
13
network.h
@ -1,7 +1,9 @@
|
||||
#define SERVER "::1"
|
||||
#define PORT 49000
|
||||
#define DEST_SERVER "::1"
|
||||
#define DEST_PORT 49000
|
||||
#define SRC_PORT 49666
|
||||
|
||||
#define SEND_BUFFER 256
|
||||
#define SEND_BUFFER 512
|
||||
#define RECV_BUFFER 256
|
||||
|
||||
#define DGRAM_MSG_LENGTH 4
|
||||
#define DGRAM_NULL_LENGTH 1
|
||||
@ -9,6 +11,7 @@
|
||||
extern const int DGRAM_MSG_START;
|
||||
extern const int DGRAM_PAYLOAD_START;
|
||||
|
||||
int sendToXPlane(int sock, char *msg, char *payload);
|
||||
int connectXPlane(char *addr, int port);
|
||||
int sendToXPlane(int sock, char *msg, void *payload, int lengthOfPayload);
|
||||
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
|
||||
int connectXPlane(char *destAddr, int destPort, int srcPort);
|
||||
int disconnectXPlane(int sock);
|
||||
|
Loading…
x
Reference in New Issue
Block a user