-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
64 lines (48 loc) · 1.39 KB
/
client.c
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
#include "enc.h"
#include "error.h"
#include "msg.h"
#include "server.h"
#include "shared.h"
#include <arpa/inet.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
volatile int streamfd__client = -1;
void register_exit__client() {
if (streamfd__client > 0) {
encrypt_write(streamfd__client, MSG_EXIT);
close(streamfd__client);
}
printf("exiting...\n");
exit(0);
}
void setup_streamfd__client(char *remote) {
char *host;
char *port;
host = strtok(remote, ":");
port = strtok(NULL, ":");
if (!host || !port)
err(-1, "failed to parse host:port");
struct sockaddr_in remote_addr;
int remote_port;
if (inet_pton(AF_INET, host, &(remote_addr.sin_addr)) != 1)
err(-1, "failed to parse host address");
if ((remote_port = atoi(port)) == 0)
err(-1, "failed to parse host port");
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
err(sockfd, "failed to create tcp socket");
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(remote_port);
err(connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(remote_addr)),
"failed to connect");
streamfd__client = sockfd;
}
void main__client(char *remote) {
signal(SIGINT, register_exit__client);
setup_streamfd__client(remote);
printf("connected to %s, \"!exit\" to exit\n", remote);
spawn_rw_join((int *)&streamfd__client);
}