support little-endian bitmaps
[tridge/junkcode.git] / socklog.c
1 #include <unistd.h>
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <netdb.h>
7 #include <syslog.h>
8
9 /*******************************************************************
10  return the IP addr of the client as a string 
11  ******************************************************************/
12 char *client_addr(int fd)
13 {
14         struct sockaddr sa;
15         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
16         int     length = sizeof(sa);
17         static char addr_buf[100];
18         static int initialised;
19
20         if (initialised) return addr_buf;
21
22         initialised = 1;
23
24         if (getpeername(fd, &sa, &length)) {
25                 exit(1);
26         }
27         
28         strncpy(addr_buf,
29                 (char *)inet_ntoa(sockin->sin_addr), sizeof(addr_buf));
30         addr_buf[sizeof(addr_buf)-1] = 0;
31
32         return addr_buf;
33 }
34
35
36 /*******************************************************************
37  return the DNS name of the client 
38  ******************************************************************/
39 char *client_name(int fd)
40 {
41         struct sockaddr sa;
42         struct sockaddr_in *sockin = (struct sockaddr_in *) (&sa);
43         int     length = sizeof(sa);
44         static char name_buf[100];
45         struct hostent *hp;
46         char **p;
47         char *def = "UNKNOWN";
48         static int initialised;
49
50         if (initialised) return name_buf;
51
52         initialised = 1;
53
54         strcpy(name_buf,def);
55
56         if (getpeername(fd, &sa, &length)) {
57                 exit(1);
58         }
59
60         /* Look up the remote host name. */
61         if ((hp = gethostbyaddr((char *) &sockin->sin_addr,
62                                 sizeof(sockin->sin_addr),
63                                 AF_INET))) {
64                 
65                 strncpy(name_buf,(char *)hp->h_name,sizeof(name_buf));
66                 name_buf[sizeof(name_buf)-1] = 0;
67         }
68
69
70         /* do a forward lookup as well to prevent spoofing */
71         hp = gethostbyname(name_buf);
72         if (!hp) {
73                 strcpy(name_buf,def);
74         } else {
75                 for (p=hp->h_addr_list;*p;p++) {
76                         if (memcmp(*p, &sockin->sin_addr, hp->h_length) == 0) {
77                                 break;
78                         }
79                 }
80                 if (!*p) {
81                         strcpy(name_buf,def);
82                 } 
83         }
84
85         return name_buf;
86 }
87
88
89 int main(int argc, char *argv[])
90 {
91         char *service = argv[1];
92
93         openlog("socklog", LOG_PID, LOG_DAEMON);
94         syslog(LOG_ERR, "%s connection from %s [%s]", 
95                service, client_name(0), client_addr(0));
96         exit(0);
97 }