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 | |
| 13 | static struct hcc *cc; |
| 14 | static unsigned cclen; |
| 15 | |
| 16 | /* to be optimized */ |
| 17 | |
| 18 | void ipsvd_perhost_init(unsigned c) |
| 19 | { |
| 20 | // free(cc); |
| 21 | cc = xzalloc(c * sizeof(*cc)); |
| 22 | cclen = c; |
| 23 | } |
| 24 | |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 25 | unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp) |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 26 | { |
| 27 | unsigned i; |
| 28 | unsigned conn = 1; |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 29 | int freepos = -1; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 30 | |
| 31 | for (i = 0; i < cclen; ++i) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 32 | if (!cc[i].ip) { |
| 33 | freepos = i; |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 34 | continue; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 35 | } |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 36 | if (strcmp(cc[i].ip, ip) == 0) { |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 37 | conn++; |
| 38 | continue; |
| 39 | } |
| 40 | } |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 41 | if (freepos == -1) return 0; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 42 | if (conn <= maxconn) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 43 | cc[freepos].ip = ip; |
| 44 | *hccpp = &cc[freepos]; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 45 | } |
| 46 | return conn; |
| 47 | } |
| 48 | |
| 49 | void ipsvd_perhost_remove(int pid) |
| 50 | { |
| 51 | unsigned i; |
| 52 | for (i = 0; i < cclen; ++i) { |
| 53 | if (cc[i].pid == pid) { |
Denis Vlasenko | 729bd9e | 2007-04-02 12:37:28 +0000 | [diff] [blame] | 54 | free(cc[i].ip); |
| 55 | cc[i].ip = NULL; |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 56 | cc[i].pid = 0; |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | //void ipsvd_perhost_free(void) |
| 63 | //{ |
| 64 | // free(cc); |
| 65 | //} |