-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket_snd_th.c
235 lines (193 loc) · 6.01 KB
/
socket_snd_th.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#define _GNU_SOURCE
#include <features.h>
#include <proton/condition.h>
#include <proton/message.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include "bridge.h"
#include "rb.h"
#include "utils.h"
static struct addrinfo *peer_addrinfo;
static pn_message_t *m_glbl = NULL;
static int prepare_send_socket_unix(app_data_t *app) {
struct sockaddr_un name;
/* Create socket on which to send. */
app->send_sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (app->send_sock < 0) {
perror("opening datagram socket");
return -1;
}
memset(&name, 0, sizeof(name));
/* Construct name of socket to send to. */
name.sun_family = AF_UNIX;
strcpy(name.sun_path, app->unix_socket_name);
printf("%s ==> (%s)\n", app->container_id, name.sun_path);
memcpy(&app->sa, &name, sizeof(name));
app->sa_len = sizeof(name);
return 0;
}
static int prepare_send_socket_inet(app_data_t *app) {
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC, hints.ai_socktype = SOCK_DGRAM,
hints.ai_protocol = 0, hints.ai_flags = AI_ADDRCONFIG;
int err =
getaddrinfo(app->peer_host, app->peer_port, &hints, &peer_addrinfo);
if (err != 0) {
fprintf(stderr, "%s: getaddrinfo returned non-zero value: %d\n",
__func__, errno);
perror("Error");
freeaddrinfo(peer_addrinfo);
return -1;
}
app->send_sock =
socket(peer_addrinfo->ai_family, peer_addrinfo->ai_socktype,
peer_addrinfo->ai_protocol);
if (app->send_sock == -1) {
fprintf(stderr, "%s: socket returned -1\n", __func__);
perror("Error");
freeaddrinfo(peer_addrinfo);
return -1;
}
char addrstr[100];
void *ptr = &((struct sockaddr_in *)peer_addrinfo->ai_addr)->sin_addr;
inet_ntop(peer_addrinfo->ai_family, ptr, addrstr, sizeof(addrstr));
printf(
"%s ==> (%s:%d)\n", app->container_id, addrstr,
ntohs(
(((struct sockaddr_in *)((struct sockaddr *)peer_addrinfo->ai_addr))
->sin_port)));
memcpy(&app->sa, peer_addrinfo->ai_addr, peer_addrinfo->ai_addrlen);
app->sa_len = peer_addrinfo->ai_addrlen;
return 0;
}
static int process_message_binary(app_data_t *app, pn_data_t *body) {
pn_bytes_t b = pn_data_get_bytes(body);
if (b.start != NULL) {
int send_flags = app->socket_flags;
ssize_t sent_bytes = sendto(app->send_sock, b.start, b.size, send_flags,
&app->sa, app->sa_len);
if (sent_bytes <= 0) {
// MSG_DONTWAIT is set
switch (errno) {
case EAGAIN:
// Normal backup
app->sock_would_block++;
break;
case EBADF:
case ENOTSOCK:
// sockfd is not a valid file descriptor
// TODO reopen socket
perror("SG Send");
return 1;
break;
case ECONNREFUSED:
break;
default:
perror("SG Send");
printf("%d ", errno);
return 1;
}
} else {
app->sock_sent++;
}
}
return 0;
}
static int process_message_body(app_data_t *app, pn_data_t *body) {
int err = 0;
if (pn_data_type(body) == PN_LIST) {
size_t count = pn_data_get_list(body);
pn_data_enter(body);
for (size_t i = 0; i < count; i++) {
if (pn_data_next(body)) {
err += process_message_body(app, body);
}
}
pn_data_exit(body);
} else if (pn_data_type(body) == PN_SYMBOL ||
pn_data_type(body) == PN_STRING ||
pn_data_type(body) == PN_BINARY) {
err = process_message_binary(app, body);
} else {
perror("Unexpected message datatype recieved.");
err = 1;
}
return err;
}
static int decode_message(app_data_t *app, pn_rwbytes_t data) {
pn_message_t *m;
// Use a static message with pn_message_clear(...)
if ((m = m_glbl) == NULL) {
m_glbl = pn_message();
m = m_glbl;
} else {
pn_message_clear(m);
}
int err = pn_message_decode(m, data.start, data.size);
if (!err) {
pn_data_t *body = pn_message_body(m);
if (pn_data_next(body)) {
err = process_message_body(app, body);
if (err) {
return 1;
}
}
} else {
// Record the error. Don't exit immediately
//
app->amqp_decode_errs++;
return 1;
}
return 0;
}
void socket_snd_th_cleanup(void *app_ptr) {
app_data_t *app = (app_data_t *)app_ptr;
if (app) {
app->socket_snd_th_running = 0;
}
fprintf(stderr, "Exit SOCKET thread...\n");
}
void *socket_snd_th(void *app_ptr) {
pthread_cleanup_push(socket_snd_th_cleanup, app_ptr);
app_data_t *app = (app_data_t *)app_ptr;
app->sa_len = sizeof(app->sa);
memset(&app->sa, 0, app->sa_len);
// Create the send socket
switch (app->domain) {
case AF_UNIX:
if (prepare_send_socket_unix(app) == -1) {
fprintf(stderr, "Failed to create socket... exiting!");
return NULL;
}
break;
case AF_INET:
if (prepare_send_socket_inet(app) == -1) {
fprintf(stderr, "Failed to create socket... exiting!");
return NULL;
}
break;
default:
fprintf(stderr, "Unknown domain type: %d", app->domain);
break;
}
clock_gettime(CLOCK_MONOTONIC, &app->rbin->total_t2);
while (1) {
pn_rwbytes_t *msg = rb_get(app->rbin);
decode_message(app, *msg);
}
if (app->send_sock != -1) {
close(app->send_sock);
fprintf(stdout, "Socket closed\n");
}
pthread_cleanup_pop(1);
return NULL;
}