socket_send.c 522 B

12345678910111213141516171819
  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. int socket_send(int fd,const unsigned char *x,long long xlen,const unsigned char *ip,const unsigned char *port)
  8. {
  9. struct sockaddr_in sa;
  10. if (xlen < 0 || xlen > 1048576) { errno = EPROTO; return -1; }
  11. byte_zero(&sa,sizeof sa);
  12. sa.sin_family = AF_INET;
  13. byte_copy(&sa.sin_addr,4,ip);
  14. byte_copy(&sa.sin_port,2,port);
  15. return sendto(fd,x,xlen,0,(struct sockaddr *) &sa,sizeof sa);
  16. }