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