summaryrefslogtreecommitdiff
path: root/serv.c
blob: 093cc1231ec4ec371eb42798fd19b3e328134b07 (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
/* A simple server to attach to
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>

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

#define DUMP_WIDTH 16

static const char *name;

static int Socket(int p)
{
    struct sockaddr_in addr;
    int sock_fd;

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

    /* Bind port to address
    */
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=INADDR_ANY;
    addr.sin_port=htons(p);

    if (bind(sock_fd,(void*)&addr,sizeof(addr))==-1)
    {
	perror("bind");
	exit(EXIT_FAILURE);
    }

    if (listen(sock_fd,5)==-1)
    {
	perror("listen");
	exit(EXIT_FAILURE);
    }

    return sock_fd;
}

static void Dump(const char *p, int len)
{
    char asc[DUMP_WIDTH + 1] = {0};
    unsigned int off = 0;
    int col = 0;

    while (len > 0)
    {
	unsigned char c = *p;

	if (col == 0)
	{
	    printf("%4.4x:", off);
	}

	printf(" %2.2x", (unsigned int)c);

    	asc[col++] = isprint(c) ? c : '.';

	if (col == DUMP_WIDTH)
	{
	    printf("    %s\n", asc);
	    memset(asc, 0, sizeof asc);
	    col = 0;
	}

	off++;
	p++;
	len--;
    }

    if (col > 0)
    {
	while (col++ < DUMP_WIDTH)
	{
	    printf(" **");
	}

	printf("    %s\n", asc);
    }
}

int main(int argc, char *argv[])
{
    struct sockaddr_in addr;
    char buff[1024];
    int len;
    int addrlen;
    int sock_fd;
    int connect_fd;

    name=argv[0];

    if (argc!=2)
    {
	fprintf(stderr,"%s: usage %s port\n", name, name);
	exit(1);
    }

    sock_fd = Socket((short)atoi(argv[1]));

    printf("%s: socket bound\n",name);

    while(1)
    {
	printf("%s: accepting\n",name);

	if ((connect_fd=accept(sock_fd,NULL,0))==-1)
	{
	    perror("accept");
	    exit(1);
	}

	addrlen=sizeof(addr);

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

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

	addrlen=sizeof(addr);

	if (getpeername(connect_fd,(void*)&addr,&addrlen)!=0)
	{
	    perror("getpeername");
	}

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

	while((len=read(connect_fd,buff,1024))>0)
	{
	    buff[len]=0;
	    printf("%s: recieved\n",name);
	    Dump(buff, len);
	}

	if (len == -1)
	{
	    perror("read");
	}

	close(connect_fd);
    }

    return EXIT_SUCCESS;
}