1*433d6423SLionel Sambuc /* 2*433d6423SLionel Sambuc 3*433d6423SLionel Sambuc getsockname() 4*433d6423SLionel Sambuc 5*433d6423SLionel Sambuc from socket emulation library for Minix 2.0.x 6*433d6423SLionel Sambuc 7*433d6423SLionel Sambuc */ 8*433d6423SLionel Sambuc 9*433d6423SLionel Sambuc #include <sys/cdefs.h> 10*433d6423SLionel Sambuc #include "namespace.h" 11*433d6423SLionel Sambuc #include <errno.h> 12*433d6423SLionel Sambuc #include <stdio.h> 13*433d6423SLionel Sambuc #include <string.h> 14*433d6423SLionel Sambuc #include <sys/ioctl.h> 15*433d6423SLionel Sambuc #include <sys/socket.h> 16*433d6423SLionel Sambuc #include <netinet/in.h> 17*433d6423SLionel Sambuc 18*433d6423SLionel Sambuc #include <net/gen/in.h> 19*433d6423SLionel Sambuc #include <net/gen/tcp.h> 20*433d6423SLionel Sambuc #include <net/gen/tcp_io.h> 21*433d6423SLionel Sambuc #include <net/gen/udp.h> 22*433d6423SLionel Sambuc #include <net/gen/udp_io.h> 23*433d6423SLionel Sambuc #include <sys/un.h> 24*433d6423SLionel Sambuc 25*433d6423SLionel Sambuc /* 26*433d6423SLionel Sambuc #define DEBUG 0 27*433d6423SLionel Sambuc */ 28*433d6423SLionel Sambuc 29*433d6423SLionel Sambuc static int _tcp_getsockname(int fd, struct sockaddr *__restrict address, 30*433d6423SLionel Sambuc socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp); 31*433d6423SLionel Sambuc 32*433d6423SLionel Sambuc static int _udp_getsockname(int fd, struct sockaddr *__restrict address, 33*433d6423SLionel Sambuc socklen_t *__restrict address_len, nwio_udpopt_t *udpopt); 34*433d6423SLionel Sambuc 35*433d6423SLionel Sambuc static int _uds_getsockname(int fd, struct sockaddr *__restrict address, 36*433d6423SLionel Sambuc socklen_t *__restrict address_len, struct sockaddr_un *uds_addr); 37*433d6423SLionel Sambuc 38*433d6423SLionel Sambuc int getsockname(int fd, struct sockaddr *__restrict address, 39*433d6423SLionel Sambuc socklen_t *__restrict address_len) 40*433d6423SLionel Sambuc { 41*433d6423SLionel Sambuc int r; 42*433d6423SLionel Sambuc nwio_tcpconf_t tcpconf; 43*433d6423SLionel Sambuc nwio_udpopt_t udpopt; 44*433d6423SLionel Sambuc struct sockaddr_un uds_addr; 45*433d6423SLionel Sambuc 46*433d6423SLionel Sambuc #ifdef DEBUG 47*433d6423SLionel Sambuc fprintf(stderr,"mnx_getsockname: ioctl fd %d.\n", fd); 48*433d6423SLionel Sambuc #endif 49*433d6423SLionel Sambuc 50*433d6423SLionel Sambuc r= ioctl(fd, NWIOGTCPCONF, &tcpconf); 51*433d6423SLionel Sambuc if (r != -1 || errno != ENOTTY) 52*433d6423SLionel Sambuc { 53*433d6423SLionel Sambuc if (r == -1) 54*433d6423SLionel Sambuc { 55*433d6423SLionel Sambuc /* Bad file descriptor */ 56*433d6423SLionel Sambuc return -1; 57*433d6423SLionel Sambuc } 58*433d6423SLionel Sambuc 59*433d6423SLionel Sambuc return _tcp_getsockname(fd, address, address_len, &tcpconf); 60*433d6423SLionel Sambuc } 61*433d6423SLionel Sambuc 62*433d6423SLionel Sambuc r= ioctl(fd, NWIOGUDPOPT, &udpopt); 63*433d6423SLionel Sambuc if (r != -1 || errno != ENOTTY) 64*433d6423SLionel Sambuc { 65*433d6423SLionel Sambuc if (r == -1) 66*433d6423SLionel Sambuc { 67*433d6423SLionel Sambuc /* Bad file descriptor */ 68*433d6423SLionel Sambuc return -1; 69*433d6423SLionel Sambuc } 70*433d6423SLionel Sambuc 71*433d6423SLionel Sambuc return _udp_getsockname(fd, address, address_len, &udpopt); 72*433d6423SLionel Sambuc } 73*433d6423SLionel Sambuc 74*433d6423SLionel Sambuc r= ioctl(fd, NWIOGUDSADDR, &uds_addr); 75*433d6423SLionel Sambuc if (r != -1 || errno != ENOTTY) 76*433d6423SLionel Sambuc { 77*433d6423SLionel Sambuc if (r == -1) 78*433d6423SLionel Sambuc { 79*433d6423SLionel Sambuc /* Bad file descriptor */ 80*433d6423SLionel Sambuc return -1; 81*433d6423SLionel Sambuc } 82*433d6423SLionel Sambuc 83*433d6423SLionel Sambuc return _uds_getsockname(fd, address, address_len, &uds_addr); 84*433d6423SLionel Sambuc } 85*433d6423SLionel Sambuc 86*433d6423SLionel Sambuc #if DEBUG 87*433d6423SLionel Sambuc fprintf(stderr, "getsockname: not implemented for fd %d\n", socket); 88*433d6423SLionel Sambuc #endif 89*433d6423SLionel Sambuc 90*433d6423SLionel Sambuc errno= ENOSYS; 91*433d6423SLionel Sambuc return -1; 92*433d6423SLionel Sambuc } 93*433d6423SLionel Sambuc 94*433d6423SLionel Sambuc 95*433d6423SLionel Sambuc static int _tcp_getsockname(int fd, struct sockaddr *__restrict address, 96*433d6423SLionel Sambuc socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconf) 97*433d6423SLionel Sambuc { 98*433d6423SLionel Sambuc socklen_t len; 99*433d6423SLionel Sambuc struct sockaddr_in sin; 100*433d6423SLionel Sambuc 101*433d6423SLionel Sambuc #ifdef DEBUG1 102*433d6423SLionel Sambuc fprintf(stderr, "mnx_getsockname: from %s, %u", 103*433d6423SLionel Sambuc inet_ntoa(tcpconf->nwtc_remaddr), 104*433d6423SLionel Sambuc ntohs(tcpconf->nwtc_remport)); 105*433d6423SLionel Sambuc fprintf(stderr," for %s, %u\n", 106*433d6423SLionel Sambuc inet_ntoa(tcpconf->nwtc_locaddr), 107*433d6423SLionel Sambuc ntohs(tcpconf->nwtc_locport)); 108*433d6423SLionel Sambuc #endif 109*433d6423SLionel Sambuc 110*433d6423SLionel Sambuc memset(&sin, '\0', sizeof(sin)); 111*433d6423SLionel Sambuc sin.sin_family= AF_INET; 112*433d6423SLionel Sambuc sin.sin_addr.s_addr= tcpconf->nwtc_locaddr ; 113*433d6423SLionel Sambuc sin.sin_port= tcpconf->nwtc_locport; 114*433d6423SLionel Sambuc sin.sin_len= sizeof(sin); 115*433d6423SLionel Sambuc 116*433d6423SLionel Sambuc len= *address_len; 117*433d6423SLionel Sambuc if (len > sizeof(sin)) 118*433d6423SLionel Sambuc len= sizeof(sin); 119*433d6423SLionel Sambuc memcpy(address, &sin, len); 120*433d6423SLionel Sambuc *address_len= len; 121*433d6423SLionel Sambuc 122*433d6423SLionel Sambuc return 0; 123*433d6423SLionel Sambuc } 124*433d6423SLionel Sambuc 125*433d6423SLionel Sambuc static int _udp_getsockname(int fd, struct sockaddr *__restrict address, 126*433d6423SLionel Sambuc socklen_t *__restrict address_len, nwio_udpopt_t *udpopt) 127*433d6423SLionel Sambuc { 128*433d6423SLionel Sambuc socklen_t len; 129*433d6423SLionel Sambuc struct sockaddr_in sin; 130*433d6423SLionel Sambuc 131*433d6423SLionel Sambuc #ifdef DEBUG1 132*433d6423SLionel Sambuc fprintf(stderr, "mnx_getsockname: from %s, %u", 133*433d6423SLionel Sambuc inet_ntoa(udpopt->nwuo_remaddr), 134*433d6423SLionel Sambuc ntohs(udpopt->nwuo_remport)); 135*433d6423SLionel Sambuc fprintf(stderr," for %s, %u\n", 136*433d6423SLionel Sambuc inet_ntoa(udpopt->nwuo_locaddr), 137*433d6423SLionel Sambuc ntohs(udpopt->nwuo_locport)); 138*433d6423SLionel Sambuc #endif 139*433d6423SLionel Sambuc 140*433d6423SLionel Sambuc memset(&sin, '\0', sizeof(sin)); 141*433d6423SLionel Sambuc sin.sin_family= AF_INET; 142*433d6423SLionel Sambuc sin.sin_addr.s_addr= udpopt->nwuo_locaddr ; 143*433d6423SLionel Sambuc sin.sin_port= udpopt->nwuo_locport; 144*433d6423SLionel Sambuc sin.sin_len= sizeof(sin); 145*433d6423SLionel Sambuc 146*433d6423SLionel Sambuc len= *address_len; 147*433d6423SLionel Sambuc if (len > sizeof(sin)) 148*433d6423SLionel Sambuc len= sizeof(sin); 149*433d6423SLionel Sambuc memcpy(address, &sin, len); 150*433d6423SLionel Sambuc *address_len= len; 151*433d6423SLionel Sambuc 152*433d6423SLionel Sambuc return 0; 153*433d6423SLionel Sambuc } 154*433d6423SLionel Sambuc 155*433d6423SLionel Sambuc static int _uds_getsockname(int fd, struct sockaddr *__restrict address, 156*433d6423SLionel Sambuc socklen_t *__restrict address_len, struct sockaddr_un *uds_addr) 157*433d6423SLionel Sambuc { 158*433d6423SLionel Sambuc socklen_t len; 159*433d6423SLionel Sambuc 160*433d6423SLionel Sambuc if (uds_addr->sun_family != AF_UNIX) 161*433d6423SLionel Sambuc { 162*433d6423SLionel Sambuc errno= EINVAL; 163*433d6423SLionel Sambuc return -1; 164*433d6423SLionel Sambuc } 165*433d6423SLionel Sambuc 166*433d6423SLionel Sambuc len= *address_len; 167*433d6423SLionel Sambuc if (len > sizeof(struct sockaddr_un)) 168*433d6423SLionel Sambuc len = sizeof(struct sockaddr_un); 169*433d6423SLionel Sambuc 170*433d6423SLionel Sambuc memcpy(address, uds_addr, len); 171*433d6423SLionel Sambuc *address_len= len; 172*433d6423SLionel Sambuc 173*433d6423SLionel Sambuc return 0; 174*433d6423SLionel Sambuc } 175