Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 80 additions & 94 deletions src/libsam3/libsam3.c
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
/*
* Copyright © 2023 I2P
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the “Software”), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
/* 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.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://git.idk.i2p/i2p-hackers/libsam3/
*/

* I2P-Bote:
* 5m77dFKGEq6~7jgtrfw56q3t~SmfwZubmGdyOLQOPoPp8MYwsZ~pfUCwud6LB1EmFxkm4C3CGlzq-hVs9WnhUV
* we are the Borg. */
#include "libsam3.h"

#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -38,40 +21,30 @@

#ifdef __MINGW32__
//#include <winsock.h>
#include <windows.h>
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif
#define close closesocket
#endif

#if defined(__unix__) || defined(__APPLE__)
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#endif

#if defined(__unix__) && !defined(__APPLE__)
#ifndef __APPLE__
#include <sys/sysinfo.h>
#endif

#if defined(__APPLE__)
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
#include <mach/mach_time.h>
uint32_t TickCount() {
uint64_t mat = mach_absolute_time();
uint32_t mul = 0x80d9594e;
return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
}
#endif

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -96,8 +69,14 @@ int sam3tcpSetTimeoutSend(int fd, int timeoutms) {
struct timeval tv;
//
ms2timeval(&tv, timeoutms);
return (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0 ? -1
#ifdef _WIN32
DWORD timeout = timeoutms;
return (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&timeout, sizeof(timeout)) < 0 ? -1
: 0);
#else
return (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv)) < 0 ? -1
: 0);
#endif
}
return -1;
}
Expand All @@ -107,8 +86,14 @@ int sam3tcpSetTimeoutReceive(int fd, int timeoutms) {
struct timeval tv;
//
ms2timeval(&tv, timeoutms);
return (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0 ? -1
#ifdef _WIN32
DWORD timeout = timeoutms;
return (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) < 0 ? -1
: 0);
#else
return (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) < 0 ? -1
: 0);
#endif
}
return -1;
}
Expand Down Expand Up @@ -148,7 +133,7 @@ int sam3tcpConnectIP(uint32_t ip, int port) {
}
}
//
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (const char*)&val, sizeof(val));
//
if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
if (libsam3_debug)
Expand Down Expand Up @@ -555,7 +540,7 @@ SAMFieldList *sam3ParseReply(const char *rep) {
// first item is always 2-word reply, with first word in name and second in
// value
SAMFieldList *sam3ReadReply(int fd) {
char rep[2048]; // should be enough for any reply
char rep[8192]; // should be enough for any reply
//
if (sam3tcpReceiveStr(fd, rep, sizeof(rep)) < 0)
return NULL;
Expand Down Expand Up @@ -643,27 +628,22 @@ static inline uint32_t hashint(uint32_t a) {
}

static uint32_t genSeed(void) {
volatile uint32_t seed = 1;
uint32_t res;
#ifndef WIN32
#ifndef __APPLE__
struct sysinfo sy;
pid_t pid = getpid();
//
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#else
res = hashint((uint32_t)getpid()) ^
hashint((uint32_t)TickCount());
#endif
#else
#if defined(__linux__)
struct sysinfo sy;
pid_t pid = getpid();
sysinfo(&sy);
res = hashint((uint32_t)pid) ^ hashint((uint32_t)time(NULL)) ^
hashint((uint32_t)sy.sharedram) ^ hashint((uint32_t)sy.bufferram) ^
hashint((uint32_t)sy.uptime);
#elif defined(WIN32)
res = hashint((uint32_t)GetCurrentProcessId()) ^
hashint((uint32_t)GetTickCount());
#else
res = hashint((uint32_t)getpid()) ^ hashint((uint32_t)time(NULL));
#endif
res += __sync_fetch_and_add(&seed, 1);
//

// Return hashint(res) if that's what the original code did
return hashint(res);
}

Expand Down Expand Up @@ -758,23 +738,29 @@ int sam3GenerateKeys(Sam3Session *ses, const char *hostname, int port,
return -1;
}
//
if (sam3tcpPrintf(fd, "DEST GENERATE %s\n", sigtypes[(int)sigType]) < 0) {
if (sam3tcpPrintf(fd, "DEST GENERATE %s\n", sigtypes[sigType]) >= 0) {
if ((rep = sam3ReadReply(fd)) != NULL &&
sam3IsGoodReply(rep, "DEST", "REPLY", NULL, NULL)) {
const char *pub = sam3FindField(rep, "PUB"),
*priv = sam3FindField(rep, "PRIV");
//
if (pub != NULL && sam3CheckValidKeyLength(pub) && priv != NULL &&
strlen(priv) >= SAM3_PRIVKEY_MIN_SIZE) {
strncpy(ses->pubkey, pub, sizeof(ses->pubkey) - 1);
ses->pubkey[sizeof(ses->pubkey) - 1] = 0;
strncpy(ses->privkey, priv, sizeof(ses->privkey) - 1);
ses->privkey[sizeof(ses->privkey) - 1] = 0;
res = 0;
} else {
if (pub == NULL || !sam3CheckValidKeyLength(pub)) strcpyerr(ses, "PUBKEY_ERROR");
else strcpyerr(ses, "PRIVKEY_ERROR");
}
} else {
strcpyerr(ses, "DEST_ERROR");
}
} else {
strcpyerr(ses, "DEST_ERROR");
}

rep = sam3ReadReply(fd);
// sam3DumpFieldList(rep);
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PUB", NULL)) {
strcpyerr(ses, "PUBKEY_ERROR");
}
if (!sam3IsGoodReply(rep, "DEST", "REPLY", "PRIV", NULL)) {
strcpyerr(ses, "PRIVKEY_ERROR");
}
const char *pub = sam3FindField(rep, "PUB");
strcpy(ses->pubkey, pub);
const char *priv = sam3FindField(rep, "PRIV");
strcpy(ses->privkey, priv);
res = 0;
//
sam3FreeFieldList(rep);
sam3tcpDisconnect(fd);
Expand Down Expand Up @@ -804,7 +790,8 @@ int sam3NameLookup(Sam3Session *ses, const char *hostname, int port,
//
if (strcmp(rs, "OK") == 0) {
if (pub != NULL && sam3CheckValidKeyLength(pub)) {
strcpy(ses->destkey, pub);
strncpy(ses->destkey, pub, sizeof(ses->destkey) - 1);
ses->destkey[sizeof(ses->destkey) - 1] = 0;
strcpyerr(ses, NULL);
res = 0;
}
Expand Down Expand Up @@ -931,18 +918,19 @@ int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
strlen(v) < SAM3_PRIVKEY_MIN_SIZE) {
if (libsam3_debug)
fprintf(stderr, "sam3CreateSession: invalid reply (%ld)...\n",
(v != NULL ? strlen(v) : -1));
(long)(v != NULL ? strlen(v) : -1));
if (libsam3_debug)
sam3DumpFieldList(rep);
sam3FreeFieldList(rep);
goto error;
}
// save our keys
if (strlen(v) > SAM3_PRIVKEY_MAX_SIZE) {
fprintf(stderr, "ERROR, Unexpected key size (%li)!\n", strlen(v));
fprintf(stderr, "ERROR, Unexpected key size (%li)!\n", (long)strlen(v));
goto error;
}
strcpy(ses->privkey, v);
strncpy(ses->privkey, v, sizeof(ses->privkey) - 1);
ses->privkey[sizeof(ses->privkey) - 1] = 0;
sam3FreeFieldList(rep);
// get public key
if (sam3tcpPrintf(ses->fd, "NAMING LOOKUP NAME=ME\n") < 0)
Expand All @@ -955,13 +943,14 @@ int sam3CreateSession(Sam3Session *ses, const char *hostname, int port,
!sam3CheckValidKeyLength(v)) {
if (libsam3_debug)
fprintf(stderr, "sam3CreateSession: invalid NAMING reply (%ld)...\n",
(v != NULL ? strlen(v) : -1));
(long)(v != NULL ? strlen(v) : -1));
if (libsam3_debug)
sam3DumpFieldList(rep);
sam3FreeFieldList(rep);
goto error;
}
strcpy(ses->pubkey, v);
strncpy(ses->pubkey, v, sizeof(ses->pubkey) - 1);
ses->pubkey[sizeof(ses->pubkey) - 1] = 0;
sam3FreeFieldList(rep);
//
if (libsam3_debug)
Expand All @@ -978,12 +967,6 @@ Sam3Connection *sam3StreamConnect(Sam3Session *ses, const char *destkey) {
SAMFieldList *rep;
Sam3Connection *conn;
//
for (size_t i = 0; destkey[i] != 0; i++){
if (destkey[i] == '\n'){
strcpyerr(ses, "INVALID_KEY_SYMBOLS");
return NULL;
}
}
if (ses->type != SAM3_SESSION_STREAM) {
strcpyerr(ses, "INVALID_SESSION_TYPE");
return NULL;
Expand Down Expand Up @@ -1029,7 +1012,8 @@ Sam3Connection *sam3StreamConnect(Sam3Session *ses, const char *destkey) {
}
sam3FreeFieldList(rep);
if (conn != NULL) {
strcpy(conn->destkey, destkey);
strncpy(conn->destkey, destkey, sizeof(conn->destkey) - 1);
conn->destkey[sizeof(conn->destkey) - 1] = 0;
conn->ses = ses;
conn->next = ses->connlist;
ses->connlist = conn;
Expand All @@ -1046,7 +1030,7 @@ Sam3Connection *sam3StreamConnect(Sam3Session *ses, const char *destkey) {
Sam3Connection *sam3StreamAccept(Sam3Session *ses) {
if (ses != NULL) {
SAMFieldList *rep = NULL;
char repstr[1024];
char repstr[8192];
Sam3Connection *conn;
//
if (ses->type != SAM3_SESSION_STREAM) {
Expand Down Expand Up @@ -1097,7 +1081,8 @@ Sam3Connection *sam3StreamAccept(Sam3Session *ses) {
goto error;
}
sam3FreeFieldList(rep);
strcpy(conn->destkey, repstr);
strncpy(conn->destkey, repstr, sizeof(conn->destkey) - 1);
conn->destkey[sizeof(conn->destkey) - 1] = 0;
conn->ses = ses;
conn->next = ses->connlist;
ses->connlist = conn;
Expand Down Expand Up @@ -1190,12 +1175,11 @@ int sam3DatagramSend(Sam3Session *ses, const char *destkey, const void *buf,
strcpyerr(ses, "INVALID_DATA");
return -1;
}
dbufsz = bufsize + 4 + strlen(destkey) + 1 + strlen(ses->channel) + 1;
dbufsz = bufsize + 4 + SAM3_PUBKEY_SIZE + 1 + strlen(ses->channel) + 1;
if ((dbuf = malloc(dbufsz)) == NULL) {
strcpyerr(ses, "OUT_OF_MEMORY");
return -1;
}
memset(dbuf, 0, dbufsz);
sprintf(dbuf, "3.0 %s %s\n", ses->channel, destkey);
memcpy(dbuf + strlen(dbuf), buf, bufsize);
res = sam3udpSendToIP(ses->ip, ses->port, dbuf, dbufsz);
Expand Down Expand Up @@ -1235,8 +1219,10 @@ ssize_t sam3DatagramReceive(Sam3Session *ses, void *buf, size_t bufsize) {
}
//
if ((v = sam3FindField(rep, "DESTINATION")) != NULL &&
sam3CheckValidKeyLength(v))
strncpy(ses->destkey, v, sizeof(ses->destkey));
sam3CheckValidKeyLength(v)) {
strncpy(ses->destkey, v, sizeof(ses->destkey) - 1);
ses->destkey[sizeof(ses->destkey) - 1] = 0;
}
v = sam3FindField(rep, "SIZE"); // we have this field -- for sure
if (!v[0] || !isdigit(*v)) {
strcpyerr(ses, "I2P_ERROR_SIZE");
Expand Down
Loading