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 | * |
| 5 | * Copyright (C) 2007 Denis Vlasenko. |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 8 | */ |
| 9 | |
Denis Vlasenko | 2856dab | 2007-04-01 01:18:20 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
| 11 | #include "ipsvd_perhost.h" |
| 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 | |
| 25 | unsigned ipsvd_perhost_add(const char *ip, unsigned maxconn, struct hcc **hccpp) |
| 26 | { |
| 27 | unsigned i; |
| 28 | unsigned conn = 1; |
| 29 | int p = -1; |
| 30 | |
| 31 | for (i = 0; i < cclen; ++i) { |
| 32 | if (cc[i].ip[0] == 0) { |
| 33 | if (p == -1) p = i; |
| 34 | continue; |
| 35 | } |
| 36 | if (strncmp(cc[i].ip, ip, sizeof(cc[i].ip)) == 0) { |
| 37 | conn++; |
| 38 | continue; |
| 39 | } |
| 40 | } |
| 41 | if (p == -1) return 0; |
| 42 | if (conn <= maxconn) { |
| 43 | strcpy(cc[p].ip, ip); |
| 44 | *hccpp = &cc[p]; |
| 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) { |
| 54 | cc[i].ip[0] = 0; |
| 55 | cc[i].pid = 0; |
| 56 | return; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | //void ipsvd_perhost_free(void) |
| 62 | //{ |
| 63 | // free(cc); |
| 64 | //} |