diff --git a/c/udpmsg/Makefile b/c/udpmsg/Makefile new file mode 100644 index 0000000..abc55a9 --- /dev/null +++ b/c/udpmsg/Makefile @@ -0,0 +1,10 @@ +all: udpmsg udpmsgd + +udpmsg: udpmsg.c + $(CC) $(CFLAGS) $^ -o udpmsg + +udpmsgd: udpmsgd.c + $(CC) $(CFLAGS) $^ -o udpmsgd + +clean: + rm -f udpmsg udpmsgd diff --git a/c/udpmsg/udpmsg.c b/c/udpmsg/udpmsg.c new file mode 100644 index 0000000..7a343a6 --- /dev/null +++ b/c/udpmsg/udpmsg.c @@ -0,0 +1,121 @@ +/* + * udpmsg: send messages via UDP + */ + +/* + * Copyright (c) 2006 Andreas Jaggi + * 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 +#include +#include +#include +#include + +#include "udpmsg.h" + +const char usagemsg[] = + "usage: udpmsg -i -m \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)); +} diff --git a/c/udpmsg/udpmsg.h b/c/udpmsg/udpmsg.h new file mode 100644 index 0000000..52b04ac --- /dev/null +++ b/c/udpmsg/udpmsg.h @@ -0,0 +1,49 @@ +#ifndef _UDPMSG_H +#define _UDPMSG_H + +#include +#include + +#define UDPMSG_PORT 4321 +#define UDPMSG_LEN 512 + +void showerror ( char* type ) { + char* errstr = strerror(errno); + write(1, type, strlen(type)); + write(1, ": ", 2); + write(1, errstr, strlen(errstr)); + write(1, "\n", 1); +} + +const char licensemsg[] = + "rexec/rexecd is copyright (c) 2006 Andreas Jaggi \n" + "All rights reserved.\n" + "\n" + "Redistribution and use in source and binary forms, with or without\n" + "modification, are permitted provided that the following conditions\n" + "are met:\n" + "1. Redistributions of source code must retain the above copyright\n" + " notice, this list of conditions and the following disclaimer.\n" + "2. Redistributions in binary form must reproduce the above copyright\n" + " notice, this list of conditions and the following disclaimer in the\n" + " documentation and/or other materials provided with the distribution.\n" + "3. The name of the author may not be used to endorse or promote products\n" + " derived from this software without specific prior written permission.\n" + "\n" + "THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n" + "IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n" + "OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n" + "IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n" + "INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n" + "NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n" + "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n" + "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n" + "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n" + "THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n" + ; + +void license ( ) { + write(1, licensemsg, strlen(licensemsg)); +} + +#endif diff --git a/c/udpmsg/udpmsgd.c b/c/udpmsg/udpmsgd.c new file mode 100644 index 0000000..0a14bba --- /dev/null +++ b/c/udpmsg/udpmsgd.c @@ -0,0 +1,101 @@ +/* + * udpmsgd: recieve messages via UDP + */ + +/* + * Copyright (c) 2006 Andreas Jaggi + * 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 +#include +#include + +#include "udpmsg.h" + +const char usagemsg[] = + "usage: udpmsgd\n" + "\n" + " options: udpmsgd -v show version\n" + " udpmsgd -h display this help\n" + " udpmsgd -l display (BSD) license\n" + ; + +const char versionmsg[] = "udpmsgd 0.2\n"; + +void usage ( ); +void version ( ); + +int main ( int argc, char* argv[] ) { + int sock; + int tmp; + struct sockaddr_in *from; + struct sockaddr_in *local_addr; + socklen_t *fromlen; + char msg[UDPMSG_LEN+1]; + + sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if ( sock < 0 ) { + showerror("socket"); + exit(-1); + } + + local_addr = alloca(sizeof(struct sockaddr_in)); + local_addr->sin_family = AF_INET; + local_addr->sin_port = UDPMSG_PORT; + local_addr->sin_addr.s_addr = INADDR_ANY; + + tmp = bind(sock, (struct sockaddr*)local_addr, sizeof(struct sockaddr_in)); + if ( tmp < 0 ) { + showerror("bind"); + exit(-1); + } + + while ( 1 ) { + tmp = recvfrom(sock, msg, UDPMSG_LEN, 0, (struct sockaddr*)from, fromlen); + if ( tmp < 0 ) { + showerror("recvfrom"); + exit(-1); + } + msg[tmp] = '\n'; + write(1,msg,tmp+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)); +}