_sock_ingetaddr.c 843 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. /* bsd extensions */
  10. #include <sys/uio.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <sys/un.h>
  14. #include "priv.h"
  15. void
  16. _sock_ingetaddr(Rock *r, struct sockaddr_in *ip, int *alen, char *a)
  17. {
  18. int n, fd;
  19. char *p;
  20. char name[Ctlsize];
  21. /* get remote address */
  22. strcpy(name, r->ctl);
  23. p = strrchr(name, '/');
  24. strcpy(p+1, a);
  25. fd = open(name, O_RDONLY);
  26. if(fd >= 0){
  27. n = read(fd, name, sizeof(name)-1);
  28. if(n > 0){
  29. name[n] = 0;
  30. p = strchr(name, '!');
  31. if(p){
  32. *p++ = 0;
  33. ip->sin_family = AF_INET;
  34. ip->sin_port = htons(atoi(p));
  35. ip->sin_addr.s_addr = inet_addr(name);
  36. if(alen)
  37. *alen = sizeof(struct sockaddr_in);
  38. }
  39. }
  40. close(fd);
  41. }
  42. }