summaryrefslogtreecommitdiff
path: root/src/bin/tcpident.c
blob: 1af3bc17c2f61fe2db4dc2106ceefae0009483b1 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//this program was written to be used on NetBSD. YMMV.
#include <string.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/sysctl.h>

#include <netinet/in.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcp_timer.h>
#include <netinet/tcp_var.h>

#include <arpa/inet.h>

#include <netdb.h>
#include <stdio.h>


//ripped from NetBSD's identd.c (found mine in /usr/src/libexec/identd/identd.c)
static int
sysctl_getuid(struct sockaddr_storage *ss, socklen_t len, uid_t *uid)
{
        int mib[4];
        uid_t myuid;
        size_t uidlen;

        uidlen = sizeof(myuid);

        mib[0] = CTL_NET;
        mib[1] = ss->ss_family;
        mib[2] = IPPROTO_TCP;
        mib[3] = TCPCTL_IDENT;

        if (sysctl(mib, sizeof(mib)/ sizeof(int), &myuid, &uidlen, ss, len) < 0)
                return -1;
        *uid = myuid;

        return 0;
}

//for debugging
void dump_sockaddr(struct sockaddr_in *sin,int len) {
 unsigned char *p=(void *)sin;
 for(;len;len--,p++) {
  printf("%02x ",*p);
 }
 printf("\n");
}

int main(int argc,char *argv[]) {
 uid_t myuid=-1;
 int len=sizeof(struct sockaddr_storage);
 struct sockaddr_storage mine[2];

//future IPv6 support?
 struct sockaddr_in *inA=(struct sockaddr_in *)(&mine[0]);
// struct sockaddr_in6 *in6A=(struct sockaddr_in6 *)(&mine[0]);
 struct sockaddr_in *inB=(struct sockaddr_in *)(&mine[1]);
// struct sockaddr_in6 *in6B=(struct sockaddr_in6 *)(&mine[1]);
 if(argc <= 4) return -2;
 memset(inA,0,len);
 memset(inB,0,len);
 inA->sin_len=16;
 inB->sin_len=16;
 inA->sin_family=AF_INET;
 inB->sin_family=AF_INET;
 inA->sin_addr.s_addr=(inet_addr(argv[1]));
 inB->sin_addr.s_addr=(inet_addr(argv[3]));
 inA->sin_port=htons(atoi(argv[2]));
 inB->sin_port=htons(atoi(argv[4]));
//these were to see what real sockaddr looked like.
// getpeername(0,inB,&len);
// getsockname(0,inA,&len);
// dump_sockaddr(inA,len);
// dump_sockaddr(inB,len);

 if(sysctl_getuid(mine,sizeof(mine),&myuid) == -1) return -1;
 printf("%d\n",myuid);
 return 0;
}