51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include <stdio.h>
-#include <stdbool.h>
-#include <string.h>
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netdb.h>
+#include "../src/system.h"
#ifdef HAVE_MINGW
-extern const char *winerror(int);
+static const char *winerror(int err) {
+ static char buf[1024], *ptr;
+
+ ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
+
+ if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
+ strncpy(buf, "(unable to format errormessage)", sizeof(buf));
+ };
+
+ if((ptr = strchr(buf, '\r'))) {
+ *ptr = '\0';
+ }
+
+ return buf;
+}
+
#define strerror(x) ((x)>0?strerror(x):winerror(GetLastError()))
#define sockerrno WSAGetLastError()
#define sockstrerror(x) winerror(x)
int sock[2];
char buf[1024];
- struct addrinfo *ai, hint;
- memset(&hint, 0, sizeof(hint));
+ const struct addrinfo hint = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ .ai_protocol = IPPROTO_TCP,
+ .ai_flags = 0,
+ };
- hint.ai_family = AF_UNSPEC;
- hint.ai_socktype = SOCK_STREAM;
- hint.ai_protocol = IPPROTO_TCP;
- hint.ai_flags = 0;
+ for(int i = 0; i < 2; i++) {
+ struct addrinfo *ai;
- for (int i = 0; i < 2; i++) {
if(getaddrinfo(argv[2 + 3 * i], argv[3 + 3 * i], &hint, &ai) || !ai) {
fprintf(stderr, "getaddrinfo() failed: %s\n", sockstrerror(sockerrno));
return 1;
if(sock[i] == -1) {
fprintf(stderr, "Could not create socket: %s\n", sockstrerror(sockerrno));
+ freeaddrinfo(ai);
return 1;
}
if(connect(sock[i], ai->ai_addr, ai->ai_addrlen)) {
fprintf(stderr, "Could not connect to %s: %s\n", argv[i + 3 * i], sockstrerror(sockerrno));
+ freeaddrinfo(ai);
return 1;
}
+ freeaddrinfo(ai);
+
fprintf(stderr, "Connected to %s\n", argv[1 + 3 * i]);
/* Pretend to be the other one */
int len = snprintf(buf, sizeof buf, "0 %s %s\n", argv[4 - 3 * i], protocol);
- if (send(sock[i], buf, len, 0) != len) {
+
+ if(send(sock[i], buf, len, 0) != len) {
fprintf(stderr, "Error sending data to %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
return 1;
}
/* Ignore the response */
do {
- if (recv(sock[i], buf, 1, 0) != 1) {
+ if(recv(sock[i], buf, 1, 0) != 1) {
fprintf(stderr, "Error reading data from %s: %s\n", argv[1 + 3 * i], sockstrerror(sockerrno));
return 1;
}
return 1;
}
- for(int i = 0; i < 2; i++ ) {
+ for(int i = 0; i < 2; i++) {
if(FD_ISSET(sock[i], &fds)) {
ssize_t len = recv(sock[i], buf, sizeof buf, 0);