forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scamper_getsrc.c
136 lines (119 loc) · 3.13 KB
/
scamper_getsrc.c
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
/*
* scamper_getsrc.c
*
* $Id: scamper_getsrc.c,v 1.13 2009/03/21 09:27:16 mjl Exp $
*
* Copyright (C) 2005-2009 The University of Waikato
* Author: Matthew Luckie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
#include "scamper.h"
#include "scamper_addr.h"
#include "scamper_debug.h"
#include "scamper_getsrc.h"
#include "utils.h"
static int udp4 = -1;
static int udp6 = -1;
extern scamper_addrcache_t *addrcache;
/*
* scamper_getsrc
*
* given a destination address, determine the src address used in the IP
* header to transmit probes to it.
*/
scamper_addr_t *scamper_getsrc(const scamper_addr_t *dst)
{
struct sockaddr_storage sas;
scamper_addr_t *src;
socklen_t socklen, sockleno;
int sock;
int af;
void *addr;
char buf[64];
if(dst->type == SCAMPER_ADDR_TYPE_IPV4)
{
if(udp4 == -1 && (udp4 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
printerror(errno, strerror, __func__, "could not open udp4 sock");
return NULL;
}
af = AF_INET;
sock = udp4;
addr = &((struct sockaddr_in *)&sas)->sin_addr;
socklen = sizeof(struct sockaddr_in);
}
else if(dst->type == SCAMPER_ADDR_TYPE_IPV6)
{
if(udp6 == -1 && (udp6 = socket(AF_INET6, SOCK_DGRAM,IPPROTO_UDP)) == -1)
{
printerror(errno, strerror, __func__, "could not open udp6 sock");
return NULL;
}
af = AF_INET6;
sock = udp6;
addr = &((struct sockaddr_in6 *)&sas)->sin6_addr;
socklen = sizeof(struct sockaddr_in6);
}
else return NULL;
sockaddr_compose((struct sockaddr *)&sas, af, dst->addr, 80);
if(connect(sock, (struct sockaddr *)&sas, socklen) != 0)
{
printerror(errno, strerror, __func__, "connect to dst failed for %s",
scamper_addr_tostr(dst, buf, sizeof(buf)));
return NULL;
}
sockleno = socklen;
if(getsockname(sock, (struct sockaddr *)&sas, &sockleno) != 0)
{
printerror(errno, strerror, __func__, "could not getsockname for %s",
scamper_addr_tostr(dst, buf, sizeof(buf)));
return NULL;
}
src = scamper_addrcache_get(addrcache, dst->type, addr);
memset(&sas, 0, sizeof(sas));
connect(sock, (struct sockaddr *)&sas, socklen);
return src;
}
int scamper_getsrc_init()
{
return 0;
}
void scamper_getsrc_cleanup()
{
if(udp4 != -1)
{
#ifndef _WIN32
close(udp4);
#else
closesocket(udp4);
#endif
udp4 = -1;
}
if(udp6 != -1)
{
#ifndef _WIN32
close(udp6);
#else
closesocket(udp6);
#endif
udp6 = -1;
}
return;
}