Newer
Older
src / c / udpmsg / udpmsg.c
  1. /*
  2. * udpmsg: send messages via UDP
  3. */
  4.  
  5. /*
  6. * Copyright (c) 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37.  
  38. #include "udpmsg.h"
  39.  
  40. const char usagemsg[] =
  41. "usage: udpmsg -i <IP> -m <message>\n"
  42. "\n"
  43. "arguments:\n"
  44. " -i IP send to host with address IP\n"
  45. " -m message message to send\n"
  46. " also: udpmsg -v show version\n"
  47. " udpmsg -h display this help\n"
  48. " udpmsg -l display (BSD) license\n"
  49. ;
  50.  
  51. const char versionmsg[] = "udpmsg 0.3\n";
  52.  
  53. void usage ( );
  54. void version ( );
  55.  
  56. int main ( int argc, char* argv[] ) {
  57. int sock;
  58. int tmp;
  59. in_addr_t remote_addr = 0;
  60. char* msg = NULL;
  61. struct sockaddr_in *destination = NULL;
  62.  
  63. while ( (tmp = getopt(argc, argv, "i:m:hlv")) != -1 ) {
  64. switch ( tmp ) {
  65. case 'h':
  66. usage();
  67. exit(0);
  68. case 'l':
  69. license();
  70. exit(0);
  71. case 'v':
  72. version();
  73. exit(0);
  74. case 'i':
  75. remote_addr = inet_addr(optarg);
  76. break;
  77. case 'm':
  78. msg = optarg;
  79. break;
  80. }
  81. }
  82.  
  83. if ( (remote_addr == 0) || (msg == NULL) ) {
  84. usage();
  85. exit(-1);
  86. }
  87.  
  88. sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  89. if ( sock < 0 ) {
  90. showerror("socket");
  91. exit(-1);
  92. }
  93.  
  94. destination = alloca(sizeof(struct sockaddr_in));
  95.  
  96. destination->sin_family = AF_INET;
  97. destination->sin_addr.s_addr = remote_addr;
  98. destination->sin_port = UDPMSG_PORT;
  99.  
  100. tmp = sendto(sock, msg, strlen(msg)+1, 0, (struct sockaddr*)destination, sizeof(struct sockaddr_in));
  101. if ( tmp < 0 ) {
  102. showerror("sendto");
  103. exit(-1);
  104. }
  105.  
  106. tmp = close(sock);
  107. if ( tmp < 0 ) {
  108. showerror("close");
  109. exit(-1);
  110. }
  111.  
  112. exit(0);
  113. }
  114.  
  115. void usage ( ) {
  116. write(1, usagemsg, strlen(usagemsg));
  117. }
  118.  
  119. void version ( ) {
  120. write(1, versionmsg, strlen(versionmsg));
  121. }