ReactOS  0.4.13-dev-551-gf37fb1f
send.c File Reference
#include <winsock.h>
#include <stdio.h>
#include <errno.h>
#include "winetest.h"
Include dependency graph for send.c:

Go to the source code of this file.

Macros

#define BUFLEN   8192
 
#define SEP   "--8<--cut-here--8<--"
 

Functions

static SOCKET open_http (const char *server)
 
static int close_http (SOCKET s)
 
static int send_buf (SOCKET s, const char *buf, size_t length)
 
static int send_str (SOCKET s,...)
 
int send_file (const char *name)
 

Macro Definition Documentation

◆ BUFLEN

#define BUFLEN   8192

◆ SEP

#define SEP   "--8<--cut-here--8<--"

Function Documentation

◆ close_http()

static int close_http ( SOCKET  s)
static

Definition at line 65 of file send.c.

66 {
67  int ret;
68 
69  ret = closesocket (s);
70  return (WSACleanup () || ret);
71 }
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
#define closesocket
Definition: main.c:39
int ret
GLdouble s
Definition: gl.h:2039

Referenced by send_file().

◆ open_http()

static SOCKET open_http ( const char server)
static

Definition at line 28 of file send.c.

29 {
30  WSADATA wsad;
31  struct sockaddr_in sa;
32  SOCKET s;
33 
34  report (R_STATUS, "Opening HTTP connection to %s", server);
35  if (WSAStartup (MAKEWORD (2,2), &wsad)) return INVALID_SOCKET;
36 
37  sa.sin_family = AF_INET;
38  sa.sin_port = htons (80);
39  sa.sin_addr.s_addr = inet_addr (server);
40  if (sa.sin_addr.s_addr == INADDR_NONE) {
41  struct hostent *host = gethostbyname (server);
42  if (!host) {
43  report (R_ERROR, "Hostname lookup failed for %s", server);
44  goto failure;
45  }
46  sa.sin_addr.s_addr = ((struct in_addr *)host->h_addr)->s_addr;
47  }
49  if (s == INVALID_SOCKET) {
50  report (R_ERROR, "Can't open network socket: %d",
51  WSAGetLastError ());
52  goto failure;
53  }
54  if (!connect (s, (struct sockaddr*)&sa, sizeof (struct sockaddr_in)))
55  return s;
56 
57  report (R_ERROR, "Can't connect: %d", WSAGetLastError ());
58  closesocket (s);
59  failure:
60  WSACleanup ();
61  return INVALID_SOCKET;
62 }
static rfbScreenInfoPtr server
Definition: vnc.c:74
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
#define inet_addr(cp)
Definition: inet.h:98
#define MAKEWORD(a, b)
Definition: typedefs.h:247
#define INADDR_NONE
Definition: tcp.c:42
char * host
Definition: whois.c:55
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
INT WSAAPI connect(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: sockctrl.c:23
#define closesocket
Definition: main.c:39
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
static void report(const DATA_BLOB *pDataIn, const DATA_BLOB *pOptionalEntropy, CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, DWORD dwFlags)
Definition: protectdata.c:769
PHOSTENT WSAAPI gethostbyname(IN const char FAR *name)
Definition: getxbyxx.c:221
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
Definition: tcpip.h:125
GLdouble s
Definition: gl.h:2039
#define AF_INET
Definition: tcpip.h:117
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_STREAM
Definition: tcpip.h:118
#define htons(x)
Definition: module.h:213
static struct sockaddr_in sa
Definition: adnsresfilter.c:69

Referenced by send_file().

◆ send_buf()

static int send_buf ( SOCKET  s,
const char buf,
size_t  length 
)
static

Definition at line 74 of file send.c.

75 {
76  int sent;
77 
78  while (length > 0) {
79  sent = send (s, buf, length, 0);
80  if (sent == SOCKET_ERROR) return 1;
81  buf += sent;
82  length -= sent;
83  }
84  return 0;
85 }
#define SOCKET_ERROR
Definition: winsock.h:333
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
Definition: msg.h:34
GLdouble s
Definition: gl.h:2039

Referenced by send_file(), send_str(), and test_iocp_fileio().

◆ send_file()

int send_file ( const char name)

Definition at line 105 of file send.c.

106 {
107  SOCKET s;
108  FILE *f;
109 #define BUFLEN 8192
110  char buffer[BUFLEN+1];
111  size_t bytes_read, total, filesize;
112  char *str;
113  int ret;
114 
115  /* RFC 2616 */
116 #define SEP "--8<--cut-here--8<--"
117  static const char head[] = "POST /submit HTTP/1.0\r\n"
118  "Host: test.winehq.org\r\n"
119  "User-Agent: Winetest Shell\r\n"
120  "Content-Type: multipart/form-data; boundary=\"" SEP "\"\r\n"
121  "Content-Length: %u\r\n\r\n";
122  static const char body1[] = "--" SEP "\r\n"
123  "Content-Disposition: form-data; name=\"reportfile\"; filename=\"%s\"\r\n"
124  "Content-Type: application/octet-stream\r\n\r\n";
125  static const char body2[] = "\r\n--" SEP "\r\n"
126  "Content-Disposition: form-data; name=\"submit\"\r\n\r\n"
127  "Upload File\r\n"
128  "--" SEP "--\r\n";
129 
130  s = open_http ("test.winehq.org");
131  if (s == INVALID_SOCKET) return 1;
132 
133  f = fopen (name, "rb");
134  if (!f) {
135  report (R_WARNING, "Can't open file '%s': %d", name, errno);
136  goto abort1;
137  }
138  fseek (f, 0, SEEK_END);
139  filesize = ftell (f);
140  if (filesize > 1.5*1024*1024) {
141  report (R_WARNING,
142  "File too big (%.1f MB > 1.5 MB); submitting partial report.",
143  filesize/1024.0/1024);
144  filesize = 1.5*1024*1024;
145  }
146  fseek (f, 0, SEEK_SET);
147 
148  report (R_STATUS, "Sending header");
149  str = strmake (&total, body1, name);
150  ret = send_str (s, head, filesize + total + sizeof body2 - 1) ||
151  send_buf (s, str, total);
152  free (str);
153  if (ret) {
154  report (R_WARNING, "Error sending header: %d, %d",
155  errno, WSAGetLastError ());
156  goto abort2;
157  }
158 
159  report (R_STATUS, "Sending %u bytes of data", filesize);
160  report (R_PROGRESS, 2, filesize);
161  total = 0;
162  while (total < filesize && (bytes_read = fread (buffer, 1, BUFLEN/2, f))) {
163  if ((signed)bytes_read == -1) {
164  report (R_WARNING, "Error reading log file: %d", errno);
165  goto abort2;
166  }
167  total += bytes_read;
168  if (total > filesize) bytes_read -= total - filesize;
169  if (send_buf (s, buffer, bytes_read)) {
170  report (R_WARNING, "Error sending body: %d, %d",
171  errno, WSAGetLastError ());
172  goto abort2;
173  }
174  report (R_DELTA, bytes_read, "Network transfer: In progress");
175  }
176  fclose (f);
177 
178  if (send_buf (s, body2, sizeof body2 - 1)) {
179  report (R_WARNING, "Error sending trailer: %d, %d",
180  errno, WSAGetLastError ());
181  goto abort1;
182  }
183  report (R_DELTA, 0, "Network transfer: Done");
184 
185  total = 0;
186  while ((bytes_read = recv (s, buffer+total, BUFLEN-total, 0))) {
187  if ((signed)bytes_read == SOCKET_ERROR) {
188  report (R_WARNING, "Error receiving reply: %d, %d",
189  errno, WSAGetLastError ());
190  goto abort1;
191  }
192  total += bytes_read;
193  if (total == BUFLEN) {
194  report (R_WARNING, "Buffer overflow");
195  goto abort1;
196  }
197  }
198  if (close_http (s)) {
199  report (R_WARNING, "Error closing connection: %d, %d",
200  errno, WSAGetLastError ());
201  return 1;
202  }
203 
204  str = strmake (&bytes_read, "Received %s (%d bytes).\n",
205  name, filesize);
206  ret = memcmp (str, buffer + total - bytes_read, bytes_read);
207  free (str);
208  if (ret) {
209  buffer[total] = 0;
210  str = strstr (buffer, "\r\n\r\n");
211  if (!str) str = buffer;
212  else str = str + 4;
213  report (R_ERROR, "Can't submit logfile '%s'. "
214  "Server response: %s", name, str);
215  }
216  return ret;
217 
218  abort2:
219  fclose (f);
220  abort1:
221  close_http (s);
222  return 1;
223 }
#define SOCKET_ERROR
Definition: winsock.h:333
static int send_buf(SOCKET s, const char *buf, size_t length)
Definition: send.c:74
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
struct outqueuenode * head
Definition: adnsresfilter.c:66
static int send_str(SOCKET s,...)
Definition: send.c:88
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
#define free
Definition: debug_ros.c:5
GLuint buffer
Definition: glext.h:5915
int errno
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
const WCHAR * str
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
static void report(const DATA_BLOB *pDataIn, const DATA_BLOB *pOptionalEntropy, CRYPTPROTECT_PROMPTSTRUCT *pPromptStruct, DWORD dwFlags)
Definition: protectdata.c:769
char * strmake(size_t *lenp,...)
Definition: util.c:82
#define SEEK_SET
Definition: jmemansi.c:26
GLfloat f
Definition: glext.h:7540
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
#define BUFLEN
static int close_http(SOCKET s)
Definition: send.c:65
int ret
GLdouble s
Definition: gl.h:2039
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
static SOCKET open_http(const char *server)
Definition: send.c:28
#define f
Definition: ke_i.h:83
Definition: name.c:36
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
#define SEEK_END
Definition: cabinet.c:27
UINT_PTR SOCKET
Definition: winsock.h:47
#define SEP
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23

Referenced by WinMain().

◆ send_str()

static int send_str ( SOCKET  s,
  ... 
)
static

Definition at line 88 of file send.c.

89 {
90  va_list ap;
91  char *p;
92  int ret;
93  size_t len;
94 
95  va_start (ap, s);
96  p = vstrmake (&len, ap);
97  va_end (ap);
98  if (!p) return 1;
99  ret = send_buf (s, p, len);
100  free (p);
101  return ret;
102 }
static int send_buf(SOCKET s, const char *buf, size_t length)
Definition: send.c:74
#define free
Definition: debug_ros.c:5
#define va_end(ap)
Definition: acmsvcex.h:90
char * va_list
Definition: acmsvcex.h:78
int ret
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
#define va_start(ap, A)
Definition: acmsvcex.h:91
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
GLfloat GLfloat p
Definition: glext.h:8902
char * vstrmake(size_t *lenp, va_list ap)
Definition: util.c:74

Referenced by send_file().