/*
* Copyright (c) 2015 Santtu Lakkala
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <netinet/in.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>
#include <stdio.h>
#define MAX_AGENTS 64
struct agent {
const char *community;
size_t c_len;
struct sockaddr_in agent;
struct sockaddr_in client;
};
int strmatchn(const char *a, size_t a_len, const char *b, size_t b_len)
{
return a_len
== b_len
&& !strncmp(a
, b
, a_len
); }
size_t find_agent(struct agent *agents, size_t n_agents, const char *community, size_t c_len)
{
size_t i;
for (i = 0; i < n_agents; i++) {
if (strmatchn(agents[i].community, agents[i].c_len, community, c_len))
break;
}
return i;
}
int set_agent(struct agent *agents, size_t *n_agents, const char *details)
{
const char *colon
= strchr(details
, ':'); size_t i;
char buffer[256];
if (!colon)
return 0;
i = find_agent(agents, *n_agents, details, colon - details);
if (i == *n_agents) {
if (i == MAX_AGENTS)
return 0;
(*n_agents)++;
agents[i].community = details;
agents[i].c_len = colon - details;
}
memset(&agents
[i
].
client, 0, sizeof(agents
[i
].
client)); memset(&agents
[i
].
agent, 0, sizeof(agents
[i
].
agent));
details = colon + 1;
if (colon) {
memcpy(buffer
, details
, colon
- details
); buffer[colon - details] = '\0';
inet_aton(buffer, &agents[i].agent.sin_addr);
agents
[i
].
agent.
sin_port = htons
(atoi(colon
+ 1)); } else {
inet_aton(details, &agents[i].agent.sin_addr);
agents[i].agent.sin_port = htons(161);
}
return 1;
}
int running = 1;
void sighand(int signo)
{
(void)signo;
running = 0;
}
int main(int argc, char **argv)
{
struct agent agents[MAX_AGENTS];
size_t n_agents;
char buffer[8192];
struct sockaddr_in serv = { 0 };
struct pollfd pfds[MAX_AGENTS + 1];
int i;
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(161);
signal(SIGINT, sighand);
for (i = 1; i < argc; i++) {
if (*argv[i] == '-') {
const char *o = &argv[i][1];
while (*o) {
if (i + 1 >= argc)
return 1;
switch (*o++) {
case 'l':
if (!inet_aton(argv[++i], &serv.sin_addr))
return 1;
break;
case 'p':
serv.
sin_port = htons
(atoi(argv
[++i
])); break;
case 'a':
set_agent(agents, &n_agents, argv[++i]);
break;
default:
return 1;
break;
}
}
} else {
return 1;
}
}
if ((pfds[0].fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Failed to create socket for listening: "); return 1;
}
pfds[0].events = POLLIN;
for (i = 0; i < n_agents; i++) {
if ((pfds[i + 1].fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("Failed to create socket for listening: "); return 1;
}
pfds[i + 1].events = POLLIN;
}
if (bind(pfds[0].fd, (const struct sockaddr *)&serv, sizeof(serv)) < 0) {
return 1;
}
while (running) {
int n;
n = poll(pfds, n_agents + 1, -1);
if (pfds[0].revents & POLLIN) {
struct sockaddr_in c;
socklen_t len = sizeof(c);
ssize_t r = recvfrom(pfds[0].fd, buffer, sizeof(buffer), 0,
(struct sockaddr *)&c, &len);
if (r == 0)
break;
if (r > 6) {
int len = (unsigned char)buffer[6];
if (r > 6 + len) {
i = find_agent(agents, n_agents, buffer + 7, len);
if (i < n_agents) {
memcpy(&agents
[i
].
client, &c
, len
); sendto(pfds[i + 1].fd, buffer, r, 0,
(const struct sockaddr *)&agents[i].agent, sizeof(struct sockaddr_in));
}
}
printf("%.*s\n", len
, buffer
+ 7); }
n--;
}
for (i = 0; n && i < n_agents; i++) {
if (pfds[i + 1].revents & POLLIN) {
ssize_t r = recv(pfds[i + 1].fd, buffer, sizeof(buffer), 0);
if (r == 0)
break;
if (r > 0)
sendto(pfds[0].fd, buffer, r, 0, (const struct sockaddr *)&agents[i].client, sizeof(struct sockaddr_in));
n--;
}
}
}
return 0;
}