Blame src/gl/recvfrom.c

Packit aea12f
/* recvfrom.c --- wrappers for Windows recvfrom function
Packit aea12f
Packit Service 991b93
   Copyright (C) 2008-2020 Free Software Foundation, Inc.
Packit aea12f
Packit aea12f
   This program is free software: you can redistribute it and/or modify
Packit aea12f
   it under the terms of the GNU General Public License as published by
Packit aea12f
   the Free Software Foundation; either version 3 of the License, or
Packit aea12f
   (at your option) any later version.
Packit aea12f
Packit aea12f
   This program is distributed in the hope that it will be useful,
Packit aea12f
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit aea12f
   GNU General Public License for more details.
Packit aea12f
Packit aea12f
   You should have received a copy of the GNU General Public License
Packit aea12f
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit aea12f
Packit aea12f
/* Written by Paolo Bonzini */
Packit aea12f
Packit aea12f
#include <config.h>
Packit aea12f
Packit aea12f
#define WIN32_LEAN_AND_MEAN
Packit aea12f
/* Get winsock2.h. */
Packit aea12f
#include <sys/socket.h>
Packit aea12f
Packit aea12f
/* Get set_winsock_errno, FD_TO_SOCKET etc. */
Packit aea12f
#include "w32sock.h"
Packit aea12f
Packit aea12f
#undef recvfrom
Packit aea12f
Packit aea12f
ssize_t
Packit aea12f
rpl_recvfrom (int fd, void *buf, size_t len, int flags, struct sockaddr *from,
Packit aea12f
              socklen_t *fromlen)
Packit aea12f
{
Packit aea12f
  SOCKET sock = FD_TO_SOCKET (fd);
Packit aea12f
Packit aea12f
  if (sock == INVALID_SOCKET)
Packit aea12f
    {
Packit aea12f
      errno = EBADF;
Packit aea12f
      return -1;
Packit aea12f
    }
Packit aea12f
  else
Packit aea12f
    {
Packit aea12f
      int frombufsize = (from != NULL ? *fromlen : 0);
Packit aea12f
      int r = recvfrom (sock, buf, len, flags, from, fromlen);
Packit aea12f
Packit aea12f
      if (r < 0)
Packit aea12f
        set_winsock_errno ();
Packit aea12f
Packit aea12f
      /* Winsock recvfrom() only returns a valid 'from' when the socket is
Packit aea12f
         connectionless.  POSIX gives a valid 'from' for all types of
Packit aea12f
         sockets.  */
Packit aea12f
      else if (from != NULL && *fromlen == frombufsize)
Packit aea12f
        rpl_getpeername (fd, from, fromlen);
Packit aea12f
Packit aea12f
      return r;
Packit aea12f
    }
Packit aea12f
}