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

« back to all changes in this revision

Viewing changes to deadwood-3.2.05/sqa/sqa_bigpacket/show_packet_stdout.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 reads a DNS packet over
20
 
 * port 53 TCP, then outputs that packet on the standard input.  Then
21
 
 * the program reads standard input and outputs the DNS packet on
22
 
 * standard input */
23
 
 
24
 
#include <stdint.h>
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <sys/socket.h>
29
 
#include <arpa/inet.h>
30
 
#include <netinet/in.h>
31
 
#include <unistd.h>
32
 
 
33
 
/* We use a special SOCKET type for easier Windows porting */
34
 
#define SOCKET int
35
 
 
36
 
/* Get port: Get a port locally and return the socket the port is on */
37
 
SOCKET get_port(uint32_t ip, struct sockaddr_in *dns) {
38
 
        SOCKET sock;
39
 
        int len_inet;
40
 
 
41
 
        /* Bind to port 53 */
42
 
        sock = socket(AF_INET,SOCK_STREAM,0);
43
 
        if(sock == -1) {
44
 
                perror("socket error");
45
 
                exit(0);
46
 
        }
47
 
        memset(dns,0,sizeof(struct sockaddr_in));
48
 
        dns->sin_family = AF_INET;
49
 
        dns->sin_port = htons(53);
50
 
        dns->sin_addr.s_addr = ip;
51
 
        if(dns->sin_addr.s_addr == INADDR_NONE) {
52
 
                perror("Problem with bind");
53
 
                exit(0);
54
 
        }
55
 
        len_inet = sizeof(struct sockaddr_in);
56
 
        if(bind(sock,(struct sockaddr *)dns,len_inet) == -1) {
57
 
                perror("bind error");
58
 
                exit(0);
59
 
        }
60
 
 
61
 
        /* Linux kernel bug */
62
 
        /* fcntl(sock, F_SETFL, O_NONBLOCK); */
63
 
 
64
 
        return sock;
65
 
}
66
 
 
67
 
#define calc_dns_len(a) ((a[0] & 0xff) << 8) | (a[1] & 0xff)
68
 
 
69
 
/* Get a single nibble from stdin in hex */
70
 
int8_t get_nibble() {
71
 
        char nib;
72
 
 
73
 
        while((nib = (getc(stdin)))) {
74
 
 
75
 
                /* We allow pauses to test TCP buffering */
76
 
                if(nib == '-') {
77
 
                        sleep(1);
78
 
                        return -2;
79
 
                /* Hex-ASCII to bin conversion */
80
 
                } else if(nib >= '0' && nib <= '9') {
81
 
                        return nib - '0';
82
 
                } else if(nib >= 'A' && nib <= 'F') {
83
 
                        return nib - 'A' + 10;
84
 
                } else if(nib >= 'a' && nib <= 'f') {
85
 
                        return nib - 'a' + 10;
86
 
                }
87
 
        }
88
 
        return -1;
89
 
 
90
 
}
91
 
 
92
 
/* Get a single byte from stdin in hex */
93
 
int16_t get_stdin_hex() {
94
 
        char low;
95
 
        char high;
96
 
 
97
 
        high = get_nibble();
98
 
        if(high == -2) {
99
 
                return -2;
100
 
        }
101
 
        low = get_nibble();
102
 
        if(low == -2) {
103
 
                return -2;
104
 
        }
105
 
 
106
 
        return (high << 4) | low;
107
 
}
108
 
 
109
 
int main(int argc, char **argv) {
110
 
        uint32_t ip;
111
 
        int a;
112
 
        SOCKET sock;
113
 
        SOCKET local;
114
 
        uint32_t dns_l, place;
115
 
        struct sockaddr_in dns;
116
 
        uint8_t buffer[1512], id1, id2;
117
 
        ssize_t len;
118
 
        int16_t get;
119
 
 
120
 
        if(argc >= 2) {
121
 
                ip = inet_addr(argv[1]);
122
 
        } else {
123
 
                ip = 0; /* All IP addresses */
124
 
        }
125
 
        sock = get_port(ip,&dns);
126
 
        if(listen(sock, 250) == -1) {
127
 
                perror("listen error");
128
 
                exit(0);
129
 
        }
130
 
        len = sizeof(struct sockaddr_in);
131
 
        local = accept(sock,(struct sockaddr *)&dns,(void *)&len);
132
 
        /* Blocking: Get 2-byte length header */
133
 
        len = recv(local, buffer, 2, MSG_WAITALL);
134
 
        if(len == -1) {
135
 
                perror("recv error");
136
 
        }
137
 
        for(a=0; a < len; a++) {
138
 
                printf("%02X",buffer[a]);
139
 
        }
140
 
        dns_l = calc_dns_len(buffer);
141
 
        /* Blocking: Get DNS packet */
142
 
        len = recv(local, buffer, dns_l, MSG_WAITALL);
143
 
        for(a=0; a < len; a++) {
144
 
                printf("%02X ",buffer[a]);
145
 
        }
146
 
        id1 = buffer[0];
147
 
        id2 = buffer[1];
148
 
        printf("\n");
149
 
        /* Now, send a packet back from stdin */
150
 
        buffer[0] = get_stdin_hex();
151
 
        buffer[1] = get_stdin_hex();
152
 
        dns_l = calc_dns_len(buffer);
153
 
        if(send(local, buffer, 2, MSG_WAITALL) == -1) {
154
 
                perror("send error len");
155
 
                exit(0);
156
 
        }
157
 
        place = 0;
158
 
        for(a=0; a < dns_l; ) {
159
 
                get = get_stdin_hex();
160
 
                if(get == -2) {
161
 
                        if(send(local,buffer + place, a - place, MSG_WAITALL)
162
 
                           == -1) {
163
 
                                perror("send error pause");
164
 
                                exit(0);
165
 
                        }
166
 
                        printf("%d bytes sent\n",a - place);
167
 
                        place = a;
168
 
                } else {
169
 
                        buffer[a] = get;
170
 
                        buffer[0] = id1;
171
 
                        buffer[1] = id2;
172
 
                        a++;
173
 
                }
174
 
        }
175
 
        if(send(local,buffer + place,dns_l - place, MSG_WAITALL) == -1) {
176
 
                perror("send error");
177
 
                exit(0);
178
 
        }
179
 
        printf("%d bytes sent\n",dns_l - place);
180
 
        return 0;
181
 
}