-
Notifications
You must be signed in to change notification settings - Fork 13
/
socket.c
201 lines (171 loc) · 5.14 KB
/
socket.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include "dtypes.h"
#if defined(__APPLE__)
#include <sys/time.h>
#include <sys/select.h>
#include <sys/types.h>
#endif
#include "socket.h"
int mysocket(int domain, int type, int protocol)
{
int val;
int s = socket(domain, type, protocol);
if (s < 0)
return s;
val = 131072;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char*)&val, sizeof(int));
val = 131072;
setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char*)&val, sizeof(int));
return s;
}
#ifdef WIN32
void bzero(void *s, size_t n)
{
memset(s, 0, n);
}
#endif
/* returns a socket on which to accept() connections */
int open_tcp_port(short portno)
{
int sockfd;
//int val;
struct sockaddr_in serv_addr;
sockfd = mysocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
return -1;
//val = 1;
//setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
return -1;
}
listen(sockfd, 4);
return sockfd;
}
/* returns a socket on which to accept() connections, finding some
available port (portno is value-return) */
int open_any_tcp_port(short *portno)
{
int sockfd;
//int val;
struct sockaddr_in serv_addr;
sockfd = mysocket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
return -1;
//val = 1;
//setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(*portno);
while (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
(*portno)++;
serv_addr.sin_port = htons(*portno);
}
listen(sockfd, 4);
return sockfd;
}
/* returns a socket on which to accept() connections, finding some
available port (portno is value-return) */
int open_any_udp_port(short *portno)
{
int sockfd;
struct sockaddr_in serv_addr;
sockfd = mysocket(PF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
return -1;
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(*portno);
while (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
(*portno)++;
serv_addr.sin_port = htons(*portno);
}
return sockfd;
}
#ifndef WIN32
void closesocket(int fd)
{
close(fd);
}
#endif
/* returns a socket to use to send data to the given address */
int connect_to_host(char *hostname, short portno)
{
struct hostent *host_info;
int sockfd, yes=1;
struct sockaddr_in host_addr;
host_info = gethostbyname(hostname);
if (host_info == NULL) {
return -1;
}
sockfd = mysocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0) {
return -1;
}
(void)setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset((char*)&host_addr, 0, sizeof(host_addr));
host_addr.sin_family = host_info->h_addrtype;
memcpy((char*)&host_addr.sin_addr, host_info->h_addr,
host_info->h_length);
host_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr*)&host_addr,
sizeof(struct sockaddr_in)) != 0) {
closesocket(sockfd);
return -1;
}
return sockfd;
}
int connect_to_addr(struct sockaddr_in *host_addr)
{
int sockfd, yes=1;
sockfd = mysocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0) {
return -1;
}
(void)setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (connect(sockfd, (struct sockaddr*)host_addr,
sizeof(struct sockaddr_in)) != 0) {
closesocket(sockfd);
return -1;
}
return sockfd;
}
DLLEXPORT
void getlocalip(char *buf, size_t len)
{
struct ifaddrs * ifAddrStruct=NULL;
struct ifaddrs * ifa=NULL;
void * tmpAddrPtr=NULL;
buf[0] = '\0';
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
inet_ntop(AF_INET, tmpAddrPtr, buf, len);
if (strcmp(buf,"127.0.0.1"))
break;
//printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
/*
else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
}
*/
}
if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
}