util  Check-in [a52546afcc]

Overview
Comment:it works!! my monster liiiiives
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a52546afcc368e92286fce558a06fc80f6ed53731542396fa61cd7d85e36cd5c
User & Date: lexi on 2022-11-01 17:28:26
Other Links: manifest | tags
Context
2022-11-01
19:10
fully functional?? check-in: 700efc70e2 user: lexi tags: trunk
17:28
it works!! my monster liiiiives check-in: a52546afcc user: lexi tags: trunk
14:33
iterate, add linked list template check-in: 81321a2c01 user: lexi tags: trunk
Changes

Modified wgsync/makefile from [11c34eecc9] to [12884906c5].

     9      9   dbg-flags = $(if $(debug),-g -D_cfg_debug,)
    10     10   cc-flags = -std=c2x $(pq-inc) $(wg-inc) $(dbg-flags)
    11     11   ld-flags = $(pq-lib) $(dbg-flags)
    12     12   
    13     13   
    14     14   # link rule 
    15     15   .PHONY: wgsync
    16         -$B/wgsync: $B/wgsync.o $B/pqp.o $B/def.o $B/wireguard.o | $B/
           16  +$B/wgsync: $B/wgsync.o $B/pqp.o $B/def.o $B/wglist.o $B/wireguard.o | $B/
    17     17   	$(CC) $(ld-flags) $^ -o $@
    18     18   
    19     19   # build rules 
    20         -$B/%.o: src/%.c | $B/
           20  +$B/%.o: src/%.c src/def.h | $B/
    21     21   	$(CC) $(cc-flags) -c $< -o $@
    22     22   
    23     23   $B/wireguard.o: $E/wglib/wireguard.c $E/wglib/wireguard.h | $B/
    24     24   	$(CC) -std=c11 -c $< -o $@
    25     25   
    26     26   # dep listings
    27         -$B/wgsync.o: $E/wglib/wireguard.h src/pqp.h src/def.h
    28         -$B/pqp.o: src/pqp.h src/def.h 
    29         -$B/def.o: src/def.h
           27  +$B/wgsync.o: $E/wglib/wireguard.h src/pqp.h src/wglist.h src/list.h
           28  +$B/wglist.o: $E/wglib/wireguard.h src/wglist.h src/list.h
           29  +$B/pqp.o: src/pqp.h 
    30     30   
    31     31   # fetch rules
    32     32   %/:
    33     33   	mkdir -p $@
    34     34   
    35     35   wg-lib-uri = https://git.zx2c4.com/wireguard-tools/plain/contrib/embeddable-wg-library
    36     36   $E/wglib/%: | $E/wglib/
    37     37   	curl $(wg-lib-uri)/$* >$@

Modified wgsync/src/list.h from [5c0572111c] to [ba4baffdd8].

     1         -#ifndef _ll_delete
     2         -#define _ll_delete free
     3         -#endif
     4         -
     5      1   #ifndef _ll_ffirst
     6      2   #define _ll_ffirst _cat(first_,_ll_rec)
     7      3   #endif
     8      4   
     9      5   #ifndef _ll_flast
    10      6   #define _ll_flast _cat(last_,_ll_rec)
    11      7   #endif
    12      8   
    13      9   #ifndef _ll_fnext
    14     10   #define _ll_fnext _cat(next_,_ll_rec)
    15     11   #endif
    16     12   
           13  +#define _LL_fn(act) _cat(_cat(_ll_ns,_),_cat(_cat(act,_), _ll_rec))
    17     14   #ifndef _ll_dropfn
    18         -#define _ll_dropfn _cat(_cat(_ll_ns,_),_cat(drop_, _ll_rec))
           15  +#define _ll_dropfn _LL_fn(drop)
    19     16   #endif
    20     17   
    21     18   #ifndef _ll_pushfn
    22         -#define _ll_pushfn _cat(_cat(_ll_ns,_),_cat(push_, _ll_rec))
           19  +#define _ll_pushfn _LL_fn(push)
           20  +#endif
           21  +
           22  +#ifndef _ll_newfn
           23  +#define _ll_newfn _LL_fn(new)
           24  +#endif
           25  +
           26  +#ifndef _ll_delete
           27  +#define _ll_delete free
           28  +#	ifdef _ll_impl
           29  +#		include <stdlib.h>
           30  +#	endif
           31  +#endif
           32  +
           33  +#ifdef _ll_impl
           34  +#	define _LL_impl(...) __VA_ARGS__
           35  +#	include "def.h"
           36  +#else
           37  +#	define _LL_impl(...) ;
    23     38   #endif
           39  +
           40  +void _ll_pushfn
           41  +(_ll_box* box, _ll_obj* obj) _LL_impl({
           42  +	if (box -> _ll_flast)
           43  +		box -> _ll_flast -> _ll_fnext = obj;
           44  +	else box -> _ll_ffirst = obj;
           45  +	box -> _ll_flast = obj;
           46  +})
           47  +
           48  +_ll_obj* _ll_newfn
           49  +(_ll_box* box) _LL_impl({
           50  +	_ll_obj* o = calloc(1, sizeof(_ll_obj));
           51  +	_ll_pushfn(box, o);
           52  +	return o;
           53  +})
    24     54   
    25     55   void _ll_dropfn
    26         -(_ll_box* box, _ll_obj* obj) {
           56  +(_ll_box* box, _ll_obj* obj) _LL_impl({
    27     57   	if(box -> _ll_ffirst == obj) {
    28     58   		if(box -> _ll_flast == obj) {
    29     59   			box -> _ll_ffirst = box -> _ll_flast = null;
    30     60   		} else {
    31     61   			box -> _ll_ffirst = obj -> _ll_fnext;
    32     62   		}
    33     63   	} else {
................................................................................
    50     80   				}
    51     81   			}
    52     82   			_fatal("BUG in elt deletion routine");
    53     83   			found2 :;
    54     84   		}
    55     85   	}
    56     86   	_ll_delete (obj);
    57         -}
           87  +})
           88  +
           89  +#undef _LL_fn
           90  +#undef _LL_impl
           91  +
           92  +#undef _ll_pushfn
           93  +#undef _ll_newfn
           94  +#undef _ll_dropfn
    58     95   
    59     96   #undef _ll_ffirst
    60     97   #undef _ll_flast
    61     98   #undef _ll_ns
    62     99   #undef _ll_box
    63    100   #undef _ll_obj
    64    101   #undef _ll_rec
    65    102   #undef _ll_iter
    66    103   #undef _ll_delete

Added wgsync/src/wglist.c version [d918a93c58].

            1  +#define _ll_impl
            2  +#include "wglist.h"

Added wgsync/src/wglist.h version [3fe54f7b74].

            1  +#pragma once
            2  +#include "def.h"
            3  +#include <wireguard.h>
            4  +
            5  +#define _ll_rec peer
            6  +#define _ll_box wg_device
            7  +#define _ll_obj wg_peer
            8  +#define _ll_iter wg_for_each_peer
            9  +#define _ll_ns wgd
           10  +#include "list.h"
           11  +
           12  +#define _ll_rec allowedip
           13  +#define _ll_box wg_peer
           14  +#define _ll_obj wg_allowedip
           15  +#define _ll_iter wg_for_each_allowedip
           16  +#define _ll_ns wgd_peer
           17  +#include "list.h"

Modified wgsync/src/wgsync.c from [40df359611] to [da84aa2670].

    10     10   #include <netinet/in.h>
    11     11   #include <unistd.h>
    12     12   #include <sys/socket.h>
    13     13   #include <netdb.h>
    14     14   
    15     15   /* libs */
    16     16   #include <wireguard.h>
           17  +#include "wglist.h"
           18  +	/* wireguard uses messy linked lists but doesn't
           19  +	 * provide any routines for manipulating them;
           20  +	 * wglist.h fills in the gap */
    17     21   
    18     22   #include <libpq-fe.h>
    19     23   
    20     24   
    21     25   size_t dumpEndpoint(char* d, const wg_endpoint* const e) {
    22     26   	const struct sockaddr* addr;
    23     27   	size_t len;
................................................................................
   125    129   	) free(allowedip);
   126    130   	/* end import */
   127    131   	free(peer);
   128    132   }
   129    133   
   130    134   /* linked list manipulation routines */
   131    135   
   132         -#define _ll_rec peer
   133         -#define _ll_box wg_device
   134         -#define _ll_obj wg_peer
   135         -#define _ll_iter wg_for_each_peer
   136         -#define _ll_ns wgd
   137         -#include "list.h"
   138         -
   139         -#define _ll_rec allowedip
   140         -#define _ll_box wg_peer
   141         -#define _ll_obj wg_allowedip
   142         -#define _ll_iter wg_for_each_allowedip
   143         -#define _ll_ns wgd_peer
   144         -#include "list.h"
   145    136   
   146    137   #if 0
   147    138   void wgd_drop_peer(wg_device* dev, wg_peer* peer) {
   148    139   	if(dev -> first_peer == peer) {
   149    140   		if(dev -> last_peer == peer) {
   150    141   			dev -> first_peer = dev -> last_peer = null;
   151    142   		} else {
................................................................................
   276    267   					}
   277    268   				++l;}
   278    269   				
   279    270   				if(!foundIP) {
   280    271   					/* this IP hasn't been loaded into the
   281    272   					 * kernel yet; upload it now */
   282    273   					_infof("inserting IP PG%zu %s", j, inetstr);
          274  +					// is this necessary? FIXME
          275  +					/* found -> flags |= WGPEER_REPLACE_ALLOWEDIPS; */
          276  +					wg_allowedip* nip = wgd_peer_new_allowedip(found);
          277  +					memcpy(nip, &aip, sizeof aip);
   283    278   					dirty = true;
   284    279   				}
   285    280   			}
   286    281   
   287    282   			if(goodIPc < ips -> sz) {
   288    283   				size_t l = 0;
   289    284   				wg_allowedip* wgip;
   290    285   				wg_for_each_allowedip(found, wgip) {
   291    286   					char inetstr[256];
   292    287   					dumpAllowedIP(inetstr, wgip);
   293    288   					_dbgf("IP WG%zu :: %s", l, inetstr);
   294         -					if(!goodIPs[l]) {
          289  +					if(l<goodIPc && !goodIPs[l]) {
   295    290   						/* this IP is stale, delete it */
   296    291   						_infof("deleting IP WG%zu %s", l, inetstr);
          292  +						wgd_peer_drop_allowedip(found, wgip);
          293  +						found -> flags |= WGPEER_REPLACE_ALLOWEDIPS;
   297    294   						dirty = true;
   298    295   					}
   299    296   				++l;}
   300    297   			}
   301    298   		} else {
   302    299   			_infof("inserting key %s", key_b64);
   303    300   			dirty = true;
   304    301   			/* install new peer */
          302  +			wg_peer* np = wgd_new_peer(wg);
          303  +			np -> flags = WGPEER_HAS_PUBLIC_KEY;
          304  +			memcpy(np -> public_key, key, sizeof key);
          305  +
   305    306   			for (size_t j = 0; j < ips -> sz; ++j) {
   306    307   				char inetstr[256];
   307    308   				wg_allowedip aip = inet_to_allowedip(ips -> elts[j].data);
   308    309   				dumpAllowedIP(inetstr, &aip);
   309    310   				_dbgf("new IP %zu :: %s", j, inetstr);
          311  +				wg_allowedip* nip = wgd_peer_new_allowedip(np);
          312  +				memcpy(nip, &aip, sizeof aip);
   310    313   			}
   311    314   		}
   312    315   
   313    316   		free(ips);
   314    317   	}
   315    318   	{ size_t i=0; wg_peer* p; wg_for_each_peer(wg, p) {
   316         -		if(valid_peers[i] == false) {
          319  +		if(i<peerc && valid_peers[i] == false) {
   317    320   			char b64 [128];
   318    321   			wg_key_to_base64(b64, p->public_key);
   319    322   			_infof("dropping peer %s", b64);
   320         -			wgd_drop_peer(wg, p);
          323  +			//wgd_drop_peer(wg, p);
          324  +			p -> flags |= WGPEER_REMOVE_ME;
   321    325   			dirty = true;
   322    326   		}
   323    327   	++i;}}
   324    328   
   325    329   	_dbg("final peer list:");
   326    330   	{ size_t j=0; wg_peer* p; wg_for_each_peer(wg, p) {
   327    331   		char b64 [128];
   328    332   		wg_key_to_base64(b64, p->public_key);
   329         -		_dbgf("P%zu :: %s", j, b64);
          333  +		_dbgf("P%zu :: %s%s", j, b64,
          334  +			p->flags & WGPEER_REMOVE_ME          ? " [DELETE]" :
          335  +			p->flags & WGPEER_REPLACE_ALLOWEDIPS ? " [CHGIP]" : "");
   330    336   	++j;}}
   331    337   	
   332         -	if(dirty) wg_set_device(wg);
          338  +	dirty = true;
          339  +	if(dirty) {
          340  +		int e = wg_set_device(wg);
          341  +		if(e != 0) 
          342  +			_fatalf("could not set wg device (error %i)", -e);
          343  +	}
   333    344   
   334    345   	PQclear(rows);
   335    346   }
   336    347   
   337    348   int main(int argc, char** argv) {
   338    349   	setvbuf(stderr, null, _IONBF, 0);
   339    350   	if (argc < 3) {