forked from hyc/wolf-xmr-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
109 lines (85 loc) · 2.04 KB
/
net.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
#include "stdafx.h"
#include <string.h>
#ifdef __linux__
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include "minerlog.h"
#include "minernet.h"
int NetworkingInit(void)
{
#ifdef __linux__
return(0);
#else
WSADATA data;
return(WSAStartup(MAKEWORD(2, 0), &data));
#endif
}
void NetworkingShutdown(void)
{
#ifndef __linux__
WSACleanup();
#endif
}
int ConnectToPool(char *URL, char *Port)
{
int ret, sockfd;
struct addrinfo filter, *poolinfo, *tmp;
memset(&filter, 0, sizeof(struct addrinfo));
filter.ai_family = AF_INET;
filter.ai_socktype = SOCK_STREAM;
filter.ai_flags = AI_PASSIVE;
filter.ai_protocol = IPPROTO_TCP;
ret = getaddrinfo(URL, Port, &filter, &poolinfo);
if(ret)
{
Log(LOG_CRITICAL, "The attempt to get the address for the pool failed with code %d.", ret);
return(INVALID_SOCKET);
}
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sockfd == INVALID_SOCKET)
{
Log(LOG_CRITICAL, "The attempt to create a socket failed.");
freeaddrinfo(poolinfo);
return(INVALID_SOCKET);
}
for(tmp = poolinfo; tmp; tmp = tmp->ai_next)
{
ret = connect(sockfd, tmp->ai_addr, tmp->ai_addrlen);
if(ret != INVALID_SOCKET) break;
Log(LOG_ADVINFO, "The attempt to connect to the pool failed.");
}
// Did we run out of addresses before successfully connecting?
if(!tmp)
{
Log(LOG_CRITICAL, "Failed to connect to any of the pool's addresses.");
freeaddrinfo(poolinfo);
return(INVALID_SOCKET);
}
freeaddrinfo(poolinfo);
return(sockfd);
}
int SetNonBlockingSocket(SOCKET sockfd)
{
// Set socket to non-blocking mode
int ret;
#ifdef __linux__
int iof = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, iof | O_NONBLOCK);
#else
unsigned long int enable = 1;
ioctlsocket(sockfd, FIONBIO, &enable);
#endif
{
bool keepalive = true;
ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (const char *)&keepalive, sizeof(keepalive));
}
return(ret);
}