2 * This library is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2.1 of the License, or (at your option) any later version.
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this library; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 * Helper functions to get/set addresses of UDP packets
17 * based on recvfromto by Miquel van Smoorenburg
19 * recvfromto Like recvfrom, but also stores the destination
20 * IP address. Useful on multihomed hosts.
22 * Should work on Linux and BSD.
24 * Copyright (C) 2002 Miquel van Smoorenburg.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU Lesser General Public
28 * License as published by the Free Software Foundation; either
29 * version 2 of the License, or (at your option) any later version.
31 * sendfromto added 18/08/2003, Jan Berkel <jan@sitadelle.com>
32 * Works on Linux and FreeBSD (5.x)
40 static const char rcsid[] = "$Id$";
42 #include <sys/types.h>
48 #include <netinet/in.h>
54 #include "udpfromto.h"
56 int udpfromto_init(int s)
58 int err = -1, opt = 1;
60 #ifdef HAVE_IP_PKTINFO
61 /* Set the IP_PKTINFO option (Linux). */
62 err = setsockopt(s, SOL_IP, IP_PKTINFO, &opt, sizeof(opt));
65 #ifdef HAVE_IP_RECVDSTADDR
67 * Set the IP_RECVDSTADDR option (BSD).
68 * Note: IP_RECVDSTADDR == IP_SENDSRCADDR
70 err = setsockopt(s, IPPROTO_IP, IP_RECVDSTADDR, &opt, sizeof(opt));
75 int recvfromto(int s, void *buf, size_t len, int flags,
76 struct sockaddr *from, socklen_t *fromlen,
77 struct sockaddr *to, socklen_t *tolen)
79 #if defined(HAVE_IP_PKTINFO) || defined(HAVE_IP_RECVDSTADDR)
87 * If from or to are set, they must be big enough
88 * to store a struct sockaddr_in.
90 if ((from && (!fromlen || *fromlen < sizeof(struct sockaddr_in))) ||
91 (to && (!tolen || *tolen < sizeof(struct sockaddr_in)))) {
97 * IP_PKTINFO / IP_RECVDSTADDR don't provide sin_port so we have to
98 * retrieve it using getsockname().
101 struct sockaddr_in si;
102 socklen_t l = sizeof(si);
104 ((struct sockaddr_in *)to)->sin_family = AF_INET;
105 ((struct sockaddr_in *)to)->sin_port = 0;
107 if (getsockname(s, (struct sockaddr *)&si, &l) == 0) {
108 ((struct sockaddr_in *)to)->sin_port = si.sin_port;
109 ((struct sockaddr_in *)to)->sin_addr = si.sin_addr;
111 if (tolen) *tolen = sizeof(struct sockaddr_in);
114 /* Set up iov and msgh structures. */
115 memset(&msgh, 0, sizeof(struct msghdr));
118 msgh.msg_control = cbuf;
119 msgh.msg_controllen = sizeof(cbuf);
120 msgh.msg_name = from;
121 msgh.msg_namelen = fromlen ? *fromlen : 0;
126 /* Receive one packet. */
127 if ((err = recvmsg(s, &msgh, flags)) < 0) {
130 if (fromlen) *fromlen = msgh.msg_namelen;
132 /* Process auxiliary received data in msgh */
133 for (cmsg = CMSG_FIRSTHDR(&msgh);
135 cmsg = CMSG_NXTHDR(&msgh,cmsg)) {
137 # ifdef HAVE_IP_PKTINFO
138 if (cmsg->cmsg_level == SOL_IP
139 && cmsg->cmsg_type == IP_PKTINFO) {
140 struct in_pktinfo *i =
141 (struct in_pktinfo *)CMSG_DATA(cmsg);
143 ((struct sockaddr_in *)to)->sin_addr =
145 if (tolen) *tolen = sizeof(struct sockaddr_in);
151 # ifdef HAVE_IP_RECVDSTADDR
152 if (cmsg->cmsg_level == IPPROTO_IP
153 && cmsg->cmsg_type == IP_RECVDSTADDR) {
154 struct in_addr *i = (struct in_addr *)CMSG_DATA(cmsg);
156 ((struct sockaddr_in *)to)->sin_addr = *i;
157 if (tolen) *tolen = sizeof(struct sockaddr_in);
165 /* fallback: call recvfrom */
166 return recvfrom(s, buf, len, flags, from, fromlen);
167 #endif /* defined(HAVE_IP_PKTINFO) || defined(HAVE_IP_RECVDSTADDR) */
170 int sendfromto(int s, void *buf, size_t len, int flags,
171 struct sockaddr *from, socklen_t fromlen,
172 struct sockaddr *to, socklen_t tolen)
174 #if defined(HAVE_IP_PKTINFO) || defined(HAVE_IP_SENDSRCADDR)
176 struct cmsghdr *cmsg;
178 # ifdef HAVE_IP_PKTINFO
179 char cmsgbuf[CMSG_SPACE(sizeof(struct in_pktinfo))];
180 struct in_pktinfo pktinfo, *pktinfo_ptr;
181 memset(&pktinfo, 0, sizeof(struct in_pktinfo));
184 # ifdef HAVE_IP_SENDSRCADDR
185 char cmsgbuf[CMSG_SPACE(sizeof(struct in_addr))];
188 /* Set up iov and msgh structures. */
189 memset(&msgh, 0, sizeof(struct msghdr));
194 msgh.msg_control = cmsgbuf;
195 msgh.msg_controllen = sizeof(cmsgbuf);
197 msgh.msg_namelen = tolen;
199 cmsg = CMSG_FIRSTHDR(&msgh);
201 # ifdef HAVE_IP_PKTINFO
202 cmsg->cmsg_level = SOL_IP;
203 cmsg->cmsg_type = IP_PKTINFO;
204 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
205 pktinfo.ipi_spec_dst = ((struct sockaddr_in *)from)->sin_addr;
206 pktinfo_ptr = (struct in_pktinfo *)CMSG_DATA(cmsg);
207 memcpy(pktinfo_ptr, &pktinfo, sizeof(struct in_pktinfo));
209 # ifdef HAVE_IP_SENDSRCADDR
210 cmsg->cmsg_level = IPPROTO_IP;
211 cmsg->cmsg_type = IP_SENDSRCADDR;
212 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
213 memcpy((struct in_addr *)CMSG_DATA(cmsg),
214 &((struct sockaddr_in *)from)->sin_addr, sizeof(struct in_addr));
217 return sendmsg(s, &msgh, flags);
219 /* fallback: call sendto() */
220 return sendto(s, buf, len, flags, to, tolen);
221 #endif /* defined(HAVE_IP_PKTINFO) || defined (HAVE_IP_SENDSRCADDR) */
227 * Small test program to test recvfromto/sendfromto
229 * use a virtual IP address as first argument to test
231 * reply packet should originate from virtual IP and not
232 * from the default interface the alias is bound to
237 #include <arpa/inet.h>
238 #include <sys/types.h>
239 #include <sys/wait.h>
241 #define DEF_PORT 20000 /* default port to listen on */
242 #define DESTIP "127.0.0.1" /* send packet to localhost per default */
243 #define TESTSTRING "foo" /* what to send */
244 #define TESTLEN 4 /* 4 bytes */
246 int main(int argc, char **argv)
248 struct sockaddr_in from, to, in;
250 char *destip = DESTIP;
252 int n, server_socket, client_socket, fl, tl, pid;
254 if (argc > 1) destip = argv[1];
255 if (argc > 2) port = atoi(argv[2]);
257 in.sin_family = AF_INET;
258 in.sin_addr.s_addr = INADDR_ANY;
259 in.sin_port = htons(port);
260 fl = tl = sizeof(struct sockaddr_in);
261 memset(&from, 0, sizeof(from));
262 memset(&to, 0, sizeof(to));
264 switch(pid = fork()) {
275 server_socket = socket(PF_INET, SOCK_DGRAM, 0);
276 if (udpfromto_init(server_socket) != 0) {
277 perror("udpfromto_init\n");
278 waitpid(pid, NULL, WNOHANG);
282 if (bind(server_socket, (struct sockaddr *)&in, sizeof(in)) < 0) {
283 perror("server: bind");
284 waitpid(pid, NULL, WNOHANG);
288 printf("server: waiting for packets on INADDR_ANY:%d\n", port);
289 if ((n = recvfromto(server_socket, buf, sizeof(buf), 0,
290 (struct sockaddr *)&from, &fl,
291 (struct sockaddr *)&to, &tl)) < 0) {
292 perror("server: recvfromto");
293 waitpid(pid, NULL, WNOHANG);
297 printf("server: received a packet of %d bytes [%s] ", n, buf);
298 printf("(src ip:port %s:%d ",
299 inet_ntoa(from.sin_addr), ntohs(from.sin_port));
300 printf(" dst ip:port %s:%d)\n",
301 inet_ntoa(to.sin_addr), ntohs(to.sin_port));
303 printf("server: replying from address packet was received on to source address\n");
305 if ((n = sendfromto(server_socket, buf, n, 0,
306 (struct sockaddr *)&to, tl,
307 (struct sockaddr *)&from, fl)) < 0) {
308 perror("server: sendfromto");
311 waitpid(pid, NULL, 0);
315 close(server_socket);
316 client_socket = socket(PF_INET, SOCK_DGRAM, 0);
317 if (udpfromto_init(client_socket) != 0) {
318 perror("udpfromto_init");
321 /* bind client on different port */
322 in.sin_port = htons(port+1);
323 if (bind(client_socket, (struct sockaddr *)&in, sizeof(in)) < 0) {
324 perror("client: bind");
328 in.sin_port = htons(port);
329 in.sin_addr.s_addr = inet_addr(destip);
331 printf("client: sending packet to %s:%d\n", destip, port);
332 if (sendto(client_socket, TESTSTRING, TESTLEN, 0,
333 (struct sockaddr *)&in, sizeof(in)) < 0) {
334 perror("client: sendto");
338 printf("client: waiting for reply from server on INADDR_ANY:%d\n", port+1);
340 if ((n = recvfromto(client_socket, buf, sizeof(buf), 0,
341 (struct sockaddr *)&from, &fl,
342 (struct sockaddr *)&to, &tl)) < 0) {
343 perror("client: recvfromto");
347 printf("client: received a packet of %d bytes [%s] ", n, buf);
348 printf("(src ip:port %s:%d",
349 inet_ntoa(from.sin_addr), ntohs(from.sin_port));
350 printf(" dst ip:port %s:%d)\n",
351 inet_ntoa(to.sin_addr), ntohs(to.sin_port));
357 #endif /* WITH_UDPFROMTO */