Skip to content

Commit

Permalink
Added basic error handling for SIGINT.
Browse files Browse the repository at this point in the history
  • Loading branch information
JDongian committed Jan 8, 2015
1 parent ddc1f01 commit a235f6c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 13 deletions.
18 changes: 13 additions & 5 deletions sample/bot_runner.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include "sample_defender.h"
Expand All @@ -6,27 +8,33 @@
#include "protocol.h"
#include "bot.h"
#include "api.h"
#include "exceptions.h"

#define SERVER_NAME "localhost"
#define DEFAULT_SERVER_PORT 25565
#define NUM_BOTS 2

extern int errno;

int main(int argc, char *argv[], char **envp)
{
char *server_name = SERVER_NAME;
int server_port = DEFAULT_SERVER_PORT;
int server_port = DEFAULT_SERVER_PORT;\

signal(SIGINT, internal_error);

if (argc == 3) {
server_name = argv[1];
server_port = strtol(argv[2], NULL, 10);
if (!server_port) {
printf("Expected arguments: ./mcc [<SERVER> [<PORT>]]\n");
return 0;
errno = 1001;
raise(SIGINT);
}
} else if (argc == 2) {
server_name = argv[1];
} else if (argc > 3) {
printf("Expected arguments: ./mcc [<SERVER> [<PORT>]]\n");
return 0;
errno = 1000;
raise(SIGINT);
}

bot_t *bots[NUM_BOTS];
Expand Down
44 changes: 44 additions & 0 deletions sample/exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>

/*
* Acts as an exception handler. Probably hacky, but works well. By declaring
* the extern int errno and registering internal_error as the SIGINT signal
* handler, this function prints nice error messages and writes to a log
* before quitting.
*/

static void internal_error(int signal) {
printf("\nCaught error. Code: %d\n", errno);
switch errno {
case 0:
printf("Program terminated by user.\n");
exit(EXIT_SUCCESS);
case 1000:
printf("Invalid arguments.\n"
"Expected arguments: ./mcc [<SERVER> [<PORT>]]\n");
exit(EXIT_FAILURE);
break;
case 1001:
printf("Invalid port.\n"
"Expected arguments: ./mcc [<SERVER> [<PORT>]]\n");
exit(EXIT_FAILURE);
break;
case 3000:
printf("Got a non-positive packet\n");
exit(EXIT_FAILURE);
case 3001:
printf("Got packet of size 0.\n");
exit(EXIT_FAILURE);
case 3002:
printf("Packet len isn't right.\n");
exit(EXIT_FAILURE);
default:
printf("Undocumented error code :(\n");
exit(EXIT_FAILURE);
break;
}

}
5 changes: 4 additions & 1 deletion src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ bool next_int_token(int* value, char *string, char **saveptr)

void execute_command(bot_t *bot, char *command, char *strargs)
{
if (strcmp(command, "slot") == 0) {
if (strcmp(command, "help") == 0) {
send_play_serverbound_chat(bot, "Available commands:");
send_play_serverbound_chat(bot, "slot, ");
} else if (strcmp(command, "slot") == 0) {
char **saveptr = calloc(1, sizeof(char *));
bool valid_input = true;
char *token = NULL;
Expand Down
20 changes: 14 additions & 6 deletions src/bot.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
Expand All @@ -8,7 +10,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "bot.h"
#include "protocol.h"
#include "marshal.h"
Expand Down Expand Up @@ -195,17 +196,24 @@ int receive_packet(bot_t *bot)
memset(bot->_data->buf, 0, bot->_data->packet_threshold);
for (i = 0; i < 5; i++) {
ret = receive_raw(bot, bot->_data->buf + i, 1);
if (ret <= 0)
return -1;
if (ret <= 0) {
errno = 3000;
raise(SIGINT);
}
if (!expect_more(bot->_data->buf[i]))
break;
}

len = varint32(bot->_data->buf, &packet_size);
if (packet_size == 0)
return -2;
if (packet_size == 0) {
errno = 3001;
raise(SIGINT);
}

assert(i != len);
if (i == len) {
errno = 3002;
raise(SIGINT);
}

packet_size += len;
received = i + 1;
Expand Down
9 changes: 8 additions & 1 deletion src/protocol.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include "protocol.h"
#include "marshal.h"
#include "bot.h"
Expand All @@ -19,6 +21,8 @@
length = format_packet(BOT, &PACKET, (void *) &packet); \
send_raw(bot, packet, length); \

extern int errno;

/*
* Handshaking serverbound functions
*/
Expand Down Expand Up @@ -573,7 +577,10 @@ void *protocol_decode(bot_t *bot)
{
int32_t pid = receive_packet(bot);
if (pid < 0) {
if (pid == -1) exit(123);
if (pid == -1) {
errno = 2000;
raise(SIGINT);
}
return NULL;
}
void *recv_struct = NULL;
Expand Down

0 comments on commit a235f6c

Please sign in to comment.