update libsam3

This commit is contained in:
sehraf 2020-11-19 17:12:15 +01:00
parent 25cb152a7e
commit 47a4b726a3
No known key found for this signature in database
GPG Key ID: DF09F6EAE356B2C6
8 changed files with 323 additions and 42 deletions

View File

@ -16,7 +16,6 @@ OBJS := ${LIB_OBJS} ${TEST_OBJS}
LIB := libsam3.a LIB := libsam3.a
all: build check all: build check
check: libsam3-tests check: libsam3-tests
@ -34,8 +33,11 @@ clean:
rm -f libsam3-tests ${LIB} ${OBJS} examples/sam3/samtest rm -f libsam3-tests ${LIB} ${OBJS} examples/sam3/samtest
%.o: %.c Makefile %.o: %.c Makefile
${CC} ${CFLAGS} -c $< -o $@ ${CC} ${CFLAGS} $(LDFLAGS) -c $< -o $@
fmt: fmt:
find . -name '*.c' -exec clang-format -i {} \; find . -name '*.c' -exec clang-format -i {} \;
find . -name '*.h' -exec clang-format -i {} \; find . -name '*.h' -exec clang-format -i {} \;
info:
@echo $(AR)

View File

@ -6,7 +6,7 @@ A C library for the [SAM v3 API](https://geti2p.net/en/docs/api/samv3).
## Development Status ## Development Status
Unmaintained, but PRs welcome! Maintained by idk, PRs are accepted on [I2P gitlab](https://i2pgit.org/i2p-hackers/libsam3)/[I2P gitlab](http://git.idk.i2p/i2p-hackers/libsam3), and on github at the official mirror repository: [i2p/libsam3](https://github.com/i2p/libsam3).
## Usage ## Usage
@ -16,3 +16,37 @@ Copy the two files from one of the following locations into your codebase:
- `src/libsam3a` - Asynchronous implementation. - `src/libsam3a` - Asynchronous implementation.
See `examples/` for how to use various parts of the API. See `examples/` for how to use various parts of the API.
## Cross-Compiling for Windows from debian:
Set your cross-compiler up:
``` sh
export CC=x86_64-w64-mingw32-gcc
export CFLAGS='-Wall -O2 '
export LDFLAGS='-lmingw32 -lws2_32 -lwsock32 -mwindows'
```
and run `make build`. Only libsam3 is available for Windows, libsam3a will be
made available at a later date.
`
## Linker(Windows)
When building for Windows remember to set the flags to link to the Winsock and Windows
libraries.
`-lmingw32 -lws2_32 -lwsock32 -mwindows`
This may apply when cross-compiling or compiling from Windows with mingw.
## Cool Projects using libsam3
Are you using libsam3 to provide an a cool I2P based feature to your project? Let us know about it(and how
it uses libsam3) and we'll think about adding it here*!
1. [Retroshare](https://retroshare.cc)
*Projects which are listed here must be actively maintained. Those which intentionally violate
the law or the rights of a person or persons directly won't be considered. Neither will obvious
trolling. The maintainer will make the final decision.

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 (sam3CreateSilentSession(&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 (sam3CreateSilentSession(&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;
}

View File

@ -11,7 +11,6 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <netdb.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -20,16 +19,27 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#ifdef __MINGW32__
//#include <winsock.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
#endif
#ifdef __unix__
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/types.h> #include <sys/types.h>
#endif
#ifdef WINDOWS_SYS
#include <winsock.h>
#endif // WINDOWS_SYS
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int libsam3_debug = 0; int libsam3_debug = 0;
@ -104,11 +114,6 @@ int sam3tcpConnectIP(uint32_t ip, int port) {
} }
} }
// //
// Set this for all outgoing SAM connections. Most SAM commands should be answered rather fast except CREATE SESSION maybe.
// This should be enough to let SAM establish a session.
sam3tcpSetTimeoutSend(fd, 5 * 60 * 1000);
sam3tcpSetTimeoutReceive(fd, 5 * 60 * 1000);
//
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
// //
if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
@ -161,13 +166,8 @@ int sam3tcpConnect(const char *hostname, int port, uint32_t *ip) {
// <0: error; 0: ok // <0: error; 0: ok
int sam3tcpDisconnect(int fd) { int sam3tcpDisconnect(int fd) {
if (fd >= 0) { if (fd >= 0) {
#ifndef WINDOWS_SYS // ie UNIX shutdown(fd, SHUT_RDWR);
shutdown(fd, SHUT_RDWR); return close(fd);
return close(fd);
#else
return closesocket(fd);
#endif
} }
// //
return -1; return -1;
@ -823,6 +823,18 @@ int sam3CloseSession(Sam3Session *ses) {
return -1; return -1;
} }
int sam3CreateSilentSession(Sam3Session *ses, const char *hostname, int port,
const char *privkey, Sam3SessionType type,
Sam3SigType sigType, const char *params) {
int r =
sam3CreateSession(ses, hostname, port, privkey, type, sigType, params);
if (r != 0) {
return r;
}
ses->silent = true;
return 0;
}
int sam3CreateSession(Sam3Session *ses, const char *hostname, int port, int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
const char *privkey, Sam3SessionType type, const char *privkey, Sam3SessionType type,
Sam3SigType sigType, const char *params) { Sam3SigType sigType, const char *params) {
@ -840,6 +852,7 @@ int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
memset(ses, 0, sizeof(Sam3Session)); memset(ses, 0, sizeof(Sam3Session));
ses->fd = -1; ses->fd = -1;
ses->fwd_fd = -1; ses->fwd_fd = -1;
ses->silent = false;
// //
if (privkey != NULL && strlen(privkey) < SAM3_PRIVKEY_MIN_SIZE) if (privkey != NULL && strlen(privkey) < SAM3_PRIVKEY_MIN_SIZE)
goto error; goto error;
@ -936,8 +949,9 @@ Sam3Connection *sam3StreamConnect(Sam3Session *ses, const char *destkey) {
strcpyerr(ses, "IO_ERROR_SK"); strcpyerr(ses, "IO_ERROR_SK");
goto error; goto error;
} }
if (sam3tcpPrintf(conn->fd, "STREAM CONNECT ID=%s DESTINATION=%s\n", if (sam3tcpPrintf(conn->fd,
ses->channel, destkey) < 0) { "STREAM CONNECT ID=%s DESTINATION=%s SILENT=%s\n",
ses->channel, destkey, checkIsSilent(ses)) < 0) {
strcpyerr(ses, "IO_ERROR"); strcpyerr(ses, "IO_ERROR");
goto error; goto error;
} }
@ -945,16 +959,18 @@ Sam3Connection *sam3StreamConnect(Sam3Session *ses, const char *destkey) {
strcpyerr(ses, "IO_ERROR"); strcpyerr(ses, "IO_ERROR");
goto error; goto error;
} }
if (!sam3IsGoodReply(rep, "STREAM", "STATUS", "RESULT", "OK")) { if (!ses->silent) {
const char *v = sam3FindField(rep, "RESULT"); if (!sam3IsGoodReply(rep, "STREAM", "STATUS", "RESULT", "OK")) {
// const char *v = sam3FindField(rep, "RESULT");
strcpyerr(ses, (v != NULL && v[0] ? v : "I2P_ERROR")); //
sam3CloseConnectionInternal(conn); strcpyerr(ses, (v != NULL && v[0] ? v : "I2P_ERROR"));
free(conn); sam3CloseConnectionInternal(conn);
conn = NULL; free(conn);
} else { conn = NULL;
// no error } else {
strcpyerr(ses, NULL); // no error
strcpyerr(ses, NULL);
}
} }
sam3FreeFieldList(rep); sam3FreeFieldList(rep);
if (conn != NULL) { if (conn != NULL) {
@ -1002,11 +1018,13 @@ Sam3Connection *sam3StreamAccept(Sam3Session *ses) {
strcpyerr(ses, "IO_ERROR_RP"); strcpyerr(ses, "IO_ERROR_RP");
goto error; goto error;
} }
if (!sam3IsGoodReply(rep, "STREAM", "STATUS", "RESULT", "OK")) { if (!ses->silent) {
const char *v = sam3FindField(rep, "RESULT"); if (!sam3IsGoodReply(rep, "STREAM", "STATUS", "RESULT", "OK")) {
// const char *v = sam3FindField(rep, "RESULT");
strcpyerr(ses, (v != NULL && v[0] ? v : "I2P_ERROR_RES")); //
goto error; strcpyerr(ses, (v != NULL && v[0] ? v : "I2P_ERROR_RES"));
goto error;
}
} }
if (sam3tcpReceiveStr(conn->fd, repstr, sizeof(repstr)) < 0) { if (sam3tcpReceiveStr(conn->fd, repstr, sizeof(repstr)) < 0) {
strcpyerr(ses, "IO_ERROR_RP1"); strcpyerr(ses, "IO_ERROR_RP1");
@ -1040,6 +1058,14 @@ Sam3Connection *sam3StreamAccept(Sam3Session *ses) {
return NULL; return NULL;
} }
const char *checkIsSilent(Sam3Session *ses) {
if (ses->silent == true) {
return "true";
} else {
return "false";
}
}
int sam3StreamForward(Sam3Session *ses, const char *hostname, int port) { int sam3StreamForward(Sam3Session *ses, const char *hostname, int port) {
if (ses != NULL) { if (ses != NULL) {
SAMFieldList *rep = NULL; SAMFieldList *rep = NULL;
@ -1060,8 +1086,9 @@ int sam3StreamForward(Sam3Session *ses, const char *hostname, int port) {
strcpyerr(ses, "IO_ERROR_SK"); strcpyerr(ses, "IO_ERROR_SK");
goto error; goto error;
} }
if (sam3tcpPrintf(ses->fwd_fd, "STREAM FORWARD ID=%s PORT=%d HOST=%s SILENT=true\n", if (sam3tcpPrintf(ses->fwd_fd,
ses->channel, port, hostname) < 0) { "STREAM FORWARD ID=%s PORT=%d HOST=%s SILENT=%s\n",
ses->channel, port, hostname, checkIsSilent(ses)) < 0) {
strcpyerr(ses, "IO_ERROR_PF"); strcpyerr(ses, "IO_ERROR_PF");
goto error; goto error;
} }

View File

@ -10,9 +10,19 @@
#ifndef LIBSAM3_H #ifndef LIBSAM3_H
#define LIBSAM3_H #define LIBSAM3_H
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#ifndef _SSIZE_T_DEFINED
#define _SSIZE_T_DEFINED
#undef ssize_t
#ifdef _WIN64
typedef signed int64 ssize_t;
typedef int ssize_t;
#endif /* _WIN64 */
#endif /* _SSIZE_T_DEFINED */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -138,6 +148,7 @@ typedef struct Sam3Session {
int port; // this will be changed to UDP port for DRAM/RAW (can be 0) int port; // this will be changed to UDP port for DRAM/RAW (can be 0)
struct Sam3Connection *connlist; // list of opened connections struct Sam3Connection *connlist; // list of opened connections
int fwd_fd; int fwd_fd;
bool silent;
} Sam3Session; } Sam3Session;
typedef struct Sam3Connection { typedef struct Sam3Connection {
@ -166,6 +177,22 @@ extern int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
const char *privkey, Sam3SessionType type, const char *privkey, Sam3SessionType type,
Sam3SigType sigType, const char *params); Sam3SigType sigType, const char *params);
/*
* create SAM session with SILENT=True
* pass NULL as hostname for 'localhost' and 0 as port for 7656
* pass NULL as privkey to create TRANSIENT session
* 'params' can be NULL
* see http://www.i2p2.i2p/i2cp.html#options for common options,
* and http://www.i2p2.i2p/streaming.html#options for STREAM options
* if result<0: error, 'ses' fields are undefined, no need to call
* sam3CloseSession() if result==0: ok, all 'ses' fields are filled
* TODO: don't clear 'error' field on error (and set it to something meaningful)
*/
extern int sam3CreateSilentSession(Sam3Session *ses, const char *hostname,
int port, const char *privkey,
Sam3SessionType type, Sam3SigType sigType,
const char *params);
/* /*
* close SAM session (and all it's connections) * close SAM session (and all it's connections)
* returns <0 on error, 0 on ok * returns <0 on error, 0 on ok
@ -173,6 +200,13 @@ extern int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
*/ */
extern int sam3CloseSession(Sam3Session *ses); extern int sam3CloseSession(Sam3Session *ses);
/*
* check to see if a SAM session is silent and output
* characters for use with sam3tcpPrintf() checkIsSilent
*/
const char *checkIsSilent(Sam3Session *ses);
/* /*
* Check to make sure that the destination in use is of a valid length, returns * Check to make sure that the destination in use is of a valid length, returns
* 1 if true and 0 if false. * 1 if true and 0 if false.

View File

@ -13,7 +13,6 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <netdb.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -22,13 +21,28 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#ifdef __MINGW32__
//#include <winsock.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
#endif
#ifdef __unix__
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/types.h> #include <sys/types.h>
#endif
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int libsam3a_debug = 0; int libsam3a_debug = 0;

View File

@ -18,6 +18,17 @@
#include <sys/types.h> #include <sys/types.h>
#ifdef __MINGW32__
//#include <winsock.h>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
//#define SOCK_CLOEXEC O_CLOEXEC
//#define SOCK_NONBLOCK O_NONBLOCK
#define SOCK_CLOEXEC 02000000
#define SOCK_NONBLOCK FIONBIO
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif