Skip to content

Commit 2ec26e4

Browse files
TCP request to some port (here: 12489)
1 parent 502b25e commit 2ec26e4

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

sockets/TCP/nsclient.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <error.h>
2+
#include <unistd.h>
3+
#include <sys/types.h>
4+
#include <sys/utsname.h>
5+
#include <sys/socket.h>
6+
#include <netinet/in.h>
7+
#include <netdb.h>
8+
#include <string.h>
9+
#include <errno.h>
10+
#include <stdlib.h>
11+
#include <stdio.h>
12+
13+
#define fatal(msg) {perror(msg);}
14+
15+
int nsclient(char *host)
16+
{
17+
int sock;
18+
struct sockaddr_in servadd;
19+
struct hostent *hp;
20+
char nodename[200], servname[200];
21+
22+
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
23+
fatal("socket");
24+
25+
bzero(&servadd, sizeof(servadd));
26+
27+
if ((hp = gethostbyname(host)) == NULL)
28+
fatal("gethostbyname");
29+
30+
servadd.sin_port = htons(12489);
31+
servadd.sin_family = AF_INET;
32+
bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
33+
34+
int status;
35+
if ((status = connect(sock, (struct sockaddr *)&servadd, sizeof(servadd))) != 0)
36+
if(errno != ECONNREFUSED)
37+
fatal("connect");
38+
39+
getnameinfo((struct sockaddr *)&servadd, sizeof(servadd),
40+
nodename, sizeof(nodename), servname, sizeof(servname), 0);
41+
42+
close(sock);
43+
return status;
44+
}
45+
46+
void usage()
47+
{
48+
printf("USAGE:\t scan <IP_address>\n");
49+
exit(1);
50+
}
51+
52+
int main(int argc, char *argv[])
53+
{
54+
if(argv[1] == NULL)
55+
usage();
56+
57+
if (nsclient(argv[1]) == 0)
58+
printf("nsclient: running\n");
59+
else
60+
printf("nsclient: no_answer\n");
61+
62+
return 0;
63+
}
64+
65+

0 commit comments

Comments
 (0)