tangled
alpha
login
or
join now
stau.space
/
enchufe
0
fork
atom
Librería para enchufes en C.
0
fork
atom
overview
issues
pulls
pipelines
feat!: Using new `enchufa` function.
stau.space
6 months ago
d81675b8
9c44c7c5
+35
-34
4 changed files
expand all
collapse all
unified
split
src
client.c
lib
enchufe.c
enchufe.h
server.c
+1
-3
src/client.c
···
5
5
#define BUF_LEN 0x100
6
6
7
7
int main() {
8
8
-
FD fd = nuevo();
9
9
-
Receptaculo rec = receptaculo((IPv4){127,0,0,1}, htons(42069));
10
10
-
Enchufe enchufe = aplasta(fd, rec);
8
8
+
Enchufe enchufe = enchufa((IPv4){127,0,0,1}, htons(42069));
11
9
log_info("Connecting to socket.\n");
12
10
conecta(enchufe);
13
11
+2
-24
src/lib/enchufe.c
···
4
4
#include <netinet/in.h> // sockaddr_in
5
5
#include <arpa/inet.h> // inet_pton
6
6
7
7
-
FD nuevo() {
8
8
-
FD fd = socket(PF_INET, SOCK_STREAM, 0);
9
9
-
try (fd);
10
10
-
return fd;
11
11
-
}
12
12
-
13
13
-
Receptaculo receptaculo(IPv4 ip, Port port) {
14
14
-
struct sockaddr_in name = {
15
15
-
.sin_family = AF_INET,
16
16
-
.sin_addr = ip.ip,
17
17
-
.sin_port = port,
18
18
-
};
19
19
-
return (Receptaculo){
20
20
-
.addr = name,
21
21
-
.addrlen = sizeof(name),
22
22
-
};
23
23
-
}
24
24
-
25
25
-
Enchufe aplasta(FD fd, Receptaculo rec) {
26
26
-
return (Enchufe){
27
27
-
.fd = fd,
28
28
-
.addr = rec.addr,
29
29
-
.addrlen = rec.addrlen,
30
30
-
};
7
7
+
Enchufe enchufa(IPv4 ip, Port port) {
8
8
+
return aplasta(nuevo(), receptaculo(ip, port));
31
9
}
32
10
33
11
void conecta(Enchufe enchufe) {
+29
-3
src/lib/enchufe.h
···
42
42
socklen_t addrlen;
43
43
} Receptaculo;
44
44
45
45
-
FD nuevo();
46
46
-
Receptaculo receptaculo(IPv4 ip, Port port);
47
47
-
Enchufe aplasta(FD fd, Receptaculo rec);
45
45
+
inline FD nuevo() {
46
46
+
FD fd = socket(PF_INET, SOCK_STREAM, 0);
47
47
+
try (fd);
48
48
+
return fd;
49
49
+
}
50
50
+
51
51
+
inline Receptaculo receptaculo(IPv4 ip, Port port) {
52
52
+
struct sockaddr_in name = {
53
53
+
.sin_family = AF_INET,
54
54
+
.sin_port = port,
55
55
+
.sin_addr = {
56
56
+
.s_addr = ip.ip,
57
57
+
},
58
58
+
};
59
59
+
return (Receptaculo){
60
60
+
.addr = name,
61
61
+
.addrlen = sizeof(name),
62
62
+
};
63
63
+
}
64
64
+
65
65
+
inline Enchufe aplasta(FD fd, Receptaculo rec) {
66
66
+
return (Enchufe){
67
67
+
.fd = fd,
68
68
+
.addr = rec.addr,
69
69
+
.addrlen = rec.addrlen,
70
70
+
};
71
71
+
}
72
72
+
73
73
+
Enchufe enchufa(IPv4 ip, Port port);
48
74
void conecta(Enchufe enchufe);
49
75
void amarra(Enchufe enchufe);
50
76
void escucha(Enchufe enchufe, size_t len);
+3
-4
src/server.c
···
21
21
};
22
22
23
23
while (1) {
24
24
+
memset(buf.buf, 0, buf.len);
25
25
+
24
26
recibe(cliente, buf);
25
27
log_info("Read message: %s", buf.buf);
26
28
log_info("Sending message: %s", buf.buf);
27
29
zumba(cliente, buf);
28
28
-
memset(buf.buf, 0, buf.len);
29
30
}
30
31
31
32
desenchufa(cliente);
···
35
36
36
37
37
38
int main() {
38
38
-
FD fd = nuevo();
39
39
-
Receptaculo rec = receptaculo((IPv4){127,0,0,1}, htons(42069));
40
40
-
Enchufe enchufe = aplasta(fd, rec);
39
39
+
Enchufe enchufe = enchufa((IPv4){127,0,0,1}, htons(42069));
41
40
42
41
amarra(enchufe);
43
42
escucha(enchufe, 5);