some coverity fixes.
[minix.git] / lib / libc / sys-minix / getpeername.c
blobd0487248a9da6ee0347012274c962061b2f15aa9
1 #include <sys/cdefs.h>
2 #include "namespace.h"
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <sys/ioctl.h>
8 #include <sys/socket.h>
9 #include <netinet/in.h>
11 #include <net/gen/in.h>
12 #include <net/gen/tcp.h>
13 #include <net/gen/tcp_io.h>
14 #include <net/gen/udp.h>
15 #include <sys/un.h>
17 #define DEBUG 0
19 static int _tcp_getpeername(int sock, struct sockaddr *__restrict address,
20 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp);
22 static int _uds_getpeername(int sock, struct sockaddr *__restrict address,
23 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr);
25 int getpeername(int sock, struct sockaddr *__restrict address,
26 socklen_t *__restrict address_len)
28 int r;
29 nwio_tcpconf_t tcpconf;
30 struct sockaddr_un uds_addr;
32 r= ioctl(sock, NWIOGTCPCONF, &tcpconf);
33 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
35 if (r == -1)
37 /* Bad file descriptor */
38 return -1;
40 return _tcp_getpeername(sock, address, address_len,
41 &tcpconf);
44 r= ioctl(sock, NWIOGUDSPADDR, &uds_addr);
45 if (r != -1 || (errno != ENOTTY && errno != EBADIOCTL))
47 if (r == -1)
49 /* Bad file descriptor */
50 return -1;
52 return _uds_getpeername(sock, address, address_len,
53 &uds_addr);
57 #if DEBUG
58 fprintf(stderr, "getpeername: not implemented for fd %d\n", sock);
59 #endif
60 errno= ENOSYS;
61 return -1;
64 static int _tcp_getpeername(int sock, struct sockaddr *__restrict address,
65 socklen_t *__restrict address_len, nwio_tcpconf_t *tcpconfp)
67 socklen_t len;
68 struct sockaddr_in sin;
70 if (tcpconfp->nwtc_remaddr == 0 ||
71 tcpconfp->nwtc_remport == 0)
73 errno= ENOTCONN;
74 return -1;
77 memset(&sin, '\0', sizeof(sin));
78 sin.sin_family= AF_INET;
79 sin.sin_addr.s_addr= tcpconfp->nwtc_remaddr;
80 sin.sin_port= tcpconfp->nwtc_remport;
82 len= *address_len;
83 if (len > sizeof(sin))
84 len= sizeof(sin);
85 memcpy(address, &sin, len);
86 *address_len= len;
88 return 0;
91 static int _uds_getpeername(int sock, struct sockaddr *__restrict address,
92 socklen_t *__restrict address_len, struct sockaddr_un *uds_addr)
94 socklen_t len;
96 if (uds_addr->sun_family != AF_UNIX)
98 errno= ENOTCONN;
99 return -1;
102 len= *address_len;
103 if (len > sizeof(struct sockaddr_un))
104 len = sizeof(struct sockaddr_un);
106 memcpy(address, uds_addr, len);
107 *address_len= len;
109 return 0;