Denis Vlasenko | 57a426b | 2007-04-01 10:59:33 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Denys Vlasenko. |
Denis Vlasenko | 57a426b | 2007-04-01 10:59:33 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 57a426b | 2007-04-01 10:59:33 +0000 | [diff] [blame] | 8 | */ |
9 | |||||
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Denis Vlasenko | 4866e90 | 2008-03-17 09:17:27 +0000 | [diff] [blame] | 11 | #include "tcpudp_perhost.h" |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 13 | struct hcc* FAST_FUNC ipsvd_perhost_init(unsigned c) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 14 | { |
15 | // free(cc); | ||||
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 16 | struct hcc *cc = xzalloc((c + 1) * sizeof(*cc)); |
17 | cc[c].pid = -1; /* "end" marker */ | ||||
18 | return cc; | ||||
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 19 | } |
20 | |||||
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 21 | unsigned FAST_FUNC ipsvd_perhost_add(struct hcc *cc, char *ip, unsigned maxconn, struct hcc **hccpp) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 22 | { |
23 | unsigned i; | ||||
24 | unsigned conn = 1; | ||||
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 25 | int freepos = -1; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 26 | |
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 27 | for (i = 0; cc[i].pid >= 0; ++i) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 28 | if (!cc[i].ip) { |
29 | freepos = i; | ||||
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 30 | continue; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 31 | } |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 32 | if (strcmp(cc[i].ip, ip) == 0) { |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 33 | conn++; |
34 | continue; | ||||
35 | } | ||||
36 | } | ||||
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 37 | if (freepos == -1) return 0; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 38 | if (conn <= maxconn) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 39 | cc[freepos].ip = ip; |
40 | *hccpp = &cc[freepos]; | ||||
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 41 | } |
42 | return conn; | ||||
43 | } | ||||
44 | |||||
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 45 | void FAST_FUNC ipsvd_perhost_remove(struct hcc *cc, int pid) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 46 | { |
47 | unsigned i; | ||||
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 48 | for (i = 0; cc[i].pid >= 0; ++i) { |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 49 | if (cc[i].pid == pid) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 50 | free(cc[i].ip); |
51 | cc[i].ip = NULL; | ||||
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 52 | cc[i].pid = 0; |
53 | return; | ||||
54 | } | ||||
55 | } | ||||
56 | } | ||||
57 | |||||
Denys Vlasenko | d82ea2b | 2018-02-27 13:03:44 +0100 | [diff] [blame^] | 58 | //void ipsvd_perhost_free(struct hcc *cc) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 59 | //{ |
60 | // free(cc); | ||||
61 | //} |