File: helpers.c

package info (click to toggle)
torsocks 2.5.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 916 kB
  • sloc: ansic: 7,437; sh: 1,038; makefile: 105
file content (48 lines) | stat: -rw-r--r-- 1,397 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * Copyright (C) 2017 - David Goulet <dgoulet@ev0ke.net>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2 only, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <arpa/inet.h>

#include "lib/torsocks.h"

#include "helpers.h"

/* Try to connect to SocksPort localhost:9050 and if we can't skip. This is
 * to avoid to have failing test if no tor daemon is available. Return 1 if
 * true else 0. */
int
helper_is_default_tor_running(void)
{
  int ret, fd;
  struct sockaddr_in sa;

  fd = tsocks_libc_socket(AF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    goto end;
  }
  sa.sin_family = AF_INET;
  sa.sin_port = htons(9050);
  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  ret = tsocks_libc_connect(fd, (const struct sockaddr *) &sa, sizeof(sa));
  close(fd);
  if (ret < 0) {
    goto end;
  }
  return 1;
end:
  return 0;
}