/lib/libc/sys-minix/getsockname.c

http://www.minix3.org/ · C · 126 lines · 90 code · 24 blank · 12 comment · 20 complexity · 41bee0a92f0b11218e370b152af60f93 MD5 · raw file

  1. /*
  2. getsockname()
  3. from socket emulation library for Minix 2.0.x
  4. */
  5. #include <sys/cdefs.h>
  6. #include "namespace.h"
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <net/gen/in.h>
  14. #include <net/gen/tcp.h>
  15. #include <net/gen/tcp_io.h>
  16. #include <net/gen/udp.h>
  17. #include <sys/un.h>
  18. /*
  19. #define DEBUG 0
  20. */
  21. static int _tcp_getsockname(int fd, struct sockaddr *__restrict address,
  22. socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
  23. static int _uds_getsockname(int fd, struct sockaddr *__restrict address,
  24. socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
  25. int getsockname(int fd, struct sockaddr *__restrict address,
  26. socklen_t *__restrict address_len)
  27. {
  28. int r;
  29. nwio_tcpconf_t tcpconf;
  30. struct sockaddr_un uds_addr;
  31. #ifdef DEBUG
  32. fprintf(stderr,"mnx_getsockname: ioctl fd %d.\n", fd);
  33. #endif
  34. r= ioctl(fd, NWIOGTCPCONF, &tcpconf);
  35. if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
  36. {
  37. if (r == -1)
  38. {
  39. /* Bad file descriptor */
  40. return -1;
  41. }
  42. return _tcp_getsockname(fd, address, address_len, &tcpconf);
  43. }
  44. r= ioctl(fd, NWIOGUDSADDR, &uds_addr);
  45. if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
  46. {
  47. if (r == -1)
  48. {
  49. /* Bad file descriptor */
  50. return -1;
  51. }
  52. return _uds_getsockname(fd, address, address_len, &uds_addr);
  53. }
  54. #if DEBUG
  55. fprintf(stderr, "getsockname: not implemented for fd %d\n", socket);
  56. #endif
  57. errno= ENOSYS;
  58. return -1;
  59. }
  60. static int _tcp_getsockname(int fd, struct sockaddr *__restrict address,
  61. socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconf)
  62. {
  63. socklen_t len;
  64. struct sockaddr_in sin;
  65. #ifdef DEBUG1
  66. fprintf(stderr, "mnx_getsockname: from %s, %u",
  67. inet_ntoa(tcpconf.nwtc_remaddr),
  68. ntohs(tcpconf.nwtc_remport));
  69. fprintf(stderr," for %s, %u\n",
  70. inet_ntoa(tcpconf.nwtc_locaddr),
  71. ntohs(tcpconf.nwtc_locport));
  72. #endif
  73. memset(&sin, '\0', sizeof(sin));
  74. sin.sin_family= AF_INET;
  75. sin.sin_addr.s_addr= tcpconf->nwtc_locaddr ;
  76. sin.sin_port= tcpconf->nwtc_locport;
  77. len= *address_len;
  78. if (len > sizeof(sin))
  79. len= sizeof(sin);
  80. memcpy(address, &sin, len);
  81. *address_len= len;
  82. return 0;
  83. }
  84. static int _uds_getsockname(int fd, struct sockaddr *__restrict address,
  85. socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
  86. {
  87. socklen_t len;
  88. if (uds_addr->sun_family != AF_UNIX)
  89. {
  90. errno= EINVAL;
  91. return -1;
  92. }
  93. len= *address_len;
  94. if (len > sizeof(struct sockaddr_un))
  95. len = sizeof(struct sockaddr_un);
  96. memcpy(address, uds_addr, len);
  97. *address_len= len;
  98. return 0;
  99. }