add fork of libsam3

add funtion to get i2p certificate crypto algo names
This commit is contained in:
sehraf 2020-10-26 21:45:03 +01:00
parent 130d846e47
commit 76f0678820
No known key found for this signature in database
GPG key ID: DF09F6EAE356B2C6
25 changed files with 5858 additions and 7 deletions

View file

@ -0,0 +1,26 @@
Examples
========
These examples show various ways of using libsam3 to enable i2p in your
application, and are also useful in other ways. If you implement an i2p
application library in another language, making variants basic tools wouldn't be
the worst way to make sure that it works.
building
--------
Once you have build the library in the root of this repository by running make
all, you can build all these examples at once by running
make
in this directory. I think it makes things easier to experiment with quickly.
namelookup
----------
Namelookup uses the SAM API to find the base64 destination of an readable "jump"
or base32 i2p address. You can use it like this:
./lookup i2p-projekt.i2p

View file

@ -0,0 +1,116 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
// comment the following if you don't want to stress UDP with 'big' datagram
// seems that up to 32000 bytes can be used for localhost
// note that we need 516+6+? bytes for header; lets reserve 1024 bytes for it
#define BIG (32000 - 1024)
#define KEYFILE "dgrams.key"
int main(int argc, char *argv[]) {
Sam3Session ses;
char buf[1024];
char destkey[517] = {0}; // 516 chars + \0
int sz;
//
libsam3_debug = 1;
//
if (argc < 2) {
FILE *fl = fopen(KEYFILE, "rb");
//
if (fl != NULL) {
if (fread(destkey, 516, 1, fl) == 1) {
fclose(fl);
goto ok;
}
fclose(fl);
}
printf("usage: dgramc PUBKEY\n");
return 1;
} else {
if (strlen(argv[1]) != 516) {
fprintf(stderr, "FATAL: invalid key length!\n");
return 1;
}
strcpy(destkey, argv[1]);
}
//
ok:
printf("creating session...\n");
/* create TRANSIENT session with temporary disposible destination */
if (sam3CreateSession(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT,
SAM3_DESTINATION_TRANSIENT, SAM3_SESSION_DGRAM, 4,
NULL) < 0) {
fprintf(stderr, "FATAL: can't create session\n");
return 1;
}
/* send datagram */
printf("sending test datagram...\n");
if (sam3DatagramSend(&ses, destkey, "test", 4) < 0) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
/** receive reply */
if ((sz = sam3DatagramReceive(&ses, buf, sizeof(buf) - 1)) < 0) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
/** null terminated string */
buf[sz] = 0;
printf("received: [%s]\n", buf);
//
#ifdef BIG
{
char *big = calloc(BIG + 1024, sizeof(char));
/** generate random string */
sam3GenChannelName(big, BIG + 1023, BIG + 1023);
printf("sending BIG datagram...\n");
if (sam3DatagramSend(&ses, destkey, big, BIG) < 0) {
free(big);
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
if ((sz = sam3DatagramReceive(&ses, big, BIG + 512)) < 0) {
free(big);
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
big[sz] = 0;
printf("received (%d): [%s]\n", sz, big);
free(big);
}
#endif
//
printf("sending quit datagram...\n");
if (sam3DatagramSend(&ses, destkey, "quit", 4) < 0) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
if ((sz = sam3DatagramReceive(&ses, buf, sizeof(buf) - 1)) < 0) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
buf[sz] = 0;
printf("received: [%s]\n", buf);
//
sam3CloseSession(&ses);
return 0;
error:
sam3CloseSession(&ses);
return 1;
}

View file

@ -0,0 +1,113 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
#define KEYFILE "dgrams.key"
int main(int argc, char *argv[]) {
Sam3Session ses;
char privkey[1024], pubkey[1024], buf[33 * 1024];
/** quit command */
const char *quitstr = "quit";
const size_t quitlen = strlen(quitstr);
/** reply response */
const char *replystr = "reply: ";
const size_t replylen = strlen(replystr);
FILE *fl;
//
libsam3_debug = 1;
//
/** generate new destination keypair */
printf("generating keys...\n");
if (sam3GenerateKeys(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, 4) < 0) {
fprintf(stderr, "FATAL: can't generate keys\n");
return 1;
}
/** copy keypair into local buffer */
strncpy(pubkey, ses.pubkey, sizeof(pubkey));
strncpy(privkey, ses.privkey, sizeof(privkey));
/** create sam session */
printf("creating session...\n");
if (sam3CreateSession(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, privkey,
SAM3_SESSION_DGRAM, 5, NULL) < 0) {
fprintf(stderr, "FATAL: can't create session\n");
return 1;
}
/** make sure we have the right destination */
// FIXME: probably not needed
if (strcmp(pubkey, ses.pubkey) != 0) {
fprintf(stderr, "FATAL: destination keys don't match\n");
sam3CloseSession(&ses);
return 1;
}
/** print destination to stdout */
printf("PUB KEY\n=======\n%s\n=======\n", ses.pubkey);
if ((fl = fopen(KEYFILE, "wb")) != NULL) {
/** write public key to keyfile */
fwrite(pubkey, strlen(pubkey), 1, fl);
fclose(fl);
}
/* now listen for UDP packets */
printf("starting main loop...\n");
for (;;) {
/** save replylen bytes for out reply at begining */
char *datagramBuf = buf + replylen;
const size_t datagramMaxLen = sizeof(buf) - replylen;
int sz, isquit;
printf("waiting for datagram...\n");
/** blocks until we get a UDP packet */
if ((sz = sam3DatagramReceive(&ses, datagramBuf, datagramMaxLen) < 0)) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
/** ensure null terminated string */
datagramBuf[sz] = 0;
/** print out datagram payload to user */
printf("FROM\n====\n%s\n====\n", ses.destkey);
printf("SIZE=%d\n", sz);
printf("data: [%s]\n", datagramBuf);
/** check for "quit" */
isquit = (sz == quitlen && memcmp(datagramBuf, quitstr, quitlen) == 0);
/** echo datagram back to sender with "reply: " at the beginning */
memcpy(buf, replystr, replylen);
if (sam3DatagramSend(&ses, ses.destkey, buf, sz + replylen) < 0) {
fprintf(stderr, "ERROR: %s\n", ses.error);
goto error;
}
/** if we got a quit command wait for 10 seconds and break out of the
* mainloop */
if (isquit) {
printf("shutting down...\n");
sleep(10); /* let dgram reach it's destination */
break;
}
}
/** close session and delete keyfile */
sam3CloseSession(&ses);
unlink(KEYFILE);
return 0;
error:
/** error case, close session, delete keyfile and return exit code 1 */
sam3CloseSession(&ses);
unlink(KEYFILE);
return 1;
}

View file

@ -0,0 +1,43 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
int main(int argc, char *argv[]) {
Sam3Session ses;
//
//
libsam3_debug = 1;
//
if (argc < 2) {
printf("usage: %s name [name...]\n", argv[0]);
return 1;
}
/** for each name in arguments ... */
for (int n = 1; n < argc; ++n) {
if (!getenv("I2P_LOOKUP_QUIET")) {
fprintf(stdout, "%s ... ", argv[n]);
fflush(stdout);
}
/** do oneshot name lookup */
if (sam3NameLookup(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, argv[n]) >=
0) {
fprintf(stdout, "%s\n\n", ses.destkey);
} else {
fprintf(stdout, "FAILED [%s]\n", ses.error);
}
}
//
return 0;
}

View file

@ -0,0 +1,51 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
int main(int argc, char *argv[]) {
int fd;
SAMFieldList *rep = NULL;
const char *v;
//
libsam3_debug = 1;
//
//
if ((fd = sam3Handshake(NULL, 0, NULL)) < 0)
return 1;
//
if (sam3tcpPrintf(fd, "DEST GENERATE\n") < 0)
goto error;
rep = sam3ReadReply(fd);
// sam3DumpFieldList(rep);
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PUB", NULL))
goto error;
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PRIV", NULL))
goto error;
v = sam3FindField(rep, "PUB");
printf("PUB KEY\n=======\n%s\n", v);
v = sam3FindField(rep, "PRIV");
printf("PRIV KEY\n========\n%s\n", v);
sam3FreeFieldList(rep);
rep = NULL;
//
sam3FreeFieldList(rep);
sam3tcpDisconnect(fd);
return 0;
error:
sam3FreeFieldList(rep);
sam3tcpDisconnect(fd);
return 1;
}

View file

@ -0,0 +1,87 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
#define KEYFILE "streams.key"
int main(int argc, char *argv[]) {
Sam3Session ses;
Sam3Connection *conn;
char cmd[1024], destkey[617]; // 616 chars + \0
//
libsam3_debug = 1;
//
memset(destkey, 0, sizeof(destkey));
//
if (argc < 2) {
FILE *fl = fopen(KEYFILE, "rb");
//
if (fl != NULL) {
if (fread(destkey, 616, 1, fl) == 1) {
fclose(fl);
goto ok;
}
fclose(fl);
}
printf("usage: streamc PUBKEY\n");
return 1;
} else {
if (!sam3CheckValidKeyLength(argv[1])) {
fprintf(stderr, "FATAL: invalid key length! %s %lu\n", argv[1],
strlen(argv[1]));
return 1;
}
strcpy(destkey, argv[1]);
}
//
ok:
printf("creating session...\n");
// create TRANSIENT session
if (sam3CreateSession(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT,
SAM3_DESTINATION_TRANSIENT, SAM3_SESSION_STREAM, 4,
NULL) < 0) {
fprintf(stderr, "FATAL: can't create session\n");
return 1;
}
//
printf("connecting...\n");
if ((conn = sam3StreamConnect(&ses, destkey)) == NULL) {
fprintf(stderr, "FATAL: can't connect: %s\n", ses.error);
sam3CloseSession(&ses);
return 1;
}
//
// now waiting for incoming connection
printf("sending test command...\n");
if (sam3tcpPrintf(conn->fd, "test\n") < 0)
goto error;
if (sam3tcpReceiveStr(conn->fd, cmd, sizeof(cmd)) < 0)
goto error;
printf("echo: %s\n", cmd);
//
printf("sending quit command...\n");
if (sam3tcpPrintf(conn->fd, "quit\n") < 0)
goto error;
//
sam3CloseConnection(conn);
sam3CloseSession(&ses);
return 0;
error:
fprintf(stderr, "FATAL: some error occured!\n");
sam3CloseConnection(conn);
sam3CloseSession(&ses);
return 1;
}

View file

@ -0,0 +1,72 @@
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*
* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../libsam3/libsam3.h"
#define KEYFILE "streams.key"
int main(int argc, char *argv[]) {
Sam3Session ses;
Sam3Connection *conn;
FILE *fl;
//
libsam3_debug = 1;
//
printf("creating session...\n");
// create TRANSIENT session
if (sam3CreateSession(&ses, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT,
SAM3_DESTINATION_TRANSIENT, SAM3_SESSION_STREAM, 4,
NULL) < 0) {
fprintf(stderr, "FATAL: can't create session\n");
return 1;
}
//
printf("PUB KEY\n=======\n%s\n=======\n", ses.pubkey);
if ((fl = fopen(KEYFILE, "wb")) != NULL) {
fwrite(ses.pubkey, strlen(ses.pubkey), 1, fl);
fclose(fl);
}
//
printf("starting stream acceptor...\n");
if ((conn = sam3StreamAccept(&ses)) == NULL) {
fprintf(stderr, "FATAL: can't accept: %s\n", ses.error);
sam3CloseSession(&ses);
return 1;
}
printf("FROM\n====\n%s\n====\n", conn->destkey);
//
printf("starting main loop...\n");
for (;;) {
char cmd[256];
//
if (sam3tcpReceiveStr(conn->fd, cmd, sizeof(cmd)) < 0)
goto error;
printf("cmd: [%s]\n", cmd);
if (strcmp(cmd, "quit") == 0)
break;
// echo command
if (sam3tcpPrintf(conn->fd, "re: %s\n", cmd) < 0)
goto error;
}
//
sam3CloseSession(&ses);
unlink(KEYFILE);
return 0;
error:
fprintf(stderr, "FATAL: some error occured!\n");
sam3CloseSession(&ses);
unlink(KEYFILE);
return 1;
}