summaryrefslogtreecommitdiff
path: root/sock.c
blob: 778f832b48d866bceb83f93425b5839c68eb57c5 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
    Reader/writer to TCP/IP socket
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.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 const char *GetLine(void)
{
    static char buff[1024];
    int l;

    if (feof(stdin))
    {
	return(NULL);
    }

    printf("> ");

    if (!fgets(buff, sizeof buff, stdin))
    {
	return(NULL);
    }

    l=strlen(buff);

    if (l > 0 && buff[l-1]=='\n')
    {
	buff[l-1]=0;
    }

    if (strlen(buff))
    {
	return buff;
    }
    else
    {
	return GetLine();
    }
}


static int Connect(const char *n, const char *p)
{
    struct addrinfo *addr, *addr_p;
    struct addrinfo hints = {0};
    int status;
    int sock = -1;

    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;

    status = getaddrinfo(n, p, &hints, &addr);

    if (status)
    {
	fprintf(stderr, "%s: %s\n", name, gai_strerror(status));
	exit(EXIT_FAILURE);
    }

    for(addr_p = addr; addr_p && sock == -1; addr_p = addr_p->ai_next)
    {
        sock = socket(addr_p->ai_family,
                      addr_p->ai_socktype,
                      addr_p->ai_protocol);

        if (sock != -1)
        {
            if (connect(sock, addr_p->ai_addr, addr_p->ai_addrlen) == -1)
            {
                close(sock);
                sock = -1;
            }
        }
    }

    if (sock == -1)
    {
        fprintf(stderr, "%s: Failed to connect to %s:%s\n", name, n, p);
        exit(EXIT_FAILURE);
    }

    return sock;
}

int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    int addrlen;
    const char *p;
    char buff[1024];
    int len;
    int sock;
    int no_read;
    int no_write;

    name=argv[0];

    if (argc<3)
    {
	fprintf(stderr,"%s: usage %s host port [nowrite|noread]\n",name,name);
	exit(EXIT_FAILURE);
    }

    if (argc > 3)
    {
    	no_read = strcmp(argv[3], "noread") == 0;
    	no_write = strcmp(argv[3], "nowrite") == 0;
    }
    else
    {
    	no_read = 0;
    	no_write = 0;
    }

    sock = Connect(argv[1],argv[2]);

    /* Test to see how to get the connected local port number
    */
    addrlen=sizeof(addr);

    if (getsockname(sock,(void*)&addr,&addrlen)!=0)
    {
	perror(name);
    }

    printf("%s: bound through port %d\n",name,ntohs(addr.sin_port));

    while(no_write || (p=GetLine()))
    {
	if (!no_write && (write(sock,p,strlen(p))==-1))
	{
	    perror(name);
	    close(sock);
	    exit(EXIT_FAILURE);
	}

	if (!no_read)
	{
	    if ((len=read(sock,buff,1024))<=0)
	    {
		perror(name);
		close(sock);
		exit(EXIT_FAILURE);
	    }

	    buff[len]=0;
	    printf("%s\n",buff);
	}
    }

    close(sock);
    printf("\n");

    return EXIT_SUCCESS;
}