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
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
static PrintConsole status_console;
|
|
|
|
static PrintConsole main_console;
|
2016-01-26 03:03:48 +01:00
|
|
|
static PrintConsole tcp_console;
|
2017-07-26 04:55:16 +02:00
|
|
|
#if ENABLE_LOGGING
|
|
|
|
static bool disable_logging = false;
|
|
|
|
#endif
|
2014-11-23 23:39:00 +01:00
|
|
|
|
|
|
|
/*! initialize console subsystem */
|
|
|
|
void
|
|
|
|
console_init(void)
|
|
|
|
{
|
2015-01-08 06:26:01 +01:00
|
|
|
consoleInit(GFX_TOP, &status_console);
|
|
|
|
consoleSetWindow(&status_console, 0, 0, 50, 1);
|
2014-11-23 23:39:00 +01:00
|
|
|
|
2015-01-08 06:26:01 +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);
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
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;
|
|
|
|
|
2015-01-08 06:26:01 +01:00
|
|
|
consoleSelect(&status_console);
|
2014-11-23 23:39:00 +01:00
|
|
|
va_start(ap, fmt);
|
2015-01-08 06:26:01 +01:00
|
|
|
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);
|
2015-01-08 06:26:01 +01:00
|
|
|
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);
|
2015-01-08 06:26:01 +01:00
|
|
|
vprintf(fmt, ap);
|
2016-01-26 06:56:29 +01:00
|
|
|
#ifdef ENABLE_LOGGING
|
2017-07-26 04:55:16 +02:00
|
|
|
if(!disable_logging)
|
|
|
|
vfprintf(stderr, fmt, ap);
|
2016-01-26 06:56:29 +01:00
|
|
|
#endif
|
2014-11-23 23:39:00 +01:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2017-07-26 04:55:16 +02:00
|
|
|
/*! print debug message
|
|
|
|
*
|
|
|
|
* @param[in] fmt format string
|
|
|
|
* @param[in] ... format arguments
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
debug_print(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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;
|
2016-06-15 10:38:53 +02:00
|
|
|
int rc, lines = 0;
|
2016-01-26 03:03:48 +01:00
|
|
|
|
2017-07-26 04:55:16 +02:00
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
disable_logging = true;
|
|
|
|
#endif
|
|
|
|
|
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);
|
2016-01-26 06:47:37 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-06-15 10:38:53 +02:00
|
|
|
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;
|
|
|
|
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print(GREEN "%stcp[%zu]: ", i == 0 ? "" : "\n", i);
|
2016-01-26 03:03:48 +01:00
|
|
|
switch(entry->state)
|
|
|
|
{
|
|
|
|
case TCP_STATE_CLOSED:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("CLOSED\x1b[K");
|
|
|
|
local = remote = NULL;
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_LISTEN:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("LISTEN\x1b[K");
|
|
|
|
remote = NULL;
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_ESTABLISHED:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("ESTABLISHED\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_FINWAIT1:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("FINWAIT1\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_FINWAIT2:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("FINWAIT2\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_CLOSE_WAIT:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("CLOSE_WAIT\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_LAST_ACK:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("LAST_ACK\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
case TCP_STATE_TIME_WAIT:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("TIME_WAIT\x1b[K");
|
|
|
|
break;
|
|
|
|
|
2016-01-26 03:03:48 +01:00
|
|
|
default:
|
2016-06-15 10:38:53 +02:00
|
|
|
console_print("State %lu\x1b[K", entry->state);
|
|
|
|
break;
|
2016-01-26 03:03:48 +01:00
|
|
|
}
|
|
|
|
|
2016-06-15 10:38:53 +02: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-06-15 10:38:53 +02: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);
|
2017-07-26 04:55:16 +02:00
|
|
|
|
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
disable_logging = false;
|
|
|
|
#endif
|
2016-01-26 03:03:48 +01:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-07-26 04:55:16 +02:00
|
|
|
void
|
|
|
|
debug_print(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_LOGGING
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-11-23 23:39:00 +01:00
|
|
|
void console_render(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|