00001 #include "action_alert_socket.h"
00002 #include <stdio.h>
00003 #include "../engine/message.h"
00004 #include <stdlib.h>
00005 #include <string.h>
00006 #include <netdb.h>
00007 #include <sys/socket.h>
00008 #include <netinet/in.h>
00009 #include <unistd.h>
00010
00011
00012
00013 #define MAX_SOCKET_RETRIES 10
00014
00015 typedef struct action_socket_rec{
00016 unsigned int IP;
00017 unsigned short Port;
00018 int SockFD;
00019 int Retries;
00020 } ActionSocketRec;
00021
00022 extern GlobalVars Globals;
00023
00024
00025
00026
00027
00028
00029 int AlertSocketConnect(ActionSocketRec* SR){
00030 struct sockaddr_in target;
00031
00032 if ( (SR->SockFD=socket(AF_INET, SOCK_STREAM, 0)) == -1){
00033 printf("Couldn't create socket\n");
00034 SR->Retries++;
00035 return FALSE;
00036 }
00037
00038 bzero(&target, sizeof(target));
00039 target.sin_family=AF_INET;
00040 target.sin_port=SR->Port;
00041 target.sin_addr=*(struct in_addr*)&SR->IP;
00042
00043 if (connect(SR->SockFD, (struct sockaddr*)&target, sizeof(struct sockaddr))==-1){
00044 printf("Couldn't connect to host\n");
00045 SR->Retries++;
00046 return FALSE;
00047 }
00048
00049 return TRUE;
00050 }
00051
00052
00053
00054
00055 void* AlertSocketParseArgs(char* Args){
00056 ActionSocketRec* data;
00057 unsigned int IP;
00058 unsigned short Port;
00059 char* c;
00060 struct hostent* he;
00061 struct sockaddr_in target;
00062
00063 #ifdef DEBUGPATH
00064 printf("In AlertSocketParseArgs\n");
00065 #endif
00066
00067 #ifdef DEBUG
00068 printf("Parsing args for action_alert_socket\n");
00069 #endif
00070
00071 while (*Args==' ') Args++;
00072
00073 c=strchr(Args, ':');
00074 if (!c){
00075 printf("Usage: response=alert socket(IP:Port)\n");
00076 return NULL;
00077 }
00078
00079 *c=0x00;
00080 c++;
00081
00082 if ( (he=gethostbyname(Args))==NULL){
00083 printf("Couldn't resolve %s\n", Args);
00084 return NULL;
00085 }
00086
00087 IP=*(unsigned int*)he->h_addr;
00088
00089 #ifdef DEBUG
00090 printf("Connecting to %s\n",inet_ntoa(*(struct in_addr*)&IP));
00091 #endif
00092
00093 Port=atoi(c);
00094 if (!Port){
00095 printf("Invalid port number %s\n",c);
00096 return NULL;
00097 }
00098 Port=htons(Port);
00099
00100 data=(ActionSocketRec*)calloc(sizeof(ActionSocketRec),1);
00101 data->IP=IP;
00102 data->Port=Port;
00103
00104 if (!AlertSocketConnect(data)){
00105 printf("Couldn't connect to %s:%s\n",Args, c);
00106 return NULL;
00107 }
00108
00109 return data;
00110 }
00111
00112
00113
00114
00115
00116 int AlertSocketMessage(char* Message, void* Data){
00117 ActionSocketRec* data;
00118
00119 #ifdef DEBUGPATH
00120 printf("In AlsertSocketMessage\n");
00121 #endif
00122
00123 #ifdef DEBUG
00124 printf("Writing to the Alert Socket\n");
00125 #endif
00126
00127 if (!Data){
00128 #ifdef DEBUG
00129 printf("I must have a socket to write to\n");
00130 #endif
00131 return FALSE;
00132 }
00133
00134 data=(ActionSocketRec*)Data;
00135
00136 if (data->Retries>MAX_SOCKET_RETRIES) return FALSE;
00137
00138 if (write(data->SockFD, Message, strlen(Message))==-1){
00139 if (AlertSocketConnect(data))
00140 write(data->SockFD, Message, strlen(Message));
00141 }
00142 if (write(data->SockFD, "\n", 1)==-1){
00143 if (AlertSocketConnect(data))
00144 write(data->SockFD, "\n", 1);
00145 }
00146
00147 return TRUE;
00148 }
00149
00150
00151
00152
00153 int AlertSocketAction(int RuleNum, int PacketSlot, void* Data){
00154 char Buff[1024];
00155 ActionSocketRec* data;
00156 PacketRec* p;
00157
00158 #ifdef DEBUGPATH
00159 printf("In AlsertSocketAction\n");
00160 #endif
00161
00162 #ifdef DEBUG
00163 printf("Writing to the Alert Socket\n");
00164 #endif
00165
00166 if (!Data){
00167 #ifdef DEBUG
00168 printf("I must have a socket to write to\n");
00169 #endif
00170 return FALSE;
00171 }
00172
00173
00174 p=&Globals.Packets[PacketSlot];
00175 data=(ActionSocketRec*)Data;
00176
00177 if (data->Retries>MAX_SOCKET_RETRIES) return FALSE;
00178
00179 if (!ApplyMessage(Globals.AlertHeader, PacketSlot, Buff, 1024)){
00180 printf("Couldn't alert header to packet\n");
00181 return FALSE;
00182 }
00183
00184 if (write(data->SockFD, Buff, strlen(Buff))==-1){
00185 if (AlertSocketConnect(data))
00186 write(data->SockFD, Buff, strlen(Buff));
00187 }
00188 if (write(data->SockFD, " ", 1)==-1){
00189 if (AlertSocketConnect(data))
00190 write(data->SockFD, " ", 1);
00191 }
00192
00193 if (!ApplyMessage(Globals.Rules[RuleNum].MessageFormat, PacketSlot, Buff, 1024)){
00194 printf("Couldn't apply message to packet\n");
00195 return FALSE;
00196 }
00197
00198 if (write(data->SockFD, Buff, strlen(Buff))==-1){
00199 if (AlertSocketConnect(data))
00200 write(data->SockFD, Buff, strlen(Buff));
00201 }
00202
00203 if (write(data->SockFD, "\n", 1)==-1){
00204 if (AlertSocketConnect(data))
00205 write(data->SockFD, "\n", 1);
00206 }
00207
00208 return TRUE;
00209 }
00210
00211
00212
00213
00214 int InitActionAlertSocket(){
00215 int ActionID;
00216
00217 #ifdef DEBUGPATH
00218 printf("In InitActionAlertSocket\n");
00219 #endif
00220
00221 ActionID=CreateAction("alert socket");
00222 if (ActionID==ACTION_NONE){
00223 #ifdef DEBUG
00224 printf("Couldn't allocation action alert socket\n");
00225 #endif
00226 return FALSE;
00227 }
00228
00229 Globals.ActionItems[ActionID].ActionFunc=AlertSocketAction;
00230 Globals.ActionItems[ActionID].MessageFunc=AlertSocketMessage;
00231 Globals.ActionItems[ActionID].ParseArgs=AlertSocketParseArgs;
00232
00233 return TRUE;
00234 }