//emul_gopher.c: /* * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 * * This file is part of roard a part of RoarAudio, * a cross-platform sound system for both, home and professional use. * See README for details. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 * as published by the Free Software Foundation. * * RoarAudio is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; see the file COPYING. If not, write to * the Free Software Foundation, 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #include "roard.h" #ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER #include #define _INFO ROAR_GOPHER_TYPE_INFO #define _DIR ROAR_GOPHER_TYPE_DIR #define _FILE ROAR_GOPHER_TYPE_FILE #define _SOUND ROAR_GOPHER_TYPE_SOUND static int scb_status_txt (int client, struct roar_vio_calls * vio, const char * selector, char ** text); static struct roar_gopher_menu_item g_gopher_root_menu[] = { {.type = _INFO, .name = "roard Root Menu"}, {.type = _FILE, .name = "Server Info", .selector = "/info.txt", .host = NULL, .port = 0}, {.type = _FILE, .name = "Server Status", .selector = "/status.txt", .host = NULL, .port = 0} }; static struct item { const char * selector; char type; struct roar_gopher_menu menu; struct roar_audio_info info; int dir; const char * text; int (*cb)(int client, struct roar_vio_calls * vio, const char * selector, char ** text); } g_gopher_items[] = { {.selector = "", .type = _DIR, .menu = {.items = g_gopher_root_menu, .items_len = sizeof(g_gopher_root_menu)/sizeof(*g_gopher_root_menu)}, .cb = NULL }, // and again as selector '/' as some clients seems to require it: {.selector = "/", .type = _DIR, .menu = {.items = g_gopher_root_menu, .items_len = sizeof(g_gopher_root_menu)/sizeof(*g_gopher_root_menu)}, .cb = NULL }, {.selector = "/info.txt", .type = _FILE, .text = "Some\nText.", .cb = NULL}, {.selector = "/status.txt", .type = _FILE, .cb = scb_status_txt} }; // SCBs: static int scb_status_txt (int client, struct roar_vio_calls * vio, const char * selector, char ** text) { const size_t len = 1024; const char * server_version = NULL; if ( DISTRIBUTION_VERSION_STRING[0] == 0 ) { server_version = "roard/" PACKAGE_VERSION " <" DEVICE_VENDOR_STRING ">"; } else { server_version = "roard/" PACKAGE_VERSION " <" DEVICE_VENDOR_STRING "> (" DISTRIBUTION_VERSION_STRING ")"; } *text = roar_mm_malloc(1024); if ( *text == NULL ) return -1; **text = 0; snprintf(*text, len, "Server version: %s\r\n" "Server location: %s\r\n" "Server description: %s\r\n" "\r\n" "Counters current: %llu clients, %llu streams\r\n", server_version, g_config->location, g_config->description, (long long unsigned int)g_counters.cur.clients, (long long unsigned int)g_counters.cur.streams ); (*text)[len-1] = 0; return 0; } // other code: static int strip_nl (char * str) { register char c; for (; (c = *str) != 0; str++) { if ( c == '\r' || c == '\n' ) { *str = 0; return 1; } } return 0; } static int send_menu (int client, struct roar_gopher_menu * menu, struct roar_vio_calls * vio) { struct roar_buffer * buf; struct roar_gopher_menu_item * item; const size_t len = 80; size_t i; void * data; char * chardata; const char * host; unsigned int port; struct roar_sockname sockaddr; if ( roar_vio_ctl(vio, ROAR_VIO_CTL_GET_SOCKNAME, &sockaddr) == -1 ) { memset(&sockaddr, 0, sizeof(sockaddr)); } for (i = 0; i < menu->items_len; i++) { item = &(menu->items[i]); if ( roar_buffer_new_data(&buf, len, &data) == -1 ) { if ( sockaddr.addr != NULL ) roar_mm_free(sockaddr.addr); return -1; } chardata = data; switch (item->type) { case _INFO: snprintf(data, len-1, "i%s\tfake\t(NULL)\t0\r\n", item->name); break; default: host = item->host == NULL ? sockaddr.addr : item->host; port = item->port == 0 ? sockaddr.port : item->port; snprintf(data, len-1, "%c%s\t%s\t%s\t%u\r\n", item->type, item->name, item->selector, host, port); break; } chardata[len-1] = 0; roar_buffer_set_len(buf, strlen(data)); clients_add_output(client, buf); } if ( sockaddr.addr != NULL ) roar_mm_free(sockaddr.addr); return 0; } static int send_text (int client, const char * text, struct roar_vio_calls * vio) { struct roar_buffer * buf; void * data; size_t len = strlen(text); if ( roar_buffer_new_data(&buf, len+6, &data) == -1 ) return -1; memcpy(data, text, len); //memcpy(data+len, "\r\n.\r\n\0", 6); memcpy(data+len, "\0", 1); clients_add_output(client, buf); return 0; } int emul_gopher_check_client(int client, struct roar_vio_calls * vio) { struct roar_client_server * cs; struct roar_vio_calls rvio; struct item * c = NULL; char inbuf[1024]; ssize_t ret; size_t i; int funcret = -1; size_t len = 0; void * data; char * text; if ( clients_get_server(client, &cs) == -1 ) { clients_delete(client); return -1; } if ( vio == NULL ) { vio = &rvio; roar_vio_open_fh_socket(vio, clients_get_fh(client)); } if ( cs->inbuf != NULL ) { len = sizeof(inbuf)-1; if ( roar_buffer_shift_out(&(cs->inbuf), inbuf, &len) == -1 ) { clients_delete(client); return -1; } if ( cs->inbuf != NULL ) { roar_buffer_free(cs->inbuf); clients_delete(client); return -1; } // test if we have still buffer space left. if ( len == (sizeof(inbuf)-1) ) { clients_delete(client); return -1; } } ret = roar_vio_read(vio, inbuf+len, sizeof(inbuf)-len-1); if ( ret < 1 ) { clients_delete(client); return -1; } ret += len; inbuf[ret] = 0; if ( !strip_nl(inbuf) ) { if ( roar_buffer_new_data(&(cs->inbuf), ret, &data) == -1 ) { clients_delete(client); return -1; } memcpy(data, inbuf, ret); return 0; } for (i = 0; i < sizeof(g_gopher_items)/sizeof(*g_gopher_items); i++) { if ( !strcmp(g_gopher_items[i].selector, inbuf) ) { c = &(g_gopher_items[i]); break; } } if ( c == NULL ) { clients_delete(client); return -1; } if ( c->cb != NULL ) { text = NULL; funcret = c->cb(client, vio, inbuf, &text); if ( funcret == 0 && text != NULL ) funcret = send_text(client, text, vio); if ( text != NULL ) roar_mm_free(text); } else { switch (c->type) { case _DIR: funcret = send_menu(client, &(c->menu), vio); break; case _FILE: funcret = send_text(client, c->text, vio); break; default: funcret = -1; break; } } if ( funcret == -1 ) { clients_delete(client); return -1; } return 0; } int emul_gopher_flushed_client(int client, struct roar_vio_calls * vio) { return clients_delete(client); } #endif //ll