monnet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* $Id: monnet.c,v 1.8 2002/08/11 20:00:57 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include "error.h"
  38. #include "data.h"
  39. #include "mon.h"
  40. /* Fill a mux structure with inet details */
  41. void
  42. connect2mux(struct mux *mux)
  43. {
  44. struct sockaddr_in sockaddr;
  45. if ((mux->monsocket = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  46. fatal("could not obtain socket: %.200s", strerror(errno));
  47. sockaddr.sin_family = AF_INET;
  48. sockaddr.sin_port = 0;
  49. sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  50. bzero(&sockaddr.sin_zero, 8);
  51. if (bind(mux->monsocket, (struct sockaddr *) &sockaddr,
  52. sizeof(struct sockaddr)) == -1)
  53. fatal("could not bind socket: %.200s", strerror(errno));
  54. mux->sockaddr.sin_family = AF_INET;
  55. mux->sockaddr.sin_port = htons(mux->port);
  56. mux->sockaddr.sin_addr.s_addr = htonl(mux->ip);
  57. bzero(&mux->sockaddr.sin_zero, 8);
  58. info("sending packets to udp:%s:%d",
  59. mux->name, mux->port);
  60. }
  61. /* Send data stored in the mux structure to a mux */
  62. void
  63. send_packet(struct mux *mux)
  64. {
  65. if (sendto(mux->monsocket, (void *)&mux->packet,
  66. mux->offset + sizeof(mux->packet.header), 0,
  67. (struct sockaddr *)&mux->sockaddr, sizeof(mux->sockaddr))
  68. != (mux->offset + sizeof(mux->packet.header))) {
  69. mux->senderr++;
  70. }
  71. if (mux->senderr >= MON_WARN_SENDERR)
  72. warning("%d updates to mux(%u.%u.%u.%u) lost due to send errors",
  73. mux->senderr,
  74. (mux->ip >> 24), (mux->ip >> 16) & 0xff,
  75. (mux->ip >> 8) & 0xff, mux->ip & 0xff), mux->senderr = 0;
  76. }
  77. /* Prepare a packet for data */
  78. void
  79. prepare_packet(struct mux *mux)
  80. {
  81. time_t t = time(NULL);
  82. bzero(&mux->packet, sizeof(mux->packet));
  83. mux->offset = 0;
  84. mux->packet.header.mon_version = MON_PACKET_VER;
  85. mux->packet.header.timestamp = htonq((u_int64_t) t);
  86. }
  87. /* Put a stream into the packet for a mux */
  88. void
  89. stream_in_packet(struct stream *stream, struct mux *mux)
  90. {
  91. mux->offset +=
  92. (streamfunc[stream->type].get) /* call getter of stream */
  93. (&mux->packet.data[mux->offset], /* packet buffer */
  94. _POSIX2_LINE_MAX - mux->offset, /* maxlen */
  95. stream->args);
  96. }
  97. /* Ready a packet for transmission, set length and crc */
  98. void finish_packet(struct mux *mux)
  99. {
  100. u_int32_t crc;
  101. mux->packet.header.length = htons(mux->offset);
  102. mux->packet.header.crc = 0;
  103. crc = crc32(&mux->packet, mux->offset + sizeof(mux->packet.header));
  104. mux->packet.header.crc = htonl(crc);
  105. }