__aeabi_ldivmod: fix sign logic
[minix.git] / lib / libc / sys-minix / getsockname.c
blobd6ea057877ca801a8b84051ec0c8ad1a1aaf645f
1 /*
3 getsockname()
5 from socket emulation library for Minix 2.0.x
7 */
9 #include <sys/cdefs.h>
10 #include "namespace.h"
11 #include <errno.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
18 #include <net/gen/in.h>
19 #include <net/gen/tcp.h>
20 #include <net/gen/tcp_io.h>
21 #include <net/gen/udp.h>
22 #include <sys/un.h>
25 #define DEBUG 0
28 static int _tcp_getsockname(int fd, struct sockaddr *__restrict address,
29 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
31 static int _uds_getsockname(int fd, struct sockaddr *__restrict address,
32 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
34 int getsockname(int fd, struct sockaddr *__restrict address,
35 socklen_t *__restrict address_len)
37 int r;
38 nwio_tcpconf_t tcpconf;
39 struct sockaddr_un uds_addr;
41 #ifdef DEBUG
42 fprintf(stderr,"mnx_getsockname: ioctl fd %d.\n", fd);
43 #endif
45 r= ioctl(fd, NWIOGTCPCONF, &tcpconf);
46 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
48 if (r == -1)
50 /* Bad file descriptor */
51 return -1;
54 return _tcp_getsockname(fd, address, address_len, &tcpconf);
57 r= ioctl(fd, NWIOGUDSADDR, &uds_addr);
58 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
60 if (r == -1)
62 /* Bad file descriptor */
63 return -1;
66 return _uds_getsockname(fd, address, address_len, &uds_addr);
69 #if DEBUG
70 fprintf(stderr, "getsockname: not implemented for fd %d\n", socket);
71 #endif
73 errno= ENOSYS;
74 return -1;
78 static int _tcp_getsockname(int fd, struct sockaddr *__restrict address,
79 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconf)
81 socklen_t len;
82 struct sockaddr_in sin;
84 #ifdef DEBUG1
85 fprintf(stderr, "mnx_getsockname: from %s, %u",
86 inet_ntoa(tcpconf.nwtc_remaddr),
87 ntohs(tcpconf.nwtc_remport));
88 fprintf(stderr," for %s, %u\n",
89 inet_ntoa(tcpconf.nwtc_locaddr),
90 ntohs(tcpconf.nwtc_locport));
91 #endif
93 memset(&sin, '\0', sizeof(sin));
94 sin.sin_family= AF_INET;
95 sin.sin_addr.s_addr= tcpconf->nwtc_locaddr ;
96 sin.sin_port= tcpconf->nwtc_locport;
98 len= *address_len;
99 if (len > sizeof(sin))
100 len= sizeof(sin);
101 memcpy(address, &sin, len);
102 *address_len= len;
104 return 0;
107 static int _uds_getsockname(int fd, struct sockaddr *__restrict address,
108 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
110 socklen_t len;
112 if (uds_addr->sun_family != AF_UNIX)
114 errno= EINVAL;
115 return -1;
118 len= *address_len;
119 if (len > sizeof(struct sockaddr_un))
120 len = sizeof(struct sockaddr_un);
122 memcpy(address, uds_addr, len);
123 *address_len= len;
125 return 0;