added latency
[tridge/junkcode.git] / issamba.c
1 #include <netinet/in.h>
2 #include <netdb.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <errno.h>
8
9
10 static int open_socket_out(char *host, int port)
11 {
12         struct sockaddr_in sock_out;
13         int res;
14         struct hostent *hp;
15
16         res = socket(PF_INET, SOCK_STREAM, 0);
17
18         hp = gethostbyname(host);
19         if (!hp) {
20                 fprintf(stderr,"unknown host: %s\n", host);
21                 exit(1);
22         }
23
24         memcpy(&sock_out.sin_addr, hp->h_addr, hp->h_length);
25         sock_out.sin_port = htons(port);
26         sock_out.sin_family = PF_INET;
27
28         if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
29                 fprintf(stderr, "failed to connect to %s - %s\n", host, strerror(errno));
30                 exit(1);
31         }
32
33         return res;
34 }
35
36 int main(int argc, char *argv[])
37 {
38         int port = 139;
39         char *host;
40         unsigned char buf[4];
41         int sock;
42
43         if (argc < 2) {
44                 printf("usage: issamba <host>\n");
45                 exit(1);
46         }
47
48         host = argv[1];
49         bzero(buf, 4);
50         buf[0] = 0x89;
51
52         sock = open_socket_out(host, port);
53
54         if (write(sock, buf, 4) != 4) {
55                 fprintf(stderr,"Failed to send request\n");
56                 exit(1);
57         }
58
59         bzero(buf, 4);
60         if (read(sock, buf, 4) != 4) {
61                 fprintf(stderr,"Failed to recv request\n");
62                 exit(1);
63         }
64
65         if (buf[0] == 0x85) {
66                 printf("%s is a Samba server\n", host);
67         } else {
68                 printf("%s is not a Samba server (0x%02x)\n", host, buf[0]);
69         }
70         return 0;
71 }