2009-03-24 Esteban Sanchez <estebans@artica.es>

* udp_server/udp_server.cc, udp_server/udp_server.h: Replaced
        blankspaces with tabs.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1556 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Esteban Sanchez 2009-03-24 14:06:23 +00:00
parent 8bd22e5ca8
commit 2d53592452
3 changed files with 93 additions and 88 deletions

View File

@ -1,3 +1,8 @@
2009-03-24 Esteban Sanchez <estebans@artica.es>
* udp_server/udp_server.cc, udp_server/udp_server.h: Replaced
blankspaces with tabs.
2009-03-16 Manuel Arostegui <marostegui@artica.es>
* bin/pandora_agent.conf: Added example of a UDP server

View File

@ -15,7 +15,7 @@ using namespace Pandora;
* @return Server address.
*/
unsigned long UDP_Server::getAddress () {
return this->address;
return this->address;
}
/**
@ -25,7 +25,7 @@ unsigned long UDP_Server::getAddress () {
* @return Authorized address.
*/
unsigned long UDP_Server::getAuthAddress () {
return this->auth_address;
return this->auth_address;
}
/**
@ -34,7 +34,7 @@ unsigned long UDP_Server::getAuthAddress () {
* @return Server port.
*/
unsigned long UDP_Server::getPort () {
return this->port;
return this->port;
}
/**
@ -43,7 +43,7 @@ unsigned long UDP_Server::getPort () {
* @return Windows service associated to the server.
*/
Pandora_Windows_Service *UDP_Server::getService () {
return this->service;
return this->service;
}
/**
@ -52,7 +52,7 @@ Pandora_Windows_Service *UDP_Server::getService () {
* @return 1 if the server is running, 0 if not.
*/
unsigned char UDP_Server::isRunning () {
return this->running;
return this->running;
}
/**
@ -67,11 +67,11 @@ UDP_Server::UDP_Server (Pandora_Windows_Service *service, string address, string
if (address.empty ()) {
this->address = INADDR_ANY;
} else {
this->address = inet_addr (address.c_str ());
}
if (auth_address.empty ()) {
this->auth_address = INADDR_ANY;
} else {
this->address = inet_addr (address.c_str ());
}
if (auth_address.empty ()) {
this->auth_address = INADDR_ANY;
} else {
this->auth_address = inet_addr (auth_address.c_str ());
}
this->port = port;
@ -92,7 +92,7 @@ int UDP_Server::start () {
/* Run in a new thread */
this->running = 1;
if (CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) listen, this, 0, NULL) == NULL) {
this->running = 0;
this->running = 0;
pandoraLog ("UDP Server: Error starting UDP Server thread");
return 1;
}
@ -122,50 +122,50 @@ int UDP_Server::stop () {
* @param server UDP Server.
*/
void Pandora::listen (UDP_Server *server) {
int sockfd,n;
struct sockaddr_in servaddr, cliaddr;
int len, err;
char mesg[MAX_PACKET_SIZE];
unsigned long auth_addr;
WSADATA wsa;
int sockfd,n;
struct sockaddr_in servaddr, cliaddr;
int len, err;
char mesg[MAX_PACKET_SIZE];
unsigned long auth_addr;
WSADATA wsa;
err = WSAStartup (MAKEWORD (2,0), &wsa);
if (err != 0) {
/* Could not find a usable Winsock DLL */
printf("UDP Server: WSAStartup failed with error: %d\n", err);
return;
}
err = WSAStartup (MAKEWORD (2,0), &wsa);
if (err != 0) {
/* Could not find a usable Winsock DLL */
printf("UDP Server: WSAStartup failed with error: %d\n", err);
return;
}
sockfd = socket (AF_INET, SOCK_DGRAM, 0);
sockfd = socket (AF_INET, SOCK_DGRAM, 0);
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl (server->getAddress ());
servaddr.sin_port = htons (server->getPort ());
bind(sockfd, (struct sockaddr *)&servaddr, sizeof (servaddr));
memset (&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl (server->getAddress ());
servaddr.sin_port = htons (server->getPort ());
bind(sockfd, (struct sockaddr *)&servaddr, sizeof (servaddr));
/* Get authorised address */
auth_addr = server->getAuthAddress ();
/* Get authorised address */
auth_addr = server->getAuthAddress ();
while (server->isRunning () == 1) {
len = sizeof(cliaddr);
n = recvfrom(sockfd, mesg, MAX_PACKET_SIZE, 0, (struct sockaddr *)&cliaddr, &len);
if (n == SOCKET_ERROR) {
pandoraLog ("UDP Server: Error %d", WSAGetLastError ());
break;
}
pandoraLog ("UDP Server: Error %d", WSAGetLastError ());
break;
}
/* Authenticate client */
if (auth_addr != INADDR_ANY && auth_addr != cliaddr.sin_addr.s_addr) {
pandoraLog ("UDP Server: Unauthorised access from %s", inet_ntoa (cliaddr.sin_addr));
continue;
pandoraLog ("UDP Server: Unauthorised access from %s", inet_ntoa (cliaddr.sin_addr));
continue;
}
mesg[n] = 0;
process_command (server->getService (), mesg);
mesg[n] = 0;
process_command (server->getService (), mesg);
}
WSACleanup ();
WSACleanup ();
}
/**
@ -180,7 +180,7 @@ int Pandora::process_command (Pandora_Windows_Service *service, char *command) {
int rc;
char operation[MAX_PACKET_SIZE], action[MAX_PACKET_SIZE], target[MAX_PACKET_SIZE];
string var, value;
Pandora_Agent_Conf *conf = NULL;
Pandora_Agent_Conf *conf = NULL;
rc = sscanf (command, "%s %s %s", operation, action, target);
if (rc < 3) {
@ -188,53 +188,53 @@ int Pandora::process_command (Pandora_Windows_Service *service, char *command) {
return 1;
}
/* Re-run */
/* Re-run */
if (strcmp (operation, "REFRESH") == 0) {
service->pandora_run ();
return 0;
}
conf = service->getConf();
/* Service management */
if (strcmp (action, "SERVICE") == 0) {
var = "service_";
var.append (target);
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
value = conf->getValue (var);
if (atoi (value.c_str ()) != 1) {
pandoraLog ("UDP Server: Unauthorised access to service %s", target);
return 1;
}
service->pandora_run ();
return 0;
}
conf = service->getConf();
/* Service management */
if (strcmp (action, "SERVICE") == 0) {
var = "service_";
var.append (target);
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
value = conf->getValue (var);
if (atoi (value.c_str ()) != 1) {
pandoraLog ("UDP Server: Unauthorised access to service %s", target);
return 1;
}
if (strcmp (operation, "START") == 0) {
Pandora_Wmi::startService (target);
} else if (strcmp (operation, "STOP") == 0) {
Pandora_Wmi::stopService (target);
}
}
if (strcmp (operation, "START") == 0) {
Pandora_Wmi::startService (target);
} else if (strcmp (operation, "STOP") == 0) {
Pandora_Wmi::stopService (target);
}
}
/* Process management */
if (strcmp (action, "PROCESS") == 0) {
var = "process_";
var.append (target);
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
/* Process management */
if (strcmp (action, "PROCESS") == 0) {
var = "process_";
var.append (target);
std::transform(var.begin(), var.end(), var.begin(), ::tolower);
if (strcmp (operation, "START") == 0) {
var.append ("_start");
} else if (strcmp (operation, "STOP") == 0) {
var.append ("_stop");
} else {
return 1;
}
if (strcmp (operation, "START") == 0) {
var.append ("_start");
} else if (strcmp (operation, "STOP") == 0) {
var.append ("_stop");
} else {
return 1;
}
value = conf->getValue (var);
if (value.empty ()) {
pandoraLog ("UDP Server: Unauthorised access to process %s", target);
return 1;
}
Pandora_Wmi::runProgram (value.c_str());
}
value = conf->getValue (var);
if (value.empty ()) {
pandoraLog ("UDP Server: Unauthorised access to process %s", target);
return 1;
}
Pandora_Wmi::runProgram (value.c_str());
}
return 0;
}

View File

@ -14,10 +14,10 @@ namespace Pandora {
public:
UDP_Server (Pandora_Windows_Service *service, string address, string auth_address, unsigned int port);
~UDP_Server ();
unsigned long getAddress ();
unsigned long getAuthAddress ();
unsigned long getPort ();
Pandora_Windows_Service *getService ();
unsigned long getAddress ();
unsigned long getAuthAddress ();
unsigned long getPort ();
Pandora_Windows_Service *getService ();
unsigned char isRunning ();
int start ();
@ -25,13 +25,13 @@ namespace Pandora {
private:
unsigned long address;
unsigned long auth_address;
unsigned long auth_address;
unsigned long port;
unsigned char running;
Pandora_Windows_Service *service;
};
void listen (UDP_Server *server);
void listen (UDP_Server *server);
int process_command (Pandora_Windows_Service *service, char *command);
}