Skip to content

Instantly share code, notes, and snippets.

@xandout
Last active September 3, 2015 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xandout/a819e3124f30de2ac627 to your computer and use it in GitHub Desktop.
Save xandout/a819e3124f30de2ac627 to your computer and use it in GitHub Desktop.
Playing with DNS packets in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock.h>
#include <time.h>
#define BUFFER_SIZE 4096
void usage(void);
int main(int argc, char **argv)
{
WSADATA w; /* Used to open windows connection */
unsigned short port_number; /* Port number to use */
int octet1, octet2, octet3, octet4; /* Components of address in xxx.xxx.xxx.xxx form */
int client_length; /* Length of client struct */
int bytes_received; /* Bytes received from client */
SOCKET sd; /* Socket descriptor of server */
struct sockaddr_in server; /* Information about the server */
struct sockaddr_in client; /* Information about the client */
char buffer[BUFFER_SIZE]; /* Where to store received data */
struct hostent *hp; /* Information about this computer */
char host_name[256]; /* Name of the server */
// Need to use a better option than this. getopt
/* Interpret command line */
if (argc == 2)
{
/* Use local address */
if (sscanf(argv[1], "%u", &port_number) != 1)
{
usage();
}
}
else if (argc >= 3) //allow
{
/* Copy address */
if (sscanf(argv[1], "%d.%d.%d.%d", &octet1, &octet2, &octet3, &octet4) != 4)
{
usage();
}
if (sscanf(argv[2], "%u", &port_number) != 1)
{
usage();
}
}
else
{
usage();
}
/* Open winsock connection */
if (WSAStartup(0x0101, &w) != 0)
{
fprintf(stderr, "Could not open winsock connection.\n");
exit(0);
}
/* Open a datagram socket */
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == INVALID_SOCKET)
{
fprintf(stderr, "Could not create socket.\n");
WSACleanup();
exit(0);
}
/* Clear out server struct */
memset((void *)&server, '\0', sizeof(struct sockaddr_in));
/* Set family and port */
server.sin_family = AF_INET;
server.sin_port = htons(port_number);
/* Set address automatically if desired */
if (argc == 2)
{
/* Get host name of this computer */
gethostname(host_name, sizeof(host_name));
hp = gethostbyname(host_name);
/* Check for NULL pointer */
if (hp == NULL)
{
fprintf(stderr, "Could not get host name.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Assign the address */
server.sin_addr.S_un.S_un_b.s_b1 = hp->h_addr_list[0][0];
server.sin_addr.S_un.S_un_b.s_b2 = hp->h_addr_list[0][1];
server.sin_addr.S_un.S_un_b.s_b3 = hp->h_addr_list[0][2];
server.sin_addr.S_un.S_un_b.s_b4 = hp->h_addr_list[0][3];
}
/* Otherwise assign it manually */
else
{
server.sin_addr.S_un.S_un_b.s_b1 = (unsigned char)octet1;
server.sin_addr.S_un.S_un_b.s_b2 = (unsigned char)octet2;
server.sin_addr.S_un.S_un_b.s_b3 = (unsigned char)octet3;
server.sin_addr.S_un.S_un_b.s_b4 = (unsigned char)octet4;
}
/* Bind address to socket */
if (bind(sd, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) == -1)
{
fprintf(stderr, "Could not bind name to socket.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Print out server information */
printf("Server running on %u.%u.%u.%u\n", (unsigned char)server.sin_addr.S_un.S_un_b.s_b1,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b2,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b3,
(unsigned char)server.sin_addr.S_un.S_un_b.s_b4);
printf("Press CTRL + C to quit\n");
/* Loop and get data from clients */
while (1)
{
client_length = (int)sizeof(struct sockaddr_in);
/* Receive bytes from client */
bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
if (bytes_received < 0)
{
fprintf(stderr, "Could not receive datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
//We have a socket...
int STARTQ = 0;
int STOPQ = bytes_received - 5;
char Q[STOPQ - 13];
Q[STOPQ - 13] = '\0';
printf("------\n");
int x = 0;
for(STARTQ = 13; STARTQ < STOPQ; STARTQ++)
{
if(buffer[STARTQ] == 3 || buffer[STARTQ] == 7)
buffer[STARTQ] = 46;
Q[x] = buffer[STARTQ];
x++;
if(argc == 4)
{
if(strcmp(argv[3], "PRINTBYTES") == 0)
printf("%i : %c\n",buffer[STARTQ],buffer[STARTQ]);
}
}
printf("%s\n", Q);
}
closesocket(sd);
WSACleanup();
return 0;
}
void usage(void)
{
fprintf(stderr, "CDNS [server_address] port \nCDNS [server_address] port [PRINTBYTES]\n");
exit(0);
}
/* http://imgur.com/RWkmflK */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment