symonnet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* $Id: symonnet.c,v 1.12 2003/12/20 16:30:44 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 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 <netdb.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <time.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_inaddrany_sockaddr(&sockaddr, family, SOCK_DGRAM, "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, sockaddr.ss_len) == -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, (void *) &mux->packet.data,
  62. mux->offset, 0, (struct sockaddr *) & mux->sockaddr,
  63. mux->sockaddr.ss_len)
  64. != mux->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)
  76. {
  77. time_t t = time(NULL);
  78. bzero(&mux->packet, sizeof(mux->packet));
  79. mux->packet.header.symon_version = SYMON_PACKET_VER;
  80. mux->packet.header.timestamp = t;
  81. /* symonpacketheader is always first stream */
  82. mux->offset =
  83. setheader((char *) &mux->packet.data,
  84. &mux->packet.header);
  85. }
  86. /* Put a stream into the packet for a mux */
  87. void
  88. stream_in_packet(struct stream * stream, struct mux * mux)
  89. {
  90. mux->offset +=
  91. (streamfunc[stream->type].get) /* call getter of stream */
  92. (&mux->packet.data[mux->offset], /* packet buffer */
  93. sizeof(mux->packet.data) - mux->offset, /* maxlen */
  94. stream->args);
  95. }
  96. /* Ready a packet for transmission, set length and crc */
  97. void
  98. finish_packet(struct mux * mux)
  99. {
  100. mux->packet.header.length = mux->offset;
  101. mux->packet.header.crc = 0;
  102. /* fill in correct header with crc = 0 */
  103. setheader((char *) &mux->packet.data, &mux->packet.header);
  104. /* fill in correct header with crc */
  105. mux->packet.header.crc = crc32(&mux->packet.data, mux->offset);
  106. setheader((char *) &mux->packet.data, &mux->packet.header);
  107. }