mirror of
https://github.com/rofl0r/proxychains-ng
synced 2026-05-21 21:29:53 +08:00
experimental new feature: proxy_dns_daemon
since many users complain about issues with modern, ultracomplex clusterfuck software such as chromium, nodejs, etc, i've reconsidered one of my original ideas how to implement remote dns lookup support. instead of having a background thread serving requests via a pipe, the user manually starts a background daemon process before running proxychains, and the two processes then communicate via UDP. this requires much less hacks (like hooking of close() to prevent pipes from getting closed) and doesn't need to call any async-signal unsafe code like malloc(). this means it should be much more compatible than the previous method, however it's not as practical and slightly slower. it's recommended that the proxychains4-daemon runs on localhost, and if you use proxychains-ng a lot you might want to set ip up as a service that starts on boot. a single proxychains4-daemon should theoretically be able to serve many parallel proxychains4 instances, but this has not yet been tested so far. it's also possible to run the daemon on other computers, even over internet, but currently there is no error-checking/ timeout code at all; that means the UDP connection needs to be very stable. the library code used for the daemon sources are from my projects libulz[0] and htab[1], and the server code is loosely based on microsocks[2]. their licenses are all compatible with the GPL. if not otherwise mentioned, they're released for this purpose under the standard proxychains-ng license (see COPYING). [0]: https://github.com/rofl0r/libulz [1]: https://github.com/rofl0r/htab [2]: https://github.com/rofl0r/microsocks
This commit is contained in:
@@ -0,0 +1,231 @@
|
||||
/*
|
||||
proxychains-ng DNS daemon
|
||||
|
||||
Copyright (C) 2020 rofl0r.
|
||||
|
||||
*/
|
||||
|
||||
#define _GNU_SOURCE
|
||||
#include <unistd.h>
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <signal.h>
|
||||
#include <sys/select.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include "udpserver.h"
|
||||
#include "sblist.h"
|
||||
#include "hsearch.h"
|
||||
#include "../remotedns.h"
|
||||
#include "../ip_type.h"
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
#endif
|
||||
|
||||
static struct htab *ip_lookup_table;
|
||||
static sblist *hostnames;
|
||||
static unsigned remote_subnet;
|
||||
static const struct server* server;
|
||||
|
||||
#ifndef CONFIG_LOG
|
||||
#define CONFIG_LOG 1
|
||||
#endif
|
||||
#if CONFIG_LOG
|
||||
/* we log to stderr because it's not using line buffering, i.e. malloc which would need
|
||||
locking when called from different threads. for the same reason we use dprintf,
|
||||
which writes directly to an fd. */
|
||||
#define dolog(...) dprintf(2, __VA_ARGS__)
|
||||
#else
|
||||
static void dolog(const char* fmt, ...) { }
|
||||
#endif
|
||||
|
||||
static char* my_inet_ntoa(unsigned char *ip_buf_4_bytes, char *outbuf_16_bytes) {
|
||||
unsigned char *p;
|
||||
char *o = outbuf_16_bytes;
|
||||
unsigned char n;
|
||||
for(p = ip_buf_4_bytes; p < ip_buf_4_bytes + 4; p++) {
|
||||
n = *p;
|
||||
if(*p >= 100) {
|
||||
if(*p >= 200)
|
||||
*(o++) = '2';
|
||||
else
|
||||
*(o++) = '1';
|
||||
n %= 100;
|
||||
}
|
||||
if(*p >= 10) {
|
||||
*(o++) = (n / 10) + '0';
|
||||
n %= 10;
|
||||
}
|
||||
*(o++) = n + '0';
|
||||
*(o++) = '.';
|
||||
}
|
||||
o[-1] = 0;
|
||||
return outbuf_16_bytes;
|
||||
}
|
||||
|
||||
|
||||
/* buf needs to be long enough for an ipv6 addr, i.e. INET6_ADDRSTRLEN + 1 */
|
||||
static char* ipstr(union sockaddr_union *su, char* buf) {
|
||||
int af = SOCKADDR_UNION_AF(su);
|
||||
void *ipdata = SOCKADDR_UNION_ADDRESS(su);
|
||||
inet_ntop(af, ipdata, buf, INET6_ADDRSTRLEN+1);
|
||||
char portbuf[7];
|
||||
snprintf(portbuf, sizeof portbuf, ":%u", (unsigned) ntohs(SOCKADDR_UNION_PORT(su)));
|
||||
strcat(buf, portbuf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int usage(char *a0) {
|
||||
dprintf(2,
|
||||
"Proxychains-NG remote dns daemon\n"
|
||||
"--------------------------------\n"
|
||||
"usage: %s -i listenip -p port -r remotesubnet\n"
|
||||
"all arguments are optional.\n"
|
||||
"by default listenip is 127.0.0.1, port 1053 and remotesubnet 224.\n\n", a0
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned index_from_ip(ip_type4 internalip) {
|
||||
ip_type4 tmp = internalip;
|
||||
uint32_t ret;
|
||||
ret = tmp.octet[3] + (tmp.octet[2] << 8) + (tmp.octet[1] << 16);
|
||||
ret -= 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *host_from_ip(ip_type4 internalip) {
|
||||
char *res = NULL;
|
||||
unsigned index = index_from_ip(internalip);
|
||||
if(index < sblist_getsize(hostnames)) {
|
||||
char **tmp = sblist_get(hostnames, index);
|
||||
if(tmp && *tmp) res = *tmp;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
ip_type4 get_ip_from_index(unsigned index) {
|
||||
ip_type4 ret;
|
||||
index++; // so we can start at .0.0.1
|
||||
if(index > 0xFFFFFF)
|
||||
return IPT4_INVALID;
|
||||
ret.octet[0] = remote_subnet & 0xFF;
|
||||
ret.octet[1] = (index & 0xFF0000) >> 16;
|
||||
ret.octet[2] = (index & 0xFF00) >> 8;
|
||||
ret.octet[3] = index & 0xFF;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ip_type4 get_ip(char* hn) {
|
||||
htab_value *v = htab_find(ip_lookup_table, hn);
|
||||
if(v) return get_ip_from_index(v->n);
|
||||
char *n = strdup(hn);
|
||||
if(!n) return IPT4_INVALID;
|
||||
if(!sblist_add(hostnames, &n)) {
|
||||
o_out:;
|
||||
free(n);
|
||||
return IPT4_INVALID;
|
||||
}
|
||||
if(!htab_insert(ip_lookup_table, n, HTV_N(sblist_getsize(hostnames)-1))) {
|
||||
sblist_delete(hostnames, sblist_getsize(hostnames)-1);
|
||||
goto o_out;
|
||||
}
|
||||
return get_ip_from_index(sblist_getsize(hostnames)-1);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int ch;
|
||||
const char *listenip = "127.0.0.1";
|
||||
unsigned port = 1053;
|
||||
remote_subnet = 224;
|
||||
while((ch = getopt(argc, argv, ":r:i:p:")) != -1) {
|
||||
switch(ch) {
|
||||
case 'r':
|
||||
remote_subnet = atoi(optarg);
|
||||
break;
|
||||
case 'i':
|
||||
listenip = optarg;
|
||||
break;
|
||||
case 'p':
|
||||
port = atoi(optarg);
|
||||
break;
|
||||
case ':':
|
||||
dprintf(2, "error: option -%c requires an operand\n", optopt);
|
||||
/* fall through */
|
||||
case '?':
|
||||
return usage(argv[0]);
|
||||
}
|
||||
}
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
struct server s;
|
||||
if(server_setup(&s, listenip, port)) {
|
||||
perror("server_setup");
|
||||
return 1;
|
||||
}
|
||||
server = &s;
|
||||
|
||||
ip_lookup_table = htab_create(64);
|
||||
hostnames = sblist_new(sizeof(char*), 64);
|
||||
|
||||
while(1) {
|
||||
struct client c;
|
||||
char ipstr_buf[INET6_ADDRSTRLEN+6+1];
|
||||
char ip4str_buf[16];
|
||||
struct at_msg msg, out;
|
||||
size_t msgl = sizeof(msg);
|
||||
int failed = 0;
|
||||
|
||||
#define FAIL() do { failed=1; goto sendresp; } while(0)
|
||||
|
||||
if(server_waitclient(&s, &c, &msg, &msgl)) continue;
|
||||
msg.h.datalen = ntohs(msg.h.datalen);
|
||||
if(msgl != sizeof(msg.h)+msg.h.datalen) {
|
||||
dolog("%s: invalid datalen\n", ipstr(&c.addr, ipstr_buf));
|
||||
FAIL();
|
||||
}
|
||||
|
||||
out.h.msgtype = msg.h.msgtype;
|
||||
if(msg.h.msgtype == ATM_GETIP) {
|
||||
if(!memchr(msg.m.host, 0, msg.h.datalen)) {
|
||||
dolog("%s: nul terminator missing\n", ipstr(&c.addr, ipstr_buf));
|
||||
FAIL();
|
||||
}
|
||||
out.h.datalen = sizeof(ip_type4);
|
||||
out.m.ip = get_ip(msg.m.host);
|
||||
failed = !memcmp(&out.m.ip, &IPT4_INVALID, 4);
|
||||
dolog("%s requested ip for %s (%s)\n", ipstr(&c.addr, ipstr_buf),
|
||||
msg.m.host, failed?"FAIL":my_inet_ntoa((void*)&out.m.ip, ip4str_buf));
|
||||
if(failed) FAIL();
|
||||
} else if (msg.h.msgtype == ATM_GETNAME) {
|
||||
if(msg.h.datalen != 4) {
|
||||
dolog("%s: invalid len for getname request\n", ipstr(&c.addr, ipstr_buf));
|
||||
FAIL();
|
||||
}
|
||||
char *hn = host_from_ip(msg.m.ip);
|
||||
if(hn) {
|
||||
size_t l = strlen(hn);
|
||||
memcpy(out.m.host, hn, l+1);
|
||||
out.h.datalen = l+1;
|
||||
}
|
||||
dolog("%s requested name for %s (%s)\n", ipstr(&c.addr, ipstr_buf),
|
||||
my_inet_ntoa((void*) &msg.m.ip, ip4str_buf), hn?hn:"FAIL");
|
||||
if(!hn) FAIL();
|
||||
} else {
|
||||
dolog("%s: unknown request %u\n", ipstr(&c.addr, ipstr_buf),
|
||||
(unsigned) msg.h.msgtype);
|
||||
}
|
||||
sendresp:;
|
||||
if(failed) {
|
||||
out.h.msgtype = ATM_FAIL;
|
||||
out.h.datalen = 0;
|
||||
}
|
||||
unsigned short dlen = out.h.datalen;
|
||||
out.h.datalen = htons(dlen);
|
||||
sendto(server->fd, &out, sizeof(out.h)+dlen, 0, (void*) &c.addr, SOCKADDR_UNION_LENGTH(&c.addr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
/*
|
||||
musl license, hsearch.c originally written by Szabolcs Nagy
|
||||
|
||||
Copyright © 2005-2020 Rich Felker, et al.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "hsearch.h"
|
||||
|
||||
/*
|
||||
open addressing hash table with 2^n table size
|
||||
quadratic probing is used in case of hash collision
|
||||
tab indices and hash are size_t
|
||||
after resize fails with ENOMEM the state of tab is still usable
|
||||
*/
|
||||
|
||||
typedef struct htab_entry {
|
||||
char *key;
|
||||
htab_value data;
|
||||
} htab_entry;
|
||||
|
||||
struct elem {
|
||||
htab_entry item;
|
||||
size_t hash;
|
||||
};
|
||||
|
||||
struct htab {
|
||||
struct elem *elems;
|
||||
size_t mask;
|
||||
size_t used;
|
||||
};
|
||||
|
||||
#define MINSIZE 8
|
||||
#define MAXSIZE ((size_t)-1/2 + 1)
|
||||
|
||||
static size_t keyhash(char *k)
|
||||
{
|
||||
unsigned char *p = (void *)k;
|
||||
size_t h = 0;
|
||||
|
||||
while (*p)
|
||||
h = 31*h + *p++;
|
||||
return h;
|
||||
}
|
||||
|
||||
static int resize(struct htab *htab, size_t nel)
|
||||
{
|
||||
size_t newsize;
|
||||
size_t i, j;
|
||||
struct elem *e, *newe;
|
||||
struct elem *oldtab = htab->elems;
|
||||
struct elem *oldend = htab->elems + htab->mask + 1;
|
||||
|
||||
if (nel > MAXSIZE)
|
||||
nel = MAXSIZE;
|
||||
for (newsize = MINSIZE; newsize < nel; newsize *= 2);
|
||||
htab->elems = calloc(newsize, sizeof *htab->elems);
|
||||
if (!htab->elems) {
|
||||
htab->elems = oldtab;
|
||||
return 0;
|
||||
}
|
||||
htab->mask = newsize - 1;
|
||||
if (!oldtab)
|
||||
return 1;
|
||||
for (e = oldtab; e < oldend; e++)
|
||||
if (e->item.key) {
|
||||
for (i=e->hash,j=1; ; i+=j++) {
|
||||
newe = htab->elems + (i & htab->mask);
|
||||
if (!newe->item.key)
|
||||
break;
|
||||
}
|
||||
*newe = *e;
|
||||
}
|
||||
free(oldtab);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static struct elem *lookup(struct htab *htab, char *key, size_t hash)
|
||||
{
|
||||
size_t i, j;
|
||||
struct elem *e;
|
||||
|
||||
for (i=hash,j=1; ; i+=j++) {
|
||||
e = htab->elems + (i & htab->mask);
|
||||
if (!e->item.key ||
|
||||
(e->hash==hash && strcmp(e->item.key, key)==0))
|
||||
break;
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
struct htab *htab_create(size_t nel)
|
||||
{
|
||||
struct htab *r = calloc(1, sizeof *r);
|
||||
if(r && !resize(r, nel)) {
|
||||
free(r);
|
||||
r = 0;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
void htab_destroy(struct htab *htab)
|
||||
{
|
||||
free(htab->elems);
|
||||
free(htab);
|
||||
}
|
||||
|
||||
static htab_entry *htab_find_item(struct htab *htab, char* key)
|
||||
{
|
||||
size_t hash = keyhash(key);
|
||||
struct elem *e = lookup(htab, key, hash);
|
||||
|
||||
if (e->item.key) {
|
||||
return &e->item;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
htab_value* htab_find(struct htab *htab, char* key)
|
||||
{
|
||||
htab_entry *i = htab_find_item(htab, key);
|
||||
if(i) return &i->data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int htab_delete(struct htab *htab, char* key)
|
||||
{
|
||||
htab_entry *i = htab_find_item(htab, key);
|
||||
if(!i) return 0;
|
||||
i->key = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int htab_insert(struct htab *htab, char* key, htab_value value)
|
||||
{
|
||||
size_t hash = keyhash(key);
|
||||
struct elem *e = lookup(htab, key, hash);
|
||||
if(e->item.key) {
|
||||
/* it's not allowed to overwrite existing data */
|
||||
return 0;
|
||||
}
|
||||
|
||||
e->item.key = key;
|
||||
e->item.data = value;
|
||||
e->hash = hash;
|
||||
if (++htab->used > htab->mask - htab->mask/4) {
|
||||
if (!resize(htab, 2*htab->used)) {
|
||||
htab->used--;
|
||||
e->item.key = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t htab_next(struct htab *htab, size_t iterator, char** key, htab_value **v)
|
||||
{
|
||||
size_t i;
|
||||
for(i=iterator;i<htab->mask+1;++i) {
|
||||
struct elem *e = htab->elems + i;
|
||||
if(e->item.key) {
|
||||
*key = e->item.key;
|
||||
*v = &e->item.data;
|
||||
return i+1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef HSEARCH_H
|
||||
#define HSEARCH_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef union htab_value {
|
||||
void *p;
|
||||
size_t n;
|
||||
} htab_value;
|
||||
|
||||
#define HTV_N(N) (htab_value) {.n = N}
|
||||
#define HTV_P(P) (htab_value) {.p = P}
|
||||
|
||||
struct htab * htab_create(size_t);
|
||||
void htab_destroy(struct htab *);
|
||||
htab_value* htab_find(struct htab *, char* key);
|
||||
int htab_insert(struct htab *, char*, htab_value);
|
||||
int htab_delete(struct htab *htab, char* key);
|
||||
size_t htab_next(struct htab *, size_t iterator, char** key, htab_value **v);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
#undef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "sblist.h"
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define MY_PAGE_SIZE 4096
|
||||
|
||||
sblist* sblist_new(size_t itemsize, size_t blockitems) {
|
||||
sblist* ret = (sblist*) malloc(sizeof(sblist));
|
||||
sblist_init(ret, itemsize, blockitems);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void sblist_clear(sblist* l) {
|
||||
l->items = NULL;
|
||||
l->capa = 0;
|
||||
l->count = 0;
|
||||
}
|
||||
|
||||
void sblist_init(sblist* l, size_t itemsize, size_t blockitems) {
|
||||
if(l) {
|
||||
l->blockitems = blockitems ? blockitems : MY_PAGE_SIZE / itemsize;
|
||||
l->itemsize = itemsize;
|
||||
sblist_clear(l);
|
||||
}
|
||||
}
|
||||
|
||||
void sblist_free_items(sblist* l) {
|
||||
if(l) {
|
||||
if(l->items) free(l->items);
|
||||
sblist_clear(l);
|
||||
}
|
||||
}
|
||||
|
||||
void sblist_free(sblist* l) {
|
||||
if(l) {
|
||||
sblist_free_items(l);
|
||||
free(l);
|
||||
}
|
||||
}
|
||||
|
||||
char* sblist_item_from_index(sblist* l, size_t idx) {
|
||||
return l->items + (idx * l->itemsize);
|
||||
}
|
||||
|
||||
void* sblist_get(sblist* l, size_t item) {
|
||||
if(item < l->count) return (void*) sblist_item_from_index(l, item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int sblist_set(sblist* l, void* item, size_t pos) {
|
||||
if(pos >= l->count) return 0;
|
||||
memcpy(sblist_item_from_index(l, pos), item, l->itemsize);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sblist_grow_if_needed(sblist* l) {
|
||||
char* temp;
|
||||
if(l->count == l->capa) {
|
||||
temp = realloc(l->items, (l->capa + l->blockitems) * l->itemsize);
|
||||
if(!temp) return 0;
|
||||
l->capa += l->blockitems;
|
||||
l->items = temp;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sblist_add(sblist* l, void* item) {
|
||||
if(!sblist_grow_if_needed(l)) return 0;
|
||||
l->count++;
|
||||
return sblist_set(l, item, l->count - 1);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#ifndef SBLIST_H
|
||||
#define SBLIST_H
|
||||
|
||||
/* this file is part of libulz, as of commit 8ab361a27743aaf025323ee43b8b8876dc054fdd
|
||||
modified for direct inclusion in microsocks. */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
/*
|
||||
* simple buffer list.
|
||||
*
|
||||
* this thing here is basically a generic dynamic array
|
||||
* will realloc after every blockitems inserts
|
||||
* can store items of any size.
|
||||
*
|
||||
* so think of it as a by-value list, as opposed to a typical by-ref list.
|
||||
* you typically use it by having some struct on the stack, and pass a pointer
|
||||
* to sblist_add, which will copy the contents into its internal memory.
|
||||
*
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
size_t itemsize;
|
||||
size_t blockitems;
|
||||
size_t count;
|
||||
size_t capa;
|
||||
char* items;
|
||||
} sblist;
|
||||
|
||||
#define sblist_getsize(X) ((X)->count)
|
||||
#define sblist_get_count(X) ((X)->count)
|
||||
#define sblist_empty(X) ((X)->count == 0)
|
||||
|
||||
// for dynamic style
|
||||
sblist* sblist_new(size_t itemsize, size_t blockitems);
|
||||
void sblist_free(sblist* l);
|
||||
|
||||
//for static style
|
||||
void sblist_init(sblist* l, size_t itemsize, size_t blockitems);
|
||||
void sblist_free_items(sblist* l);
|
||||
|
||||
// accessors
|
||||
void* sblist_get(sblist* l, size_t item);
|
||||
// returns 1 on success, 0 on OOM
|
||||
int sblist_add(sblist* l, void* item);
|
||||
int sblist_set(sblist* l, void* item, size_t pos);
|
||||
void sblist_delete(sblist* l, size_t item);
|
||||
char* sblist_item_from_index(sblist* l, size_t idx);
|
||||
int sblist_grow_if_needed(sblist* l);
|
||||
int sblist_insert(sblist* l, void* item, size_t pos);
|
||||
/* same as sblist_add, but returns list index of new item, or -1 */
|
||||
size_t sblist_addi(sblist* l, void* item);
|
||||
void sblist_sort(sblist *l, int (*compar)(const void *, const void *));
|
||||
/* insert element into presorted list, returns listindex of new entry or -1*/
|
||||
size_t sblist_insert_sorted(sblist* l, void* o, int (*compar)(const void *, const void *));
|
||||
|
||||
#ifndef __COUNTER__
|
||||
#define __COUNTER__ __LINE__
|
||||
#endif
|
||||
|
||||
#define __sblist_concat_impl( x, y ) x##y
|
||||
#define __sblist_macro_concat( x, y ) __sblist_concat_impl( x, y )
|
||||
#define __sblist_iterator_name __sblist_macro_concat(sblist_iterator, __COUNTER__)
|
||||
|
||||
// use with custom iterator variable
|
||||
#define sblist_iter_counter(LIST, ITER, PTR) \
|
||||
for(size_t ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
|
||||
|
||||
// use with custom iterator variable, which is predeclared
|
||||
#define sblist_iter_counter2(LIST, ITER, PTR) \
|
||||
for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < sblist_getsize(LIST); ITER++)
|
||||
|
||||
// use with custom iterator variable, which is predeclared and signed
|
||||
// useful for a loop which can delete items from the list, and then decrease the iterator var.
|
||||
#define sblist_iter_counter2s(LIST, ITER, PTR) \
|
||||
for(ITER = 0; (PTR = sblist_get(LIST, ITER)), ITER < (ssize_t) sblist_getsize(LIST); ITER++)
|
||||
|
||||
|
||||
// uses "magic" iterator variable
|
||||
#define sblist_iter(LIST, PTR) sblist_iter_counter(LIST, __sblist_iterator_name, PTR)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#pragma RcB2 DEP "sblist.c" "sblist_delete.c"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "sblist.h"
|
||||
#include <string.h>
|
||||
|
||||
void sblist_delete(sblist* l, size_t item) {
|
||||
if (l->count && item < l->count) {
|
||||
memmove(sblist_item_from_index(l, item), sblist_item_from_index(l, item + 1), (sblist_getsize(l) - (item + 1)) * l->itemsize);
|
||||
l->count--;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "../remotedns.h"
|
||||
#include "../ip_type.h"
|
||||
|
||||
int main() {
|
||||
int fd;
|
||||
int port = 1053;
|
||||
char srvn[] = "127.0.0.1";
|
||||
struct sockaddr_in srva = {.sin_family = AF_INET, .sin_port = htons(port)};
|
||||
inet_pton(AF_INET, srvn, &srva.sin_addr);
|
||||
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
char namebuf[260];
|
||||
while(fgets(namebuf, sizeof namebuf, stdin)) {
|
||||
size_t l = strlen(namebuf);
|
||||
if(namebuf[l-1] == '\n') {
|
||||
l--;
|
||||
namebuf[l] = 0;
|
||||
}
|
||||
struct at_msg msg = {0};
|
||||
unsigned msglen;
|
||||
if(isdigit(namebuf[0])) {
|
||||
msglen = 4;
|
||||
msg.h.msgtype = ATM_GETNAME;
|
||||
inet_aton(namebuf, (void*) &msg.m.ip);
|
||||
} else {
|
||||
msglen = l+1;
|
||||
msg.h.msgtype = ATM_GETIP;
|
||||
memcpy(msg.m.host, namebuf, msglen);
|
||||
}
|
||||
msg.h.datalen = htons(msglen);
|
||||
sendto(fd, &msg, sizeof(msg.h)+msglen, 0, (void*)&srva, sizeof(srva));
|
||||
char rcvbuf[512];
|
||||
recvfrom(fd, rcvbuf, sizeof rcvbuf, 0, (void*)0, (void*)0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "udpserver.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int resolve(const char *host, unsigned short port, struct addrinfo** addr) {
|
||||
struct addrinfo hints = {
|
||||
.ai_family = AF_UNSPEC,
|
||||
.ai_socktype = SOCK_STREAM,
|
||||
.ai_flags = AI_PASSIVE,
|
||||
};
|
||||
char port_buf[8];
|
||||
snprintf(port_buf, sizeof port_buf, "%u", port);
|
||||
return getaddrinfo(host, port_buf, &hints, addr);
|
||||
}
|
||||
|
||||
int resolve_sa(const char *host, unsigned short port, union sockaddr_union *res) {
|
||||
struct addrinfo *ainfo = 0;
|
||||
int ret;
|
||||
SOCKADDR_UNION_AF(res) = AF_UNSPEC;
|
||||
if((ret = resolve(host, port, &ainfo))) return ret;
|
||||
memcpy(res, ainfo->ai_addr, ainfo->ai_addrlen);
|
||||
freeaddrinfo(ainfo);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bindtoip(int fd, union sockaddr_union *bindaddr) {
|
||||
socklen_t sz = SOCKADDR_UNION_LENGTH(bindaddr);
|
||||
if(sz)
|
||||
return bind(fd, (struct sockaddr*) bindaddr, sz);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int server_waitclient(struct server *server, struct client* client, void* buf, size_t *buflen) {
|
||||
socklen_t clen = sizeof client->addr;
|
||||
ssize_t ret = recvfrom(server->fd, buf, *buflen, 0, (void*)&client->addr, &clen);
|
||||
if(ret >= 0) {
|
||||
*buflen = ret;
|
||||
return 0;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int server_setup(struct server *server, const char* listenip, unsigned short port) {
|
||||
struct addrinfo *ainfo = 0;
|
||||
if(resolve(listenip, port, &ainfo)) return -1;
|
||||
struct addrinfo* p;
|
||||
int listenfd = -1;
|
||||
for(p = ainfo; p; p = p->ai_next) {
|
||||
if((listenfd = socket(p->ai_family, SOCK_DGRAM, 0)) < 0)
|
||||
continue;
|
||||
int yes = 1;
|
||||
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
|
||||
if(bind(listenfd, p->ai_addr, p->ai_addrlen) < 0) {
|
||||
close(listenfd);
|
||||
listenfd = -1;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
freeaddrinfo(ainfo);
|
||||
if(listenfd < 0) return -2;
|
||||
server->fd = listenfd;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#undef _POSIX_C_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#pragma RcB2 DEP "udpserver.c"
|
||||
|
||||
union sockaddr_union {
|
||||
struct sockaddr_in v4;
|
||||
struct sockaddr_in6 v6;
|
||||
};
|
||||
|
||||
#define SOCKADDR_UNION_AF(PTR) (PTR)->v4.sin_family
|
||||
|
||||
#define SOCKADDR_UNION_LENGTH(PTR) ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? sizeof((PTR)->v4) : ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? sizeof((PTR)->v6) : 0 ) )
|
||||
|
||||
#define SOCKADDR_UNION_ADDRESS(PTR) ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? (void*) &(PTR)->v4.sin_addr : ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? (void*) &(PTR)->v6.sin6_addr : (void*) 0 ) )
|
||||
|
||||
#define SOCKADDR_UNION_PORT(PTR) ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? (PTR)->v4.sin_port : ( \
|
||||
( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? (PTR)->v6.sin6_port : 0 ) )
|
||||
|
||||
struct client {
|
||||
union sockaddr_union addr;
|
||||
};
|
||||
|
||||
struct server {
|
||||
int fd;
|
||||
};
|
||||
|
||||
int resolve(const char *host, unsigned short port, struct addrinfo** addr);
|
||||
int resolve_sa(const char *host, unsigned short port, union sockaddr_union *res);
|
||||
int bindtoip(int fd, union sockaddr_union *bindaddr);
|
||||
|
||||
int server_waitclient(struct server *server, struct client* client, void* buf, size_t *buflen);
|
||||
int server_setup(struct server *server, const char* listenip, unsigned short port);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user