Permalink
Browse files

response is being returned. Now I need to parse it

  • Loading branch information...
1 parent 58cf83a commit 56119bca7528f59f52fee2e81395fa9a9b47c072 @uberj committed Mar 5, 2011
Showing with 220 additions and 28 deletions.
  1. +173 −0 c_utils.c
  2. +21 −0 c_utils.h
  3. BIN c_utils.o
  4. +21 −12 client.c
  5. +5 −16 socket_utils.c
View
173 c_utils.c
@@ -0,0 +1,173 @@
+#include "c_utils.h"
+
+/*
+ * function: takes a socket and listens for connections. Uses accept to return a new
+ * file descriptor.
+ * param: file descriptor to listen to.
+ * return: live fd to pass data into and receive from.
+ */
+int wait_and_listen(int listener){
+ int newfd;
+ struct sockaddr_storage remoteaddr;
+ socklen_t addrlen;
+ /*
+ * Wait for new connections.
+ */
+ if (listen(listener, 10) == -1) {
+ perror("listen");
+ exit(3);
+ }
+ /*
+ * A client has connected to our server.
+ */
+ addrlen = sizeof remoteaddr;
+ newfd = accept(listener,(struct sockaddr *)&remoteaddr,&addrlen);
+ if (newfd == -1) {
+ perror("accept");
+ }
+ return newfd;
+}
+/*
+ * function: Used to open a tcp connected and create a file descriptor to write into that connection.
+ * param: PORT - what to bind the socket to.
+ * remote_host - what remote (chat server in out case) to connect to.
+ */
+int client_get_socket(char *PORT, char *remote_host){
+ int rv;
+ int sockfd; // Socket we will return.
+ struct addrinfo hints, *servinfo, *p;
+ memset(&hints, 0, sizeof(struct addrinfo));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ rv = getaddrinfo(remote_host, PORT, &hints, &servinfo);
+ if(rv != 0) {
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
+ return 1;
+ }
+ // loop through all the results and connect to the first we can
+ for(p = servinfo; p != NULL; p = p->ai_next) {
+ if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
+ perror("client: socket");
+ continue;
+ }
+ if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
+ close(sockfd);
+ perror("client: connect");
+ continue;
+ }
+ break;
+ }
+ if (p == NULL) {
+ fprintf(stderr, "client: failed to connect\n");
+ return 2;
+ }
+ freeaddrinfo(servinfo); // all done with this structure
+ return sockfd;
+}
+/*
+ * function: Create a SOCK_STREAM socket bound to PORT.
+ * param: Port to bind to.
+ * return: a valid "connected" file descriptor.
+ */
+int get_socket(char *PORT){
+ int rv;
+ int yes=1;
+ int listener;
+ struct addrinfo hints, *ai, *p;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_PASSIVE;
+ if ((rv = getaddrinfo(NULL,PORT, &hints, &ai)) != 0) {
+ fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
+ exit(1);
+ }
+ for(p = ai; p != NULL; p = p->ai_next) {
+ listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
+ if (listener < 0) {
+ continue;
+ }
+ // lose the pesky "address already in use" error message
+ setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
+ if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
+ close(listener);
+ continue;
+ }
+ break;
+ }
+ // if we got here, it means we didn't get bound
+ if (p == NULL) {
+ fprintf(stderr, "selectserver: failed to bind\n");
+ exit(2);
+ }
+ freeaddrinfo(ai); // all done with this
+ return listener;
+}
+/*
+ * NOT USED
+ */
+void load_handle(char *array,char* handle){
+ int i;
+ for (i=0; i<sizeof handle;i++){
+ array[i]=handle[i];
+ }
+}
+/*
+ * NOT USED
+ */
+void print_handle(char *array){
+ int i;
+ for (i=0; array[i] != '\0';i++){
+ printf("%c",array[i]);
+ }
+}
+/*
+ * Used by client to choose a users handle.
+ */
+void get_handle(char *array){
+ char handle[11];
+ int x;
+ printf("Enter a handle less at most 10 chars long: ");
+ fgets(handle,10,stdin);
+ printf("You chose %s",handle);
+ x = strlen(handle);
+ handle[x-1]='\0'; // We don't want the new line.
+ strcpy(array,handle);
+}
+
+/*
+ * function: Get's the message a user wants to send to their peer and prepends their
+ * handle to that message.
+ * param: msg - message they want to send
+ * handle - the peer's handle.
+ * return: args passed by reference.
+ */
+void get_msg(char *msg, char *handle){
+ int i,m;
+ char text[500];
+ memset(text,0,500);
+
+ fgets(text,sizeof text,stdin);
+ m=strlen(handle);
+ strcpy(msg,handle);
+ msg[m++]='?';
+ i=0;
+ while(text[i] != '\n'){
+ msg[m]=text[i];
+ i++;
+ m++;
+ }
+ msg[m]='\0';
+}
+/*
+ * Utility function to do a strcpy() :\
+ */
+void get_port(char *PORT, char *arg){
+ int i;
+ for (i=0; i<strlen(arg);i++){
+ PORT[i]=arg[i];
+ }
+ PORT[i]='\0';
+}
View
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#define STDIN 0
+#define MAX_MSGSIZE 500
+
+void load_handle(char *array,char* handle);
+void print_handle(char *array);
+void get_handle(char *array);
+void get_msg(char *msg, char *handle);
+void get_port(char *PORT, char *arg);
+int get_socket(char *PORT);
+int wait_and_listen(int listener);
View
BIN c_utils.o
Binary file not shown.
View
@@ -7,15 +7,8 @@
#include "constructor.h"
#include "dns_c.h"
-int _hex_print( unsigned char* array, int size){
- int i;
- printf("hex string: ");
- for(i=0;i<size;i++){
- printf("%02hhX ",array[i]);
- }
- printf("\n");
- return 1;
-}
+
+int _hex_print( unsigned char* array, int size);
int main( int argc, char **argv ){
struct DNS_REQUEST question;
@@ -35,7 +28,10 @@ int main( int argc, char **argv ){
question.qtype[1] = '\1'; //0001
question.qclass[0] = '\0'; //0000
question.qclass[1] = '\1'; //0001
- question.query = (unsigned char*) malloc((sizeof(unsigned char)*strlen(argv[1]))+5);
+// int qsize = ( sizeof(unsigned char) * strlen(argv[1]) ) + 5;
+ //question.query = (unsigned char *) malloc( qsize );
+ unsigned char query[19];
+ question.query = &query;
// Fill in that cruft
int i;
for(i=0;i<6;i++){
@@ -63,10 +59,23 @@ int main( int argc, char **argv ){
/*
* Sending request
*/
- send_request(&question,answer);
- _hex_print(answer,512);
+ send_request(&question,&answer);
+ printf("RESPONSE:\n");
+ _hex_print(&answer,512);
//free(answer);
//free(question.query);
return 0;
}
+
+int _hex_print( unsigned char* array, int size){
+ int i;
+ printf("hex string: \n");
+ for(i=0;i<size;i++){
+ printf("%02hhX ",array[i]);
+ if((i+1)%20==0)
+ printf("\n");
+ }
+ printf("\n");
+ return 1;
+}
View
@@ -1,4 +1,5 @@
#include "socket_utils.h"
+#include "c_utils.c"
void *get_in_addr(struct sockaddr *sa){
if (sa->sa_family == AF_INET) {
@@ -51,22 +52,10 @@ int set_up_socket(struct addrinfo *server){
* Send data in the DNS_REQUEST struct to the file descriptor sockfd.
*/
int send_request(struct DNS_REQUEST* data, unsigned char *answer){
- struct addrinfo server;
- int s;
- int sockfd;
- struct sockaddr_storage temp;
- socklen_t addr_len;
- sockfd = set_up_socket( &server );
- s = sendto(sockfd, data->query, data->size, 0,server.ai_addr, server.ai_addrlen);
- if (s == -1) {
- perror("talker: sendto");
- exit(1);
- } if(s != data->size){
- printf("Not all data was sent.\n");
- return -1;
- }
- addr_len = sizeof temp;
- s = recvfrom(sockfd,answer,SIZE_OF_RESP,0,(struct sockaddr *)&temp,&addr_len);
+ int nb;
+ int sockfd = client_get_socket(DNS_PORT,NAME_SERVER);
+ send(sockfd,data->query,data->size,0);
+ nb = recv(sockfd,answer,512,0);
return 0;
}

0 comments on commit 56119bc

Please sign in to comment.