Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/communication.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdbool.h>

bool ping();
void connect(int port);
void sendcmd(const char* cmd, ...);
void ioctl(...)
85 changes: 85 additions & 0 deletions include/systemapicommunication/communication.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "../communication.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

static int sock = -1;

void connect(int port) {
struct sockaddr_in server;

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return;
}

server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr("127.0.0.1");

if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("connect");
close(sock);
sock = -1;
return;
}

printf("Connected to server on port %d\n", port);

}

void sendcmd(const char* cmd, ...) {
if (sock == -1) {
printf("Not connected to server\n");
return;
}

char buffer[4096];

va_list args;
va_start(args, cmd);
vsnprintf(buffer, sizeof(buffer), cmd, args);
va_end(args);

send(sock, buffer, strlen(buffer), 0);
}


void ioctl(...) {
sendcmd("ioctl", ...)
}

bool ping() {
sendcmd(
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"\r\n"
);

char buffer[4096];
ssize_t bytes = recv(sock, buffer, sizeof(buffer), 0);

if (bytes <= 0) {
printf("No response from server\n");
return 0;
}

buffer[bytes] = '\0';

if (strstr(buffer, "Pong") != NULL) {
if (output) {
printf("Server replied with Pong\n");
return 1;
}
} else {
if (output) {
printf("Unexpected response:\n%s\n", buffer);
return 0;
}
}
}
2 changes: 1 addition & 1 deletion input/gamepad.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "../libfinite/libfinite/include/communication.h" //replaces ioctl has the same stuff
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
Expand Down
2 changes: 1 addition & 1 deletion mailroom/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

int main() {
init_log(stdout, LOG_LEVEL_DEBUG, false);
FINITE_LOG("Test");
FINITE_LOG("Test");
}