blob: ba24aaabe0027e4a8e4396038bb3757b23d43c23 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#ifndef __UPNPHTTP_H__
2#define __UPNPHTTP_H__
3#include <sys/queue.h>
4#include <sys/un.h>
5
6#include "mini_upnp_global.h"
7
8/** @name UPNP_E_SUCCESS [0]
9 * {\tt UPNP_E_SUCCESS} signifies that the operation completed successfully.
10 * For asynchronous functions, this only means that the packet generated by
11 * the operation was successfully transmitted on the network. The result of
12 * the entire operation comes as part of the callback for that operation.
13 */
14//@{
15#define UPNP_E_SUCCESS 0
16//@}
17
18/** @name UPNP_E_INVALID_PARAM [-101]
19 * {\tt UPNP_E_INVALID_PARAM} signifies that one or more of the parameters
20 * passed to the function is not valid. Refer to the documentation for each
21 * function for more information on the valid ranges of the parameters.
22 */
23//@{
24#define UPNP_E_INVALID_PARAM -101
25//@}
26
27/** @name UPNP_E_OUTOF_MEMORY [-104]
28 * {\tt UPNP_E_OUTOF_MEMORY} signifies that not enough resources are
29 * currently available to complete the operation. Most operations require
30 * some free memory in order to complete their work.
31 */
32//@{
33#define UPNP_E_OUTOF_MEMORY -102
34//@}
35
36#define UPNP_E_TOOMANY_SUBSCRIBERS -103
37
38/* event ID */
39#define UPNP_EVENT_SUBSCRIPTION_REQUEST 1
40#define UPNP_EVENT_RENEWAL_COMPLETE 2
41#define UPNP_EVENT_UNSUBSCRIBE_COMPLETE 3
42
43typedef struct _IPCon {
44 char *ifname;
45} _IPCon;
46
47typedef struct _IPCon *IPCon;
48
49/*
50 States :
51 0 - Waiting for data to read
52 1 - Waiting for HTTP Post Content.
53 ...
54 >= 100 - to be deleted
55*/
56enum httpCommands {
57 EUnknown = 0,
58 EGet,
59 EPost
60};
61
62struct Upnp_Document_element {
63 char *VarName;
64 char *message;
65 LIST_ENTRY(Upnp_Document_element) entries;
66};
67
68typedef struct Upnp_Document_record {
69 unsigned int TotalMessageLen; // (total strlen(VarName) * 2) + total strlen(message)
70 unsigned short NumOfVarName;
71 LIST_HEAD(DocumentHead, Upnp_Document_element) doc_head;
72} Upnp_Document_CTX, *Upnp_Document;
73
74struct EvtRespElement {
75 int socket;
76 char sid[SID_LEN];
77 unsigned short TimeOut;
78 LIST_ENTRY(EvtRespElement) entries;
79};
80
81struct process_upnp_subscription {
82 char IP[IP_ADDRLEN];
83 int IP_inet_addr;
84 unsigned int port;
85 char sid[SID_LEN];
86 char callback_url[URL_MAX_LEN];
87 unsigned long TimeOut;
88};
89
90struct upnp_subscription_element {
91 char IP[IP_ADDRLEN];
92 int IP_inet_addr;
93 unsigned int port;
94 char sid[SID_LEN];
95 char callback_url[URL_MAX_LEN];
96 unsigned long seq;
97 unsigned long TimeOut;
98 unsigned int subscription_timeout;
99 int eventID;
100 int wscdReNewState; //add for wscd report renew state;2009-09-10
101 LIST_ENTRY(upnp_subscription_element) entries;
102};
103
104struct upnp_subscription_record {
105 char my_IP[IP_ADDRLEN];
106 unsigned int my_port;
107 unsigned short total_subscription;
108 unsigned short max_subscription_num;
109 char event_url[URL_MAX_LEN];
110 unsigned short max_subscription_time;
111 unsigned short subscription_timeout;
112 void (*EventCallBack)(struct upnp_subscription_element *sub);
113 LIST_HEAD(subscriptionlisthead, upnp_subscription_element) subscription_head;
114 LIST_HEAD(EvtResplisthead, EvtRespElement) EvtResp_head;
115};
116
117struct upnphttp {
118 int socket;
119 /* Data structure added by Sean --begin-- */
120 char IP[IP_ADDRLEN];
121 struct _soapMethods *soapMethods;
122 struct _sendDesc *sendDesc;
123 struct upnp_subscription_record *subscribe_list;
124 /* Data structure added by Sean --end-- */
125 struct in_addr clientaddr; /* client address */
126 int state;
127 char HttpVer[16];
128 /* request */
129 char * req_buf;
130 int req_buflen;
131 int req_contentlen;
132 int req_contentoff; /* header length */
133 enum httpCommands req_command;
134 char * req_soapAction;
135 int req_soapActionLen;
136 /* response */
137 char * res_buf;
138 int res_buflen;
139 int res_buf_alloclen;
140 /*int res_contentlen;*/
141 /*int res_contentoff;*/ /* header length */
142 LIST_ENTRY(upnphttp) entries;
143};
144
145struct _soapMethods {
146 char * methodName;
147 void (*methodImpl)(struct upnphttp *h);
148};
149
150struct _sendDesc {
151 char * DescName;
152 char * (*sendDescImpl)(int *len);
153};
154
155struct upnphttp * New_upnphttp(int);
156
157void CloseSocket_upnphttp(struct upnphttp *);
158
159void Delete_upnphttp(struct upnphttp *);
160
161void Process_upnphttp(struct upnphttp *);
162
163/* Build the header for the HTTP Response
164 * Also allocate the buffer for body data */
165void
166BuildHeader_upnphttp(struct upnphttp * h, int respcode,
167 const char * respmsg,
168 int bodylen);
169/* BuildResp_upnphttp() fill the res_buf buffer with the complete
170 * HTTP 200 OK response from the body passed as argument */
171void BuildResp_upnphttp(struct upnphttp *, const char *, int);
172
173/* same but with given response code/message */
174void
175BuildResp2_upnphttp(struct upnphttp * h, int respcode,
176 const char * respmsg,
177 const char * body, int bodylen);
178
179void SendResp_upnphttp(struct upnphttp *);
180
181extern Upnp_Document CreatePropertySet(void);
182extern int UpnpAddToPropertySet(Upnp_Document PropSet,
183 const char *VarName, const char *message);
184extern void UpnpSendEventSingle(Upnp_Document PropSet,
185 struct upnp_subscription_record *subscribe_list,
186 struct upnp_subscription_element *sub);
187extern void UpnpSendEventAll(Upnp_Document PropSet,
188 struct upnp_subscription_record *sub);
189extern void UpnpDocument_free(Upnp_Document PropSet);
190extern void ProcessEventingResp(struct EvtRespElement *EvtResp);
191extern int OpenAndConfHTTPSocket(const char * addr, unsigned short port);
192extern IPCon IPCon_New(char *ifname);
193extern IPCon IPCon_Destroy(IPCon this);
194extern BOOLEAN IPCon_GetIpAddr(IPCon this, struct in_addr *adr);
195extern char *IPCon_GetIpAddrByStr(IPCon this);
196extern int ILibBase64Encode(unsigned char* input, const int inputlen, unsigned char** output);
197extern int ILibBase64Decode(unsigned char* input, const int inputlen, unsigned char** output);
198extern int ReliableSend(int socket, const char *data, const int len);
199
200// support HNAP1
201extern void SendError(struct upnphttp * h, const int code, const char *title, const char *realm, const char *body);
202extern int OpenAndConfUNIXSocket(const char *file_path);
203extern int CreateUnixSocket(const char *function_name,
204 const char *file_path,
205 const int time_out);
206extern int UnixSocketSendAndReceive(const char *function_name,
207 const char *file_path,
208 const int time_out,
209 const char *in, char *out, const int out_len);
210#endif
211
212