3
0

tcpudp_perhost.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. struct hcc* FAST_FUNC ipsvd_perhost_init(unsigned c)
  12. {
  13. // free(cc);
  14. struct hcc *cc = xzalloc((c + 1) * sizeof(*cc));
  15. cc[c].pid = -1; /* "end" marker */
  16. return cc;
  17. }
  18. unsigned FAST_FUNC ipsvd_perhost_add(struct hcc *cc, char *ip, unsigned maxconn, struct hcc **hccpp)
  19. {
  20. unsigned i;
  21. unsigned conn = 1;
  22. int freepos = -1;
  23. for (i = 0; cc[i].pid >= 0; ++i) {
  24. if (!cc[i].ip) {
  25. freepos = i;
  26. continue;
  27. }
  28. if (strcmp(cc[i].ip, ip) == 0) {
  29. conn++;
  30. continue;
  31. }
  32. }
  33. if (freepos == -1) return 0;
  34. if (conn <= maxconn) {
  35. cc[freepos].ip = ip;
  36. *hccpp = &cc[freepos];
  37. }
  38. return conn;
  39. }
  40. void FAST_FUNC ipsvd_perhost_remove(struct hcc *cc, int pid)
  41. {
  42. unsigned i;
  43. for (i = 0; cc[i].pid >= 0; ++i) {
  44. if (cc[i].pid == pid) {
  45. free(cc[i].ip);
  46. cc[i].ip = NULL;
  47. cc[i].pid = 0;
  48. return;
  49. }
  50. }
  51. }
  52. //void ipsvd_perhost_free(struct hcc *cc)
  53. //{
  54. // free(cc);
  55. //}