-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_send.c
executable file
·151 lines (118 loc) · 3.38 KB
/
tcp_send.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "select.h"
#include "list.h"
#include "time.h"
static int tcp_handler(int sockid, int flags, void *ptr) {
tcp_port_t *port = (tcp_port_t *) ptr;
int optval;
socklen_t optlen = sizeof (int);
if (0 == getsockopt(port->sockfd, SOL_SOCKET, SO_ERROR,
(char *) &optval, &optlen)) {
switch (optval) {
case 0:
select_file_remove(port->sockfd);
port->status = TCP_PORT_OPEN;
port->addr->status = IP_ADDR_UP;
close(port->sockfd);
break;
default:
select_file_remove(port->sockfd);
port->status = TCP_PORT_CLOSED;
close(port->sockfd);
break;
}
} else {
perror("getsockopt");
}
return 1;
}
void tcp_send_msg(tcp_port_t *port) {
int flags;
int rv;
memset(&port->sin, 0, sizeof(port->sin));
port->sin.sin_family = AF_INET;
port->sin.sin_addr.s_addr = port->addr->addr;
port->sin.sin_port = htons(port->dest_port);
port->sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (port->sockfd < 0) {
perror("socket()");
return;
}
flags = fcntl(port->sockfd, F_GETFL, 0);
if (flags < 0) {
perror("fcntl()");
return;
}
flags = fcntl(port->sockfd, F_SETFL, flags | O_NONBLOCK);
if (flags < 0) {
perror("fcntl()");
return;
}
rv = connect(port->sockfd, (struct sockaddr *)&port->sin, sizeof(port->sin));
if (rv == 0) {
port->status = TCP_PORT_OPEN;
port->addr->status = IP_ADDR_UP;
close(port->sockfd);
} else {
switch (errno) {
case EINPROGRESS:
case EAGAIN:
case EALREADY:
case EISCONN:
case EINTR:
select_file_add(port->sockfd, SELECT_FLAGS_WRITE,
tcp_handler, port);
/* try again */
break;
default:
/* remove fd if it's there */
port->status = TCP_PORT_CLOSED;
close(port->sockfd);
break;
}
}
gettimeofday(&port->start_time, NULL);
}
static int tcp_check_status(tcp_port_t *port, void *ptr) {
int *status = (int *)(ptr);
if (port->status == TCP_PORT_UNKNOWN) {
*status = 0;
}
return 1;
}
static int ip_check_status(ip_addr_t *addr, void *ptr) {
tcp_port_list_for_each(addr->tcp_port_list, tcp_check_status, ptr);
return 1;
}
int tcp_is_done(ip_addr_list_t *list) {
int status = 1;
ip_addr_list_for_each(list, ip_check_status, &status);
return status;
}
static int tcp_check_timeout(tcp_port_t *port, void *ptr) {
int timeout = GPOINTER_TO_INT(ptr);
struct timeval now;
struct timeval timeouttime;
if (port->status == TCP_PORT_UNKNOWN) {
gettimeofday(&now, NULL);
tv_add_ms(&timeouttime, &port->start_time, timeout);
if (0 < tv_cmp(&now, &timeouttime)) {
port->status = TCP_PORT_CLOSED;
}
}
return 1;
}
static int ip_check_timeout(ip_addr_t *addr, void *ptr) {
tcp_port_list_for_each(addr->tcp_port_list, tcp_check_timeout, ptr);
return 1;
}
void tcp_check_timeouts(ip_addr_list_t *list, int timeout) {
ip_addr_list_for_each(list, ip_check_timeout, GINT_TO_POINTER(timeout));
}