~ubuntu-branches/ubuntu/wily/maradns/wily-proposed

« back to all changes in this revision

Viewing changes to deadwood-3.2.05/sqa/sqa_tcp_buffering/send_packet_stdin.c

  • Committer: Package Import Robot
  • Author(s): Dariusz Dwornikowski, Tomasz Buchert, Dariusz Dwornikowski
  • Date: 2015-03-27 18:34:08 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20150327183408-wnfachdkdjt96yu6
Tags: 2.0.11-1
[ Tomasz Buchert ]
* Imported Upstream version 2.0.11

[ Dariusz Dwornikowski ]
* d/patches: 
  - refreshed all patches for new deadwood version
  - removed generating of random prime on build (Closes: #785536) 
* d/rules: date taken from changelog (Closes: #785535)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2009 Sam Trenholme
2
 
 *
3
 
 * TERMS
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions
7
 
 * are met:
8
 
 *
9
 
 * 1. Redistributions of source code must retain the above copyright
10
 
 *    notice, this list of conditions and the following disclaimer.
11
 
 * 2. Redistributions in binary form must reproduce the above copyright
12
 
 *    notice, this list of conditions and the following disclaimer in the
13
 
 *    documentation and/or other materials provided with the distribution.
14
 
 *
15
 
 * This software is provided 'as is' with no guarantees of correctness or
16
 
 * fitness for purpose.
17
 
 */
18
 
 
19
 
/* This is a simple DNS-over-TCP server that sends a DNS packet from
20
 
 * stdin over port 53 TCP (the first two bytes of the packet are the
21
 
 * length header), then outputs on stdout the received packet */
22
 
 
23
 
#include <stdint.h>
24
 
#include <stdlib.h>
25
 
#include <string.h>
26
 
#include <stdio.h>
27
 
#include <sys/socket.h>
28
 
#include <arpa/inet.h>
29
 
#include <netinet/in.h>
30
 
#include <unistd.h>
31
 
 
32
 
/* We use a special SOCKET type for easier Windows porting */
33
 
#define SOCKET int
34
 
 
35
 
/* Get port: Connect to a remote machine via TCP */
36
 
SOCKET get_port(uint32_t ip, struct sockaddr_in *dns) {
37
 
        SOCKET sock;
38
 
        int len_inet;
39
 
 
40
 
        /* Bind to port 53 */
41
 
        sock = socket(AF_INET,SOCK_STREAM,0);
42
 
        if(sock == -1) {
43
 
                perror("socket error");
44
 
                exit(0);
45
 
        }
46
 
        memset(dns,0,sizeof(struct sockaddr_in));
47
 
        dns->sin_family = AF_INET;
48
 
        dns->sin_port = htons(53);
49
 
        dns->sin_addr.s_addr = ip;
50
 
        if(dns->sin_addr.s_addr == INADDR_NONE) {
51
 
                perror("Problem with s_addr");
52
 
                exit(0);
53
 
        }
54
 
        len_inet = sizeof(struct sockaddr_in);
55
 
        if(connect(sock,(struct sockaddr *)dns,len_inet) == -1) {
56
 
                perror("connect error");
57
 
                exit(0);
58
 
        }
59
 
 
60
 
        /* Linux kernel bug */
61
 
        /* fcntl(sock, F_SETFL, O_NONBLOCK); */
62
 
 
63
 
        return sock;
64
 
}
65
 
 
66
 
#define calc_dns_len(a) ((a[0] & 0xff) << 8) | (a[1] & 0xff)
67
 
 
68
 
/* Get a single nibble from stdin in hex */
69
 
int8_t get_nibble() {
70
 
        char nib;
71
 
 
72
 
        while((nib = (getc(stdin)))) {
73
 
 
74
 
                /* We allow pauses to test TCP buffering */
75
 
                if(nib == '-') {
76
 
                        sleep(1);
77
 
                        return -2; /* Time to send() the packet */
78
 
                /* Hex-ASCII to bin conversion */
79
 
                } else if(nib >= '0' && nib <= '9') {
80
 
                        return nib - '0';
81
 
                } else if(nib >= 'A' && nib <= 'F') {
82
 
                        return nib - 'A' + 10;
83
 
                } else if(nib >= 'a' && nib <= 'f') {
84
 
                        return nib - 'a' + 10;
85
 
                }
86
 
        }
87
 
        return -1;
88
 
 
89
 
}
90
 
 
91
 
/* Get a single byte from stdin in hex */
92
 
int16_t get_stdin_hex() {
93
 
        char low;
94
 
        char high;
95
 
 
96
 
        high = get_nibble();
97
 
        if(high == -2) {
98
 
                return -2;
99
 
        }
100
 
        low = get_nibble();
101
 
        if(low == -2) {
102
 
                return -2;
103
 
        }
104
 
 
105
 
        return (high << 4) | low;
106
 
}
107
 
 
108
 
int main(int argc, char **argv) {
109
 
        uint32_t ip;
110
 
        int a;
111
 
        SOCKET sock;
112
 
        uint32_t dns_l, place;
113
 
        struct sockaddr_in dns;
114
 
        uint8_t buffer[1512];
115
 
        ssize_t len;
116
 
        int16_t get;
117
 
 
118
 
        if(argc >= 2) {
119
 
                ip = inet_addr(argv[1]);
120
 
        } else {
121
 
                ip = 0x0100007f; /* Localhost (127.0.0.1) */
122
 
        }
123
 
        sock = get_port(ip,&dns);
124
 
 
125
 
        /* Send hex bytes from stdin upstream */
126
 
        buffer[0] = get_stdin_hex();
127
 
        buffer[1] = get_stdin_hex();
128
 
        dns_l = calc_dns_len(buffer);
129
 
        if(send(sock, buffer, 2, MSG_WAITALL) == -1) {
130
 
                perror("send error len");
131
 
                exit(0);
132
 
        }
133
 
        place = 0;
134
 
        for(a=0; a < dns_l; ) {
135
 
                get = get_stdin_hex();
136
 
                if(get == -2) {
137
 
                        if(send(sock,buffer + place, a - place, MSG_WAITALL)
138
 
                           == -1) {
139
 
                                perror("send error pause");
140
 
                                exit(0);
141
 
                        }
142
 
                        printf("%d bytes sent\n",a - place);
143
 
                        place = a;
144
 
                } else {
145
 
                        buffer[a] = get;
146
 
                        a++;
147
 
                }
148
 
        }
149
 
        if(send(sock,buffer + place,dns_l - place, MSG_WAITALL) == -1) {
150
 
                perror("send error");
151
 
                exit(0);
152
 
        }
153
 
        printf("%d bytes sent\n",dns_l - place);
154
 
        /* Get reply from upstream, show on stdout */
155
 
        /* Blocking: Get 2-byte length header */
156
 
        len = recv(sock, buffer, 2, MSG_WAITALL);
157
 
        if(len == -1) {
158
 
                perror("recv error len");
159
 
                exit(0);
160
 
        }
161
 
        for(a=0; a < len; a++) {
162
 
                printf("%02X ",buffer[a]);
163
 
        }
164
 
        dns_l = calc_dns_len(buffer);
165
 
        /* Blocking: Get DNS packet */
166
 
        len = recv(sock, buffer, dns_l, MSG_WAITALL);
167
 
        if(len == -1) {
168
 
                perror("recv error");
169
 
                exit(0);
170
 
        }
171
 
        for(a=0; a < len; a++) {
172
 
                printf("%02X ",buffer[a]);
173
 
        }
174
 
        printf("\n");
175
 
 
176
 
 
177
 
        printf("\n");
178
 
        return 0;
179
 
}