symonnet.c 3.9 KB

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