]>
Commit | Line | Data |
---|---|---|
4a83b7b0 DD |
1 | /* |
2 | * Copyright (c) 2000, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* Simplified socket access functions */ | |
17 | ||
946fc045 DD |
18 | static char *cvsid = "\n%%% $Id$\n"; |
19 | ||
4a83b7b0 DD |
20 | #include "win32.h" |
21 | #include <winsock.h> | |
22 | #include <stdio.h> | |
23 | #include <stdarg.h> | |
24 | #include <stdlib.h> | |
25 | ||
26 | #include "simpsock.h" | |
27 | #include "msg.h" | |
28 | ||
29 | #define SSBUFSZ 1024 | |
30 | ||
31 | SimpleSocket::SimpleSocket (char *hostname, int port) | |
32 | { | |
33 | static int initted = 0; | |
34 | if (!initted) | |
35 | { | |
36 | initted = 1; | |
37 | WSADATA d; | |
38 | WSAStartup (MAKEWORD (1,1), &d); | |
39 | } | |
40 | ||
41 | s = INVALID_SOCKET; | |
42 | buf = (char *) malloc (SSBUFSZ + 3); | |
43 | putp = getp = 0; | |
44 | ||
45 | int i1, i2, i3, i4; | |
46 | unsigned char ip[4]; | |
47 | ||
48 | if (sscanf (hostname, "%d.%d.%d.%d", &i1, &i2, &i3, &i4) == 4) | |
49 | { | |
50 | ip[0] = i1; | |
51 | ip[1] = i2; | |
52 | ip[2] = i3; | |
53 | ip[3] = i4; | |
54 | } | |
55 | else | |
56 | { | |
57 | struct hostent *he; | |
58 | he = gethostbyname (hostname); | |
59 | if (!he) | |
60 | { | |
61 | msg ("Can't resolve `%s'\n", hostname); | |
62 | return; | |
63 | } | |
64 | memcpy (ip, he->h_addr_list[0], 4); | |
65 | } | |
66 | ||
67 | s = socket (AF_INET, SOCK_STREAM, 0); | |
68 | if (s == INVALID_SOCKET) | |
69 | { | |
70 | msg ("Can't create socket, %d", WSAGetLastError ()); | |
71 | return; | |
72 | } | |
73 | ||
74 | struct sockaddr_in name; | |
75 | ||
76 | memset (&name, 0, sizeof (name)); | |
77 | name.sin_family = AF_INET; | |
78 | name.sin_port = htons (port); | |
79 | memcpy (&name.sin_addr, ip, 4); | |
80 | ||
81 | if (connect (s, (sockaddr *)&name, sizeof(name))) | |
82 | { | |
83 | msg ("Can't connect to %s:%d", hostname, port); | |
84 | closesocket (s); | |
85 | s = INVALID_SOCKET; | |
86 | return; | |
87 | } | |
88 | ||
89 | return; | |
90 | } | |
91 | ||
92 | SimpleSocket::~SimpleSocket () | |
93 | { | |
94 | if (s != INVALID_SOCKET) | |
95 | closesocket (s); | |
96 | s = INVALID_SOCKET; | |
97 | if (buf) | |
98 | free (buf); | |
99 | buf = 0; | |
100 | } | |
101 | ||
102 | int | |
103 | SimpleSocket::ok () | |
104 | { | |
105 | if (s == INVALID_SOCKET) | |
106 | return 0; | |
107 | return 1; | |
108 | } | |
109 | ||
110 | int | |
111 | SimpleSocket::printf (char *fmt, ...) | |
112 | { | |
113 | char buf[SSBUFSZ]; | |
114 | va_list args; | |
115 | va_start (args, fmt); | |
116 | vsprintf (buf, fmt, args); | |
117 | return send (s, buf, strlen (buf), 0); | |
118 | } | |
119 | ||
120 | int | |
121 | SimpleSocket::write (char *buf, int len) | |
122 | { | |
123 | return send (s, buf, len, 0); | |
124 | } | |
125 | ||
126 | int | |
127 | SimpleSocket::fill () | |
128 | { | |
129 | if (putp == getp) | |
130 | putp = getp = 0; | |
131 | ||
132 | int n = SSBUFSZ - putp; | |
133 | if (n == 0) | |
134 | return 0; | |
135 | int r = recv (s, buf + putp, n, 0); | |
136 | if (r > 0) | |
137 | { | |
138 | putp += r; | |
139 | return r; | |
140 | } | |
141 | return 0; | |
142 | } | |
143 | ||
144 | char * | |
145 | SimpleSocket::gets () | |
146 | { | |
147 | if (getp > 0 && putp > getp) | |
148 | { | |
149 | memmove (buf, buf+getp, putp-getp); | |
150 | putp -= getp; | |
151 | getp = 0; | |
152 | } | |
153 | if (putp == getp) | |
154 | fill(); | |
155 | ||
156 | // getp is zero, always, here, and putp is the count | |
157 | char *nl; | |
158 | while ((nl = (char *)memchr (buf, '\n', putp)) == NULL && putp < SSBUFSZ) | |
159 | if (fill () <= 0) | |
160 | break; | |
161 | ||
162 | if (nl) | |
163 | { | |
164 | getp = nl - buf + 1; | |
165 | while ((*nl == '\n' || *nl == '\r') && nl >= buf) | |
166 | *nl-- = 0; | |
167 | } | |
168 | else | |
169 | { | |
170 | getp = putp; | |
171 | nl = buf + putp; | |
172 | nl[1] = 0; | |
173 | } | |
174 | ||
175 | return buf; | |
176 | } | |
177 | ||
178 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
179 | ||
180 | int | |
181 | SimpleSocket::read (char *ubuf, int ulen) | |
182 | { | |
183 | int n, rv=0; | |
184 | if (putp > getp) | |
185 | { | |
186 | n = MIN (ulen, putp-getp); | |
187 | memmove (ubuf, buf+getp, n); | |
188 | getp += n; | |
189 | ubuf += n; | |
190 | ulen -= n; | |
191 | rv += n; | |
192 | } | |
193 | while (ulen > 0) | |
194 | { | |
195 | n = recv (s, ubuf, ulen, 0); | |
196 | if (n <= 0) | |
197 | return rv; | |
198 | ubuf += n; | |
199 | ulen -= n; | |
200 | rv += n; | |
201 | } | |
202 | return rv; | |
203 | } |