blob: e4dd3109cb601e275b6854c1ce649100486fa363 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/* posix */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
/* bsd extensions */
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include "priv.h"
int
getpeername(int fd, struct sockaddr *addr, int *alen)
{
Rock *r;
int i;
struct sockaddr_in *rip;
struct sockaddr_un *runix;
r = _sock_findrock(fd, 0);
if(r == 0){
errno = ENOTSOCK;
return -1;
}
switch(r->domain){
case PF_INET:
rip = (struct sockaddr_in*)&r->raddr;
memmove(addr, rip, sizeof(struct sockaddr_in));
*alen = sizeof(struct sockaddr_in);
break;
case PF_UNIX:
runix = (struct sockaddr_un*)&r->raddr;
i = &runix->sun_path[strlen(runix->sun_path)] - (char*)runix;
memmove(addr, runix, i);
*alen = i;
break;
default:
errno = EAFNOSUPPORT;
return -1;
}
return 0;
}
|