From a0ca8184a43966f1b82862ce7aa7caaa3cfb176e Mon Sep 17 00:00:00 2001
From: Augustin Cavalier <waddlesplash@gmail.com>
Date: Fri, 27 Feb 2015 01:14:25 -0500
Subject: [PATCH] Merging Axel's patches from trunk.

---
 argz.h     |  6 +++++-
 logger.c   |  1 -
 tftp_def.c |  7 +++----
 tftp_io.c  | 13 +++++++++++--
 tftpd.c    |  6 +++++-
 5 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/argz.h b/argz.h
index 582be55..f1ded53 100644
--- a/argz.h
+++ b/argz.h
@@ -38,7 +38,7 @@
 #ifndef _ARGZ_H
 #define _ARGZ_H	1
 
-#include <features.h>
+#include <sys/cdefs.h>
 
 #define __need_error_t
 #include <errno.h>
@@ -52,6 +52,10 @@
 typedef int error_t;
 #endif
 
+#define __THROW
+#define __restrict
+#define __attribute_pure__
+#define __BEGIN_DECLS
 
 __BEGIN_DECLS
 
diff --git a/logger.c b/logger.c
index 2ef1ba3..40d0707 100644
--- a/logger.c
+++ b/logger.c
@@ -20,7 +20,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
 #include <stdarg.h>
 #include <string.h>
diff --git a/tftp_def.c b/tftp_def.c
index 96abdc5..9fcbf36 100644
--- a/tftp_def.c
+++ b/tftp_def.c
@@ -159,20 +159,19 @@ int Gethostbyname(char *addr, struct hostent *host)
      struct hostent *hp;
      char *tmpbuf;
      size_t tmpbuflen;
-     int res;
      int herr;
-     
+
      tmpbuflen = 1024;
 
      if ((tmpbuf = (char *)malloc(tmpbuflen)) == NULL)
           return ERR;
 
-     res = gethostbyname_r(addr, host, tmpbuf, tmpbuflen, &hp, &herr);
+     hp = gethostbyname_r(addr, host, tmpbuf, tmpbuflen, &herr);
 
      free(tmpbuf);
 
      /*  Check for errors. */
-     if (res != 0)
+     if (hp == NULL)
      {
           logger(LOG_ERR, "%s: %d: gethostbyname_r: %s",
                  __FILE__, __LINE__, strerror(herr));
diff --git a/tftp_io.c b/tftp_io.c
index 605a9c2..f84a8b9 100644
--- a/tftp_io.c
+++ b/tftp_io.c
@@ -217,8 +217,6 @@ int tftp_get_packet(int sock1, int sock2, int *sock, struct sockaddr_storage *sa
 
      struct msghdr msg;         /* used to get client's packet info */
      struct cmsghdr *cmsg;
-     struct in_pktinfo *pktinfo4;
-     struct in6_pktinfo *pktinfo6;
      struct iovec iov;
      char cbuf[1024];
 
@@ -288,10 +286,12 @@ int tftp_get_packet(int sock1, int sock2, int *sock, struct sockaddr_storage *sa
                     cmsg != NULL && cmsg->cmsg_len >= sizeof(*cmsg);
                     cmsg = CMSG_NXTHDR(&msg, cmsg))
                {
+#ifndef __HAIKU__
 #if defined(SOL_IP) && defined(IP_PKTINFO)
                     if (cmsg->cmsg_level == SOL_IP
                         && cmsg->cmsg_type == IP_PKTINFO)
                     {
+                         struct in_pktinfo *pktinfo4;
                          pktinfo4 = (struct in_pktinfo *)CMSG_DATA(cmsg);
                          sa_to->ss_family = AF_INET;
                          ((struct sockaddr_in *)sa_to)->sin_addr =
@@ -302,12 +302,21 @@ int tftp_get_packet(int sock1, int sock2, int *sock, struct sockaddr_storage *sa
                     if (cmsg->cmsg_level == SOL_IPV6
                         && cmsg->cmsg_type == IPV6_PKTINFO)
                     {
+                         struct in6_pktinfo *pktinfo6;
                          pktinfo6 = (struct in6_pktinfo *)CMSG_DATA(cmsg);
                          sa_to->ss_family = AF_INET6;
                          ((struct sockaddr_in6 *)sa_to)->sin6_addr =
                               pktinfo6->ipi6_addr;
                     }
 #endif
+#else	// !__HAIKU__
+                    if (cmsg->cmsg_level == IPPROTO_IP
+                        && cmsg->cmsg_type == IP_RECVDSTADDR)
+                    {
+                         struct in_addr *addr = (struct in_addr *)CMSG_DATA(cmsg);
+                         ((struct sockaddr_in *)sa_to)->sin_addr = *addr;
+                    }
+#endif
                     break;
                }
           }
diff --git a/tftpd.c b/tftpd.c
index b9822c4..8611a6f 100644
--- a/tftpd.c
+++ b/tftpd.c
@@ -334,9 +334,13 @@ int main(int argc, char **argv)
           open_logger("atftpd", log_file, logging_level);
      }
 
-#if defined(SOL_IP) && defined(IP_PKTINFO)
+#if defined(SOL_IP) && defined(IP_PKTINFO) || defined(__HAIKU__)
      /* We need to retieve some information from incomming packets */
+#ifndef __HAIKU__
      if (setsockopt(0, SOL_IP, IP_PKTINFO, &one, sizeof(one)) != 0)
+#else
+     if (setsockopt(0, IPPROTO_IP, IP_RECVDSTADDR, &one, sizeof(one)) != 0)
+#endif
      {
           logger(LOG_WARNING, "Failed to set socket option: %s", strerror(errno));
      }
-- 
2.2.2


From fbddc427e3eb9259093c5d7e392b1dd40a615d69 Mon Sep 17 00:00:00 2001
From: Augustin Cavalier <waddlesplash@gmail.com>
Date: Fri, 27 Feb 2015 01:26:36 -0500
Subject: [PATCH] Disable IPv6.

---
 tftp_file.c   | 4 ++++
 tftp_mtftp.c  | 4 ++++
 tftpd_file.c  | 2 ++
 tftpd_mtftp.c | 2 ++
 4 files changed, 12 insertions(+)

diff --git a/tftp_file.c b/tftp_file.c
index 27ae4d0..6dcbcac 100644
--- a/tftp_file.c
+++ b/tftp_file.c
@@ -505,10 +505,12 @@ int tftp_receive_file(struct client_data *data)
                               err = setsockopt(mcast_sockfd, IPPROTO_IP,
                                                IP_ADD_MEMBERSHIP,
                                                &mreq.v4, sizeof(mreq.v4));
+#ifdef USE_IPV6
                          else
                               err = setsockopt(mcast_sockfd, IPPROTO_IPV6,
                                                IPV6_ADD_MEMBERSHIP,
                                                &mreq.v6, sizeof(mreq.v6));
+#endif
                          if (err < 0)
                          {
                               perror("setsockopt");
@@ -579,10 +581,12 @@ int tftp_receive_file(struct client_data *data)
                          err = setsockopt(mcast_sockfd, IPPROTO_IP,
                                           IP_DROP_MEMBERSHIP,
                                           &mreq.v4, sizeof(mreq.v4));
+#ifdef USE_IPV6
                     else
                          err = setsockopt(mcast_sockfd, IPPROTO_IPV6,
                                           IPV6_DROP_MEMBERSHIP,
                                           &mreq.v6, sizeof(mreq.v6));
+#endif
                     if (err < 0)
                     {
                          perror("setsockopt");
diff --git a/tftp_mtftp.c b/tftp_mtftp.c
index edc739c..74a5bf3 100644
--- a/tftp_mtftp.c
+++ b/tftp_mtftp.c
@@ -214,9 +214,11 @@ int tftp_mtftp_receive_file(struct client_data *data)
      if (sa_mcast_group.ss_family == AF_INET)
           err = setsockopt(mcast_sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
                            &mreq.v4, sizeof(mreq.v4));
+#ifdef USE_IPV6
      else
           err = setsockopt(mcast_sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
                            &mreq.v6, sizeof(mreq.v6));
+#endif
      if (err < 0)
      {
 	  perror("atftp: setsockopt");
@@ -504,10 +506,12 @@ int tftp_mtftp_receive_file(struct client_data *data)
                     err = setsockopt(mcast_sockfd, IPPROTO_IP,
                                      IP_DROP_MEMBERSHIP, 
                                      &mreq.v4, sizeof(mreq.v4));
+#ifdef USE_IPV6
                else
                     err = setsockopt(mcast_sockfd, IPPROTO_IPV6,
                                      IPV6_DROP_MEMBERSHIP,
                                      &mreq.v6, sizeof(mreq.v6));
+#endif
                if (err < 0)
                {
                     perror("setsockopt");
diff --git a/tftpd_file.c b/tftpd_file.c
index da1d6c9..075a29b 100644
--- a/tftpd_file.c
+++ b/tftpd_file.c
@@ -665,9 +665,11 @@ int tftpd_send_file(struct thread_data *data)
                if (data->sa_mcast.ss_family == AF_INET)
                     setsockopt(data->sockfd, IPPROTO_IP, IP_MULTICAST_TTL,
                                &data->mcast_ttl, sizeof(data->mcast_ttl));
+#ifdef USE_IPV6
                else
                     setsockopt(data->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
                                &data->mcast_ttl, sizeof(data->mcast_ttl));
+#endif
                
                /* set options data for OACK */
                opt_set_multicast(data->tftp_options, data->mc_addr,
diff --git a/tftpd_mtftp.c b/tftpd_mtftp.c
index f69f9b0..36f9562 100644
--- a/tftpd_mtftp.c
+++ b/tftpd_mtftp.c
@@ -475,9 +475,11 @@ void *tftpd_mtftp_server(void *arg)
                if (thread->sa_mcast.ss_family == AF_INET)
                     setsockopt(thread->sockfd, IPPROTO_IP, IP_MULTICAST_TTL,
                                &data->mcast_ttl, sizeof(data->mcast_ttl));
+#ifdef USE_IPV6
                else
                     setsockopt(thread->sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
                                &data->mcast_ttl, sizeof(data->mcast_ttl));
+#endif
 
                /* give server thread access to mtftp options */
                thread->mtftp_data = data;
-- 
2.2.2

