split network code into extra file
This commit is contained in:
parent
d82cf253bd
commit
0104e542e1
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
bin/
|
2
Makefile
2
Makefile
@ -3,7 +3,7 @@ BIN=bin
|
|||||||
all: dirs command
|
all: dirs command
|
||||||
|
|
||||||
command: command.c
|
command: command.c
|
||||||
gcc -o ${BIN}/command command.c
|
gcc -o ${BIN}/command network.c command.c
|
||||||
|
|
||||||
.PHONY: clean dirs
|
.PHONY: clean dirs
|
||||||
clean:
|
clean:
|
||||||
|
71
command.c
71
command.c
@ -2,52 +2,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <sys/socket.h>
|
#include "network.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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int xplaneSocket;
|
int xPlaneSocket;
|
||||||
struct sockaddr_in6 xplaneAddress;
|
|
||||||
|
|
||||||
int err;
|
|
||||||
|
|
||||||
// check argument
|
// check argument
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
@ -55,29 +13,18 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// create socket
|
// network
|
||||||
xplaneSocket = socket(AF_INET6, SOCK_DGRAM, 0);
|
xPlaneSocket = connectXPlane(SERVER, PORT);
|
||||||
if (xplaneSocket == -1) {
|
if (xPlaneSocket < 0) {
|
||||||
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));
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// send payload
|
// send payload
|
||||||
sendCommand(xplaneSocket, argv[1]);
|
if (sendCommand(xPlaneSocket, argv[1]) < 0) {
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
close(xplaneSocket);
|
disconnectXPlane(xPlaneSocket);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
75
network.c
Normal file
75
network.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
#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"
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int 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));
|
||||||
|
return numBytesSend;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to send message to %s:%d (%d): %s", SERVER, PORT, errno, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int connectXPlane(char *addr, int port) {
|
||||||
|
int xPlaneSocket;
|
||||||
|
struct sockaddr_in6 xPlaneAddress;
|
||||||
|
|
||||||
|
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 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));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return xPlaneSocket;
|
||||||
|
}
|
||||||
|
|
||||||
|
int disconnectXPlane(int sock) {
|
||||||
|
// bye bye (and no, I don't care about error anymore; I'll exit anyway?!)
|
||||||
|
close(sock);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user