2 * Copyright (c) 2007, IRTrans GmbH
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 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 copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of IRTrans GmbH nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
37 #include <sys/timeb.h>
39 #define MSG_NOSIGNAL 0
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
56 #include <arpa/inet.h>
68 #define closesocket close
84 BOOL WINAPI
ShutdownHandler (DWORD type
);
85 extern WSAEVENT IrtLanEvent
;
88 extern int protocol_version
;
89 int AnalyzeUDPString (char *st
,int *netcom
,char *remote
,char *command
,char *ccf
,int *netmask
,int *bus
,int *led
,int *port
);
91 void CloseIRSocket (int client
);
92 int FlashHTML (byte
*command
,byte result
[]);
93 int GetHTMLFileList (byte
*command
,byte result
[]);
94 int IRTransLanFlash (DEVICEINFO
*dev
,IRDATA_LAN_FLASH
*ird
,int len
,uint32_t ip
);
95 int SendASCII (byte
*command
,byte result
[]);
96 int GetRemotelist (byte
*data
,byte result
[]);
97 int GetCommandlist (byte
*data
,byte result
[]);
99 void SwapWordN (word
*pnt
);
100 void SwapIntN (int32_t *pnt
);
102 void DoExecuteASCIICommand (byte command
[],SOCKET sockfd
,int client
)
104 int len
,errcnt
,sz
,res
;
107 memset (result
,0,sizeof (result
));
108 if (!strncmp (command
,"GET_HTML_FILELIST",17)) {
109 len
= GetHTMLFileList (command
+17,result
);
111 else if (!strncmp (command
,"FLASH_HTML",10)) {
112 len
= FlashHTML (command
+10,result
);
114 else if (!strncmp (command
,"snd",3)) {
115 len
= SendASCII (command
,result
);
117 else if (!strncmp (command
,"getremotes",10)) {
118 len
= GetRemotelist (command
,result
);
120 else if (!strncmp (command
,"getcommands",11)) {
121 len
= GetCommandlist (command
,result
);
125 sprintf (result
,"Illegal ASCII Command: %s\n",command
);
126 log_print (result
,LOG_ERROR
);
130 sprintf (result
+2,"%05d",len
);
132 log_print (result
,LOG_INFO
);
135 while (sz
< len
&& errcnt
< 20) {
136 res
= send (sockfd
,result
+ sz
,len
- sz
,MSG_NOSIGNAL
);
137 if (res
> 0) sz
+= res
;
144 if (res
<= 0 && len
) {
145 CloseIRSocket (client
);
146 sprintf (result
,"IP Connection lost\n");
147 log_print (result
,LOG_ERROR
);
151 int GetCommandlist (byte
*data
,byte result
[])
158 while (data
[i
] && data
[i
] != ',') i
++;
159 if (data
[i
]) data
[i
++] = 0;
161 res
= GetCommandDatabase (&cb
,data
+12,atoi (data
+i
));
163 sprintf (result
,"**00000 RESULT ERROR: Remote %s not found\n",data
+12);
164 log_print (result
+15, LOG_ERROR
);
165 return ((int)strlen (result
));
168 sprintf (result
,"**00000 COMMANDLIST %d,%d,%d",cb
.offset
,cb
.count_total
,cb
.count_buffer
);
170 for (i
=0;i
< cb
.count_buffer
;i
++) {
173 while (j
&& cb
.commands
[i
][j
] == ' ') j
--;
174 cb
.commands
[i
][j
+1] = 0;
175 strcat (result
,cb
.commands
[i
]);
177 strcat (result
,"\n");
179 return ((int)strlen (result
));
183 int GetRemotelist (byte
*data
,byte result
[])
187 GetRemoteDatabase (&rb
,atoi (data
+ 11));
189 sprintf (result
,"**00000 REMOTELIST %d,%d,%d",rb
.offset
,rb
.count_total
,rb
.count_buffer
);
191 for (i
=0;i
< rb
.count_buffer
;i
++) {
194 while (j
&& rb
.remotes
[i
].name
[j
] == ' ') j
--;
195 rb
.remotes
[i
].name
[j
+1] = 0;
196 strcat (result
,rb
.remotes
[i
].name
);
198 strcat (result
,"\n");
200 return ((int)strlen (result
));
204 int SendASCII (byte
*data
,byte result
[])
208 char err
[256],txt
[256];
209 char remote
[80],command
[20],ccf
[2048];
210 int netcom
,netmask
,bus
,led
,port
;
212 res
= AnalyzeUDPString (data
,&netcom
,remote
,command
,ccf
,&netmask
,&bus
,&led
,&port
);
214 log_print ("Illegal IRTrans ASCII Command\n", LOG_ERROR
);
215 strcpy (result
,"**00000 RESULT FORMAT ERROR\n");
216 return ((int)strlen (result
));
219 sprintf (txt
,"IRTrans ASCII Command: %d %s,%s,%d,%d\n", netcom
,remote
,command
,bus
,led
);
220 log_print (txt
,LOG_DEBUG
);
224 if (netmask
) adr
|= 0x10000 | netmask
;
225 adr
|= (led
& 3) << 17;
226 if (bus
== 255) adr
|= 0x40000000;
227 else adr
|= bus
<< 19;
228 protocol_version
= 210;
231 res
= DBFindRemoteCommand (remote
,command
,&cmd_num
,NULL
);
235 case ERR_REMOTENOTFOUND
:
236 sprintf (err
, txt
, remote
);
238 case ERR_COMMANDNOTFOUND
:
239 sprintf (err
, txt
, command
);
245 sprintf (result
,"**00000 RESULT ERROR: %s",err
);
246 log_print (err
, LOG_ERROR
);
247 return ((int)strlen (result
));
249 SendIR (cmd_num
,adr
);
250 strcpy (result
,"**00000 RESULT OK\n");
252 return ((int)strlen (result
));
257 int FlashHTML (byte
*command
,byte result
[])
263 unsigned int mem
[65536];
266 IRDATA_LAN_FLASH ird
;
269 while (*command
== ' ') command
++;
274 memset (mem
,0,sizeof (mem
));
275 dir
= (HTTP_DIRECTORY
*)mem
;
276 memset (&ird
,0,sizeof (IRDATA_LAN_FLASH
));
281 while (command
[i
] != ';') i
++;
285 sprintf (fname
,"..\\html\\%s",command
+p
);
286 fp
= fopen (fname
,"rb");
288 sprintf (fname
,"../html/%s",command
+p
);
289 fp
= fopen (fname
,"r");
292 strncpy (dir
->dir
[cnt
].name
,command
+p
,22);
293 dir
->dir
[cnt
].name
[22] = 0;
296 fstat (_fileno(fp
),&fst
);
298 fstat (fileno(fp
),&fst
);
301 dir
->dir
[cnt
].timestamp
= (unsigned int)(fst
.st_mtime
+ ((unsigned int)70 * 365 * 24 * 3600) + ((unsigned int)17 * 24 * 3600)); // Umrechnung auf NTP Format
302 SwapIntN (&dir
->dir
[cnt
].timestamp
);
304 if (fst
.st_size
> 0xffff) dir
->dir
[cnt
].len
= 0xffff;
305 else dir
->dir
[cnt
].len
= (word
)fst
.st_size
;
307 if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 4,".txt")) dir
->dir
[cnt
].filetype
= CONTENT_PLAIN
| EXTERNAL_FILE
;
308 else if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 4,".htm")) dir
->dir
[cnt
].filetype
= CONTENT_HTML
| EXTERNAL_FILE
;
309 else if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 5,".html")) dir
->dir
[cnt
].filetype
= CONTENT_HTML
| EXTERNAL_FILE
;
310 else if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 4,".jpg")) dir
->dir
[cnt
].filetype
= CONTENT_JPEG
| EXTERNAL_FILE
;
311 else if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 5,".jpeg")) dir
->dir
[cnt
].filetype
= CONTENT_JPEG
| EXTERNAL_FILE
;
312 else if (!strcmp (dir
->dir
[cnt
].name
+ strlen (dir
->dir
[cnt
].name
) - 4,".gif")) dir
->dir
[cnt
].filetype
= CONTENT_GIF
| EXTERNAL_FILE
;
313 else dir
->dir
[cnt
].filetype
= EXTERNAL_FILE
;
321 dir
->count
= (word
)cnt
;
322 dir
->magic
= F_MAGIC
;
323 pos
= (cnt
* sizeof (HTTP_DIRENTRY
)) / 4 + 1;
325 for (cnt
=0;cnt
< dir
->count
&& pos
< 32768;cnt
++) {
327 sprintf (fname
,"..\\html\\%s",dir
->dir
[cnt
].name
);
328 fp
= fopen (fname
,"rb");
330 sprintf (fname
,"../html/%s",dir
->dir
[cnt
].name
);
331 fp
= fopen (fname
,"r");
335 fread (&mem
[pos
],1,dir
->dir
[cnt
].len
,fp
);
338 dir
->dir
[cnt
].adr
= pos
;
339 pos
+= (dir
->dir
[cnt
].len
+ 3) / 4;
341 SwapWordN (&dir
->dir
[cnt
].adr
);
342 SwapWordN (&dir
->dir
[cnt
].len
);
344 SwapWordN (&dir
->count
);
345 SwapWordN (&dir
->magic
);
347 sprintf (fname
,"HTML Size: %d\n",pos
* 4);
348 log_print (fname
,LOG_INFO
);
351 sprintf (fname
,"HTML Size %d (Max. is 128K)\n",pos
* 4);
352 log_print (fname
,LOG_ERROR
);
353 sprintf (result
,"**00000 RESULT_HTML_FLASH E%s\n",fname
);
354 return ((int)strlen (result
));
360 while (adr
< pos
&& p
!= 'E') {
366 SwapWordN (&ird
.adr
);
367 SwapWordN (&ird
.len
);
369 memcpy (ird
.data
,mem
+ adr
,flashpage
);
370 ird
.netcommand
= COMMAND_FLASH_HTML
;
372 p
= IRTransLanFlash (IRDevices
,&ird
,flashpage
,IRDevices
[0].io
.IPAddr
[0].sin_addr
.S_un
.S_addr
);
374 p
= IRTransLanFlash (IRDevices
,&ird
,flashpage
,IRDevices
[0].io
.IPAddr
[0].sin_addr
.s_addr
);
378 } while (p
== 'E' && i
< 5);
379 adr
+= flashpage
/ 4;
383 strcpy (result
,"**00000 RESULT_HTML_FLASH E\n");
386 sprintf (result
,"**00000 RESULT_HTML_FLASH O%d\n",pos
* 4);
388 return ((int)strlen (result
));
393 int GetHTMLFileList (byte
*command
,byte result
[])
403 struct _finddata_t c_file
;
418 while (*command
== ' ') command
++;
420 memcpy (result
,"**00000 RESULT_HTML_FILELIST ",29);
424 if((hFile
= _findfirst( "..\\html\\*.*", &c_file
)) != -1L) {
425 do if (c_file
.attrib
!= _A_SUBDIR
) {
426 sprintf (fname
,"..\\html\\%s",c_file
.name
);
427 fp
= fopen (fname
,"r");
429 fstat (_fileno(fp
),&fst
);
430 strncpy (result
+len
,c_file
.name
,22);
433 atime
= localtime (&fst
.st_mtime
);
434 sprintf (st
,"%d;%02d.%02d.%04d %02d:%02d;",fst
.st_size
,atime
->tm_mday
,atime
->tm_mon
+1,atime
->tm_year
+ 1900,atime
->tm_hour
,atime
->tm_min
);
437 len
= (word
)strlen (result
);
441 } while( _findnext( hFile
, &c_file
) == 0);
447 fd
= open ("../html",0);
449 lend
= getdirentries (fd
,mem
,2048,&off
);
452 di
= (struct dirent
*)&mem
[pos
];
454 sprintf (fname
,"../html/%s",di
->d_name
);
455 fp
= fopen (fname
,"r");
456 if (fp
&& !fstat (fileno(fp
),&fst
) && S_ISREG (fst
.st_mode
)) {
457 strncpy (result
+len
,di
->d_name
,22);
460 atime
= localtime (&fst
.st_mtime
);
461 sprintf (st
,"%d;%02d.%02d.%04d %02d:%02d;",fst
.st_size
,atime
->tm_mday
,atime
->tm_mon
+1,atime
->tm_year
+ 1900,atime
->tm_hour
,atime
->tm_min
);
464 len
= strlen (result
);
469 pos
+= di
-> d_reclen
;
476 strcat (result
,"\n");