-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathexample_nc.c
122 lines (109 loc) · 2.59 KB
/
example_nc.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
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
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "pt.h"
typedef pt_queue(uint8_t, 4096) byte_queue_t;
static void stdin_loop(struct pt *pt, byte_queue_t *out) {
static uint8_t b;
static int r;
pt_begin(pt);
for (;;) {
pt_sys(pt, r = read(STDIN_FILENO, &b, 1));
if (r == 0) {
pt_wait(pt, pt_queue_empty(out));
pt_exit(pt, 1);
}
pt_wait(pt, !pt_queue_full(out));
pt_queue_push(out, b);
}
pt_end(pt);
}
static void socket_write_loop(struct pt *pt, int fd, byte_queue_t *in) {
static uint8_t *b;
pt_begin(pt);
for (;;) {
pt_wait(pt, !pt_queue_empty(in));
b = pt_queue_pop(in);
pt_sys(pt, send(fd, b, 1, 0));
}
pt_end(pt);
}
static void socket_read_loop(struct pt *pt, int fd) {
static uint8_t b;
static int r;
pt_begin(pt);
for (;;) {
pt_sys(pt, r = recv(fd, &b, 1, 0));
if (r == 0) {
pt_exit(pt, 1);
}
pt_sys(pt, write(STDOUT_FILENO, &b, 1));
}
pt_end(pt);
}
static int nonblock(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) {
return -1;
}
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "USAGE: example_nc <ip> <port>\n");
return 1;
}
char *host = argv[1];
int port = atoi(argv[2]);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket()");
return 1;
}
if (nonblock(fd) < 0) {
perror("nonblock() socket");
return 1;
}
if (nonblock(STDIN_FILENO) < 0) {
perror("nonblock() stdin");
return 1;
}
if (nonblock(STDOUT_FILENO) < 0) {
perror("nonblock() stdout");
return 1;
}
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_addr =
{
.s_addr = inet_addr(host),
},
.sin_port = htons(port),
};
connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
struct pt pt_stdin = pt_init();
struct pt pt_socket_read = pt_init();
struct pt pt_socket_write = pt_init();
byte_queue_t queue = pt_queue_init();
while (pt_status(&pt_stdin) == 0 && pt_status(&pt_socket_read) == 0) {
if (pt_queue_empty(&queue)) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(fd, &fds);
select(fd + 1, &fds, NULL, NULL, NULL);
}
socket_read_loop(&pt_socket_read, fd);
socket_write_loop(&pt_socket_write, fd, &queue);
stdin_loop(&pt_stdin, &queue);
}
close(fd);
return 0;
}