-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.c
252 lines (219 loc) · 5.42 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* (c) Yves Lafon <yves@raubacapeu.net> */
/* */
/* $Id: socket.c,v 1.2 2007/07/03 14:02:58 ylafon Exp $ */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#ifdef NODELAY
#include <netinet/tcp.h>
#endif /* NODELAY */
#include <netdb.h>
#include <fcntl.h>
#include "defs.h"
#include "strip.h"
#include "socket.h"
#ifdef _AION_USE_SIGNAL_
#include "signal.h"
#endif /* _AION_USE_SIGNAL_ */
extern int h_errno;
int Connection(server, port)
char *server;
int port;
{
int sock;
struct hostent *hptr;
struct sockaddr_in adsock = {AF_INET};
#ifdef NODELAY
int val = 1;
#endif /* NODELAY */
alarm(CONNECT_MAX_TIME);
printf("Trying to connect to %s on port %d\n",server,port);
if ((hptr=gethostbyname(server))==NULL) {
perror("Unable to get host");
return 0;
}
#ifdef SUNOS
bcopy((char *)hptr->h_addr, (char *)&adsock.sin_addr, hptr->h_length);
#else
memcpy((void *)&adsock.sin_addr, (void *)hptr->h_addr, (unsigned int)hptr->h_length);
#endif /* SUNOS */
adsock.sin_family=hptr->h_addrtype;
adsock.sin_port=htons((unsigned int)port);
if ((sock=socket(AF_INET,SOCK_STREAM,0))<0) {
perror("Unable to create socket");
return 0;
}
setsockopt(sock, SOL_SOCKET, SO_LINGER, 0, 0);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 0, 0);
#ifdef NODELAY
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *)&val, sizeof(val));
#endif /* NODELAY */
fcntl(sock, F_SETFL, O_NONBLOCK);
if (connect(sock,(struct sockaddr *)&adsock,sizeof(adsock))<0) {
if (errno==EINTR) {
perror("Timed out");
} else {
if (errno == EINPROGRESS) {
alarm(0);
return sock;
}
perror("Unable to connect");
}
close(sock);
return 0;
}
printf("Connected !\n");
alarm(0);
if (!sock)
printf("opened descriptor NULL\n");
return(sock);
}
int SendSocket(sock, msg)
int sock;
char *msg;
{
#ifdef DEBUG
printf("-> %s",msg);
#endif
return((write(sock, msg, strlen(msg)) > 0));
}
int ReadSocket(sock, buffer, curr, result)
int sock;
char *buffer,*result;
char **curr;
{
register char *npos, *cpy, *cpyresult;
int nb_read, s_size;
register int i;
errno=0;
nb_read=read(sock, *curr, (unsigned int)(1022+buffer-*curr));
#ifdef _AION_USE_SIGNAL_
if (errno==EINTR)
signal(SIGALRM,alarm_handler);
#endif /* _AION_USE_SIGNAL_ */
if (!nb_read)
return 0;
else {
*(*curr+nb_read)=0;
npos=buffer;
cpy =buffer;
#ifdef STRICT_DELIMITER
while (*cpy)
if (*cyp++ == '\r')
if (*cpy++ == '\n')
npos = cpy;
#else
while (*cpy)
if (*cpy++ == '\n')
npos = cpy;
#endif /* STRICT_DELIMITER */
#ifdef STRICT_DELIMITER
s_size = (npos-buffer-2);
#else
s_size = (npos-buffer-1);
#endif
cpy = buffer;
cpyresult = result;
for (i=0;i<s_size;i++)
*cpyresult++ = *cpy++;
*cpyresult=0;
cpy = buffer;
while (*npos)
*cpy++ = *npos++;
*curr=cpy;;
return 1;
}
}
char *NameToIp(name)
char *name;
{
struct hostent *host_info;
char buffer[512];
char *result;
char *ip;
int i;
if (IsValidName(name)) {
host_info = gethostbyname(name);
if (!host_info) {
if (h_errno == HOST_NOT_FOUND) {
result = (char *)malloc(strlen(name) + 16);
sprintf(result,"Host %s not found", name);
return result;
}
if (h_errno == NO_DATA) {
result = (char *)malloc(strlen(name) + 64);
sprintf(result,"The requested name %s is valid but does not have an IP address", name);
return result;
}
result = (char *)malloc(strlen(name) + 32);
sprintf(result,"Unrecoverable Error for %s", name);
return result;
}
result = buffer;
sprintf(result, "%s:", name);
result += strlen(result);
i = 0;
while (host_info->h_addr_list[i]) {
ip = host_info->h_addr_list[i];
sprintf(result, " %u.%u.%u.%u", *ip & 0xFF , *(ip+1) & 0xFF, *(ip+2) & 0xFF, *(ip+3) & 0xFF);
result += strlen(result);
i++;
}
result = (char *)malloc(strlen(buffer)+1);
strcpy(result, buffer);
return result;
}
result = (char *)malloc(16);
strcpy(result,"Invalid Name");
return result;
}
char *IpToName(ip_addr)
char *ip_addr;
{
struct hostent *host_info;
struct hostent *ip_info;
char buffer[512];
char *result;
int i;
if (IsIp(ip_addr)) {
ip_info = gethostbyname(ip_addr);
if (!ip_info) {
result = (char *)malloc(strlen(ip_addr) + 32);
sprintf(result,"Unrecoverable Error for %s", ip_addr);
return result;
}
host_info = gethostbyaddr(ip_info->h_addr_list[0], ip_info->h_length, ip_info->h_addrtype);
if (!host_info) {
if (h_errno == HOST_NOT_FOUND) {
result = (char *)malloc(strlen(ip_addr) + 16);
sprintf(result,"Host %s not found", ip_addr);
return result;
}
result = (char *)malloc(strlen(ip_addr) + 32);
sprintf(result,"Unrecoverable Error for %s", ip_addr);
return result;
}
result = buffer;
sprintf(result, "%s: %s", ip_addr, host_info->h_name);
result += strlen(result);
i = 0;
while (host_info->h_aliases[i]) {
sprintf(result, " %s", host_info->h_aliases[i]);
result += strlen(result);
i++;
}
result = (char *)malloc(strlen(buffer)+1);
strcpy(result, buffer);
return result;
}
result = (char *)malloc(17);
strcpy(result,"Invalid Address");
return result;
}