-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.c
131 lines (98 loc) · 3.14 KB
/
conn.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
122
123
124
125
126
127
128
129
130
131
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>
#include "conn.h"
conn *listening(void) {
conn* Conn = (conn *)malloc(sizeof(conn));
int len = sizeof(Conn->local);
Conn->sockfd = s_socket(DOMAIN, TYPE, PROTOCOL);
bzero(&(Conn->local), sizeof(Conn->local));
Conn->local.sin_family = DOMAIN;
Conn->local.sin_addr.s_addr = htonl(INADDR_ANY); //??
Conn->local.sin_port = htons(DEFAULT_PORT);
s_bind(Conn->sockfd, (struct sockaddr *)&(Conn->local),
sizeof(Conn->local));
s_getsockname(Conn->sockfd, (struct sockaddr *)&(Conn->local),
(socklen_t *)&len);
Conn->len = sizeof(Conn->remote);
return Conn;
}
int s_socket(int domain, int type, int protocol) {
int sockfd = socket(domain, type, protocol);
if (sockfd == SOCK_ERR)
perror("s_socket"), exit(EXIT_FAILURE);
return sockfd;
}
void s_bind(int sockfd, const struct sockaddr *addr,
socklen_t addrlen) {
int ret_val = bind(sockfd, addr, addrlen);
if (ret_val == BIND_ERR)
perror("s_socket"), exit(EXIT_FAILURE);
}
void s_getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
int ret_val = getsockname(sockfd, addr, addrlen);
if (ret_val == GETSOCKNAME_ERR)
perror("s_socket"), exit(EXIT_FAILURE);
}
void send_pkt(conn *Conn, pkt *Pkt) {
int flags = SENDTO_FLAGS;
if (Conn != NULL && Pkt != NULL && Pkt->datagram != NULL) {
s_sendto(Conn->sockfd, Pkt->datagram, Pkt->datagram_len, flags,
(struct sockaddr *)&(Conn->remote), Conn->len);
}
else
fprintf(stderr, "send_pkt argument error\n");
}
void s_sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen) {
int bytes_sent = sendto(sockfd, buf, len, flags, dest_addr, addrlen);
if (bytes_sent == SENDTO_ERR)
perror("s_sendto"), exit(EXIT_FAILURE);
}
void recv_pkt(conn *Conn, pkt *Pkt) {
int flags = RECVFROM_FLAGS;
if (Conn != NULL && Pkt != NULL) {
Pkt->datagram_len = s_recvfrom(Conn->sockfd, Pkt->datagram, Pkt->mtu, flags,
(struct sockaddr *)&(Conn->remote), (socklen_t *)&(Conn->len));
}
else {
fprintf(stderr, "recv_pkt: conn %p | pkt %p\n", Conn, Pkt);
}
}
ssize_t s_recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen) {
int bytes_recvd = recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
if (bytes_recvd == RECVFROM_ERR)
perror("s_recvfrom"), exit(EXIT_FAILURE);
return bytes_recvd;
}
int select_call(int socket, int seconds, int useconds)
{
static struct timeval timeout;
fd_set fdvar;
int select_out;
timeout.tv_sec = seconds;
timeout.tv_usec = useconds;
FD_ZERO(&fdvar);
FD_SET(socket, &fdvar);
select_out = select(socket+1, (fd_set *)&fdvar, (fd_set *)0, (fd_set *)0, &timeout);
if (FD_ISSET(socket, &fdvar)) {
return 1;
}
else if (select_out < 0) {
perror("select_call");
exit(EXIT_FAILURE);
}
else
return 0;
}
pkt *pkt_alloc(uint32_t mtu) {
pkt *Pkt = (pkt *)malloc(sizeof(pkt));
Pkt->mtu = mtu;
return Pkt;
}