3
0

tcpudp_perhost.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 Denys Vlasenko.
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. #include "tcpudp_perhost.h"
  11. static struct hcc *cc;
  12. static unsigned cclen;
  13. /* to be optimized */
  14. void ipsvd_perhost_init(unsigned c)
  15. {
  16. // free(cc);
  17. cc = xzalloc(c * sizeof(*cc));
  18. cclen = c;
  19. }
  20. unsigned ipsvd_perhost_add(char *ip, unsigned maxconn, struct hcc **hccpp)
  21. {
  22. unsigned i;
  23. unsigned conn = 1;
  24. int freepos = -1;
  25. for (i = 0; i < cclen; ++i) {
  26. if (!cc[i].ip) {
  27. freepos = i;
  28. continue;
  29. }
  30. if (strcmp(cc[i].ip, ip) == 0) {
  31. conn++;
  32. continue;
  33. }
  34. }
  35. if (freepos == -1) return 0;
  36. if (conn <= maxconn) {
  37. cc[freepos].ip = ip;
  38. *hccpp = &cc[freepos];
  39. }
  40. return conn;
  41. }
  42. void ipsvd_perhost_remove(int pid)
  43. {
  44. unsigned i;
  45. for (i = 0; i < cclen; ++i) {
  46. if (cc[i].pid == pid) {
  47. free(cc[i].ip);
  48. cc[i].ip = NULL;
  49. cc[i].pid = 0;
  50. return;
  51. }
  52. }
  53. }
  54. //void ipsvd_perhost_free(void)
  55. //{
  56. // free(cc);
  57. //}