summaryrefslogtreecommitdiff
path: root/nacl/curvecp/socket_recv.c
blob: 8b266ba2b6b1f0c71ee2bdf9b6c43762e2384075 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include "socket.h"
#include "byte.h"

long long socket_recv(int fd,unsigned char *x,long long xlen,unsigned char *ip,unsigned char *port)
{
  struct sockaddr_in sa;
  socklen_t salen;
  int r;

  if (xlen < 0) { errno = EPROTO; return -1; }
  if (xlen > 1048576) xlen = 1048576;

  byte_zero(&sa,sizeof sa);
  salen = sizeof sa;
  r = recvfrom(fd,x,xlen,0,(struct sockaddr *) &sa,&salen);
  byte_copy(ip,4,&sa.sin_addr);
  byte_copy(port,2,&sa.sin_port);
  return r;
}