ftpiiu_plugin/source/console.c

197 lines
4.0 KiB
C
Raw Normal View History

2014-11-23 23:39:00 +01:00
#include "console.h"
2016-01-26 03:03:48 +01:00
#include <arpa/inet.h>
#include <errno.h>
2016-01-26 06:55:59 +01:00
#include <stdarg.h>
2014-11-23 23:39:00 +01:00
#include <stdio.h>
#include <stdlib.h>
2016-01-26 06:55:59 +01:00
#include <string.h>
2014-11-23 23:39:00 +01:00
#ifdef _3DS
2016-01-26 07:02:07 +01:00
#include <3ds.h>
2014-11-23 23:39:00 +01:00
static PrintConsole status_console;
static PrintConsole main_console;
2016-01-26 03:03:48 +01:00
static PrintConsole tcp_console;
2014-11-23 23:39:00 +01:00
/*! initialize console subsystem */
void
console_init(void)
{
consoleInit(GFX_TOP, &status_console);
consoleSetWindow(&status_console, 0, 0, 50, 1);
2014-11-23 23:39:00 +01:00
consoleInit(GFX_TOP, &main_console);
consoleSetWindow(&main_console, 0, 1, 50, 29);
2014-11-23 23:39:00 +01:00
2016-01-26 03:03:48 +01:00
consoleInit(GFX_BOTTOM, &tcp_console);
consoleSelect(&main_console);
2014-11-23 23:39:00 +01:00
}
/*! set status bar contents
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_set_status(const char *fmt, ...)
{
va_list ap;
consoleSelect(&status_console);
2014-11-23 23:39:00 +01:00
va_start(ap, fmt);
vprintf(fmt, ap);
2016-01-26 06:56:29 +01:00
#ifdef ENABLE_LOGGING
2015-01-08 06:30:22 +01:00
vfprintf(stderr, fmt, ap);
2016-01-26 06:56:29 +01:00
#endif
2014-11-23 23:39:00 +01:00
va_end(ap);
consoleSelect(&main_console);
2014-11-23 23:39:00 +01:00
}
/*! add text to the console
*
* @param[in] fmt format string
* @param[in] ... format arguments
*/
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
2016-01-26 06:56:29 +01:00
#ifdef ENABLE_LOGGING
2015-01-08 06:30:22 +01:00
vfprintf(stderr, fmt, ap);
2016-01-26 06:56:29 +01:00
#endif
2014-11-23 23:39:00 +01:00
va_end(ap);
}
2016-01-26 03:03:48 +01:00
/*! print tcp tables */
static void
print_tcp_table(void)
{
static SOCU_TCPTableEntry tcp_entries[32];
socklen_t optlen;
size_t i;
int rc, lines = 0;
2016-01-26 03:03:48 +01:00
consoleSelect(&tcp_console);
2016-06-15 10:39:02 +02:00
console_print("\x1b[0;0H\x1b[K");
2016-01-26 03:03:48 +01:00
optlen = sizeof(tcp_entries);
rc = SOCU_GetNetworkOpt(SOL_CONFIG, NETOPT_TCP_TABLE, tcp_entries, &optlen);
2016-01-26 06:56:29 +01:00
if(rc != 0 && errno != ENODEV)
console_print(RED "tcp table: %d %s\n\x1b[J\n" RESET, errno, strerror(errno));
2016-01-26 03:03:48 +01:00
else if(rc == 0)
{
for(i = 0; lines < 30 && i < optlen / sizeof(SOCU_TCPTableEntry); ++i)
2016-01-26 03:03:48 +01:00
{
SOCU_TCPTableEntry *entry = &tcp_entries[i];
struct sockaddr_in *local = (struct sockaddr_in*)&entry->local;
struct sockaddr_in *remote = (struct sockaddr_in*)&entry->remote;
console_print(GREEN "%stcp[%zu]: ", i == 0 ? "" : "\n", i);
2016-01-26 03:03:48 +01:00
switch(entry->state)
{
case TCP_STATE_CLOSED:
console_print("CLOSED\x1b[K");
local = remote = NULL;
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_LISTEN:
console_print("LISTEN\x1b[K");
remote = NULL;
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_ESTABLISHED:
console_print("ESTABLISHED\x1b[K");
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_FINWAIT1:
console_print("FINWAIT1\x1b[K");
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_FINWAIT2:
console_print("FINWAIT2\x1b[K");
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_CLOSE_WAIT:
console_print("CLOSE_WAIT\x1b[K");
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_LAST_ACK:
console_print("LAST_ACK\x1b[K");
break;
2016-01-26 03:03:48 +01:00
case TCP_STATE_TIME_WAIT:
console_print("TIME_WAIT\x1b[K");
break;
2016-01-26 03:03:48 +01:00
default:
console_print("State %lu\x1b[K", entry->state);
break;
2016-01-26 03:03:48 +01:00
}
++lines;
if(local && (lines++ < 30))
console_print("\n Local %s:%u\x1b[K", inet_ntoa(local->sin_addr),
ntohs(local->sin_port));
if(remote && (lines++ < 30))
console_print("\n Peer %s:%u\x1b[K", inet_ntoa(remote->sin_addr),
ntohs(remote->sin_port));
2016-01-26 03:03:48 +01:00
}
2016-01-26 06:56:29 +01:00
console_print(RESET "\x1b[J");
2016-01-26 03:03:48 +01:00
}
else
console_print("\x1b[2J");
consoleSelect(&main_console);
}
2014-11-23 23:39:00 +01:00
/*! draw console to screen */
void
console_render(void)
{
2016-01-26 03:03:48 +01:00
/* print tcp table */
print_tcp_table();
2014-11-23 23:39:00 +01:00
/* flush framebuffer */
gfxFlushBuffers();
gspWaitForVBlank();
gfxSwapBuffers();
}
#else
/* this is a lot easier when you have a real console */
void
console_init(void)
{
}
void
console_set_status(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
fputc('\n', stdout);
}
void
console_print(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void console_render(void)
{
}
#endif