1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/* locbrok - location broker
* vix 13sep91 [written]
*/
#ifndef LINT
static char RCSid[] = "$Id: locbrok.c,v 1.9 2001/03/24 21:14:26 vixie Exp $";
#endif
/* Copyright (c) 1996 by Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
* SOFTWARE.
*/
#ifdef DEBUG
int Debug = 0;
#endif
#ifdef WANT_TCPIP
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "rtty.h"
#include "misc.h"
#include "locbrok.h"
#define USAGE_STR "[-s service] [-x debuglev]"
/* misc.c */
#ifndef isnumber
extern int isnumber(char *);
#endif
typedef struct reg_db {
char *name;
u_short port;
u_short client;
struct reg_db *next;
} reg_db;
static reg_db *find_byname(char *name),
*find_byport(u_int port);
static int add(char *name, u_int port, u_int client);
static void server(void),
client_input(int fd),
rm_byclient(u_int client),
print(void);
static char *ProgName = "amnesia",
*Service = LB_SERVNAME;
static int Port,
MaxFD;
static fd_set Clients;
static reg_db *RegDB = NULL;
main(int argc, char *argv[]) {
struct servent *serv;
char ch;
ProgName = argv[0];
while ((ch = getopt(argc, argv, "s:x:")) != EOF) {
switch (ch) {
case 's':
Service = optarg;
break;
case 'x':
Debug = atoi(optarg);
break;
default:
USAGE((stderr, "%s: getopt=%c ?\n", ProgName, ch));
}
}
if (isnumber(Service) && (Port = atoi(Service))) {
/* numeric service; we're ok */
;
} else if (NULL != (serv = getservbyname(Service, "tcp"))) {
/* found the service name; we're ok */
Port = ntohs(serv->s_port);
} else {
/* nothing worked; use default */
Port = LB_SERVPORT;
fprintf(stderr, "%s: service `%s' not found, using port %d\n",
ProgName, Service, Port);
}
server();
}
static void
server(void) {
int serv, on = 1;
struct sockaddr_in name;
serv = socket(PF_INET, SOCK_STREAM, 0);
ASSERT(serv>=0, "socket")
setsockopt(serv, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
name.sin_family = AF_INET;
#ifndef NO_SOCKADDR_LEN
name.sin_len = sizeof(struct sockaddr_in);
#endif
name.sin_addr.s_addr = INADDR_ANY;
name.sin_port = htons(Port);
ASSERT(bind(serv, (struct sockaddr *)&name, sizeof name)>=0, "bind")
FD_ZERO(&Clients);
MaxFD = serv;
listen(serv, 5);
for (;;) {
fd_set readfds;
int nset, fd;
readfds = Clients;
FD_SET(serv, &readfds);
nset = select(MaxFD+1, &readfds, NULL, NULL, NULL);
if (nset < 0 && errno == EINTR)
continue;
ASSERT(nset>=0, "assert")
for (fd = 0; fd <= MaxFD; fd++) {
if (!FD_ISSET(fd, &readfds))
continue;
if (fd == serv) {
int namesize = sizeof name;
int addr, local, cl;
ASSERT((cl=accept(serv,
(struct sockaddr *)&name,
&namesize))>=0,
"accept")
addr = ntohl(name.sin_addr.s_addr);
local = (addr == INADDR_ANY) ||
((IN_CLASSA(addr) &&
(addr & IN_CLASSA_NET) >>
IN_CLASSA_NSHIFT)
== IN_LOOPBACKNET);
fprintf(stderr,
"accept from %08x (%slocal)\n",
addr, local?"":"not ");
FD_SET(cl, &Clients);
if (cl > MaxFD)
MaxFD = cl;
continue;
}
if (FD_ISSET(fd, &Clients)) {
client_input(fd);
}
}
}
}
static void
client_input(int fd) {
locbrok lb;
reg_db *db;
int keepalive = 0;
if (0 >= read(fd, &lb, sizeof lb)) {
fputs("locbrok.client_input: ", stderr);
perror("read");
goto death;
}
lb.lb_port = ntohs(lb.lb_port);
lb.lb_nlen = ntohs(lb.lb_nlen);
if (lb.lb_nlen >= LB_MAXNAMELEN) {
fprintf(stderr, "client_input: fd%d sent oversize req\n", fd);
goto death;
}
lb.lb_name[lb.lb_nlen] = '\0';
fprintf(stderr, "client_input(fd %d, port %d, %d:`%s')\n",
fd, lb.lb_port, lb.lb_nlen, lb.lb_name);
if (lb.lb_port && !lb.lb_nlen) {
if (NULL != (db = find_byport(lb.lb_port))) {
lb.lb_nlen = min(strlen(db->name), LB_MAXNAMELEN);
strncpy(lb.lb_name, db->name, lb.lb_nlen);
}
} else if (!lb.lb_port && lb.lb_nlen) {
if (NULL != (db = find_byname(lb.lb_name))) {
lb.lb_port = db->port;
}
} else if (lb.lb_port && lb.lb_nlen) {
if (add(lb.lb_name, lb.lb_port, fd) == -1) {
lb.lb_nlen = 0;
} else {
keepalive++;
print();
}
} else {
fprintf(stderr, "bogus client_input (port,nlen both 0)\n");
goto death;
}
lb.lb_port = htons(lb.lb_port);
lb.lb_nlen = htons(lb.lb_nlen);
write(fd, &lb, sizeof lb);
if (keepalive)
return;
death:
close(fd);
FD_CLR(fd, &Clients);
rm_byclient(fd);
print();
}
static reg_db *
find_byname(const char *name) {
reg_db *db;
for (db = RegDB; db; db = db->next)
if (!strcmp(name, db->name))
return (db);
return (NULL);
}
static reg_db *
find_byport(u_int port) {
reg_db *db;
for (db = RegDB; db; db = db->next)
if (port == db->port)
return (db);
return (NULL);
}
static int
add(const char *name, u_int port, u_int client) {
reg_db *db;
if (find_byname(name) || find_byport(port))
return (-1);
db = (reg_db *) safe_malloc(sizeof(reg_db));
db->name = safe_malloc(strlen(name)+1);
strcpy(db->name, name);
db->port = port;
db->client = client;
db->next = RegDB;
RegDB = db;
return (0);
}
static void
rm_byclient(u_int client) {
reg_db *cur = RegDB, *prev = NULL;
while (cur) {
if (cur->client == client) {
reg_db *tmp = cur;
if (prev)
prev->next = cur->next;
else
RegDB = cur->next;
cur = cur->next;
free(tmp->name);
free(tmp);
} else {
prev = cur;
cur = cur->next;
}
}
}
static void
print(void) {
reg_db *db;
fprintf(stderr, "db:\n");
for (db = RegDB; db; db = db->next)
fprintf(stderr, "(%s %d %d)\n",
db->name, db->port, db->client);
fprintf(stderr, "---\n");
}
#else /*WANT_TCPIP*/
#include <stdio.h>
int
main(int argc, char *argv[]) {
fprintf(stderr, "There is no location broker for this system.\n");
exit(1);
}
#endif /*WANT_TCPIP*/
|