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
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
dbg-flags = $(if $(debug),-g -D_cfg_debug,)
cc-flags = -std=c2x $(pq-inc) $(wg-inc) $(dbg-flags)
ld-flags = $(pq-lib) $(dbg-flags)


# link rule 
.PHONY: wgsync
$B/wgsync: $B/wgsync.o $B/pqp.o $B/def.o $B/wireguard.o | $B/
	$(CC) $(ld-flags) $^ -o $@

# build rules 
$B/%.o: src/%.c | $B/
	$(CC) $(cc-flags) -c $< -o $@

$B/wireguard.o: $E/wglib/wireguard.c $E/wglib/wireguard.h | $B/
	$(CC) -std=c11 -c $< -o $@

# dep listings
$B/wgsync.o: $E/wglib/wireguard.h src/pqp.h src/def.h
$B/pqp.o: src/pqp.h src/def.h 
$B/def.o: src/def.h

# fetch rules
%/:
	mkdir -p $@

wg-lib-uri = https://git.zx2c4.com/wireguard-tools/plain/contrib/embeddable-wg-library
$E/wglib/%: | $E/wglib/
	curl $(wg-lib-uri)/$* >$@







|



|






|
|
|








9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
dbg-flags = $(if $(debug),-g -D_cfg_debug,)
cc-flags = -std=c2x $(pq-inc) $(wg-inc) $(dbg-flags)
ld-flags = $(pq-lib) $(dbg-flags)


# link rule 
.PHONY: wgsync
$B/wgsync: $B/wgsync.o $B/pqp.o $B/def.o $B/wglist.o $B/wireguard.o | $B/
	$(CC) $(ld-flags) $^ -o $@

# build rules 
$B/%.o: src/%.c src/def.h | $B/
	$(CC) $(cc-flags) -c $< -o $@

$B/wireguard.o: $E/wglib/wireguard.c $E/wglib/wireguard.h | $B/
	$(CC) -std=c11 -c $< -o $@

# dep listings
$B/wgsync.o: $E/wglib/wireguard.h src/pqp.h src/wglist.h src/list.h
$B/wglist.o: $E/wglib/wireguard.h src/wglist.h src/list.h
$B/pqp.o: src/pqp.h 

# fetch rules
%/:
	mkdir -p $@

wg-lib-uri = https://git.zx2c4.com/wireguard-tools/plain/contrib/embeddable-wg-library
$E/wglib/%: | $E/wglib/
	curl $(wg-lib-uri)/$* >$@

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
24

































25
26
27
28
29
30
31
32
33
..
50
51
52
53
54
55
56
57







58
59
60
61
62
63
64
65
66
#ifndef _ll_delete
#define _ll_delete free
#endif

#ifndef _ll_ffirst
#define _ll_ffirst _cat(first_,_ll_rec)
#endif

#ifndef _ll_flast
#define _ll_flast _cat(last_,_ll_rec)
#endif

#ifndef _ll_fnext
#define _ll_fnext _cat(next_,_ll_rec)
#endif


#ifndef _ll_dropfn
#define _ll_dropfn _cat(_cat(_ll_ns,_),_cat(drop_, _ll_rec))
#endif

#ifndef _ll_pushfn
#define _ll_pushfn _cat(_cat(_ll_ns,_),_cat(push_, _ll_rec))
#endif


































void _ll_dropfn
(_ll_box* box, _ll_obj* obj) {
	if(box -> _ll_ffirst == obj) {
		if(box -> _ll_flast == obj) {
			box -> _ll_ffirst = box -> _ll_flast = null;
		} else {
			box -> _ll_ffirst = obj -> _ll_fnext;
		}
	} else {
................................................................................
				}
			}
			_fatal("BUG in elt deletion routine");
			found2 :;
		}
	}
	_ll_delete (obj);
}








#undef _ll_ffirst
#undef _ll_flast
#undef _ll_ns
#undef _ll_box
#undef _ll_obj
#undef _ll_rec
#undef _ll_iter
#undef _ll_delete
<
<
<
<












>

|



|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|







 







|
>
>
>
>
>
>
>













1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
..
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103




#ifndef _ll_ffirst
#define _ll_ffirst _cat(first_,_ll_rec)
#endif

#ifndef _ll_flast
#define _ll_flast _cat(last_,_ll_rec)
#endif

#ifndef _ll_fnext
#define _ll_fnext _cat(next_,_ll_rec)
#endif

#define _LL_fn(act) _cat(_cat(_ll_ns,_),_cat(_cat(act,_), _ll_rec))
#ifndef _ll_dropfn
#define _ll_dropfn _LL_fn(drop)
#endif

#ifndef _ll_pushfn
#define _ll_pushfn _LL_fn(push)
#endif

#ifndef _ll_newfn
#define _ll_newfn _LL_fn(new)
#endif

#ifndef _ll_delete
#define _ll_delete free
#	ifdef _ll_impl
#		include <stdlib.h>
#	endif
#endif

#ifdef _ll_impl
#	define _LL_impl(...) __VA_ARGS__
#	include "def.h"
#else
#	define _LL_impl(...) ;
#endif

void _ll_pushfn
(_ll_box* box, _ll_obj* obj) _LL_impl({
	if (box -> _ll_flast)
		box -> _ll_flast -> _ll_fnext = obj;
	else box -> _ll_ffirst = obj;
	box -> _ll_flast = obj;
})

_ll_obj* _ll_newfn
(_ll_box* box) _LL_impl({
	_ll_obj* o = calloc(1, sizeof(_ll_obj));
	_ll_pushfn(box, o);
	return o;
})

void _ll_dropfn
(_ll_box* box, _ll_obj* obj) _LL_impl({
	if(box -> _ll_ffirst == obj) {
		if(box -> _ll_flast == obj) {
			box -> _ll_ffirst = box -> _ll_flast = null;
		} else {
			box -> _ll_ffirst = obj -> _ll_fnext;
		}
	} else {
................................................................................
				}
			}
			_fatal("BUG in elt deletion routine");
			found2 :;
		}
	}
	_ll_delete (obj);
})

#undef _LL_fn
#undef _LL_impl

#undef _ll_pushfn
#undef _ll_newfn
#undef _ll_dropfn

#undef _ll_ffirst
#undef _ll_flast
#undef _ll_ns
#undef _ll_box
#undef _ll_obj
#undef _ll_rec
#undef _ll_iter
#undef _ll_delete

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





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

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



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include "def.h"
#include <wireguard.h>

#define _ll_rec peer
#define _ll_box wg_device
#define _ll_obj wg_peer
#define _ll_iter wg_for_each_peer
#define _ll_ns wgd
#include "list.h"

#define _ll_rec allowedip
#define _ll_box wg_peer
#define _ll_obj wg_allowedip
#define _ll_iter wg_for_each_allowedip
#define _ll_ns wgd_peer
#include "list.h"

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

10
11
12
13
14
15
16




17
18
19
20
21
22
23
...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
...
276
277
278
279
280
281
282




283
284
285
286
287
288
289
290
291
292
293
294
295
296


297
298
299
300
301
302
303
304




305
306
307
308
309


310
311
312
313
314
315
316
317
318
319
320

321
322
323
324
325
326
327
328
329


330
331


332



333
334
335
336
337
338
339
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>

/* libs */
#include <wireguard.h>





#include <libpq-fe.h>


size_t dumpEndpoint(char* d, const wg_endpoint* const e) {
	const struct sockaddr* addr;
	size_t len;
................................................................................
	) free(allowedip);
	/* end import */
	free(peer);
}

/* linked list manipulation routines */

#define _ll_rec peer
#define _ll_box wg_device
#define _ll_obj wg_peer
#define _ll_iter wg_for_each_peer
#define _ll_ns wgd
#include "list.h"

#define _ll_rec allowedip
#define _ll_box wg_peer
#define _ll_obj wg_allowedip
#define _ll_iter wg_for_each_allowedip
#define _ll_ns wgd_peer
#include "list.h"

#if 0
void wgd_drop_peer(wg_device* dev, wg_peer* peer) {
	if(dev -> first_peer == peer) {
		if(dev -> last_peer == peer) {
			dev -> first_peer = dev -> last_peer = null;
		} else {
................................................................................
					}
				++l;}
				
				if(!foundIP) {
					/* this IP hasn't been loaded into the
					 * kernel yet; upload it now */
					_infof("inserting IP PG%zu %s", j, inetstr);




					dirty = true;
				}
			}

			if(goodIPc < ips -> sz) {
				size_t l = 0;
				wg_allowedip* wgip;
				wg_for_each_allowedip(found, wgip) {
					char inetstr[256];
					dumpAllowedIP(inetstr, wgip);
					_dbgf("IP WG%zu :: %s", l, inetstr);
					if(!goodIPs[l]) {
						/* this IP is stale, delete it */
						_infof("deleting IP WG%zu %s", l, inetstr);


						dirty = true;
					}
				++l;}
			}
		} else {
			_infof("inserting key %s", key_b64);
			dirty = true;
			/* install new peer */




			for (size_t j = 0; j < ips -> sz; ++j) {
				char inetstr[256];
				wg_allowedip aip = inet_to_allowedip(ips -> elts[j].data);
				dumpAllowedIP(inetstr, &aip);
				_dbgf("new IP %zu :: %s", j, inetstr);


			}
		}

		free(ips);
	}
	{ size_t i=0; wg_peer* p; wg_for_each_peer(wg, p) {
		if(valid_peers[i] == false) {
			char b64 [128];
			wg_key_to_base64(b64, p->public_key);
			_infof("dropping peer %s", b64);
			wgd_drop_peer(wg, p);

			dirty = true;
		}
	++i;}}

	_dbg("final peer list:");
	{ size_t j=0; wg_peer* p; wg_for_each_peer(wg, p) {
		char b64 [128];
		wg_key_to_base64(b64, p->public_key);
		_dbgf("P%zu :: %s", j, b64);


	++j;}}
	


	if(dirty) wg_set_device(wg);




	PQclear(rows);
}

int main(int argc, char** argv) {
	setvbuf(stderr, null, _IONBF, 0);
	if (argc < 3) {







>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<
<







 







>
>
>
>











|


>
>








>
>
>
>





>
>






|



|
>








|
>
>


>
>
|
>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
...
129
130
131
132
133
134
135













136
137
138
139
140
141
142
...
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <netinet/in.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>

/* libs */
#include <wireguard.h>
#include "wglist.h"
	/* wireguard uses messy linked lists but doesn't
	 * provide any routines for manipulating them;
	 * wglist.h fills in the gap */

#include <libpq-fe.h>


size_t dumpEndpoint(char* d, const wg_endpoint* const e) {
	const struct sockaddr* addr;
	size_t len;
................................................................................
	) free(allowedip);
	/* end import */
	free(peer);
}

/* linked list manipulation routines */















#if 0
void wgd_drop_peer(wg_device* dev, wg_peer* peer) {
	if(dev -> first_peer == peer) {
		if(dev -> last_peer == peer) {
			dev -> first_peer = dev -> last_peer = null;
		} else {
................................................................................
					}
				++l;}
				
				if(!foundIP) {
					/* this IP hasn't been loaded into the
					 * kernel yet; upload it now */
					_infof("inserting IP PG%zu %s", j, inetstr);
					// is this necessary? FIXME
					/* found -> flags |= WGPEER_REPLACE_ALLOWEDIPS; */
					wg_allowedip* nip = wgd_peer_new_allowedip(found);
					memcpy(nip, &aip, sizeof aip);
					dirty = true;
				}
			}

			if(goodIPc < ips -> sz) {
				size_t l = 0;
				wg_allowedip* wgip;
				wg_for_each_allowedip(found, wgip) {
					char inetstr[256];
					dumpAllowedIP(inetstr, wgip);
					_dbgf("IP WG%zu :: %s", l, inetstr);
					if(l<goodIPc && !goodIPs[l]) {
						/* this IP is stale, delete it */
						_infof("deleting IP WG%zu %s", l, inetstr);
						wgd_peer_drop_allowedip(found, wgip);
						found -> flags |= WGPEER_REPLACE_ALLOWEDIPS;
						dirty = true;
					}
				++l;}
			}
		} else {
			_infof("inserting key %s", key_b64);
			dirty = true;
			/* install new peer */
			wg_peer* np = wgd_new_peer(wg);
			np -> flags = WGPEER_HAS_PUBLIC_KEY;
			memcpy(np -> public_key, key, sizeof key);

			for (size_t j = 0; j < ips -> sz; ++j) {
				char inetstr[256];
				wg_allowedip aip = inet_to_allowedip(ips -> elts[j].data);
				dumpAllowedIP(inetstr, &aip);
				_dbgf("new IP %zu :: %s", j, inetstr);
				wg_allowedip* nip = wgd_peer_new_allowedip(np);
				memcpy(nip, &aip, sizeof aip);
			}
		}

		free(ips);
	}
	{ size_t i=0; wg_peer* p; wg_for_each_peer(wg, p) {
		if(i<peerc && valid_peers[i] == false) {
			char b64 [128];
			wg_key_to_base64(b64, p->public_key);
			_infof("dropping peer %s", b64);
			//wgd_drop_peer(wg, p);
			p -> flags |= WGPEER_REMOVE_ME;
			dirty = true;
		}
	++i;}}

	_dbg("final peer list:");
	{ size_t j=0; wg_peer* p; wg_for_each_peer(wg, p) {
		char b64 [128];
		wg_key_to_base64(b64, p->public_key);
		_dbgf("P%zu :: %s%s", j, b64,
			p->flags & WGPEER_REMOVE_ME          ? " [DELETE]" :
			p->flags & WGPEER_REPLACE_ALLOWEDIPS ? " [CHGIP]" : "");
	++j;}}
	
	dirty = true;
	if(dirty) {
		int e = wg_set_device(wg);
		if(e != 0) 
			_fatalf("could not set wg device (error %i)", -e);
	}

	PQclear(rows);
}

int main(int argc, char** argv) {
	setvbuf(stderr, null, _IONBF, 0);
	if (argc < 3) {