Compare commits
4 Commits
d82cf253bd
...
ace99dfe93
Author | SHA1 | Date | |
---|---|---|---|
ace99dfe93 | |||
6c04cdf97f | |||
56bb116ab9 | |||
0104e542e1 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
bin/
|
9
Makefile
9
Makefile
@ -1,9 +1,12 @@
|
||||
BIN=bin
|
||||
|
||||
all: dirs command
|
||||
all: dirs command readref
|
||||
|
||||
command: command.c
|
||||
gcc -o ${BIN}/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:
|
||||
|
71
command.c
71
command.c
@ -2,52 +2,10 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h> // BSD compatible
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#define SERVER "::1"
|
||||
#define PORT 49000
|
||||
|
||||
#define COMMAND_BUFFER 256
|
||||
|
||||
char* printable(char *str) {
|
||||
char *ret = strdup(str);
|
||||
|
||||
for (int i = strlen(ret); i >= 0; i--) {
|
||||
if (ret[i] == '\r' || ret[i] == '\n') {
|
||||
ret[i] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void sendCommand(int sock, char *cmd) {
|
||||
char bytesToSend[COMMAND_BUFFER];
|
||||
memset(bytesToSend, 0, COMMAND_BUFFER);
|
||||
|
||||
strcpy(&bytesToSend[0], "CMND");
|
||||
strcpy(&bytesToSend[5], cmd);
|
||||
|
||||
int numBytesSend = send(sock, bytesToSend, 5 + strlen(cmd), 0);
|
||||
if (numBytesSend >= 0) {
|
||||
//printf("Send %d bytes to %s:%d: %s\n", numBytesSend, SERVER, PORT, printable(bytesToSend));
|
||||
} else {
|
||||
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
#include "network.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int xplaneSocket;
|
||||
struct sockaddr_in6 xplaneAddress;
|
||||
|
||||
int err;
|
||||
int xPlaneSocket;
|
||||
|
||||
// check argument
|
||||
if (argc != 2) {
|
||||
@ -55,29 +13,18 @@ int main(int argc, char *argv[]) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// create socket
|
||||
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if (xplaneSocket == -1) {
|
||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// prepare address struct
|
||||
xplaneAddress.sin6_family = AF_INET6;
|
||||
xplaneAddress.sin6_port = htons(PORT);
|
||||
inet_pton(AF_INET6, SERVER, &xplaneAddress.sin6_addr.s6_addr);
|
||||
|
||||
// connect to xplane
|
||||
err = connect(xplaneSocket, (struct sockaddr *)&xplaneAddress, sizeof(xplaneAddress));
|
||||
if (err) {
|
||||
fprintf(stderr, "Error connecting %s:%d (%d): %s\n", SERVER, PORT, err, gai_strerror(err));
|
||||
// network
|
||||
xPlaneSocket = createSocket(SRC_PORT);
|
||||
if (xPlaneSocket < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// send payload
|
||||
sendCommand(xplaneSocket, argv[1]);
|
||||
if (sendToXPlane(xPlaneSocket, DEST_SERVER, DEST_PORT, "CMND", argv[1], strlen(argv[1])) < 0) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||
close(xplaneSocket);
|
||||
closeSocket(xPlaneSocket);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
100
network.c
Normal file
100
network.c
Normal file
@ -0,0 +1,100 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h> // BSD compatible
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include "network.h"
|
||||
|
||||
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 *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload) {
|
||||
char bytesToSend[SEND_BUFFER];
|
||||
struct sockaddr_in6 xPlaneAddress;
|
||||
memset(bytesToSend, 0, SEND_BUFFER);
|
||||
|
||||
// msg is always 4 bytes long
|
||||
if (strlen(msg) != DGRAM_MSG_LENGTH) {
|
||||
return -10;
|
||||
}
|
||||
|
||||
// payload shouldn't exceed buffer
|
||||
if (lengthOfPayload > (SEND_BUFFER - DGRAM_MSG_LENGTH - DGRAM_NULL_LENGTH)) {
|
||||
return -11;
|
||||
}
|
||||
|
||||
strcpy(&bytesToSend[DGRAM_MSG_START], msg);
|
||||
memcpy(&bytesToSend[DGRAM_PAYLOAD_START], payload, lengthOfPayload);
|
||||
|
||||
// prepare destination address struct
|
||||
xPlaneAddress.sin6_family = AF_INET6;
|
||||
xPlaneAddress.sin6_port = htons(destPort);
|
||||
inet_pton(AF_INET6, destAddr, &xPlaneAddress.sin6_addr.s6_addr);
|
||||
|
||||
int numBytesSend = sendto(sock, bytesToSend, DGRAM_MSG_LENGTH + DGRAM_NULL_LENGTH + lengthOfPayload, 0, (struct sockaddr *)&xPlaneAddress, sizeof(xPlaneAddress));
|
||||
if (numBytesSend >= 0) {
|
||||
return numBytesSend;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to send message (%d): %s", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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 createSocket(int srcPort) {
|
||||
int xPlaneSocket;
|
||||
struct sockaddr_in6 localAddress;
|
||||
|
||||
int err;
|
||||
|
||||
// create socket
|
||||
xPlaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||
if (xPlaneSocket == -1) {
|
||||
fprintf(stderr, "Error creating socket (%d): %s\n", errno, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
// prepare source address struct
|
||||
localAddress.sin6_family = AF_INET6;
|
||||
localAddress.sin6_port = htons(srcPort);
|
||||
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;
|
||||
}
|
||||
|
||||
return xPlaneSocket;
|
||||
}
|
||||
|
||||
void closeSocket(int sock) {
|
||||
close(sock);
|
||||
}
|
17
network.h
Normal file
17
network.h
Normal file
@ -0,0 +1,17 @@
|
||||
#define DEST_SERVER "::1"
|
||||
#define DEST_PORT 49000
|
||||
#define SRC_PORT 49666
|
||||
|
||||
#define SEND_BUFFER 512
|
||||
#define RECV_BUFFER 256
|
||||
|
||||
#define DGRAM_MSG_LENGTH 4
|
||||
#define DGRAM_NULL_LENGTH 1
|
||||
|
||||
extern const int DGRAM_MSG_START;
|
||||
extern const int DGRAM_PAYLOAD_START;
|
||||
|
||||
int sendToXPlane(int sock, char *destAddr, int destPort, char *msg, void *payload, int lengthOfPayload);
|
||||
int recvFromXPlane(int sock, char msg[4], void *payload, int maxLength);
|
||||
int createSocket(int srcPort);
|
||||
void closeSocket(int sock);
|
Loading…
x
Reference in New Issue
Block a user