Fossil

Changes On Branch win-server-ipv6
Login

Changes On Branch win-server-ipv6

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch win-server-ipv6 Excluding Merge-Ins

This is equivalent to a diff from c105c2fd to 1627571b

2018-01-01
18:56
The "fossil server" command on windows now listens for both IPv4 and IPv6 connections. ... (check-in: 21d5038f user: drh tags: trunk)
18:48
Get "fossil server" working for IPv6 on MinGW, using hints from Olivier Mascia. ... (Closed-Leaf check-in: 1627571b user: drh tags: win-server-ipv6)
00:57
Check for explicit error condition from ticket_put() because otherwise Fossil thinks a successful ticket add is an error and outputs an empty line. ... (check-in: d4c6f3c4 user: andybradford tags: trunk)
2017-12-31
20:02
Fix a C89 variable declaration that prevents compiling on older versions of MSVC. ... (check-in: 80b3b127 user: drh tags: win-server-ipv6)
19:24
Changes from Olivier Mascia to implement IPV6 support for "fossil server" on Windows. Currently does not compile on MinGW due to a missing header file. ... (check-in: 76c7a9a7 user: drh tags: win-server-ipv6)
2017-12-28
21:52
Updated the Xekri skin to work with the recent Timeline changes. ... (check-in: c105c2fd user: zakero tags: trunk)
21:06
Initiate the TCP FIN on Windows before closing a socket. ... (check-in: ef44abc6 user: drh tags: trunk)
03:37
Fixed some misnamed classes. ... (Closed-Leaf check-in: 313e27ad user: zakero tags: skin-xekri-timeline)

Changes to src/winhttp.c.

18
19
20
21
22
23
24

25
26
27




28
29
30
31
32
33
34
** This file implements a very simple (and low-performance) HTTP server
** for windows. It also implements a Windows Service which allows the HTTP
** server to be run without any user logged on.
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */

#include <windows.h>
#include <process.h>
#include "winhttp.h"





/*
** The HttpServer structure holds information about an instance of
** the HTTP server itself.
*/
typedef struct HttpServer HttpServer;
struct HttpServer {







>



>
>
>
>







18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
** This file implements a very simple (and low-performance) HTTP server
** for windows. It also implements a Windows Service which allows the HTTP
** server to be run without any user logged on.
*/
#include "config.h"
#ifdef _WIN32
/* This code is for win32 only */
#include <ws2tcpip.h>
#include <windows.h>
#include <process.h>
#include "winhttp.h"

#ifndef IPV6_V6ONLY
# define IPV6_V6ONLY 27  /* Because this definition is missing in MinGW */
#endif

/*
** The HttpServer structure holds information about an instance of
** the HTTP server itself.
*/
typedef struct HttpServer HttpServer;
struct HttpServer {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
** The HttpRequest structure holds information about each incoming
** HTTP request.
*/
typedef struct HttpRequest HttpRequest;
struct HttpRequest {
  int id;                /* ID counter */
  SOCKET s;              /* Socket on which to receive data */
  SOCKADDR_IN addr;      /* Address from which data is coming */
  int flags;             /* Flags passed to win32_http_server() */
  const char *zOptions;  /* --baseurl, --notfound, --localauth, --th-trace */
};

/*
** Prefix for a temporary file.
*/







|







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
** The HttpRequest structure holds information about each incoming
** HTTP request.
*/
typedef struct HttpRequest HttpRequest;
struct HttpRequest {
  int id;                /* ID counter */
  SOCKET s;              /* Socket on which to receive data */
  SOCKADDR_IN6 addr;     /* Address from which data is coming */
  int flags;             /* Flags passed to win32_http_server() */
  const char *zOptions;  /* --baseurl, --notfound, --localauth, --th-trace */
};

/*
** Prefix for a temporary file.
*/
133
134
135
136
137
138
139


140
141
142
143
144
145
146
*/
static void win32_http_request(void *pAppData){
  HttpRequest *p = (HttpRequest*)pAppData;
  FILE *in = 0, *out = 0, *aux = 0;
  int amt, got, i;
  int wanted = 0;
  char *z;


  char zCmdFName[MAX_PATH];
  char zRequestFName[MAX_PATH];
  char zReplyFName[MAX_PATH];
  char zCmd[2000];          /* Command-line to process the request */
  char zHdr[4000];          /* The HTTP request header */

  sqlite3_snprintf(MAX_PATH, zCmdFName,







>
>







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
*/
static void win32_http_request(void *pAppData){
  HttpRequest *p = (HttpRequest*)pAppData;
  FILE *in = 0, *out = 0, *aux = 0;
  int amt, got, i;
  int wanted = 0;
  char *z;
  char zIp[50];
  DWORD nIp = sizeof(zIp);
  char zCmdFName[MAX_PATH];
  char zRequestFName[MAX_PATH];
  char zReplyFName[MAX_PATH];
  char zCmd[2000];          /* Command-line to process the request */
  char zHdr[4000];          /* The HTTP request header */

  sqlite3_snprintf(MAX_PATH, zCmdFName,
181
182
183
184
185
186
187




188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  }

  /*
  ** The repository name is only needed if there was no open checkout.  This
  ** is designed to allow the open checkout for the interactive user to work
  ** with the local Fossil server started via the "ui" command.
  */




  if( (p->flags & HTTP_SERVER_HAD_CHECKOUT)==0 ){
    assert( g.zRepositoryName && g.zRepositoryName[0] );
    sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s\n%s",
      get_utf8_bom(0), zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr),
      g.zRepositoryName
    );
  }else{
    sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s",
      get_utf8_bom(0), zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr)
    );
  }
  aux = fossil_fopen(zCmdFName, "wb");
  if( aux==0 ) goto end_request;
  fwrite(zCmd, 1, strlen(zCmd), aux);

  sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http -args \"%s\" --nossl%s",







>
>
>
>



|
<



|







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
  }

  /*
  ** The repository name is only needed if there was no open checkout.  This
  ** is designed to allow the open checkout for the interactive user to work
  ** with the local Fossil server started via the "ui" command.
  */
  if( WSAAddressToStringA((SOCKADDR*)&p->addr, sizeof(p->addr),
                          NULL, zIp, &nIp)!=0 ){
    zIp[0] = 0;
  }
  if( (p->flags & HTTP_SERVER_HAD_CHECKOUT)==0 ){
    assert( g.zRepositoryName && g.zRepositoryName[0] );
    sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s\n%s",
      get_utf8_bom(0), zRequestFName, zReplyFName, zIp, g.zRepositoryName

    );
  }else{
    sqlite3_snprintf(sizeof(zCmd), zCmd, "%s%s\n%s\n%s",
      get_utf8_bom(0), zRequestFName, zReplyFName, zIp
    );
  }
  aux = fossil_fopen(zCmdFName, "wb");
  if( aux==0 ) goto end_request;
  fwrite(zCmd, 1, strlen(zCmd), aux);

  sqlite3_snprintf(sizeof(zCmd), zCmd, "\"%s\" http -args \"%s\" --nossl%s",
233
234
235
236
237
238
239


240
241
242
243
244
245
246
** Process a single incoming SCGI request.
*/
static void win32_scgi_request(void *pAppData){
  HttpRequest *p = (HttpRequest*)pAppData;
  FILE *in = 0, *out = 0;
  int amt, got, nHdr, i;
  int wanted = 0;


  char zRequestFName[MAX_PATH];
  char zReplyFName[MAX_PATH];
  char zCmd[2000];          /* Command-line to process the request */
  char zHdr[4000];          /* The SCGI request header */

  sqlite3_snprintf(MAX_PATH, zRequestFName,
                   "%s_%06d_in.txt", zTempPrefix, p->id);







>
>







243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
** Process a single incoming SCGI request.
*/
static void win32_scgi_request(void *pAppData){
  HttpRequest *p = (HttpRequest*)pAppData;
  FILE *in = 0, *out = 0;
  int amt, got, nHdr, i;
  int wanted = 0;
  char zIp[50];
  DWORD nIp = sizeof(zIp);
  char zRequestFName[MAX_PATH];
  char zReplyFName[MAX_PATH];
  char zCmd[2000];          /* Command-line to process the request */
  char zHdr[4000];          /* The SCGI request header */

  sqlite3_snprintf(MAX_PATH, zRequestFName,
                   "%s_%06d_in.txt", zTempPrefix, p->id);
263
264
265
266
267
268
269




270
271
272
273
274
275
276
277
278
279
  while( wanted>amt ){
    got = recv(p->s, zHdr, wanted<sizeof(zHdr) ? wanted : sizeof(zHdr), 0);
    if( got<=0 ) break;
    fwrite(zHdr, 1, got, out);
    wanted += got;
  }
  assert( g.zRepositoryName && g.zRepositoryName[0] );




  sqlite3_snprintf(sizeof(zCmd), zCmd,
    "\"%s\" http \"%s\" \"%s\" %s \"%s\" --scgi --nossl%s",
    g.nameOfExe, zRequestFName, zReplyFName, inet_ntoa(p->addr.sin_addr),
    g.zRepositoryName, p->zOptions
  );
  in = fossil_fopen(zReplyFName, "w+b");
  fflush(out);
  fossil_system(zCmd);
  if( in ){
    while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){







>
>
>
>


|







275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  while( wanted>amt ){
    got = recv(p->s, zHdr, wanted<sizeof(zHdr) ? wanted : sizeof(zHdr), 0);
    if( got<=0 ) break;
    fwrite(zHdr, 1, got, out);
    wanted += got;
  }
  assert( g.zRepositoryName && g.zRepositoryName[0] );
  if (WSAAddressToStringA((SOCKADDR*)&p->addr, sizeof(p->addr),
                          NULL, zIp, &nIp)!=0){
    zIp[0] = 0;
  }
  sqlite3_snprintf(sizeof(zCmd), zCmd,
    "\"%s\" http \"%s\" \"%s\" %s \"%s\" --scgi --nossl%s",
    g.nameOfExe, zRequestFName, zReplyFName, zIp,
    g.zRepositoryName, p->zOptions
  );
  in = fossil_fopen(zReplyFName, "w+b");
  fflush(out);
  fossil_system(zCmd);
  if( in ){
    while( (got = fread(zHdr, 1, sizeof(zHdr), in))>0 ){
309
310
311
312
313
314
315
316

317
318
319
320
321
322
323
  const char *zFileGlob,    /* The --fileglob option, or NULL */
  const char *zIpAddr,      /* Bind to this IP address, if not NULL */
  int flags                 /* One or more HTTP_SERVER_ flags */
){
  HANDLE hStoppedEvent;
  WSADATA wd;
  SOCKET s = INVALID_SOCKET;
  SOCKADDR_IN addr;

  int idCnt = 0;
  int iPort = mnPort;
  Blob options;
  wchar_t zTmpPath[MAX_PATH];
  const char *zSkin;
#if USE_SEE
  const char *zSavedKey = 0;







|
>







325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
  const char *zFileGlob,    /* The --fileglob option, or NULL */
  const char *zIpAddr,      /* Bind to this IP address, if not NULL */
  int flags                 /* One or more HTTP_SERVER_ flags */
){
  HANDLE hStoppedEvent;
  WSADATA wd;
  SOCKET s = INVALID_SOCKET;
  SOCKADDR_IN6 addr;
  int addrlen;
  int idCnt = 0;
  int iPort = mnPort;
  Blob options;
  wchar_t zTmpPath[MAX_PATH];
  const char *zSkin;
#if USE_SEE
  const char *zSavedKey = 0;
351
352
353
354
355
356
357
358
359
360



361

362
363
364
365


366
367
368




369

370


371
372
373
374

375
376


377
378
379
380
381
382
383
  zSavedKey = db_get_saved_encryption_key();
  savedKeySize = db_get_saved_encryption_key_size();
  if( zSavedKey!=0 && savedKeySize>0 ){
    blob_appendf(&options, " --usepidkey %lu:%p:%u", GetCurrentProcessId(),
                 zSavedKey, savedKeySize);
  }
#endif
  if( WSAStartup(MAKEWORD(1,1), &wd) ){
    fossil_fatal("unable to initialize winsock");
  }



  while( iPort<=mxPort ){

    s = socket(AF_INET, SOCK_STREAM, 0);
    if( s==INVALID_SOCKET ){
      fossil_fatal("unable to create a socket");
    }


    addr.sin_family = AF_INET;
    addr.sin_port = htons(iPort);
    if( zIpAddr ){




      addr.sin_addr.s_addr = inet_addr(zIpAddr);

      if( addr.sin_addr.s_addr == (-1) ){


        fossil_fatal("not a valid IP address: %s", zIpAddr);
      }
    }else if( flags & HTTP_SERVER_LOCALHOST ){
      addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    }else{
      addr.sin_addr.s_addr = htonl(INADDR_ANY);


    }
    if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
      closesocket(s);
      iPort++;
      continue;
    }
    if( listen(s, SOMAXCONN)==SOCKET_ERROR ){







|


>
>
>

>
|



>
>
|
|

>
>
>
>
|
>
|
>
>


<
|
>

|
>
>







368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410
411
412
413
414
415
  zSavedKey = db_get_saved_encryption_key();
  savedKeySize = db_get_saved_encryption_key_size();
  if( zSavedKey!=0 && savedKeySize>0 ){
    blob_appendf(&options, " --usepidkey %lu:%p:%u", GetCurrentProcessId(),
                 zSavedKey, savedKeySize);
  }
#endif
  if( WSAStartup(MAKEWORD(2,0), &wd) ){
    fossil_fatal("unable to initialize winsock");
  }
  if( flags & HTTP_SERVER_LOCALHOST ){
    zIpAddr = "127.0.0.1";
  }
  while( iPort<=mxPort ){
    DWORD ipv6only = 0;
    s = socket(AF_INET6, SOCK_STREAM, 0);
    if( s==INVALID_SOCKET ){
      fossil_fatal("unable to create a socket");
    }
    setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&ipv6only,
               sizeof(ipv6only));
    memset(&addr, 0, sizeof(addr));
    addrlen = sizeof(addr);
    if( zIpAddr ){
      char* zIp;
      if( strstr(zIpAddr, ".") ){
        zIp = mprintf("::ffff:%s", zIpAddr);
      }else{
        zIp = mprintf("%s", zIpAddr);
      }
      addr.sin6_family = AF_INET6;
      if (WSAStringToAddress(zIp, AF_INET6, NULL,
                             (struct sockaddr *)&addr, &addrlen) != 0){
        fossil_fatal("not a valid IP address: %s", zIpAddr);
      }

      ((SOCKADDR_IN6*)&addr)->sin6_port = htons(iPort);
      fossil_free(zIp);
    }else{
      addr.sin6_family = AF_INET6;
      addr.sin6_port = htons(iPort);
      memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
    }
    if( bind(s, (struct sockaddr*)&addr, sizeof(addr))==SOCKET_ERROR ){
      closesocket(s);
      iPort++;
      continue;
    }
    if( listen(s, SOMAXCONN)==SOCKET_ERROR ){
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
    _beginthread(win32_server_stopper, 0, (void*)pServer);
  }
  /* Set the service status to running and pass the listener socket to the
  ** service handling procedures. */
  win32_http_service_running(s);
  for(;;){
    SOCKET client;
    SOCKADDR_IN client_addr;
    HttpRequest *pRequest;
    int len = sizeof(client_addr);
    int wsaError;

    client = accept(s, (struct sockaddr*)&client_addr, &len);
    if( client==INVALID_SOCKET ){
      /* If the service control handler has closed the listener socket,







|







460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
    _beginthread(win32_server_stopper, 0, (void*)pServer);
  }
  /* Set the service status to running and pass the listener socket to the
  ** service handling procedures. */
  win32_http_service_running(s);
  for(;;){
    SOCKET client;
    SOCKADDR_IN6 client_addr;
    HttpRequest *pRequest;
    int len = sizeof(client_addr);
    int wsaError;

    client = accept(s, (struct sockaddr*)&client_addr, &len);
    if( client==INVALID_SOCKET ){
      /* If the service control handler has closed the listener socket,