Newer
Older
src / c / udpmsg / udpmsg.c
/*
 * udpmsg: send messages via UDP
 */

/*
 * Copyright (c) 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "udpmsg.h"

const char usagemsg[] =
	"usage: udpmsg -i <IP> -m <message>\n"
	"\n"
	"arguments:\n"
	"    -i IP         send to host with address IP\n"
	"    -m message    message to send\n"
	" also: udpmsg -v    show version\n"
	"       udpmsg -h    display this help\n"
	"       udpmsg -l    display (BSD) license\n"
	;

const char versionmsg[] = "udpmsg 0.3\n";

void usage ( );
void version ( );

int main ( int argc, char* argv[] ) {
	int sock;
	int tmp;
	in_addr_t remote_addr = 0;
	char* msg = NULL;
	struct sockaddr_in *destination = NULL;

	while ( (tmp = getopt(argc, argv, "i:m:hlv")) != -1 ) {
		switch ( tmp ) {
			case 'h':
				usage();
				exit(0);
			case 'l':
				license();
				exit(0);
			case 'v':
				version();
				exit(0);
			case 'i':
				remote_addr = inet_addr(optarg);
				break;
			case 'm':
				msg = optarg;
				break;
		}
	}

	if ( (remote_addr == 0) || (msg == NULL) ) {
		usage();
		exit(-1);
	}

	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if ( sock < 0 ) {
		showerror("socket");
		exit(-1);
	}

	destination = alloca(sizeof(struct sockaddr_in));

	destination->sin_family = AF_INET;
	destination->sin_addr.s_addr = remote_addr;
	destination->sin_port = UDPMSG_PORT;

	tmp = sendto(sock, msg, strlen(msg)+1, 0, (struct sockaddr*)destination, sizeof(struct sockaddr_in));
	if ( tmp < 0 ) {
		showerror("sendto");
		exit(-1);
	}

	tmp = close(sock);
	if ( tmp < 0 ) {
		showerror("close");
		exit(-1);
	}

	exit(0);
}

void usage ( ) {
	write(1, usagemsg, strlen(usagemsg));
}

void version ( ) {
	write(1, versionmsg, strlen(versionmsg));
}