summaryrefslogtreecommitdiff
path: root/src/socket_info.c
blob: 14ec6b0fff5505e2b23669ed9639a0b92c270d7d (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
50
51
52
53
54
55
56
57
58
59
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/types.h>

#include "byte.h"
#include "ip.h"
#include "socket_if.h"

/**
  @file socket_info.c
  @author djb, fefe, feh
  @source ucspi-tcp6
  @brief querying local and remote info for socket
*/

int socket_local(int s, char ip[16], uint16 *port, uint32 *scope_id)
{
  struct sockaddr_in6 sa;
  unsigned int dummy = sizeof(sa);

  if (getsockname(s, (struct sockaddr *)&sa, &dummy) == -1) return -1;

  if (sa.sin6_family == AF_INET) {
    struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa;
    byte_copy(ip, 12, V4mappedprefix);
    byte_copy(ip + 12, 4, (char *)&sa4->sin_addr);
    uint16_unpack_big((char *)&sa4->sin_port, port);
    if (scope_id) *scope_id = 0;
  } else {
    byte_copy(ip, 16, (char *)&sa.sin6_addr);
    uint16_unpack_big((char *)&sa.sin6_port, port);
    if (scope_id) *scope_id = sa.sin6_scope_id;
  }

  return 0;
}

int socket_remote(int s, char ip[16], uint16 *port, uint32 *scope_id)
{
  struct sockaddr_in6 sa;
  unsigned int dummy = sizeof(sa);

  if (getpeername(s, (struct sockaddr *)&sa, &dummy) == -1) return -1;

  if (sa.sin6_family == AF_INET) {
    struct sockaddr_in *sa4 = (struct sockaddr_in *)&sa;
    byte_copy(ip, 12, V4mappedprefix);
    byte_copy(ip + 12, 4, (char *)&sa4->sin_addr);
    uint16_unpack_big((char *)&sa4->sin_port, port);
    *scope_id = 0;
  } else {
    byte_copy(ip, 16, (char *)&sa.sin6_addr);
    uint16_unpack_big((char *)&sa.sin6_port, port);
    *scope_id = sa.sin6_scope_id;
  }

  return 0;
}