-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockutil.c
190 lines (164 loc) · 3.59 KB
/
sockutil.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
/* Socket utility routines.
* Magick IRC Services are copyright (c) 1996-1998 Preston A. Elder.
* E-mail: <prez@magick.tm> IRC: PreZ@RelicNet
* Originally based on EsperNet services (c) 1996-1998 Andy Church
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
#include <setjmp.h>
FILE **files; /* Array of FILE *'s; files[s] = fdopen(s, "r+") */
int filescnt = 0; /* Size of files array */
int read_timeout;
static jmp_buf alarm_jmp;
static void
alarm_handler (int sig_unused)
{
longjmp (alarm_jmp, 1);
}
/*************************************************************************/
static int lastchar = EOF;
int
sgetc (int s)
{
unsigned char c;
if (lastchar != EOF)
{
c = lastchar;
lastchar = EOF;
return c;
}
if (read (s, &c, 1) <= 0)
return EOF;
return c;
}
int
sungetc (int c, int s)
{
return lastchar = c;
}
/*************************************************************************/
/* If connection was broken, return NULL. If the read timed out, return
* (char *)-1. */
char *
sgets (char *buf, unsigned int len, int s)
{
int c;
char *ptr = buf;
if (len == 0)
return NULL;
if (setjmp (alarm_jmp))
return (char *) -1;
signal (SIGALRM, alarm_handler);
alarm (read_timeout);
c = sgetc (s);
alarm (0);
signal (SIGALRM, SIG_IGN);
while (--len && (*ptr++ = c) != '\n' && (c = sgetc (s)) >= 0)
;
if (c < 0)
return NULL;
*ptr = 0;
return buf;
}
/*************************************************************************/
int
sputs (char *str, int s)
{
return write (s, str, strlen (str));
}
int
sockprintf (int s, char *fmt,...)
{
va_list args;
int i;
va_start (args, fmt);
if (s >= filescnt)
{
int oldcnt = filescnt;
filescnt *= 2;
if (filescnt <= s)
filescnt = s + 1;
files = realloc (files, sizeof (FILE *) * filescnt);
if (!files)
{
filescnt = 0;
errno = ENOMEM;
return 0;
}
memset (files + oldcnt, 0, sizeof (FILE *) * (filescnt - oldcnt));
}
if (!files[s])
{
if (!(files[s] = fdopen (s, "r+")))
return 0;
setbuf (files[s], NULL);
}
i = vfprintf (files[s], fmt, args);
fflush (files[s]);
return i;
}
/*************************************************************************/
int
conn (char *host, int port)
{
struct hostent *hp;
struct sockaddr_in sa;
int sock;
if (!(hp = gethostbyname (host)))
return -1;
bzero (&sa, sizeof (sa));
bcopy (hp->h_addr, (char *) &sa.sin_addr, hp->h_length);
sa.sin_family = hp->h_addrtype;
sa.sin_port = htons ((unsigned short) port);
if ((sock = socket (hp->h_addrtype, SOCK_STREAM, 0)) < 0)
return -1;
if (filescnt <= sock)
{
int oldcnt = filescnt;
filescnt *= 2;
if (filescnt <= sock)
filescnt = sock + 1;
files = realloc (files, sizeof (FILE *) * filescnt);
if (!files)
{
filescnt = 0;
shutdown (sock, 2);
#ifdef WIN32
closesocket (sock);
#else
close (sock);
#endif
errno = ENOMEM;
return -1;
}
memset (files + oldcnt, 0, sizeof (FILE *) * (filescnt - oldcnt));
}
if (!(files[sock] = fdopen (sock, "r+")))
{
int errno_save = errno;
shutdown (sock, 2);
close (sock);
errno = errno_save;
return -1;
}
setbuf (files[sock], NULL);
if (connect (sock, (struct sockaddr *) &sa, sizeof (sa)) < 0)
{
int errno_save = errno;
shutdown (sock, 2);
fclose (files[sock]);
errno = errno_save;
return -1;
}
return sock;
}
void
disconn (int s)
{
shutdown (s, 2);
if (s < filescnt) {
fclose (files[s]);
} else
close (s);
}