Skip to content

Instantly share code, notes, and snippets.

@vkmc
Last active December 17, 2015 06:28
Show Gist options
  • Save vkmc/5565178 to your computer and use it in GitHub Desktop.
Save vkmc/5565178 to your computer and use it in GitHub Desktop.
Proyecto 1 - Redes de Computadoras
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h> // Linux socket interface
#include <arpa/inet.h> // ?
#include <netinet/in.h> // ?
#include <unistd.h>
#include <sys/types.h>
#include <regex.h>
#define T_A 1 // ipv4 record
#define T_MX 15 // nameserver record
#define T_LOC 29 // location record RFC 1876
// DNS Header
typedef struct dns_header
{
unsigned short id; // id number
unsigned char qr :1; // query-response
unsigned char opcode :4; //opcode
unsigned char aa :1; // authoritative answer
unsigned char tc :1; // truncated message
unsigned char rd :1; // recursion desired
unsigned char ra :1; // recursion availability
unsigned char z :1; // zero
unsigned char rcode :4; // response code
unsigned short qdcount; // no question entries
unsigned short ancount; // no answer entries
unsigned short nscount; // no name server RRs in the authority section
unsigned short arcount; // no name server RRs in the additional section
} * headerp;
// Section used to carry the question in queries
typedef struct dns_question
{
unsigned char * qname;
unsigned int qtype;
unsigned int qclass;
} * question;
// Resource data - Shared by Answer, Authority and Additional sections
typedef struct record_data
{
unsigned int type; // RR type codes
unsigned int class; // class of the data in RDATA
unsigned int ttl; // time before the record is discarded
unsigned int rdlength; // lenght of the RDATA field
} * r_data;
// Pointers to resource records contents
typedef struct resource_record
{
unsigned char * name; // resource record's domain name
struct record_data * resource;
unsigned char * r_data; // describes the resource
} * r_record;
void init_query(unsigned char buffer[], headerp * header);
int main (int argc, char * argv[])
{
char * nameserver;
char * programa;
headerp h;
unsigned char buffer[65536];
nameserver = argv[1];
programa = argv[0];
printf("Programa: %s\n\n", programa);
printf("Nameserver: %s\n\n", nameserver);
init_query(buffer, &h);
unsigned char * unaURL = "http://google.com";
printf("Una URL %s\n", unaURL);
int url = checkURL(unaURL);
return 0;
}
int get_dns_servers()
{
FILE * fp;
char line[200], * p;
fp = fopen("/etc/resolv.conf", "r");
if (fp == NULL)
{
printf("Failed opening /etc/resolv.conf file \n");
return EXIT_FAILURE;
}
while (fgets(line, 200, fp))
{
if (line[0] == '#')
{
continue;
}
if (strncmp(line, "nameserver", 10) == 0)
{
p = strtok(line, " ");
}
}
}
void init_query(unsigned char buffer[], headerp * header)
{
headerp dnsh;
dnsh = *header;
dnsh = (struct dns_header *) &buffer;
dnsh->id = (unsigned short) htons(getpid());
dnsh->qr = 0;
dnsh->opcode = 0;
dnsh->aa = 0;
dnsh->tc = 0;
dnsh->rd = 1;
dnsh->ra = 0;
dnsh->z = 0;
dnsh->rcode = 0;
dnsh->qdcount = htons(1); // 1 question
dnsh->ancount = 0;
dnsh->nscount = 0;
dnsh->arcount = 0;
}
int checkURL (unsigned char * url) {
unsigned char * reg_exp = "^((https?|ftp)://|(www|ftp)\\.)?[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
regex_t regex_http;
int regc = regcomp(&regex_http, reg_exp , REG_EXTENDED);
if (regc) {
printf("Could not compile regex");
return 1;
}
int rege = regexec(&regex_http, url, 0, NULL, 0);
if (!rege) {
printf("Match URL!");
} else if (rege = REG_NOMATCH) {
printf("No match URL!");
} else {
printf("Error!");
return 1;
}
regfree(&regex_http);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment