aboutsummaryrefslogtreecommitdiff
path: root/src/tools/tor-fw-helper/tor-fw-helper-natpmp.c
blob: ebcd9e61ad40b3574faf70ad2e7616b74d3acb88 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
 * Copyright (c) 2010, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>

#include "tor-fw-helper.h"
#include "tor-fw-helper-natpmp.h"

int
tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options)
{
    int r = 0;
    int x = 0;
    int sav_errno;
    int protocol = NATPMP_PROTOCOL_TCP;
    int lease = NATPMP_DEFAULT_LEASE;
    natpmp_t natpmp;
    natpmpresp_t response;

    fd_set fds;
    struct timeval timeout;

    if (tor_fw_options->verbose)
        fprintf(stdout, "V: natpmp init...\n");
    initnatpmp(&natpmp);

    if (tor_fw_options->verbose)
        fprintf(stdout, "V: sending natpmp portmapping request...\n");
    r = sendnewportmappingrequest(&natpmp, protocol,
                                  tor_fw_options->internal_port,
                                  tor_fw_options->external_port,
                                  lease);
    fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest returned"
            " %d (%s)\n", r, r==12?"SUCCESS":"FAILED");

    do {
        FD_ZERO(&fds);
        FD_SET(natpmp.s, &fds);
        getnatpmprequesttimeout(&natpmp, &timeout);
        select(FD_SETSIZE, &fds, NULL, NULL, &timeout);

        if (tor_fw_options->verbose)
            fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
        r = readnatpmpresponseorretry(&natpmp, &response);
        sav_errno = errno;

        if (r<0 && r!=NATPMP_TRYAGAIN)
        {
            fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
            fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
                    strerror(sav_errno));
        }

    } while ( r == NATPMP_TRYAGAIN );

    if (r == NATPMP_SUCCESS) {
        fprintf(stdout, "tor-fw-helper: NAT-PMP mapped public port %hu to"
                " localport %hu liftime %u\n",
                response.pnu.newportmapping.mappedpublicport,
                response.pnu.newportmapping.privateport,
                response.pnu.newportmapping.lifetime);
    }

    x = closenatpmp(&natpmp);
    if (tor_fw_options->verbose)
        fprintf(stdout, "V: closing natpmp socket: %d\n", x);
    return r;
}

int
tor_natpmp_fetch_public_ip(tor_fw_options_t *tor_fw_options)
{
    int r = 0;
    int x = 0;
    int sav_errno;
    natpmp_t natpmp;
    natpmpresp_t response;
    struct timeval timeout;
    fd_set fds;

    r = initnatpmp(&natpmp);
    if (tor_fw_options->verbose)
        fprintf(stdout, "V: NAT-PMP init: %d\n", r);

    r = sendpublicaddressrequest(&natpmp);
    fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
            " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");

    do {
        FD_ZERO(&fds);
        FD_SET(natpmp.s, &fds);
        getnatpmprequesttimeout(&natpmp, &timeout);
        select(FD_SETSIZE, &fds, NULL, NULL, &timeout);

        if (tor_fw_options->verbose)
            fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
        r = readnatpmpresponseorretry(&natpmp, &response);
        sav_errno = errno;

        if (tor_fw_options->verbose)
            fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
                    " %d\n", r);

        if ( r < 0 && r != NATPMP_TRYAGAIN)
        {
            fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
                    r);
            fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
                    strerror(sav_errno));
        }

    } while ( r == NATPMP_TRYAGAIN );

    if (r != 0)
    {
        fprintf(stderr, "E: NAT-PMP It appears that something went wrong:"
                " %d\n", r);
        return r;
    }

    fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
           inet_ntoa(response.pnu.publicaddress.addr));

    x = closenatpmp(&natpmp);

    if (tor_fw_options->verbose)
    {
        fprintf(stdout, "V: result = %u\n", r);
        fprintf(stdout, "V: type = %u\n", response.type);
        fprintf(stdout, "V: resultcode = %u\n", response.resultcode);
        fprintf(stdout, "V: epoch = %u\n", response.epoch);
        fprintf(stdout, "V: closing natpmp result: %d\n", r);
    }

    return r;
}