File: | libs/miniupnpc/miniwget.c |
Location: | line 55, column 2 |
Description: | Value stored to 'fd_flags' is never read |
1 | /* $Id: miniwget.c,v 1.22 2009/02/28 10:36:35 nanard Exp $ */ |
2 | /* Project : miniupnp |
3 | * Author : Thomas Bernard |
4 | * Copyright (c) 2005 Thomas Bernard |
5 | * This software is subject to the conditions detailed in the |
6 | * LICENCE file provided in this distribution. |
7 | * */ |
8 | #include <stdio.h> |
9 | #include <stdlib.h> |
10 | #include <string.h> |
11 | #include "miniupnpc.h" |
12 | #ifdef WIN32 |
13 | #include <winsock2.h> |
14 | #include <io.h> |
15 | #define MAXHOSTNAMELEN64 64 |
16 | #define MIN(x,y)(((x)<(y))?(x):(y)) (((x)<(y))?(x):(y)) |
17 | #define snprintf _snprintf |
18 | #define herror |
19 | #define socklen_t int |
20 | #else |
21 | #include <unistd.h> |
22 | #include <sys/time.h> |
23 | #include <sys/types.h> |
24 | #include <sys/param.h> |
25 | #include <sys/socket.h> |
26 | #include <netdb.h> |
27 | #include <netinet/in.h> |
28 | #include <arpa/inet.h> |
29 | #include <fcntl.h> |
30 | #define closesocketclose close |
31 | #endif |
32 | #if defined(__sun) || defined(sun) |
33 | #define MIN(x,y)(((x)<(y))?(x):(y)) (((x)<(y))?(x):(y)) |
34 | #endif |
35 | |
36 | #include "miniupnpcstrings.h" |
37 | |
38 | /* miniwget2() : |
39 | * */ |
40 | static void * |
41 | miniwget2(const char * url, const char * host, |
42 | unsigned short port, const char * path, |
43 | int * size, char * addr_str, int addr_str_len) |
44 | { |
45 | char buf[2048]; |
46 | #ifdef WIN32 |
47 | SOCKET s; |
48 | #else |
49 | int s; |
50 | #endif |
51 | int fd_flags; |
52 | struct sockaddr_in dest; |
53 | struct hostent *hp; |
54 | |
55 | fd_flags = 0; |
Value stored to 'fd_flags' is never read | |
56 | |
57 | *size = 0; |
58 | hp = gethostbyname(host); |
59 | if(hp==NULL((void*)0)) |
60 | { |
61 | herror(host); |
62 | return NULL((void*)0); |
63 | } |
64 | /* memcpy((char *)&dest.sin_addr, hp->h_addr, hp->h_length); */ |
65 | memcpy(&dest.sin_addr, hp->h_addrh_addr_list[0], sizeof(dest.sin_addr)); |
66 | memset(dest.sin_zero, 0, sizeof(dest.sin_zero)); |
67 | s = socket(PF_INET2, SOCK_STREAMSOCK_STREAM, 0); |
68 | if(s < 0) |
69 | { |
70 | perror("socket"); |
71 | return NULL((void*)0); |
72 | } |
73 | dest.sin_family = AF_INET2; |
74 | dest.sin_port = htons(port)(__extension__ ({ unsigned short int __v, __x = (unsigned short int) (port); if (__builtin_constant_p (__x)) __v = ((unsigned short int) ((((__x) >> 8) & 0xff) | (((__x) & 0xff ) << 8))); else __asm__ ("rorw $8, %w0" : "=r" (__v) : "0" (__x) : "cc"); __v; })); |
75 | |
76 | |
77 | |
78 | { |
79 | #ifdef WIN32 |
80 | u_long arg = 1; |
81 | if (ioctlsocket((SOCKET) s, FIONBIO, &arg) == SOCKET_ERROR) { |
82 | return NULL((void*)0); |
83 | } |
84 | |
85 | #else |
86 | fd_flags = fcntl(s, F_GETFL3, 0); |
87 | |
88 | if (fcntl(s, F_SETFL4, fd_flags | O_NONBLOCK04000)) { |
89 | return NULL((void*)0); |
90 | } |
91 | |
92 | #endif |
93 | |
94 | } |
95 | |
96 | connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in)); |
97 | |
98 | { |
99 | fd_set wfds; |
100 | struct timeval tv = { 2, 0 }; |
101 | int r; |
102 | |
103 | FD_ZERO(&wfds)do { int __d0, __d1; __asm__ __volatile__ ("cld; rep; " "stosq" : "=c" (__d0), "=D" (__d1) : "a" (0), "0" (sizeof (fd_set) / sizeof (__fd_mask)), "1" (&((&wfds)->fds_bits)[0] ) : "memory"); } while (0); |
104 | FD_SET(s, &wfds)((void) (((&wfds)->fds_bits)[((s) / (8 * (int) sizeof ( __fd_mask)))] |= ((__fd_mask) 1 << ((s) % (8 * (int) sizeof (__fd_mask)))))); |
105 | |
106 | r = select(s + 1, NULL((void*)0), &wfds, NULL((void*)0), &tv); |
107 | |
108 | if (r <= 0) { |
109 | return NULL((void*)0); |
110 | } |
111 | |
112 | if (!FD_ISSET(s, &wfds)((((&wfds)->fds_bits)[((s) / (8 * (int) sizeof (__fd_mask )))] & ((__fd_mask) 1 << ((s) % (8 * (int) sizeof ( __fd_mask))))) != 0)) { |
113 | return NULL((void*)0); |
114 | } |
115 | |
116 | } |
117 | |
118 | #ifdef WIN32 |
119 | { |
120 | u_long arg = 0; |
121 | if (ioctlsocket((SOCKET) s, FIONBIO, &arg) == SOCKET_ERROR) { |
122 | return NULL((void*)0); |
123 | } |
124 | } |
125 | #else |
126 | fcntl(s, F_SETFL4, fd_flags); |
127 | #endif |
128 | |
129 | |
130 | /* get address for caller ! */ |
131 | if(addr_str) |
132 | { |
133 | struct sockaddr_in saddr; |
134 | socklen_t len; |
135 | |
136 | len = sizeof(saddr); |
137 | getsockname(s, (struct sockaddr *)&saddr, &len); |
138 | #ifndef WIN32 |
139 | inet_ntop(AF_INET2, &saddr.sin_addr, addr_str, addr_str_len); |
140 | #else |
141 | /* using INT WINAPI WSAAddressToStringA(LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFOA, LPSTR, LPDWORD); |
142 | * But his function make a string with the port : nn.nn.nn.nn:port */ |
143 | /* if(WSAAddressToStringA((SOCKADDR *)&saddr, sizeof(saddr), |
144 | NULL, addr_str, (DWORD *)&addr_str_len)) |
145 | { |
146 | printf("WSAAddressToStringA() failed : %d\n", WSAGetLastError()); |
147 | }*/ |
148 | strncpy(addr_str, inet_ntoa(saddr.sin_addr), addr_str_len)__builtin_strncpy (addr_str, inet_ntoa(saddr.sin_addr), addr_str_len ); |
149 | #endif |
150 | #ifdef DEBUG |
151 | printf("address miniwget : %s\n", addr_str); |
152 | #endif |
153 | } |
154 | |
155 | snprintf(buf, sizeof(buf), |
156 | "GET %s HTTP/1.1\r\n" |
157 | "Host: %s:%d\r\n" |
158 | "Connection: Close\r\n" |
159 | "User-Agent: " OS_STRING"Debian/4.0" ", UPnP/1.0, MiniUPnPc/" MINIUPNPC_VERSION_STRING"1.2" "\r\n" |
160 | |
161 | "\r\n", |
162 | path, host, port); |
163 | /*write(s, buf, strlen(buf));*/ |
164 | send(s, buf, strlen(buf), 0); |
165 | { |
166 | int n, headers=1; |
167 | char * respbuffer = NULL((void*)0); |
168 | int allreadyread = 0; |
169 | /*while((n = recv(s, buf, 2048, 0)) > 0)*/ |
170 | while((n = ReceiveData(s, buf, 2048, 5000)) > 0) |
171 | { |
172 | if(headers) |
173 | { |
174 | int i=0; |
175 | while(i<n-3) |
176 | { |
177 | if(buf[i]=='\r' && buf[i+1]=='\n' |
178 | && buf[i+2]=='\r' && buf[i+3]=='\n') |
179 | { |
180 | headers = 0; /* end */ |
181 | if(i<n-4) |
182 | { |
183 | respbuffer = (char *)realloc((void *)respbuffer, |
184 | allreadyread+(n-i-4)); |
185 | memcpy(respbuffer+allreadyread, buf + i + 4, n-i-4); |
186 | allreadyread += (n-i-4); |
187 | } |
188 | break; |
189 | } |
190 | i++; |
191 | } |
192 | } |
193 | else |
194 | { |
195 | respbuffer = (char *)realloc((void *)respbuffer, |
196 | allreadyread+n); |
197 | memcpy(respbuffer+allreadyread, buf, n); |
198 | allreadyread += n; |
199 | } |
200 | } |
201 | *size = allreadyread; |
202 | #ifdef DEBUG |
203 | printf("%d bytes read\n", *size); |
204 | #endif |
205 | closesocketclose(s); |
206 | return respbuffer; |
207 | } |
208 | } |
209 | |
210 | /* parseURL() |
211 | * arguments : |
212 | * url : source string not modified |
213 | * hostname : hostname destination string (size of MAXHOSTNAMELEN+1) |
214 | * port : port (destination) |
215 | * path : pointer to the path part of the URL |
216 | * |
217 | * Return values : |
218 | * 0 - Failure |
219 | * 1 - Success */ |
220 | int parseURL(const char * url, char * hostname, unsigned short * port, char * * path) |
221 | { |
222 | char * p1, *p2, *p3; |
223 | p1 = strstr(url, "://"); |
224 | if(!p1) |
225 | return 0; |
226 | p1 += 3; |
227 | if( (url[0]!='h') || (url[1]!='t') |
228 | ||(url[2]!='t') || (url[3]!='p')) |
229 | return 0; |
230 | p2 = strchr(p1, ':')(__extension__ (__builtin_constant_p (':') && !__builtin_constant_p (p1) && (':') == '\0' ? (char *) __rawmemchr (p1, ':' ) : __builtin_strchr (p1, ':'))); |
231 | p3 = strchr(p1, '/')(__extension__ (__builtin_constant_p ('/') && !__builtin_constant_p (p1) && ('/') == '\0' ? (char *) __rawmemchr (p1, '/' ) : __builtin_strchr (p1, '/'))); |
232 | if(!p3) |
233 | return 0; |
234 | memset(hostname, 0, MAXHOSTNAMELEN64 + 1); |
235 | if(!p2 || (p2>p3)) |
236 | { |
237 | strncpy(hostname, p1, MIN(MAXHOSTNAMELEN, (int)(p3-p1)))__builtin_strncpy (hostname, p1, (((64)<((int)(p3-p1)))?(64 ):((int)(p3-p1)))); |
238 | *port = 80; |
239 | } |
240 | else |
241 | { |
242 | strncpy(hostname, p1, MIN(MAXHOSTNAMELEN, (int)(p2-p1)))__builtin_strncpy (hostname, p1, (((64)<((int)(p2-p1)))?(64 ):((int)(p2-p1)))); |
243 | *port = 0; |
244 | p2++; |
245 | while( (*p2 >= '0') && (*p2 <= '9')) |
246 | { |
247 | *port *= 10; |
248 | *port += (unsigned short)(*p2 - '0'); |
249 | p2++; |
250 | } |
251 | } |
252 | *path = p3; |
253 | return 1; |
254 | } |
255 | |
256 | void * miniwget(const char * url, int * size) |
257 | { |
258 | unsigned short port; |
259 | char * path; |
260 | /* protocol://host:port/chemin */ |
261 | char hostname[MAXHOSTNAMELEN64+1]; |
262 | *size = 0; |
263 | if(!parseURL(url, hostname, &port, &path)) { |
264 | return NULL((void*)0); |
265 | } |
266 | return miniwget2(url, hostname, port, path, size, 0, 0); |
267 | } |
268 | |
269 | void * miniwget_getaddr(const char * url, int * size, char * addr, int addrlen) |
270 | { |
271 | unsigned short port; |
272 | char * path; |
273 | /* protocol://host:port/chemin */ |
274 | char hostname[MAXHOSTNAMELEN64+1]; |
275 | *size = 0; |
276 | if(addr) |
277 | addr[0] = '\0'; |
278 | if(!parseURL(url, hostname, &port, &path)) |
279 | return NULL((void*)0); |
280 | return miniwget2(url, hostname, port, path, size, addr, addrlen); |
281 | } |
282 |