Advertisement
Advertisement
| 06.22.2008 at 08:31AM PDT, ID: 23505779 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: |
//search function()
int search_value(struct NODE *llist, int text_line, int fd) {
int retval = -1;
int i = 1;
int net;
while(llist != NULL) {
if(llist->line == text_line){ /* if there is a result for the request */
net = htons(llist->code);
Writeline(fd, &net, 2); /* send the value to the client */
llist = llist->next;
return i;
}
else i++;
llist = llist->next;
}
if (i != 1) /* else htons(0) si returned */
net = htons(65535);
Writeline(fd, &net, 2);
return retval;
}
// end
// How i trap the command
while (comando)
{
char *next = strstr(comando, "\r\n");
if (next)
{
*next = '\0'; //Dont move next yet.
}
if ( lenght <= 500 )
{
if ( strncmp(comando, "HELO", 4) == 0)
{
//username = strtok((comando+4), "\r\n"); If I remember correctly, username is a 16 char string right after HELO. Right now, comando contains HELOusername\0\nnextcommand\r\n ... So you do not want a strtok on \r\n
username = comando+4;
if (strlen(username) > 16)
{
Writeline(fd, alert, strlen(alert));
}
Writeline(fd, passw, strlen(passw));
read(fd, password, MAX_LINE);
passwd = strtok(password, "\r\n");
if (verify_login(username, passwd)==1)
{
auth=1;
Writeline(fd, good, strlen(good));
}
else if(verify_login(username, passwd)==-1)
{
auth=0;
Writeline(fd, fail, strlen(fail));
}
}
else if ( strncmp(comando, "LIST", 4) == 0)
{
display_list(llist, fd);
}
else if ( strncmp(comando, "QUIT", 4) == 0)
{
Writeline(fd, bye, strlen(bye));
free(llist);
return;
}
else if(strncmp(comando, "FIND", 4) == 0)
{
if (auth == 0)
{
Writeline(fd, noauth, strlen(noauth));
}
else {
if (llist->next == NULL )
{
Writeline(fd, noob, strlen(noob));
}
else if (llist ->next != NULL )
{
char * temp = comando+4;
short arr;
int val = 0;
do
{
Writeline(fd, here, strlen(here));
memcpy(&arr, temp, 2);
Writeline(fd, &arr, 2);
temp = temp + 2;
val = ntohs(arr);
search_value(llist, val, fd);
} while (val);
}
}
}
}
if (next)
comando = next+2; //move to next command here
else
comando = NULL; // could as well have used break here
}
}
//load the content in a linked list
void load_file_in_list(){
llist = (struct NODE *)malloc(sizeof(struct NODE));
llist->code = 0;
llist->next = NULL;
llist->size = 0;
llist->line = 0;
FILE * pFile;
char buffer [100];
int code;
char name[1000];
pFile = fopen (objs_file , "r");
if (pFile == NULL) perror ("Error opening file");
else
{
while ( fgets (buffer , 100 , pFile) != NULL )
{
sscanf(buffer,"%d %s \n", &code, name);
line++; // global variable
append_node(llist, name, code, line); /* insert values in the list */
size++;
}
fclose (pFile);
}
}
|