/* * data_server.cpp -- Telnet interface thread * * Copyright (C) 2004 Tony Sin(x) '76 * All rights reserved. * */ /* * GNU GENERAL PUBLIC LICENSE * Version 2, June 1991 * * Copyright (C) 1989, 1991 Free Software Foundation, Inc. * 675 Mass Ave, Cambridge, MA 02139, USA * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * This program 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 program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifdef HAVE_CONFIG_H #ifndef __main_config_h__ #define __main_config_h__ #include "../config.h" #endif #ifdef HAVE_CTYPE_H #include #endif #ifdef HAVE_STRING_H #include #endif #ifndef _BSD_SOCKLEN_T_ #define _BSD_SOCKLEN_T_ #endif #ifdef HAVE_SYS_TYPES_H #include #endif #ifdef HAVE_SYS_SOCKET_H #include #endif #ifdef HAVE_NETINET_IN_H #include #endif #ifdef HAVE_ARPA_INET_H #include #endif #ifdef HAVE_UNISTD_H #include #endif #endif #include "globals.h" #include "data_server.h" void *data_server(void *arg) { int my_sock, ext_sock; socklen_t len; struct sockaddr_in from, sin; sig_obj->Block(); if (conf_obj->GetValue(_dataport) != NULL) { memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(atoi(conf_obj->GetValue(_dataport))); if (!sin.sin_port) sin.sin_port = 8796; sin.sin_addr.s_addr = htonl(INADDR_ANY); if ((my_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) log_obj->WriteLog("Socket error. Telnet interface not available"); else if (bind(my_sock,(struct sockaddr *) &sin, sizeof sin) == -1) log_obj->WriteLog("Can't bind to specified port"); else { if (listen(my_sock, 1) == -1) log_obj->WriteLog("Cannot listen on specified port"); else while (!ok_to_end) { char ext_data[127]; int buf_len, i; bool dot_found = false; len = sizeof from; ext_sock = accept(my_sock, (struct sockaddr *) &from, &len); if (!ok_to_end) { log_obj->WriteLog("Connected to client ",inet_ntoa(from.sin_addr)); buf_len = read(ext_sock,ext_data,127); ext_data[buf_len+1] = '\0'; for (i = 0; i < buf_len; i++) if (ext_data[i] == '.') { ext_data[i] = '\0'; dot_found = true; } else ext_data[i] = toupper(ext_data[i]); if ((buf_len > 0) && dot_found) if (!strcmp(ext_data,"TRACK")) { sem_obj->Wait(data_mutex); ready_obj->GetFile(ext_data); sem_obj->Signal(data_mutex); ext_data[strlen(ext_data)-4] = '\0'; log_obj->WriteLog("Received TRACK command, releasing information..."); } else if (!strcmp(ext_data,"QUIT")) { signal_termination_proc(0); strcpy(ext_data,"OK"); } else if (!strcmp(ext_data,"LOOP")) { if (conf_obj->GetValue(_loop) != NULL) strcpy(ext_data,conf_obj->GetValue(_loop)); else strcpy(ext_data,"1"); log_obj->WriteLog("Received LOOP command, releasing information..."); } else if (!strcmp(ext_data,"LOOPON")) { conf_obj->SetValue(_loop,"1"); log_obj->WriteLog("LOOP enabled"); strcpy(ext_data,"OK"); } else if (!strcmp(ext_data,"LOOPOFF")) { conf_obj->SetValue(_loop,"0"); log_obj->WriteLog("LOOP disabled"); strcpy(ext_data,"OK"); } else if (!strcmp(ext_data,"SHUFFLE")) { if (conf_obj->GetValue(_shuffle) != NULL) strcpy(ext_data,conf_obj->GetValue(_shuffle)); else strcpy(ext_data,"1"); log_obj->WriteLog("Received SHUFFLE command, releasing information..."); } else if (!strcmp(ext_data,"SHUFFLEON")) { conf_obj->SetValue(_shuffle,"1"); log_obj->WriteLog("SHUFFLE enabled"); strcpy(ext_data,"OK"); } else if (!strcmp(ext_data,"SHUFFLEOFF")) { conf_obj->SetValue(_shuffle,"0"); log_obj->WriteLog("SHUFFLE disabled"); strcpy(ext_data,"OK"); } else dot_found = false; if (!dot_found) strcpy(ext_data,"Unknown command"); write(ext_sock,ext_data,strlen(ext_data)+1); } close(ext_sock); } } close(my_sock); } return NULL; } void close_data_server() { struct sockaddr_in sin; int s = socket(AF_INET, SOCK_STREAM, 0); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(atoi(conf_obj->GetValue(_dataport))); if (!sin.sin_port) sin.sin_port = 8796; sin.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(s, (struct sockaddr *)&sin,sizeof sin); close(s); }