-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsock.c
executable file
·255 lines (220 loc) · 4.66 KB
/
sock.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
253
254
255
/*
*
* Copyright (C) portshare Corporation, 1999-1999. All rights reserved.
*
*
* sock.c
* Unix socket routines
*
* History
* 08/16/1999 V.1.0.0 Initial revision
*
*/
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/ioctl.h>
# include <fcntl.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <netdb.h>
# include <errno.h>
# include <string.h>
# include <unistd.h>
# include <stdio.h>
# define _TSR_SOCK_
#include "inc/portshare-ser-cli.h"
#include "inc/system.h"
#include "inc/tsrio.h"
#include "inc/sock.h"
#ifdef TSR_MEASURE
#include "inc/tsrmeasure.h"
#endif
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
/*
* Internal Variables
*/
struct sockaddr_in S_inaddr;
int
sock_getaddr(char *host, int tcpport)
{
struct sockaddr_in *sp = &S_inaddr;
struct hostent *hp;
if ((hp = gethostbyname(host)) != (struct hostent *) NULL) {
memcpy((char *) &sp->sin_addr, hp->h_addr, hp->h_length);
sp->sin_family = hp->h_addrtype;
}
else if ((sp->sin_addr.s_addr = inet_addr(host)) != INADDR_NONE) {
sp->sin_family = AF_INET;
}
else {
sysmessage(MSG_ERR, "%s: No such host\n", host);
return (E_PARMINVAL);
}
//tcpport = base + physport;
sp->sin_port = htons(tcpport);
sysmessage(MSG_NOTICE, "Using %s:%d socket\n",
inet_ntoa(sp->sin_addr), tcpport);
return (E_NORMAL);
}
int
sock_link(int iosize)
{
int fd;
struct sockaddr_in *sp = &S_inaddr;
int flag;
char dummy[4];
int bufsize;
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
sysmessage(MSG_ERR,
"Can't create a socket for communication : %s\n",
strerror(errno));
return (E_FILEIO);
}
bufsize = 2 * iosize;
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(int))
== -1) {
sysmessage(MSG_ERR, "Can't set socket buffer size : %s\n",
strerror(errno));
return (E_FILEIO);
}
if (connect(fd, (struct sockaddr *) sp, sizeof(struct sockaddr_in))
== -1) {
sysmessage(MSG_WARNING,
"Can't initiate connection on a socket : %s\n",
strerror(errno));
close(fd);
return (E_CONNECT);
}
# ifdef USE_FIONBIO
flag = 1;
if (ioctl(fd, FIONBIO, &flag) == -1) {
sysmessage(MSG_ERR,
"Can't set non-block on a socket : %s\n", strerror(errno));
close(fd);
return (E_FILEIO);
}
# else
if ((flag = fcntl(fd, F_GETFL, 0)) == -1) {
sysmessage(MSG_ERR,
"Can't get file flags of a socket : %s\n",
strerror(errno));
close(fd);
return (E_FILEIO);
}
# ifdef USE_STD_NDELAY
flag |= O_NDELAY;
# elif defined USE_POSIX_NONBLOCK
flag |= O_NONBLOCK;
# else
close(fd);
return (E_PARMINVAL); /* Socket must be in non_blockin mode */
# endif
if (fcntl(fd, F_SETFL, flag) == -1) {
sysmessage(MSG_ERR,
"Can't set file flags of a socket : %s\n",
strerror(errno));
close(fd);
return (E_FILEIO);
}
# endif
# if defined(TCP_NODELAY)
# ifndef SOL_TCP
# define SOL_TCP IPPROTO_TCP
# endif
flag = 1;
setsockopt(fd, SOL_TCP, TCP_NODELAY, &flag, sizeof(flag));
# endif
sysdelay(SERVER_DELAY); /* Wait server startup */
if (recv(fd, dummy, 0, 0) == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
sysmessage(MSG_WARNING,
"Can't initiate connection on a socket : (recv) %s\n",
strerror(errno));
close(fd);
return (E_CONNECT);
}
}
S_fd = fd;
return (E_NORMAL);
}
int
sock_unlink(void)
{
(void) close(S_fd);
return (E_NORMAL);
}
/*
* Network routines using BSD socket interface
*/
int
sock_write(unsigned char *buf, int n)
{
int ret;
if ((ret = send(S_fd, buf, n, 0)) < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
ret = 0;
}
else {
sysmessage(MSG_ERR, "send : %s\n", strerror(errno));
}
}
# ifdef TSR_MEASURE
if (ret > 0) {
netwrites++;
netwbytes += ret;
}
else {
netnwrites++;
}
# endif
return (ret);
}
int
sock_read(unsigned char *buf, int n)
{
register int ret;
int tot;
int rcnt;
tot = rcnt = 0;
do {
if ((ret = recv(S_fd, buf, n, 0)) > 0) {
tot += ret;
buf += ret;
n -= ret;
# ifdef TSR_MEASURE
netreads++;
netrbytes += ret;
# endif
# ifdef BRAKES_ON
/* Delays to avoid system overload on small reads */
mindelay();
if (rcnt++) {
if (n > 0) {
rcnt = 0;
delay(100);
}
}
# endif
}
else if (ret < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
ret = 0;
}
else {
sysmessage(MSG_ERR, "recv : %s\n", strerror(errno));
}
}
# ifdef TSR_MEASURE
if (ret == 0) {
netnreads++;
}
# endif
} while (n > 0 && ret > 0);
if (ret >= 0) {
ret = tot;
}
return (ret);
}