blob: 2643f8d16ad368854725937988461268acdaae41 [file] [log] [blame]
Denis Vlasenko57a426b2007-04-01 10:59:33 +00001/* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org>
2 * which are released into public domain by the author.
3 * Homepage: http://smarden.sunsite.dk/ipsvd/
4 *
Denis Vlasenkod18f52b2008-03-02 12:53:15 +00005 * Copyright (C) 2007 Denys Vlasenko.
Denis Vlasenko57a426b2007-04-01 10:59:33 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko57a426b2007-04-01 10:59:33 +00008 */
9
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Denis Vlasenko4866e902008-03-17 09:17:27 +000011#include "tcpudp_perhost.h"
Denis Vlasenko2856dab2007-04-01 01:18:20 +000012
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010013struct hcc* FAST_FUNC ipsvd_perhost_init(unsigned c)
Denis Vlasenko2856dab2007-04-01 01:18:20 +000014{
15// free(cc);
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010016 struct hcc *cc = xzalloc((c + 1) * sizeof(*cc));
17 cc[c].pid = -1; /* "end" marker */
18 return cc;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000019}
20
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010021unsigned FAST_FUNC ipsvd_perhost_add(struct hcc *cc, char *ip, unsigned maxconn, struct hcc **hccpp)
Denis Vlasenko2856dab2007-04-01 01:18:20 +000022{
23 unsigned i;
24 unsigned conn = 1;
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000025 int freepos = -1;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000026
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010027 for (i = 0; cc[i].pid >= 0; ++i) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000028 if (!cc[i].ip) {
29 freepos = i;
Denis Vlasenko4b924f32007-05-30 00:29:55 +000030 continue;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000031 }
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000032 if (strcmp(cc[i].ip, ip) == 0) {
Denis Vlasenko2856dab2007-04-01 01:18:20 +000033 conn++;
34 continue;
35 }
36 }
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000037 if (freepos == -1) return 0;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000038 if (conn <= maxconn) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000039 cc[freepos].ip = ip;
40 *hccpp = &cc[freepos];
Denis Vlasenko2856dab2007-04-01 01:18:20 +000041 }
42 return conn;
43}
44
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010045void FAST_FUNC ipsvd_perhost_remove(struct hcc *cc, int pid)
Denis Vlasenko2856dab2007-04-01 01:18:20 +000046{
47 unsigned i;
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010048 for (i = 0; cc[i].pid >= 0; ++i) {
Denis Vlasenko2856dab2007-04-01 01:18:20 +000049 if (cc[i].pid == pid) {
Denis Vlasenko729bd9e2007-04-02 12:37:28 +000050 free(cc[i].ip);
51 cc[i].ip = NULL;
Denis Vlasenko2856dab2007-04-01 01:18:20 +000052 cc[i].pid = 0;
53 return;
54 }
55 }
56}
57
Denys Vlasenkod82ea2b2018-02-27 13:03:44 +010058//void ipsvd_perhost_free(struct hcc *cc)
Denis Vlasenko2856dab2007-04-01 01:18:20 +000059//{
60// free(cc);
61//}