| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2010, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_config.h" |
|---|
| 34 |
#include "utils/noit_getip.h" |
|---|
| 35 |
#include <netinet/in.h> |
|---|
| 36 |
#include <sys/socket.h> |
|---|
| 37 |
#include <unistd.h> |
|---|
| 38 |
#include <errno.h> |
|---|
| 39 |
|
|---|
| 40 |
int |
|---|
| 41 |
noit_getip_ipv4(struct in_addr remote, struct in_addr *local) { |
|---|
| 42 |
int s; |
|---|
| 43 |
struct sockaddr_in r, l; |
|---|
| 44 |
socklen_t llen; |
|---|
| 45 |
s = socket(PF_INET, SOCK_DGRAM, 17); /* 17 is UDP */ |
|---|
| 46 |
if(s < 0) return -1; |
|---|
| 47 |
memset(&r, 0, sizeof(r)); |
|---|
| 48 |
r.sin_port = htons(53); /* DNS port, it's fairly standard. */ |
|---|
| 49 |
r.sin_addr = remote; |
|---|
| 50 |
r.sin_family = AF_INET; |
|---|
| 51 |
if(connect(s, (struct sockaddr *)&r, sizeof(r)) < 0) { |
|---|
| 52 |
close(s); |
|---|
| 53 |
return -1; |
|---|
| 54 |
} |
|---|
| 55 |
llen = sizeof(l); |
|---|
| 56 |
if(getsockname(s, (struct sockaddr *)&l, &llen) < 0) { |
|---|
| 57 |
close(s); |
|---|
| 58 |
return -1; |
|---|
| 59 |
} |
|---|
| 60 |
close(s); |
|---|
| 61 |
*local = l.sin_addr; |
|---|
| 62 |
return 0; |
|---|
| 63 |
} |
|---|