PERFORCE change 180091 for review
Sergio Ligregni
ligregni at FreeBSD.org
Tue Jun 22 06:34:43 UTC 2010
http://p4web.freebsd.org/@@180091?ac=10
Change 180091 by ligregni at ligPhenom on 2010/06/22 06:34:14
Client socket connection done,
ready to start master daemon (mainly to test the shipd daemon).
Affected files ...
.. //depot/projects/soc2010/disaudit/damasterd.c#1 add
.. //depot/projects/soc2010/disaudit/damasterd.h#1 add
.. //depot/projects/soc2010/disaudit/msocket_work.c#1 add
.. //depot/projects/soc2010/disaudit/msocket_work.h#1 add
.. //depot/projects/soc2010/disaudit/ssocket_work.c#2 edit
.. //depot/projects/soc2010/disaudit/ssocket_work.h#2 edit
Differences ...
==== //depot/projects/soc2010/disaudit/ssocket_work.c#2 (text+ko) ====
@@ -25,8 +25,102 @@
*
*/
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
int
do_socket_check_file(char *host, int port, char *path, char *fullpath, char *md5)
{
+ int sockfd;
+
+ if (init_socket(host, port, &sockfd) == -1)
+ return -1;
+
return -1;
}
+
+int
+is_ipv4(char *address)
+{
+ int points=0, last_point=0, i, len=strlen(address);
+
+ /* Here we will check if the string is a valid IPv4 address */
+
+ for(i=0; i<len && points<=3; ++i)
+ if (i==0 || i==len-1) /* In the first and last positions there can be only a digit */
+ {
+ if (!isdigit(address[i]))
+ break;
+ }
+ else if (address[i] == '.' && i > last_point+1) /* Counting the points and making sure there are no two consecutive points */
+ {
+ last_point = i;
+ ++points;
+ }
+ else if (!isdigit(address[i]))
+ break;
+
+ if (points == 3 && i == len)
+ return 1;
+ return 0;
+}
+
+int
+init_socket(char *host, int port, int *sfd)
+{
+ struct sockaddr_in sockaddr;
+ struct in_addr inaddr;
+ struct hostent *hostentry = NULL;
+ char message[256];
+ int sockfd, res;
+ char ipv4[16];
+
+ if (!is_ipv4(host))
+ {
+ hostentry = gethostbyname(host);
+ if (!hostentry)
+ {
+ to_log("Error gettig the host");
+ return -1;
+ }
+
+ sprintf(message, "Got for the host: %s the IPv4 address: %s", host, inet_ntoa(*((struct in_addr *)hostentry->h_addr)));
+ to_log(message);
+ }
+
+ sockfd = socket(PF_INET, SOCK_STREAM, 0);
+
+ if (sockfd == -1)
+ {
+ to_log("Cannot create socket!");
+ return -1;
+ }
+
+ bzero(&sockaddr, sizeof(sockaddr));
+
+ sockaddr.sin_family = AF_INET;
+ sockaddr.sin_addr.s_addr = hostentry != NULL ? ((struct in_addr *) hostentry->h_addr)->s_addr : inet_addr(host);
+ sockaddr.sin_port = htons(port);
+
+ res = connect(sockfd, (struct sockaddr *) &sockaddr, sizeof(sockaddr));
+
+ if (res < 0)
+ {
+ to_log("Can't connect to server!");
+ return -1;
+ }
+
+ sprintf(message, "Successfully connected to: %s", hostentry != NULL ? inet_ntoa(*((struct in_addr *) hostentry->h_addr)) : host);
+ to_log(message);
+
+ *sfd = sockfd;
+ return 0;
+}
+
==== //depot/projects/soc2010/disaudit/ssocket_work.h#2 (text+ko) ====
@@ -29,5 +29,6 @@
#define _SSOCKET_WORK_H_
int do_socket_check_file(char *, int, char *, char *, char *);
+int init_socket(char *, int, int *);
#endif
More information about the p4-projects
mailing list