socket_recv.c 560 B

1234567891011121314151617181920212223
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <errno.h>
  5. #include "socket.h"
  6. #include "byte.h"
  7. long long socket_recv(int fd,unsigned char *x,long long xlen,unsigned char *ip,unsigned char *port)
  8. {
  9. struct sockaddr_in sa;
  10. socklen_t salen;
  11. int r;
  12. if (xlen < 0) { errno = EPROTO; return -1; }
  13. if (xlen > 1048576) xlen = 1048576;
  14. byte_zero(&sa,sizeof sa);
  15. salen = sizeof sa;
  16. r = recvfrom(fd,x,xlen,0,(struct sockaddr *) &sa,&salen);
  17. byte_copy(ip,4,&sa.sin_addr);
  18. byte_copy(port,2,&sa.sin_port);
  19. return r;
  20. }