fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
socket_info.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/param.h>
3#include <sys/socket.h>
4#include <netinet/in.h>
5#include "byte.h"
6#include "socket_if.h"
7#include "ip.h"
8
16int socket_local(int s,char ip[16],uint16 *port,uint32 *scope_id)
17{
18 struct sockaddr_in6 sa;
19 unsigned int dummy = sizeof(sa);
20
21 if (getsockname(s,(struct sockaddr *)&sa,&dummy) == -1) return -1;
22
23 if (sa.sin6_family == AF_INET) {
24 struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa;
26 byte_copy(ip + 12,4,(char *)&sa4->sin_addr);
27 uint16_unpack_big((char *)&sa4->sin_port,port);
28 if (scope_id) *scope_id = 0;
29 } else {
30 byte_copy(ip,16,(char *)&sa.sin6_addr);
31 uint16_unpack_big((char *)&sa.sin6_port,port);
32 if (scope_id) *scope_id = sa.sin6_scope_id;
33 }
34
35 return 0;
36}
37
38int socket_remote(int s,char ip[16],uint16 *port,uint32 *scope_id)
39{
40 struct sockaddr_in6 sa;
41 unsigned int dummy = sizeof(sa);
42
43 if (getpeername(s,(struct sockaddr *)&sa,&dummy) == -1) return -1;
44
45 if (sa.sin6_family == AF_INET) {
46 struct sockaddr_in *sa4 = (struct sockaddr_in*)&sa;
48 byte_copy(ip + 12,4,(char *)&sa4->sin_addr);
49 uint16_unpack_big((char *)&sa4->sin_port,port);
50 *scope_id = 0;
51 } else {
52 byte_copy(ip,16,(char *)&sa.sin6_addr);
53 uint16_unpack_big((char *)&sa.sin6_port,port);
54 *scope_id = sa.sin6_scope_id;
55 }
56
57 return 0;
58}
const unsigned char V4mappedprefix[12]
Definition: socket_if.c:18
int socket_local(int s, char ip[16], uint16 *port, uint32 *scope_id)
Definition: socket_info.c:16
int socket_remote(int s, char ip[16], uint16 *port, uint32 *scope_id)
Definition: socket_info.c:38
uint16_t uint16
Definition: uint_t.h:29
uint32_t uint32
Definition: uint_t.h:40
void uint16_unpack_big(char[16], uint16 *)
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20