1 /*
2 Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7 		* Redistributions of source code must retain the above copyright
8 			notice, this list of conditions and the following disclaimer.
9 		* Redistributions in binary form must reproduce the above
10 			copyright notice, this list of conditions and the following
11 			disclaimer in the documentation and/or other materials provided
12 			with the distribution.
13 		* Neither the name of The Linux Foundation nor the names of its
14 			contributors may be used to endorse or promote products derived
15 			from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*!
30 	@file
31 	IPACM_log.cpp
32 
33 	@brief
34 	This file implements the IPAM log functionality.
35 
36 	@Author
37 	Skylar Chang
38 
39 */
40 #include "IPACM_Log.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <asm/types.h>
48 #include <linux/if.h>
49 #include <sys/un.h>
50 #include <errno.h>
51 #include <IPACM_Defs.h>
52 
53 /* start IPACMDIAG socket*/
create_socket(int * sockfd)54 int create_socket(int *sockfd)
55 {
56 
57   if ((*sockfd = socket(AF_UNIX, SOCK_DGRAM, 0)) == IPACM_FAILURE)
58   {
59     perror("Error creating ipacm_log socket\n");
60     return IPACM_FAILURE;
61   }
62 
63   if(fcntl(*sockfd, F_SETFD, FD_CLOEXEC) < 0)
64   {
65     perror("Couldn't set ipacm_log Close on Exec\n");
66   }
67 
68   return IPACM_SUCCESS;
69 }
70 
ipacm_log_send(void * user_data)71 void ipacm_log_send( void * user_data)
72 {
73 	ipacm_log_buffer_t ipacm_log_buffer;
74 	int numBytes=0, len;
75 	struct sockaddr_un ipacmlog_socket;
76 	static int ipacm_log_sockfd = 0;
77 
78 	if(ipacm_log_sockfd == 0)
79 	{
80 		/* start ipacm_log socket */
81 		if(create_socket(&ipacm_log_sockfd) < 0)
82 		{
83 			printf("unable to create ipacm_log socket\n");
84 			return;
85 		}
86 		printf("create ipacm_log socket successfully\n");
87 	}
88 	ipacmlog_socket.sun_family = AF_UNIX;
89 	strcpy(ipacmlog_socket.sun_path, IPACMLOG_FILE);
90 	len = strlen(ipacmlog_socket.sun_path) + sizeof(ipacmlog_socket.sun_family);
91 
92 	memcpy(ipacm_log_buffer.user_data, user_data, MAX_BUF_LEN);
93 
94 	//printf("send : %s\n", ipacm_log_buffer.user_data);
95 	if ((numBytes = sendto(ipacm_log_sockfd, (void *)&ipacm_log_buffer, sizeof(ipacm_log_buffer.user_data), 0,
96 			(struct sockaddr *)&ipacmlog_socket, len)) == -1)
97 	{
98 		printf("Send Failed(%d) %s \n",errno,strerror(errno));
99 		return;
100 	}
101 	return;
102 }
103