diff options
| author | Ian C <ianc@noddybox.co.uk> | 2010-11-08 15:27:41 +0000 | 
|---|---|---|
| committer | Ian C <ianc@noddybox.co.uk> | 2010-11-08 15:27:41 +0000 | 
| commit | d901c4d5078192e113542648cb49e8700db3b142 (patch) | |
| tree | 69ace887332a484ff43b02dbacf0f7c78436efcc | |
| parent | 0657049c03daec5ff88043f12d0f6ce6534942bb (diff) | |
Tidied up seekp.c
| -rw-r--r-- | seekp.c | 125 | 
1 files changed, 50 insertions, 75 deletions
| @@ -1,110 +1,85 @@  /*      Query remote host for TCP/IP socket  */ +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <string.h> +  #include <sys/types.h>  #include <sys/socket.h>  #include <netinet/in.h>  #include <netinet/tcp.h>  #include <netdb.h> -#include <stdio.h> -#include <errno.h> +static const char *name; -char	*name; +static int Connect(const char *n, int p) +{ +    static int init=0; +    static struct hostent *remote; +    static struct sockaddr_in addr; +    int sock; -int	Connect(); +    if (!init) +    { +	if (!(remote=gethostbyname(n))) +	{ +	    fprintf(stderr,"%s: unknown host %s\n",name,n); +	    exit(EXIT_FAILURE); +	} -main(argc,argv) -int argc; -char *argv[]; +	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(1); -	} +	exit(EXIT_FAILURE); +    }      setbuf(stdout,NULL); -    for(f=0;f<0x10000;f++) -	{ +    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(0); -} - - -char *GetLine() - -{ -    static char buff[1024]; -    int l; - -    if (feof(stdin)) -	return(NULL); - -    printf("> "); - -    if (!gets(buff)) -	return(NULL); - -    l=strlen(buff); - -    if (buff[l-1]=='\n') -	buff[l-1]=0; - -    if (strlen(buff)) -	return(buff); -    else -	return(GetLine()); +    return EXIT_SUCCESS;  } -int Connect(n,p) -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(1); -	    } - -	bcopy(remote->h_addr,&addr.sin_addr,remote->h_length); -	init=1; -	} - -    if ((sock=socket(AF_INET,SOCK_STREAM,0))==-1) -	{ -	perror(name); -	exit(1); -	} - -    addr.sin_family=AF_INET; -    addr.sin_port=htons(p); - -    if (connect(sock,&addr,sizeof(addr))!=-1) -	printf("%s:%d\n",n,p); - -    close(sock); -} | 
