summaryrefslogtreecommitdiff
path: root/seekp.c
blob: d94f1b568a18d2331095c1ab36f7176f73f8542a (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
    Query remote host for TCP/IP socket
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>

static const char *name;

static void Connect(const char *n, int p)
{
    static int init=0;
    static struct hostent *remote;
    static struct sockaddr_in addr;
    int sock;

    if (!init)
    {
	if (!(remote=gethostbyname(n)))
	{
	    fprintf(stderr,"%s: unknown host %s\n",name,n);
	    exit(EXIT_FAILURE);
	}

	memcpy(&addr.sin_addr, remote->h_addr, remote->h_length);
	init=1;
    }

    if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
	perror(name);
	exit(EXIT_FAILURE);
    }

    addr.sin_family=AF_INET;
    addr.sin_port=htons(p);

    if (connect(sock, (void *)&addr, sizeof(addr))!=-1)
    {
	printf("%s:%d\n",n,p);
    }

    close(sock);
}

int main(int argc, char *argv[])
{
    int f;

    name=argv[0];

    if (argc<2)
    {
	fprintf(stderr,"%s: usage %s host\n",name,name);
	exit(EXIT_FAILURE);
    }

    setbuf(stdout,NULL);

    for(f=1;f<0x10000;f++)
    {
	if (argc>2)
	{
	    printf("Trying %s:%d...\n",argv[1],f);
	}

	Connect(argv[1],f);

	if ((argc==2)&&(f)&&((f%10000)==0))
	{
	    fprintf(stderr,"Tried up to %s:%d\n",argv[1],f);
	}
    }

    return EXIT_SUCCESS;
}