~ubuntu-branches/ubuntu/trusty/tor/trusty-proposed

« back to all changes in this revision

Viewing changes to src/tools/tor-fw-helper/tor-fw-helper-natpmp.c

  • Committer: Package Import Robot
  • Author(s): Peter Palfrader
  • Date: 2013-12-12 14:31:32 UTC
  • mfrom: (1.5.25) (2.1.82 experimental)
  • Revision ID: package-import@ubuntu.com-20131212143132-zjz1rgameoxts5t0
Tags: 0.2.4.19-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (c) 2010, Jacob Appelbaum, Steven J. Murdoch.
2
 
 * Copyright (c) 2010-2012, The Tor Project, Inc. */
 
2
 * Copyright (c) 2010-2013, The Tor Project, Inc. */
3
3
/* See LICENSE for licensing information */
4
4
 
5
5
/**
60
60
  state->lease = NATPMP_DEFAULT_LEASE;
61
61
 
62
62
  if (tor_fw_options->verbose)
63
 
    fprintf(stdout, "V: natpmp init...\n");
 
63
    fprintf(stderr, "V: natpmp init...\n");
64
64
 
65
65
  r = initnatpmp(&(state->natpmp), 0, 0);
66
66
  if (r == 0) {
67
67
    state->init = 1;
68
 
    fprintf(stdout, "tor-fw-helper: natpmp initialized...\n");
 
68
    fprintf(stderr, "V: natpmp initialized...\n");
69
69
    return r;
70
70
  } else {
71
 
    fprintf(stderr, "tor-fw-helper: natpmp failed to initialize...\n");
 
71
    fprintf(stderr, "V: natpmp failed to initialize...\n");
72
72
    return r;
73
73
  }
74
74
}
80
80
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
81
81
  int r = 0;
82
82
  if (tor_fw_options->verbose)
83
 
    fprintf(stdout, "V: natpmp cleanup...\n");
 
83
    fprintf(stderr, "V: natpmp cleanup...\n");
84
84
  r = closenatpmp(&(state->natpmp));
85
85
  if (tor_fw_options->verbose)
86
 
    fprintf(stdout, "V: closing natpmp socket: %d\n", r);
 
86
    fprintf(stderr, "V: closing natpmp socket: %d\n", r);
87
87
  return r;
88
88
}
89
89
 
93
93
{
94
94
  int r;
95
95
  fd_set fds;
 
96
 
 
97
#ifndef WIN32
96
98
  if (fd >= FD_SETSIZE) {
97
99
    fprintf(stderr, "E: NAT-PMP FD_SETSIZE error %d\n", fd);
98
100
    return -1;
99
101
  }
 
102
#endif
 
103
 
100
104
  FD_ZERO(&fds);
101
105
  FD_SET(fd, &fds);
102
106
  r = select(fd+1, &fds, NULL, NULL, timeout);
103
107
  if (r == -1) {
104
 
    fprintf(stdout, "V: select failed in wait_until_fd_readable: %s\n",
105
 
            strerror(errno));
 
108
    fprintf(stderr, "V: select failed in wait_until_fd_readable: %s\n",
 
109
            tor_socket_strerror(tor_socket_errno(fd)));
106
110
    return -1;
107
111
  }
108
112
  /* XXXX we should really check to see whether fd was readable, or we timed
110
114
  return 0;
111
115
}
112
116
 
113
 
/** Add a TCP port mapping for a single port stored in <b>tor_fw_options</b>
114
 
 * using the <b>natpmp_t</b> stored in <b>backend_state</b>. */
115
117
int
116
 
tor_natpmp_add_tcp_mapping(tor_fw_options_t *tor_fw_options,
117
 
                           void *backend_state)
 
118
tor_natpmp_add_tcp_mapping(uint16_t internal_port, uint16_t external_port,
 
119
                           int is_verbose, void *backend_state)
118
120
{
119
 
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
120
121
  int r = 0;
121
122
  int x = 0;
122
123
  int sav_errno;
 
124
  natpmp_state_t *state = (natpmp_state_t *) backend_state;
123
125
 
124
126
  struct timeval timeout;
125
127
 
126
 
  if (tor_fw_options->verbose)
127
 
    fprintf(stdout, "V: sending natpmp portmapping request...\n");
 
128
  if (is_verbose)
 
129
    fprintf(stderr, "V: sending natpmp portmapping request...\n");
128
130
  r = sendnewportmappingrequest(&(state->natpmp), state->protocol,
129
 
                                tor_fw_options->internal_port,
130
 
                                tor_fw_options->external_port,
 
131
                                internal_port,
 
132
                                external_port,
131
133
                                state->lease);
132
 
  if (tor_fw_options->verbose)
133
 
    fprintf(stdout, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
 
134
  if (is_verbose)
 
135
    fprintf(stderr, "tor-fw-helper: NAT-PMP sendnewportmappingrequest "
134
136
            "returned %d (%s)\n", r, r==12?"SUCCESS":"FAILED");
135
137
 
136
138
  do {
139
141
    if (x == -1)
140
142
      return -1;
141
143
 
142
 
    if (tor_fw_options->verbose)
143
 
      fprintf(stdout, "V: attempting to readnatpmpreponseorretry...\n");
 
144
    if (is_verbose)
 
145
      fprintf(stderr, "V: attempting to readnatpmpreponseorretry...\n");
144
146
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
145
 
    sav_errno = errno;
 
147
    sav_errno = tor_socket_errno(state->natpmp.s);
146
148
 
147
149
    if (r<0 && r!=NATPMP_TRYAGAIN) {
148
150
      fprintf(stderr, "E: readnatpmpresponseorretry failed %d\n", r);
149
151
      fprintf(stderr, "E: errno=%d '%s'\n", sav_errno,
150
 
              strerror(sav_errno));
 
152
              tor_socket_strerror(sav_errno));
151
153
    }
152
154
 
153
155
  } while (r == NATPMP_TRYAGAIN);
163
165
  }
164
166
 
165
167
  if (r == NATPMP_SUCCESS) {
166
 
    fprintf(stdout, "tor-fw-helper: NAT-PMP mapped public port %hu to"
 
168
    fprintf(stderr, "tor-fw-helper: NAT-PMP mapped public port %hu to"
167
169
            " localport %hu liftime %u\n",
168
170
            (state->response).pnu.newportmapping.mappedpublicport,
169
171
            (state->response).pnu.newportmapping.privateport,
170
172
            (state->response).pnu.newportmapping.lifetime);
171
173
  }
172
174
 
173
 
  tor_fw_options->nat_pmp_status = 1;
174
 
 
175
 
  return r;
 
175
  return (r == NATPMP_SUCCESS) ? 0 : -1;
176
176
}
177
177
 
178
178
/** Fetch our likely public IP from our upstream NAT-PMP enabled NAT device.
189
189
  struct timeval timeout;
190
190
 
191
191
  r = sendpublicaddressrequest(&(state->natpmp));
192
 
  fprintf(stdout, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
 
192
  fprintf(stderr, "tor-fw-helper: NAT-PMP sendpublicaddressrequest returned"
193
193
          " %d (%s)\n", r, r==2?"SUCCESS":"FAILED");
194
194
 
195
195
  do {
200
200
      return -1;
201
201
 
202
202
    if (tor_fw_options->verbose)
203
 
      fprintf(stdout, "V: NAT-PMP attempting to read reponse...\n");
 
203
      fprintf(stderr, "V: NAT-PMP attempting to read reponse...\n");
204
204
    r = readnatpmpresponseorretry(&(state->natpmp), &(state->response));
205
 
    sav_errno = errno;
 
205
    sav_errno = tor_socket_errno(state->natpmp.s);
206
206
 
207
207
    if (tor_fw_options->verbose)
208
 
      fprintf(stdout, "V: NAT-PMP readnatpmpresponseorretry returned"
 
208
      fprintf(stderr, "V: NAT-PMP readnatpmpresponseorretry returned"
209
209
              " %d\n", r);
210
210
 
211
211
    if ( r < 0 && r != NATPMP_TRYAGAIN) {
212
212
      fprintf(stderr, "E: NAT-PMP readnatpmpresponseorretry failed %d\n",
213
213
              r);
214
214
      fprintf(stderr, "E: NAT-PMP errno=%d '%s'\n", sav_errno,
215
 
              strerror(sav_errno));
 
215
              tor_socket_strerror(sav_errno));
216
216
    }
217
217
 
218
218
  } while (r == NATPMP_TRYAGAIN );
223
223
    return r;
224
224
  }
225
225
 
226
 
  fprintf(stdout, "tor-fw-helper: ExternalIPAddress = %s\n",
 
226
  fprintf(stderr, "tor-fw-helper: ExternalIPAddress = %s\n",
227
227
          inet_ntoa((state->response).pnu.publicaddress.addr));
228
228
  tor_fw_options->public_ip_status = 1;
229
229
 
230
230
  if (tor_fw_options->verbose) {
231
 
    fprintf(stdout, "V: result = %u\n", r);
232
 
    fprintf(stdout, "V: type = %u\n", (state->response).type);
233
 
    fprintf(stdout, "V: resultcode = %u\n", (state->response).resultcode);
234
 
    fprintf(stdout, "V: epoch = %u\n", (state->response).epoch);
 
231
    fprintf(stderr, "V: result = %u\n", r);
 
232
    fprintf(stderr, "V: type = %u\n", (state->response).type);
 
233
    fprintf(stderr, "V: resultcode = %u\n", (state->response).resultcode);
 
234
    fprintf(stderr, "V: epoch = %u\n", (state->response).epoch);
235
235
  }
236
236
 
237
237
  return r;