1// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "sandbox/win/sandbox_poc/pocdll/exports.h"
6#include "sandbox/win/sandbox_poc/pocdll/utils.h"
7
8// This file contains the tests used to verify the security of the network.
9
10void POCDLL_API TestNetworkListen(HANDLE log) {
11  HandleToFile handle2file;
12  FILE *output = handle2file.Translate(log, "w");
13#if DONT_WANT_INTERCEPTIONS_JUST_WANT_NETWORK
14  // Initialize Winsock
15  WSADATA wsa_data;
16  int result = ::WSAStartup(MAKEWORD(2, 2), &wsa_data);
17  if (result != NO_ERROR) {
18    fprintf(output, "[ERROR] Cannot initialize winsock. Error%d\r\n", result);
19    return;
20  }
21
22  // Create a SOCKET for listening for
23  // incoming connection requests.
24  SOCKET listen_socket;
25  listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
26  if (listen_socket == INVALID_SOCKET) {
27    fprintf(output, "[ERROR] Failed to create socket. Error %ld\r\n",
28           ::WSAGetLastError());
29    ::WSACleanup();
30    return;
31  }
32
33  // The sockaddr_in structure specifies the address family,
34  // IP address, and port for the socket that is being bound.
35  sockaddr_in service;
36  service.sin_family = AF_INET;
37  service.sin_addr.s_addr = inet_addr("127.0.0.1");
38  service.sin_port = htons(88);
39
40  if (bind(listen_socket, reinterpret_cast<SOCKADDR*>(&service),
41           sizeof(service)) == SOCKET_ERROR) {
42    fprintf(output, "[BLOCKED] Bind socket on port 88. Error %ld\r\n",
43            ::WSAGetLastError());
44    closesocket(listen_socket);
45    ::WSACleanup();
46    return;
47  }
48
49  // Listen for incoming connection requests
50  // on the created socket
51  if (listen(listen_socket, SOMAXCONN) == SOCKET_ERROR) {
52    fprintf(output, "[BLOCKED] Listen socket on port 88. Error %ld\r\n",
53            ::WSAGetLastError());
54
55  } else {
56    fprintf(output, "[GRANTED] Listen socket on port 88.\r\n",
57            ::WSAGetLastError());
58  }
59
60  ::WSACleanup();
61  return;
62#else  // DONT_WANT_INTERCEPTIONS_JUST_WANT_NETWORK
63  // Just print out that this test is not running.
64  fprintf(output, "[ERROR] No network tests.\r\n");
65#endif  // DONT_WANT_INTERCEPTIONS_JUST_WANT_NETWORK
66}
67