Commit d65a88a2f0db40d2c2ac34ee34e8aff8ac629d52

Stefan Sperling 2021-09-05T20:19:14

move duplicated dial_ssh() and dial_git() functions into a common file These functions are used by 'got send' and 'got fetch' in order to open network connections to a server. Move them into new file lib/dial.c and declare relevant functions in got_dial.h and lib/got_lib_dial.h. No functional change.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
diff --git a/got/Makefile b/got/Makefile
index 14f769b..a53d258 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -12,7 +12,7 @@ SRCS=		got.c blame.c commit_graph.c delta.c diff.c \
 		gotconfig.c diff_main.c diff_atomize_text.c \
 		diff_myers.c diff_output.c diff_output_plain.c \
 		diff_output_unidiff.c diff_output_edscript.c \
-		diff_patience.c send.c deltify.c pack_create.c
+		diff_patience.c send.c deltify.c pack_create.c dial.c
 
 MAN =		${PROG}.1 got-worktree.5 git-repository.5 got.conf.5
 
diff --git a/got/got.c b/got/got.c
index e7638e1..a907f12 100644
--- a/got/got.c
+++ b/got/got.c
@@ -55,6 +55,7 @@
 #include "got_privsep.h"
 #include "got_opentemp.h"
 #include "got_gotconfig.h"
+#include "got_dial.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -1598,13 +1599,10 @@ cmd_clone(int argc, char *argv[])
 		}
 	}
 
-	if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
-		if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
-			error = got_error_from_errno2("unveil",
-			    GOT_FETCH_PATH_SSH);
-			goto done;
-		}
-	}
+	error = got_dial_apply_unveil(proto);
+	if (error)
+		goto done;
+
 	error = apply_unveil(repo_path, 0, NULL);
 	if (error)
 		goto done;
@@ -2408,13 +2406,10 @@ cmd_fetch(int argc, char *argv[])
 		goto done;
 	}
 
-	if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
-		if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
-			error = got_error_from_errno2("unveil",
-			    GOT_FETCH_PATH_SSH);
-			goto done;
-		}
-	}
+	error = got_dial_apply_unveil(proto);
+	if (error)
+		goto done;
+
 	error = apply_unveil(got_repo_get_path(repo), 0, NULL);
 	if (error)
 		goto done;
@@ -7726,13 +7721,10 @@ cmd_send(int argc, char *argv[])
 		goto done;
 	}
 
-	if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
-		if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
-			error = got_error_from_errno2("unveil",
-			    GOT_FETCH_PATH_SSH);
-			goto done;
-		}
-	}
+	error = got_dial_apply_unveil(proto);
+	if (error)
+		goto done;
+
 	error = apply_unveil(got_repo_get_path(repo), 0, NULL);
 	if (error)
 		goto done;
diff --git a/include/got_dial.h b/include/got_dial.h
new file mode 100644
index 0000000..b213aad
--- /dev/null
+++ b/include/got_dial.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+const struct got_error *got_dial_apply_unveil(const char *proto);
diff --git a/include/got_fetch.h b/include/got_fetch.h
index ec4d2dc..0678325 100644
--- a/include/got_fetch.h
+++ b/include/got_fetch.h
@@ -14,14 +14,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* IANA assigned */
-#define GOT_DEFAULT_GIT_PORT		9418
-#define GOT_DEFAULT_GIT_PORT_STR	"9418"
-
-#ifndef GOT_FETCH_PATH_SSH
-#define GOT_FETCH_PATH_SSH	"/usr/bin/ssh"
-#endif
-
 #define GOT_FETCH_DEFAULT_REMOTE_NAME	"origin"
 
 #define GOT_FETCH_PKTMAX	65536
diff --git a/include/got_send.h b/include/got_send.h
index 2f0388e..a805a93 100644
--- a/include/got_send.h
+++ b/include/got_send.h
@@ -15,14 +15,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-/* IANA assigned */
-#define GOT_DEFAULT_GIT_PORT		9418
-#define GOT_DEFAULT_GIT_PORT_STR	"9418"
-
-#ifndef GOT_SEND_PATH_SSH
-#define GOT_SEND_PATH_SSH	"/usr/bin/ssh"
-#endif
-
 #define GOT_SEND_DEFAULT_REMOTE_NAME	"origin"
 
 #define GOT_SEND_PKTMAX	65536
diff --git a/lib/dial.c b/lib/dial.c
new file mode 100644
index 0000000..43fd6d1
--- /dev/null
+++ b/lib/dial.c
@@ -0,0 +1,179 @@
+/*
+ * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "got_error.h"
+
+#include "got_lib_dial.h"
+
+#ifndef ssizeof
+#define ssizeof(_x) ((ssize_t)(sizeof(_x)))
+#endif
+
+#ifndef MIN
+#define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
+#endif
+
+#ifndef GOT_DIAL_PATH_SSH
+#define GOT_DIAL_PATH_SSH	"/usr/bin/ssh"
+#endif
+
+/* IANA assigned */
+#define GOT_DEFAULT_GIT_PORT		9418
+#define GOT_DEFAULT_GIT_PORT_STR	"9418"
+
+const struct got_error *
+got_dial_apply_unveil(const char *proto)
+{
+	if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
+		if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
+			return got_error_from_errno2("unveil",
+			    GOT_DIAL_PATH_SSH);
+		}
+	}
+
+	return NULL;
+}
+
+const struct got_error *
+got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
+    const char *port, const char *path, const char *direction, int verbosity)
+{
+	const struct got_error *error = NULL;
+	int pid, pfd[2];
+	char cmd[64];
+	char *argv[11];
+	int i = 0, j;
+
+	*newpid = -1;
+	*newfd = -1;
+
+	argv[i++] = GOT_DIAL_PATH_SSH;
+	if (port != NULL) {
+		argv[i++] = "-p";
+		argv[i++] = (char *)port;
+	}
+	if (verbosity == -1) {
+		argv[i++] = "-q";
+	} else {
+		/* ssh(1) allows up to 3 "-v" options. */
+		for (j = 0; j < MIN(3, verbosity); j++)
+			argv[i++] = "-v";
+	}
+	argv[i++] = "--";
+	argv[i++] = (char *)host;
+	argv[i++] = (char *)cmd;
+	argv[i++] = (char *)path;
+	argv[i++] = NULL;
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
+		return got_error_from_errno("socketpair");
+
+	pid = fork();
+	if (pid == -1) {
+		error = got_error_from_errno("fork");
+		close(pfd[0]);
+		close(pfd[1]);
+		return error;
+	} else if (pid == 0) {
+		int n;
+		if (close(pfd[1]) == -1)
+			err(1, "close");
+		if (dup2(pfd[0], 0) == -1)
+			err(1, "dup2");
+		if (dup2(pfd[0], 1) == -1)
+			err(1, "dup2");
+		n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
+		if (n < 0 || n >= ssizeof(cmd))
+			err(1, "snprintf");
+		if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
+			err(1, "execv");
+		abort(); /* not reached */
+	} else {
+		if (close(pfd[0]) == -1)
+			return got_error_from_errno("close");
+		*newpid = pid;
+		*newfd = pfd[1];
+		return NULL;
+	}
+}
+
+const struct got_error *
+got_dial_git(int *newfd, const char *host, const char *port,
+    const char *path, const char *direction)
+{
+	const struct got_error *err = NULL;
+	struct addrinfo hints, *servinfo, *p;
+	char *cmd = NULL;
+	int fd = -1, len, r, eaicode;
+
+	*newfd = -1;
+
+	if (port == NULL)
+		port = GOT_DEFAULT_GIT_PORT_STR;
+
+	memset(&hints, 0, sizeof hints);
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+	eaicode = getaddrinfo(host, port, &hints, &servinfo);
+	if (eaicode) {
+		char msg[512];
+		snprintf(msg, sizeof(msg), "%s: %s", host,
+		    gai_strerror(eaicode));
+		return got_error_msg(GOT_ERR_ADDRINFO, msg);
+	}
+
+	for (p = servinfo; p != NULL; p = p->ai_next) {
+		if ((fd = socket(p->ai_family, p->ai_socktype,
+		    p->ai_protocol)) == -1)
+			continue;
+		if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
+			err = NULL;
+			break;
+		}
+		err = got_error_from_errno("connect");
+		close(fd);
+	}
+	if (p == NULL)
+		goto done;
+
+	if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
+		err = got_error_from_errno("asprintf");
+		goto done;
+	}
+	len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
+	r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
+	if (r < 0)
+		err = got_error_from_errno("dprintf");
+done:
+	free(cmd);
+	if (err) {
+		if (fd != -1)
+			close(fd);
+	} else
+		*newfd = fd;
+	return err;
+}
diff --git a/lib/fetch.c b/lib/fetch.c
index 798e728..58ebe83 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -39,8 +39,6 @@
 #include <imsg.h>
 #include <time.h>
 #include <uuid.h>
-#include <netdb.h>
-#include <netinet/in.h>
 
 #include "got_error.h"
 #include "got_reference.h"
@@ -62,6 +60,7 @@
 #include "got_lib_privsep.h"
 #include "got_lib_object_cache.h"
 #include "got_lib_repository.h"
+#include "got_lib_dial.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -87,126 +86,6 @@ hassuffix(char *base, char *suf)
 	return 0;
 }
 
-static const struct got_error *
-dial_ssh(pid_t *fetchpid, int *fetchfd, const char *host, const char *port,
-    const char *path, const char *direction, int verbosity)
-{
-	const struct got_error *error = NULL;
-	int pid, pfd[2];
-	char cmd[64];
-	char *argv[11];
-	int i = 0, j;
-
-	*fetchpid = -1;
-	*fetchfd = -1;
-
-	argv[i++] = GOT_FETCH_PATH_SSH;
-	if (port != NULL) {
-		argv[i++] = "-p";
-		argv[i++] = (char *)port;
-	}
-	if (verbosity == -1) {
-		argv[i++] = "-q";
-	} else {
-		/* ssh(1) allows up to 3 "-v" options. */
-		for (j = 0; j < MIN(3, verbosity); j++)
-			argv[i++] = "-v";
-	}
-	argv[i++] = "--";
-	argv[i++] = (char *)host;
-	argv[i++] = (char *)cmd;
-	argv[i++] = (char *)path;
-	argv[i++] = NULL;
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
-		return got_error_from_errno("socketpair");
-
-	pid = fork();
-	if (pid == -1) {
-		error = got_error_from_errno("fork");
-		close(pfd[0]);
-		close(pfd[1]);
-		return error;
-	} else if (pid == 0) {
-		int n;
-		if (close(pfd[1]) == -1)
-			err(1, "close");
-		if (dup2(pfd[0], 0) == -1)
-			err(1, "dup2");
-		if (dup2(pfd[0], 1) == -1)
-			err(1, "dup2");
-		n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
-		if (n < 0 || n >= ssizeof(cmd))
-			err(1, "snprintf");
-		if (execv(GOT_FETCH_PATH_SSH, argv) == -1)
-			err(1, "execv");
-		abort(); /* not reached */
-	} else {
-		if (close(pfd[0]) == -1)
-			return got_error_from_errno("close");
-		*fetchpid = pid;
-		*fetchfd = pfd[1];
-		return NULL;
-	}
-}
-
-static const struct got_error *
-dial_git(int *fetchfd, const char *host, const char *port, const char *path,
-    const char *direction)
-{
-	const struct got_error *err = NULL;
-	struct addrinfo hints, *servinfo, *p;
-	char *cmd = NULL;
-	int fd = -1, len, r, eaicode;
-
-	*fetchfd = -1;
-
-	if (port == NULL)
-		port = GOT_DEFAULT_GIT_PORT_STR;
-
-	memset(&hints, 0, sizeof hints);
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
-	eaicode = getaddrinfo(host, port, &hints, &servinfo);
-	if (eaicode) {
-		char msg[512];
-		snprintf(msg, sizeof(msg), "%s: %s", host,
-		    gai_strerror(eaicode));
-		return got_error_msg(GOT_ERR_ADDRINFO, msg);
-	}
-
-	for (p = servinfo; p != NULL; p = p->ai_next) {
-		if ((fd = socket(p->ai_family, p->ai_socktype,
-		    p->ai_protocol)) == -1)
-			continue;
-		if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
-			err = NULL;
-			break;
-		}
-		err = got_error_from_errno("connect");
-		close(fd);
-	}
-	if (p == NULL)
-		goto done;
-
-	if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
-		err = got_error_from_errno("asprintf");
-		goto done;
-	}
-	len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
-	r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
-	if (r < 0)
-		err = got_error_from_errno("dprintf");
-done:
-	free(cmd);
-	if (err) {
-		if (fd != -1)
-			close(fd);
-	} else
-		*fetchfd = fd;
-	return err;
-}
-
 const struct got_error *
 got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
     const char *host, const char *port, const char *server_path, int verbosity)
@@ -217,10 +96,11 @@ got_fetch_connect(pid_t *fetchpid, int *fetchfd, const char *proto,
 	*fetchfd = -1;
 
 	if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
-		err = dial_ssh(fetchpid, fetchfd, host, port, server_path,
-		    "upload", verbosity);
+		err = got_dial_ssh(fetchpid, fetchfd, host, port,
+		    server_path, GOT_DIAL_DIRECTION_FETCH, verbosity);
 	else if (strcmp(proto, "git") == 0)
-		err = dial_git(fetchfd, host, port, server_path, "upload");
+		err = got_dial_git(fetchfd, host, port, server_path,
+		    GOT_DIAL_DIRECTION_FETCH);
 	else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
 		err = got_error_path(proto, GOT_ERR_NOT_IMPL);
 	else
diff --git a/lib/got_lib_dial.h b/lib/got_lib_dial.h
new file mode 100644
index 0000000..cbaf4ea
--- /dev/null
+++ b/lib/got_lib_dial.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#define GOT_DIAL_DIRECTION_SEND		"receive"
+#define GOT_DIAL_DIRECTION_FETCH	"upload"
+
+const struct got_error *got_dial_git(int *newfd, const char *host,
+    const char *port, const char *path, const char *direction);
+
+const struct got_error *got_dial_ssh(pid_t *newpid, int *newfd,
+    const char *host, const char *port, const char *path,
+    const char *direction, int verbosity);
diff --git a/lib/send.c b/lib/send.c
index 6c977a9..389638c 100644
--- a/lib/send.c
+++ b/lib/send.c
@@ -40,8 +40,6 @@
 #include <imsg.h>
 #include <time.h>
 #include <uuid.h>
-#include <netdb.h>
-#include <netinet/in.h>
 
 #include "got_error.h"
 #include "got_reference.h"
@@ -66,6 +64,7 @@
 #include "got_lib_object_cache.h"
 #include "got_lib_repository.h"
 #include "got_lib_pack_create.h"
+#include "got_lib_dial.h"
 
 #ifndef nitems
 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
@@ -79,126 +78,6 @@
 #define	MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
 #endif
 
-static const struct got_error *
-dial_ssh(pid_t *sendpid, int *sendfd, const char *host, const char *port,
-    const char *path, const char *direction, int verbosity)
-{
-	const struct got_error *error = NULL;
-	int pid, pfd[2];
-	char cmd[64];
-	char *argv[11];
-	int i = 0, j;
-
-	*sendpid = -1;
-	*sendfd = -1;
-
-	argv[i++] = GOT_SEND_PATH_SSH;
-	if (port != NULL) {
-		argv[i++] = "-p";
-		argv[i++] = (char *)port;
-	}
-	if (verbosity == -1) {
-		argv[i++] = "-q";
-	} else {
-		/* ssh(1) allows up to 3 "-v" options. */
-		for (j = 0; j < MIN(3, verbosity); j++)
-			argv[i++] = "-v";
-	}
-	argv[i++] = "--";
-	argv[i++] = (char *)host;
-	argv[i++] = (char *)cmd;
-	argv[i++] = (char *)path;
-	argv[i++] = NULL;
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
-		return got_error_from_errno("socketpair");
-
-	pid = fork();
-	if (pid == -1) {
-		error = got_error_from_errno("fork");
-		close(pfd[0]);
-		close(pfd[1]);
-		return error;
-	} else if (pid == 0) {
-		int n;
-		if (close(pfd[1]) == -1)
-			err(1, "close");
-		if (dup2(pfd[0], 0) == -1)
-			err(1, "dup2");
-		if (dup2(pfd[0], 1) == -1)
-			err(1, "dup2");
-		n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
-		if (n < 0 || n >= ssizeof(cmd))
-			err(1, "snprintf");
-		if (execv(GOT_SEND_PATH_SSH, argv) == -1)
-			err(1, "execv");
-		abort(); /* not reached */
-	} else {
-		if (close(pfd[0]) == -1)
-			return got_error_from_errno("close");
-		*sendpid = pid;
-		*sendfd = pfd[1];
-		return NULL;
-	}
-}
-
-static const struct got_error *
-dial_git(int *sendfd, const char *host, const char *port, const char *path,
-    const char *direction)
-{
-	const struct got_error *err = NULL;
-	struct addrinfo hints, *servinfo, *p;
-	char *cmd = NULL;
-	int fd = -1, len, r, eaicode;
-
-	*sendfd = -1;
-
-	if (port == NULL)
-		port = GOT_DEFAULT_GIT_PORT_STR;
-
-	memset(&hints, 0, sizeof hints);
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
-	eaicode = getaddrinfo(host, port, &hints, &servinfo);
-	if (eaicode) {
-		char msg[512];
-		snprintf(msg, sizeof(msg), "%s: %s", host,
-		    gai_strerror(eaicode));
-		return got_error_msg(GOT_ERR_ADDRINFO, msg);
-	}
-
-	for (p = servinfo; p != NULL; p = p->ai_next) {
-		if ((fd = socket(p->ai_family, p->ai_socktype,
-		    p->ai_protocol)) == -1)
-			continue;
-		if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
-			err = NULL;
-			break;
-		}
-		err = got_error_from_errno("connect");
-		close(fd);
-	}
-	if (p == NULL)
-		goto done;
-
-	if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
-		err = got_error_from_errno("asprintf");
-		goto done;
-	}
-	len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
-	r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
-	if (r < 0)
-		err = got_error_from_errno("dprintf");
-done:
-	free(cmd);
-	if (err) {
-		if (fd != -1)
-			close(fd);
-	} else
-		*sendfd = fd;
-	return err;
-}
-
 const struct got_error *
 got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
     const char *host, const char *port, const char *server_path, int verbosity)
@@ -209,10 +88,11 @@ got_send_connect(pid_t *sendpid, int *sendfd, const char *proto,
 	*sendfd = -1;
 
 	if (strcmp(proto, "ssh") == 0 || strcmp(proto, "git+ssh") == 0)
-		err = dial_ssh(sendpid, sendfd, host, port, server_path,
-		    "receive", verbosity);
+		err = got_dial_ssh(sendpid, sendfd, host, port, server_path,
+		    GOT_DIAL_DIRECTION_SEND, verbosity);
 	else if (strcmp(proto, "git") == 0)
-		err = dial_git(sendfd, host, port, server_path, "receive");
+		err = got_dial_git(sendfd, host, port, server_path,
+		    GOT_DIAL_DIRECTION_SEND);
 	else if (strcmp(proto, "http") == 0 || strcmp(proto, "git+http") == 0)
 		err = got_error_path(proto, GOT_ERR_NOT_IMPL);
 	else