PERFORCE change 180750 for review
Sergio Ligregni
ligregni at FreeBSD.org
Sun Jul 11 05:45:15 UTC 2010
http://p4web.freebsd.org/@@180750?ac=10
Change 180750 by ligregni at ligPhenom on 2010/07/11 05:44:15
The functionality of the project
is finished, some testing pending
Ready to MidTerm Eval
Affected files ...
.. //depot/projects/soc2010/disaudit/damasterd.c#3 edit
.. //depot/projects/soc2010/disaudit/damasterd.h#3 edit
.. //depot/projects/soc2010/disaudit/msocket_work.c#3 edit
.. //depot/projects/soc2010/disaudit/msocket_work.h#3 edit
.. //depot/projects/soc2010/disaudit/shipd.c#6 edit
.. //depot/projects/soc2010/disaudit/shipd.h#6 edit
.. //depot/projects/soc2010/disaudit/ssocket_work.c#4 edit
.. //depot/projects/soc2010/disaudit/ssocket_work.h#4 edit
Differences ...
==== //depot/projects/soc2010/disaudit/damasterd.c#3 (text+ko) ====
@@ -28,6 +28,7 @@
/*** INCLUDES ***/
#include "damasterd.h"
+#include <fcntl.h>
#include "msocket_work.h"
#include <stdio.h>
#include <stdlib.h>
@@ -38,6 +39,7 @@
#include <sys/types.h>
#include <sys/md5.h>
#include <sys/stat.h>
+#include <time.h>
/*** DECLARATIONS ***/
@@ -55,6 +57,20 @@
/* The destination of the messages are controlled by this variable */
int debug;
+/*
+ * The searching for a trail by it's name,
+ * or by it's name & origin host, this is for example,
+ * if we change a host name and this flag is on,
+ * all it's trails will be sync again since the entire path
+ * will be different, this is:
+ * 2010xxxxx.HOST_ONE.201006date_recvd
+ * then we change HOST_ONE to HOST_OTHER
+ * with the flag on, there must be a trail:
+ * 2010xxxxx.HOST_OTHER.201006date_recvd
+ * but, there is not such trail so we sync again.
+ */
+int lookup_host;
+
/* Socket buffer management */
char buffer[MAX_BUF_SIZE + 1];
int brecv;
@@ -139,23 +155,26 @@
return -1;
}
- if ((childpid = fork()) < 0)
+
+ if (!debug && (childpid = fork()) < 0)
{
to_log("Error forking the process");
return -1;
}
- else if (childpid == 0)
+ else if (debug || childpid == 0)
{
- debug = 0;
if (process_request(newsockfd, &clientinfo) == -1)
{
to_log("Error processing client's request");
return -1;
}
- close(socketfd);
+ if (!debug)
+ close(socketfd);
}
close(newsockfd);
+
+ usleep(1000);
}
return 0;
@@ -167,6 +186,7 @@
FILE *fpars = fopen("/etc/security/damasterd_control", "r");
char sslave_dirs[10];
+ char lkup_host[10];
if (!fpars)
return -1;
@@ -178,33 +198,46 @@
fscanf(fpars, "%s", sslave_dirs);
fscanf(fpars, "%d", &panic_level);
fscanf(fpars, "%d", &port_number);
+ fscanf(fpars, "%s", lkup_host);
if (strcmp(sslave_dirs, "no"))
slave_dirs = 1;
else
slave_dirs = 0;
+ if (strcmp(sslave_dirs, "yes"))
+ lookup_host = 1;
+ else
+ lookup_host = 0;
+
return 0;
}
process_request(int sfd, struct sockaddr *clientinfo)
{
- int res = -1;
+ int res = 0;
char opt[1];
get_from_socket(sfd, opt);
- switch(opt[0])
+ while (res != -1)
{
- case '1': /* The request is about searching for a file */
- res = search_trail(sfd, clientinfo);
- break;
- case '2': /* The request is about receiving a trail */
- res = receive_trail(sfd, clientinfo);
- break;
- default:
- to_log("Can't understand user's request!");
+ switch(opt[0])
+ {
+ case '0': /* The process is finished */
+ return 0;
+ break;
+ case '1': /* The request is about searching for a file */
+ res = search_trail(sfd, clientinfo);
+ break;
+ case '2': /* The request is about receiving a trail */
+ res = receive_trail(sfd, clientinfo);
+ break;
+ default:
+ to_log("Can't understand user's request!");
+ }
+ get_from_socket(sfd, opt);
}
close(sfd);
@@ -214,12 +247,20 @@
search_trail(int sfd, struct sockaddr *clientinfo)
{
+ DIR *dp;
+ struct dirent *dirp;
+ struct stat statbuf;
+
+ char fullpath[MAX_PATH_SIZE + 1];
+ char *ptr;
char hbuf[NI_MAXHOST+1];
char message[MAX_PATH_SIZE + 50];
char hostname[NI_MAXHOST+1];
- char path[MAX_TRAILPATH_SIZE+1], md5slave[33];
+ char pathslave[MAX_TRAILPATH_SIZE+1], md5slave[33];
+ char dirpath[MAX_DIR_SIZE+1];
+ char found_trail[] = "n";
- get_from_socket(sfd, path);
+ get_from_socket(sfd, pathslave);
get_from_socket(sfd, md5slave);
strcpy(hostname, inet_ntoa(((struct sockaddr_in *) clientinfo)->sin_addr));
@@ -229,12 +270,99 @@
else
strcpy(hostname, hbuf);
- sprintf(message, "Looking for \"%s\" from \"%s\" with MD5: \"%s\"", path, hostname, md5slave);
+ sprintf(message, "Looking for \"%s\" from \"%s\" with MD5: \"%s\"", pathslave, hostname, md5slave);
to_log(message);
+ strcpy(dirpath, slave_trails_dir);
+
+ if (slave_dirs)
+ {
+ strcat(dirpath, "/");
+ strcat(dirpath, hostname);
+ }
+
+ /* Setting the fullpath to search */
+ /* Fancy way to use the fullpath */
+ strcpy(fullpath, dirpath);
+ ptr = fullpath + strlen(fullpath);
+ *ptr = '/';
+ *(++ptr) = 0;
+
+ if ( !(dp = opendir(dirpath)) )
+ {
+ to_log("Can't open slave trails' directory");
+ send_to_socket(sfd, "n");
+ return 1;
+ }
+
+ /* We must count the elements (just the valid ones, this is: the trails) of the directory */
+ while (strcmp(found_trail, "y") && (dirp = readdir(dp)) != NULL )
+ if (strcmp(dirp->d_name, ".") && strcmp(dirp->d_name, "..")) /* We have other than . or .. */
+ {
+ strcpy(ptr, dirp->d_name);
+
+ if ( stat(fullpath, &statbuf) < 0 )
+ {
+ to_log("Stat error!");
+ return -1;
+ }
+
+ if (S_ISDIR(statbuf.st_mode) == 0) /* It's not a directory */
+ if ( check_files_equal(pathslave, md5slave, hostname, dirp->d_name, fullpath) ) /* Check that the trails are the same name + MD5 */
+ strcpy(found_trail, "y");
+ }
+
+ closedir(dp);
+
+ sprintf(message, "The search for %s resulted: %s\n", pathslave, found_trail[0] == 'y' ? "YES" : "NO");
+ send_to_socket(sfd, found_trail);
+
return 1;
}
+/*
+ * This function receives the name and MD5 checksum of the slave trail
+ * and the path and fullpath (to get MD5) of the master trail
+ * and checks if they are the same, also checks the lookup_host
+ * variable to determine if the hostname must be searched as part
+ * of the trails name
+ */
+
+check_files_equal(char *pathslave, char *md5slave, char *hostname, char *path, char *fullpath)
+{
+ char path_to_find[strlen(pathslave) + strlen(hostname) + 2];
+
+ strcpy(path_to_find, pathslave);
+
+ if (lookup_host)
+ {
+ strcat(path_to_find, ".");
+ strcat(path_to_find, hostname);
+ }
+
+ if (!strncmp(path, path_to_find, strlen(path_to_find)))
+ {
+ char *md5 = (char *) malloc (sizeof(char) * 33);
+ md5 = MD5File(fullpath, md5);
+
+ if (!strcmp(md5, md5slave))
+ return 1;
+
+ free(md5);
+ }
+
+ return 0;
+}
+
+void
+send_to_socket(int sfd, char *data)
+{
+ int len = strlen(data);
+
+ send(sfd, &len, sizeof(int), 0);
+ send(sfd, data, len, 0);
+}
+
void
get_from_socket(int sfd, char *dest)
{
@@ -258,6 +386,128 @@
receive_trail(int sfd, struct sockaddr *clientinfo)
{
+ DIR *dp;
+
+ char fullpath[MAX_PATH_SIZE + 1];
+ char hbuf[NI_MAXHOST+1];
+ char message[MAX_PATH_SIZE + 50];
+ char hostname[NI_MAXHOST+1];
+ char pathslave[MAX_TRAILPATH_SIZE+1];
+ char dirpath[MAX_DIR_SIZE+1];
+ time_t mtime;
+ struct tm *ltime;
+ unsigned long file_size;
+ int fd, bwrtn, bread;
+
+ get_from_socket(sfd, pathslave);
+
+ strcpy(hostname, inet_ntoa(((struct sockaddr_in *) clientinfo)->sin_addr));
+
+ if (getnameinfo(clientinfo, clientinfo->sa_len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD))
+ to_log("Couldn't resolve hostname, using IP address");
+ else
+ strcpy(hostname, hbuf);
+
+ sprintf(message, "Receiving \"%s\" from \"%s\"", pathslave, hostname);
+ to_log(message);
+
+ strcpy(dirpath, slave_trails_dir);
+
+ if ( !(dp = opendir(dirpath)) )
+ {
+ to_log("Can't open Slave trails dir, please create it or change the value at config files!");
+ return -1;
+ }
+
+ closedir(dp);
+
+ if (slave_dirs)
+ {
+ strcat(dirpath, "/");
+ strcat(dirpath, hostname);
+ }
+
+ if ( !(dp = opendir(dirpath)) )
+ {
+ to_log("Creating slave dir for this host");
+ if ( mkdir(dirpath, S_IRWXU | S_IRGRP | S_IXGRP) < 0 )
+ {
+ to_log("Cant't create host's directory!");
+ return -1;
+ }
+ }
+ else
+ closedir(dp);
+
+ strcat(dirpath, "/");
+
+ mtime = time(NULL);
+ ltime = gmtime(&mtime);
+
+ sprintf(fullpath, "%s%s.%s.%04d%02d%02d%02d%02d%02d",
+ dirpath,
+ pathslave,
+ hostname,
+ ltime->tm_year + 1900,
+ ltime->tm_mon + 1,
+ ltime->tm_mday,
+ ltime->tm_hour,
+ ltime->tm_min,
+ ltime->tm_sec);
+ sprintf(message, "Create: %s", fullpath);
+ to_log(message);
+
+ /* We get the trail size */
+ recv(sfd, &file_size, sizeof(file_size), 0);
+
+ fd = open(fullpath, O_CREAT | O_WRONLY);
+ if (fd < 0)
+ {
+ to_log("Can't create the trail at master system");
+ return -1;
+ }
+
+ while (file_size)
+ {
+ brecv = recv(sfd, &bread, sizeof(bread), 0);
+ if (brecv < 0)
+ {
+ to_log("Error receiving the file");
+ return -1;
+ }
+
+ brecv = recv(sfd, buffer, bread, 0);
+
+ if (brecv < 0)
+ {
+ to_log("Error receiving the file");
+ return -1;
+ }
+ else if (brecv == 0)
+ break;
+
+ bwrtn = write(fd, buffer, brecv);
+
+ if (bwrtn < 0 || bwrtn != brecv)
+ {
+ to_log("Error writting the file");
+ return -1;
+ }
+
+ file_size -= brecv;
+ }
+
+ sprintf(message, "Master got: %s", fullpath);
+
+ close(fd);
+
+ /* Change the permissions to be the same than the local trails */
+ if (chmod(fullpath, S_IRUSR | S_IRGRP) < 0)
+ {
+ to_log("Error changing permissions");
+ return -1;
+ }
+
return 0;
}
==== //depot/projects/soc2010/disaudit/damasterd.h#3 (text+ko) ====
@@ -48,5 +48,7 @@
int search_trail(int, struct sockaddr *);
int receive_trail(int, struct sockaddr *);
void get_from_socket(int, char *);
+void send_to_socket(int, char *);
+int check_files_equal(char *, char *, char *, char *, char *);
#endif
==== //depot/projects/soc2010/disaudit/msocket_work.c#3 (text+ko) ====
@@ -74,7 +74,7 @@
retval = accept(sfd, (struct sockaddr *) &clientaddr, &clientlen);
if (retval >= 0)
- clientinfo = (struct sockaddr *) &clientaddr;
+ memcpy(clientinfo, &clientaddr, sizeof(struct sockaddr));
return retval;
}
==== //depot/projects/soc2010/disaudit/msocket_work.h#3 (text+ko) ====
==== //depot/projects/soc2010/disaudit/shipd.c#6 (text+ko) ====
@@ -146,23 +146,27 @@
void
do_last()
{
- char last_trail[MAX_PATH_SIZE + 1];
+ char last_trail[MAX_TRAILPATH_SIZE + 1];
char message[MAX_PATH_SIZE + 30];
+ trail_entry *root = NULL;
if (get_last_trail(last_trail) == -1)
to_log("Nothing to send!");
sprintf(message, "Will send \"%s\" to %s", last_trail, master_host);
+ to_log(message);
- to_log(message);
+ add_trail_entry(&root, last_trail);
- if (send_trail(last_trail) == -1)
+ if (send_trail(root) == -1)
to_log("Error sending the last trail");
else
{
sprintf(message, "Successfully sent \"%s\" to %s", last_trail, master_host);
to_log(message);
}
+
+ delete_trail_entry(&root, DEL_ALL, 0);
}
/*
@@ -210,9 +214,9 @@
if ( is_audit_trail(dirp->d_name) ) /* It's not other file */
{
if (*path == 0) /* This is our first trail, so assumme is the last */
- strcpy(path, fullpath);
+ strcpy(path, dirp->d_name);
else if (strcmp (path, fullpath) < 0) /* Fortunately, the older a trail is, the lower lexocographic value it has */
- strcpy(path, fullpath);
+ strcpy(path, dirp->d_name);
}
}
}
@@ -261,9 +265,45 @@
}
}
-send_trail(char *path)
+send_trail(trail_entry *cur)
{
- return 0;
+ char message[MAX_PATH_SIZE + 33];
+ char fullpath[MAX_PATH_SIZE];
+ char *ptr;
+ int ret_val=1;
+ int sockfd;
+
+ strcpy(fullpath, audit_trails_dir);
+ ptr = fullpath + strlen(fullpath);
+ *ptr = '/';
+ *(++ptr) = 0;
+
+ if (init_socket(master_host, port_number, &sockfd) == -1)
+ return 0;
+
+ while (cur) /* Iterate through the items in the linked list */
+ {
+ if (!cur->found) /* Only send those that have the found flag turned off */
+ {
+ strcpy(ptr, cur->name);
+
+ /* Included in socket_work.c, this intended to implement SSL later */
+ if (do_socket_send_file(sockfd, cur->name, fullpath) != -1)
+ {
+ sprintf(message, "Error sending: %s to %s", cur->name, master_host);
+ to_log(message);
+ ret_val = -1;
+ }
+ }
+ cur = cur->next;
+ }
+
+ /* Here we tell master we are done for now */
+ do_socket_check_file(sockfd, NULL, NULL);
+
+ close(sockfd);
+
+ return ret_val;
}
/*
@@ -282,7 +322,9 @@
char message[MAX_PATH_SIZE + 30];
char *ptr;
- int n_elements = 0, i;
+ int n_elements = 0, i, index;
+
+ trail_entry *root = NULL;
if ( !(dp = opendir(audit_trails_dir)) )
{
@@ -308,6 +350,8 @@
return;
}
+printf("TT %s\n", dirp->d_name);
+
if (S_ISDIR(statbuf.st_mode) == 0) /* It's not a directory */
if ( is_audit_trail(dirp->d_name) ) /* It's not other file */
++n_elements;
@@ -345,36 +389,40 @@
qsort(trail_paths, n_elements, sizeof(*trail_paths), cmp_trails);
for (i=0; i<n_elements; ++i)
- {
- strcpy(ptr, trail_paths[i]);
- if (is_in_master(trail_paths[i], fullpath))
- break;
- }
+ if (add_trail_entry(&root, trail_paths[i]) == -1)
+ {
+ delete_trail_entry(&root, DEL_ALL, 0);
+ return;
+ }
+
+ /* Free the memory */
+ for (i=n_elements-1; i>0; --i)
+ free(trail_paths[i]);
+ free(trail_paths);
+ is_in_master(root, &index);
+
/*
- * At this point, the variable i holds the index of the first ok trail in master system
- * and we will go backwards (i-1 ... 0) in the array to sync the newer ones
+ * At this point, the variable index holds the index of the first ok trail in master system
+ * and we will go backwards (index-1 ... 0) in the array to sync the newer ones
*/
- while (i--)
+ delete_trail_entry(&root, DEL_LAST, n_elements - index);
+
+ if (!root)
+ {
+ to_log("All daemon date trails OK!");
+ return;
+ }
+
+ if (send_trail(root) == -1)
{
- strcpy(ptr, trail_paths[i]);
- if (send_trail(fullpath) == -1)
- {
- sprintf(message, "ERROR Sending \"%s\" to %s", trail_paths[i], master_host);
- to_log(message);
- }
- else
- {
- sprintf(message, "Successfully sent \"%s\" to %s", trail_paths[i], master_host);
- to_log(message);
- }
+ delete_trail_entry(&root, DEL_ALL, 0);
+ return;
}
- /* Free the memory */
- for (i=n_elements-1; i>0; --i)
- free(trail_paths[i]);
- free(trail_paths);
+ to_log("Sending Date Daemon DONE!");
+ delete_trail_entry(&root, DEL_ALL, 0);
return;
}
@@ -387,8 +435,8 @@
cmp_trails (const void *A, const void *B)
{
if (strcmp(*((char **)A), *((char **)B)) < 0)
- return 1;
- return -1;
+ return -1;
+ return 1;
}
/*
@@ -399,21 +447,52 @@
* checksums on both systems are not equal, then the function
* will return "false" since an incomplete trail is not valid here.
*/
-is_in_master(char *path, char *fullpath)
+is_in_master(trail_entry *cur, int *first_found)
{
char *md5 = (char *) malloc (sizeof(char) * 33);
char message[MAX_PATH_SIZE + 33];
+ char fullpath[MAX_PATH_SIZE];
+ char *ptr;
int ret_val=1;
- MD5_CTX context;
+ int counter=0;
+ int sockfd;
+
+ strcpy(fullpath, audit_trails_dir);
+ ptr = fullpath + strlen(fullpath);
+ *ptr = '/';
+ *(++ptr) = 0;
+
+ if (init_socket(master_host, port_number, &sockfd) == -1)
+ return 0;
+
+ while (cur)
+ {
+ /* Get the MD5 checksum for the file (notice that here we use the fullpath) */
+ strcpy(ptr, cur->name);
+ md5 = MD5File(fullpath, md5);
+ sprintf(message, "The MD5 checksum for %s is %s", cur->name, md5);
+ to_log(message);
+
+ /* Included in socket_work.c, this intended to implement SSL later */
+ if (do_socket_check_file(sockfd, cur->name, md5) != -1)
+ {
+ cur->found = 1;
+ if (first_found != NULL)
+ {
+ *first_found = counter;
+ break;
+ }
+ }
+ else
+ ret_val = 0;
+ ++counter;
+ cur = cur->next;
+ }
- /* Get the MD5 checksum for the file (notice that here we use the fullpath) */
- md5 = MD5File(fullpath, md5);
- sprintf(message, "The MD5 checksum for %s is %s", path, md5);
- to_log(message);
+ /* Here we tell master we are done for now */
+ do_socket_check_file(sockfd, NULL, NULL);
- /* Included in socket_work.c, this intended to implement SSL later */
- if (do_socket_check_file(master_host, port_number, path, md5) == -1)
- ret_val = 0;
+ close(sockfd);
free(md5);
@@ -436,6 +515,8 @@
char message[MAX_PATH_SIZE + 30];
char *ptr;
+ trail_entry *root = NULL;
+
if ( !(dp = opendir(audit_trails_dir)) )
{
to_log("Can't open directory");
@@ -462,21 +543,124 @@
if (S_ISDIR(statbuf.st_mode) == 0) /* It's not a directory */
if ( is_audit_trail(dirp->d_name) ) /* It's not other file */
- if ( !is_in_master(dirp->d_name, fullpath) )
- if (send_trail(fullpath) == -1)
- {
- sprintf(message, "ERROR Sending \"%s\" to %s", fullpath, master_host);
- to_log(message);
- }
- else
- {
- sprintf(message, "Successfully sent \"%s\" to %s", fullpath, master_host);
- to_log(message);
- }
+ if (add_trail_entry(&root, dirp->d_name) == -1)
+ {
+ delete_trail_entry(&root, DEL_ALL, 0);
+ return;
+ }
}
closedir(dp);
+ is_in_master(root, NULL);
+
+pll(root);
+
+ if (send_trail(root) == -1)
+ {
+ to_log("Error sending daemon all trails!");
+ delete_trail_entry(&root, DEL_ALL, 0);
+ return;
+ }
+
+ delete_trail_entry(&root, DEL_ALL, 0);
+ to_log("Successfully sent daemon all trails!");
return;
}
+void
+pll(trail_entry *root)
+{
+ while (root)
+ {
+ printf("TRAIL: %s - %d\n", root->name, root->found);
+ root = root->next;
+ }
+}
+
+void
+delete_trail_entry(trail_entry **root, int criteria, int number)
+{
+ switch (criteria)
+ {
+ case DEL_FIRST:
+ {
+ int i=0;
+ trail_entry *temp;
+ while (*root && i++ < number)
+ {
+ temp = *root;
+ *root = (*root)->next;
+ free(temp);
+ }
+ break;
+ }
+ case DEL_LAST:
+ {
+ trail_entry *temp = *root, *oth;
+ int total = 0;
+ while (temp)
+ {
+ ++total;
+ temp = temp->next;
+ }
+
+ number = number > total ? total : number; /* We can't delete more nodes than the ones we already have */
+
+ number = total - number; /* How many we will be left in the queue */
+
+ oth = temp = *root;
+
+ while (temp && number--)
+ {
+ temp = temp->next;
+ if (!number) /* If this will be the last node in the queue, cut it */
+ {
+ oth = temp->next; /* But save the address to delete the following nodes */
+ temp->next = NULL;
+ }
+ }
+
+ temp = oth;
+ while (temp)
+ {
+ oth = oth->next;
+ free(temp);
+ temp = oth;
+ }
+
+ *root = temp;
+ break;
+ }
+ case DEL_ALL:
+ {
+ trail_entry *temp = *root;
+ while (temp)
+ {
+ *root = (*root)->next;
+ free(temp);
+ temp = *root;
+ }
+
+ *root = temp;
+ break;
+ }
+ }
+}
+
+add_trail_entry(trail_entry **root, char *name)
+{
+ trail_entry *cur = (trail_entry *) malloc (sizeof(trail_entry));
+
+ if (!cur)
+ return -1;
+
+ strcpy(cur->name, name);
+ cur->found = 0;
+ cur->next = *root;
+
+ *root = cur;
+
+ return 0;
+}
+
==== //depot/projects/soc2010/disaudit/shipd.h#6 (text+ko) ====
@@ -35,17 +35,35 @@
#define PANIC_DATE 2
#define PANIC_ALL 3
+#define DEL_FIRST 1
+#define DEL_LAST 2
+#define DEL_ALL 3
+/* Trail Entries */
+
+typedef struct s_trail_entry
+{
+ char name[MAX_TRAILPATH_SIZE + 1];
+ int found;
+ struct s_trail_entry *next;
+} trail_entry;
+
+int add_trail_entry (trail_entry **, char *);
+
+void delete_trail_entry (trail_entry **, int, int);
+
+/* Daemon functions */
+
int get_parameters();
void to_log(char *);
void do_last();
void do_daemon();
int get_last_trail(char *);
int is_audit_trail(char *);
-int send_trail(char *);
+int send_trail(trail_entry *);
void do_daemon_date();
int cmp_trails(const void *, const void *);
-int is_in_master(char *, char *);
+int is_in_master(trail_entry *, int *);
void do_daemon_all();
-
+void pll(trail_entry *root);
#endif
==== //depot/projects/soc2010/disaudit/ssocket_work.c#4 (text+ko) ====
@@ -26,7 +26,9 @@
*/
#include <sys/types.h>
+#include <fcntl.h>
#include <sys/socket.h>
+#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
@@ -36,20 +38,29 @@
#include <unistd.h>
#include "ssocket_work.h"
-do_socket_check_file(char *host, int port, char *path, char *md5)
+/* Socket buffer management */
+unsigned char buffer[MAX_BUF_SIZE + 1];
+int bsent, brecv;
+
+do_socket_check_file(int sockfd, char *path, char *md5)
{
- int sockfd;
+ send_to_socket(sockfd, path ? "1" : "0");
+ if (path) /* If there is a file to search for */
+ {
+ char *result;
- if (init_socket(host, port, &sockfd) == -1)
- return -1;
+ send_to_socket(sockfd, path);
+ send_to_socket(sockfd, md5);
- send_to_socket(sockfd, "1");
- send_to_socket(sockfd, path);
- send_to_socket(sockfd, md5);
+ get_from_socket(sockfd, result);
- close(sockfd);
+ if (result[0] == 'y')
+ return 0;
+ else
+ return -1;
+ }
- return -1;
+ return 0;
}
void
@@ -61,6 +72,76 @@
send(sfd, data, len, 0);
}
+do_socket_send_file(int sockfd, char *path, char *fullpath)
+{
+ send_to_socket(sockfd, path ? "2" : "0");
+ if (path) /* If there is a file to send through the socket */
+ {
+ int fd = open(fullpath, O_RDONLY);
+ unsigned long file_size;
+ int bread;
+ struct stat statbuf;
+
+ if (fd < 0)
+ {
+ to_log("Error opening the file");
+ return -1;
+ }
+
+ send_to_socket(sockfd, path);
+
+ if ( stat(fullpath, &statbuf) < 0 )
+ {
+ to_log("Stat error!");
+ return -1;
+ }
+
+ file_size = statbuf.st_size;
+ send(sockfd, &file_size, sizeof(file_size), 0);
+
+ while (file_size)
+ {
+ bread = read(fd, buffer, min(file_size, MAX_BUF_SIZE));
+
+ if (bread < 0)
+ {
+ to_log("Error reading the file");
+ return -1;
+ }
+ else if (bread == 0)
+ break;
+
+ send(sockfd, &bread, sizeof(bread), 0);
+ send(sockfd, buffer, bread, 0);
+
+ file_size -= bread;
+ }
+ }
+
+ return 0;
+}
+
+void
+get_from_socket(int sfd, char *dest)
+{
+ int len, left;
+ char *ptr;
+ brecv = recv(sfd, buffer, sizeof(int), 0);
+ strncpy((char *) &len, buffer, sizeof(int));
+
+ left = len;
+ ptr = dest;
+
+ while (left > 0)
+ {
+ brecv = recv(sfd, buffer, min(MAX_BUF_SIZE, left), 0);
+ buffer[brecv] = 0;
+ strcpy(ptr, buffer);
+ ptr += brecv;
+ left -= brecv;
+ }
+}
+
is_ipv4(char *address)
{
int points=0, last_point=0, i, len=strlen(address);
>>> TRUNCATED FOR MAIL (1000 lines) <<<
More information about the p4-projects
mailing list