LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2012, 11:45 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
sockets project help


So, I am writing a client and server with a login system with the transfered data hashed with sha1.

So I have a few questions. One, I am working on the name checking function. It checks if the name already exists in the database. I have it working with the first name only and wanted to know if I can traverse the names without a singly linked list? Would using a singly linked list be more efficient?

Here is what I have for the name checking function. The file being opened for reading only makes no difference because it will eventually be integrated with the main code.
Code:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {

FILE *file = fopen("dbase.txt", "rb");
	if(file == NULL) {
	printf("FOPEN(NULL) error --> %s\n", strerror(errno));
	exit(EXIT_FAILURE);	
	}
char name[35], *chpoint;
printf("Please enter a name to check: ");
fgets(name, sizeof(name), stdin);
printf("Name -> %s", name);

chpoint = strchr(name, '\n');
	if (chpoint != NULL) {
	*chpoint = '\0';
	}

int size = sizeof(name);
int n = strlen(name);
char temp[size];

fseek(file, 0, SEEK_END);
int fsize = ftell(file);
rewind(file);
//printf("The size of your file is %d characters.\n", fsize);

int bytes_read = fread(temp, fsize, 1, file);
	if(bytes_read < 0) {
	printf("FREAD(-1) error --> %s\n", strerror(errno));
	exit(EXIT_FAILURE);
	}

printf("Temp -> \n%s", temp);

int mcmp = memcmp(temp, name, n);
	if(mcmp == 0) {
	printf("! Name already exists\n");
	exit(EXIT_FAILURE);
	}
	
	else {
	printf("Name does not exist.\n");
	exit(EXIT_SUCCESS);
	}

return 0;
}
Question number two; Can I use a newline char as a condition for a loop while working with binary? For example:
Code:
while(ch != '\n') {
move memory a into memory b. 
}
What I do not know is weather the newline char remains an ascii char while working with binary files.

Question 3; In order to get the data that is sent and received from client to server(Yes it is synchronous comm and not asynch... yet) hashed, I am going to use Openssl and it's sha1 function. I have downloaded Openssl from the official site and installed it that way as well as the apt-get method.

I am compiling with:
Code:
gcc -Wall -lcrypto test02.c -o test02
But my compiler cannot find crypto

if i compile with;
Code:
gcc -Wall -L/usr/include/crypto.h test02.c -o test02
I get an undefined reference to SHA1 in main


I have configured openssl's install like so
Code:
./config --prefix=/usr/include/openssl --openssldir=/usr/include/openssl
What could be wrong?

Last edited by amboxer21; 07-04-2012 at 12:12 AM.
 
Old 07-04-2012, 07:28 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I see a potential bug in this line of code:
Code:
int bytes_read = fread(temp, fsize, 1, file);
You are attempting to read 'fsize' number of bytes from the file and store it into an array 'temp' that has been declared with the same size as the array 'name' (35 bytes). If 'fsize' is greater than 35, then you will overflow the 'temp' array, thus inevitably causing grief within your application.

Answers...

Question 1: A linked list is slow, since you might potentially have to traverse every element to search for the one of interest. How about using a btree?

Question 2:
It makes no difference.

Question 3:
I can't comment on either openssl; I've never used it. However, the following statement in incorrect...
Code:
gcc -Wall -lcrypto test02.c -o test02
Should be:
Code:
gcc -Wall test02.c -lcrypto -o test02
Libraries are always specified after the source code modules.
 
Old 07-04-2012, 01:52 PM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dwhitney67 View Post
I see a potential bug in this line of code:
Code:
int bytes_read = fread(temp, fsize, 1, file);
You are attempting to read 'fsize' number of bytes from the file and store it into an array 'temp' that has been declared with the same size as the array 'name' (35 bytes). If 'fsize' is greater than 35, then you will overflow the 'temp' array, thus inevitably causing grief within your application.
Thank you and I will fix that!


Quote:
Answers...

Question 1: A linked list is slow, since you might potentially have to traverse every element to search for the one of interest. How about using a btree?
I had a look at the binary tree tutorial provided at HERE and it seems very simple! Much more so than a singly linked list! Thanks for the suggestion! I like the way the binary tree works!

Quote:
Question 2:
It makes no difference.
OK i was wrong. Thank you for the clarification! Much appreciated!

Quote:
Question 3:
I can't comment on either openssl; I've never used it. However, the following statement in incorrect...
Code:
gcc -Wall -lcrypto test02.c -o test02
Should be:
Code:
gcc -Wall test02.c -lcrypto -o test02
Yeah I figured that out shortly after posting this last night. Thanks again!

EDIT:
I am going to leave this open and not mark it as solved just in case I have any relevant questions.

Last edited by amboxer21; 07-04-2012 at 02:59 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
project related help needed...linux based project tpsamant88 Programming 2 06-20-2012 11:42 AM
Adding a file to a project via project.vim usernameinuse Linux - Newbie 0 06-22-2010 01:02 PM
[SOLVED] How to store all files related to a c++ project and build in linux. Like project dir sharanlinux Programming 1 04-29-2010 06:52 AM
Program to forward tcp sockets to unix domain sockets mikepol Linux - Networking 0 09-27-2007 09:49 AM
Beginning a big project - Need an Good Project Manager gamehack Programming 3 01-15-2004 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration