forked from zlargon/lssdp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlssdp.h
235 lines (211 loc) · 6.83 KB
/
lssdp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef __LSSDP_H
#define __LSSDP_H
#include <stdbool.h> // bool, true, false
#include <stdint.h> // uint32_t
#ifdef WIN32
#include <WinSock2.h>
#define SOCKET_TYPE SOCKET
#else //WIN32
#define SOCKET_TYPE int
#endif //WIN32
// LSSDP Log Level
enum LSSDP_LOG {
LSSDP_LOG_DEBUG = 1 << 0,
LSSDP_LOG_INFO = 1 << 1,
LSSDP_LOG_WARN = 1 << 2,
LSSDP_LOG_ERROR = 1 << 3
};
/* Struct : lssdp_nbr */
#define LSSDP_FIELD_LEN 128
#define LSSDP_LOCATION_LEN 256
typedef struct lssdp_nbr {
char usn [LSSDP_FIELD_LEN]; // Unique Service Name (Device Name or MAC)
char location [LSSDP_LOCATION_LEN]; // URL or IP(:Port)
/* Additional SSDP Header Fields */
char sm_id [LSSDP_FIELD_LEN];
char device_type [LSSDP_FIELD_LEN];
long long update_time;
struct lssdp_nbr * next;
} lssdp_nbr;
/* Struct : lssdp_ctx */
#define LSSDP_INTERFACE_NAME_LEN 16 // IFNAMSIZ
#define LSSDP_INTERFACE_LIST_SIZE 16
#define LSSDP_IP_LEN 16
typedef struct lssdp_ctx {
SOCKET_TYPE sock; // SSDP socket
unsigned short port; // SSDP port (0x0000 ~ 0xFFFF)
lssdp_nbr * neighbor_list; // SSDP neighbor list
long neighbor_timeout; // milliseconds
bool debug; // show debug log
bool rcv_packets_from_myself; // receive packets from this machine
bool send_to_localhost; // sending to localhost as well
/* Network Interface */
size_t interface_num; // interface number
struct lssdp_interface {
char name [LSSDP_INTERFACE_NAME_LEN]; // name[16]
char ip [LSSDP_IP_LEN]; // ip[16] = "xxx.xxx.xxx.xxx"
uint32_t addr; // address in network byte order
uint32_t netmask; // mask in network byte order
} intf[LSSDP_INTERFACE_LIST_SIZE]; // interface[16]
/* SSDP Header Fields */
struct {
/* SSDP Standard Header Fields */
char search_target [LSSDP_FIELD_LEN]; // Search Target
char unique_service_name [LSSDP_FIELD_LEN]; // Unique Service Name: MAC or User Name
struct { // Location (optional):
char prefix [LSSDP_FIELD_LEN]; // Protocal: "https://" or "http://"
char domain [LSSDP_FIELD_LEN]; // if domain is empty, using Interface IP as default
char suffix [LSSDP_FIELD_LEN]; // URI or Port: "/index.html" or ":80"
} location;
/* Additional SSDP Header Fields */
char sm_id [LSSDP_FIELD_LEN];
char device_type [LSSDP_FIELD_LEN];
} header;
/* Callback Function */
int (* network_interface_changed_callback) (struct lssdp_ctx * lssdp);
int (* neighbor_list_changed_callback) (struct lssdp_ctx * lssdp);
int (* packet_received_callback) (struct lssdp_ctx * lssdp, const char * packet, size_t packet_len);
} lssdp_ctx;
/*
* 00. lssdp_init
*
* initialized the socket api within lssdp
*
* Note:
* - this is usually only necessary for window
* - do not forget to call lssdp_deinit
*
* @return = 0 success
* < 0 failed
*/
int lssdp_init();
/*
* 00. lssdp_deinit
*
* deintializes the socket api within lssdp
*
* Note:
* - this is usually only necessary for window
* - only call it after lssdp_init
*
*/
void lssdp_deinit();
/*
* 01. lssdp_network_interface_update
*
* update network interface.
*
* Note:
* - lssdp.interface, lssdp.interface_num will be updated.
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_network_interface_update(lssdp_ctx * lssdp);
/*
* 02. lssdp_socket_create
*
* create SSDP socket.
*
* Note:
* - SSDP port must be setup ready before call this function. (lssdp.port > 0)
* - if SSDP socket is already exist (lssdp.sock > 0),
* the socket will be closed, and create a new one.
* - SSDP neighbor list will be force clean up.
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_socket_create(lssdp_ctx * lssdp);
/*
* 03. lssdp_socket_close
*
* close SSDP socket.
*
* Note:
* - if SSDP socket <= 0, will be ignore, and lssdp.sock will be set to -1.
* - SSDP neighbor list will be force clean up.
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_socket_close(lssdp_ctx * lssdp);
/*
* 04. lssdp_socket_read
*
* read SSDP socket.
*
* 1. if read success, packet_received_callback will be invoked.
* 2. if received SSDP packet is match to Search Target (lssdp.header.search_target),
* - M-SEARCH: send RESPONSE back
* - NOTIFY/RESPONSE: add/update to SSDP neighbor list
*
* Note:
* - SSDP socket and port must be setup ready before call this function. (sock, port > 0)
* - if SSDP neighbor list has been changed, neighbor_list_changed_callback will be invoked.
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_socket_read(lssdp_ctx * lssdp);
/*
* 05. lssdp_send_msearch
*
* send SSDP M-SEARCH packet to multicast address (239.255.255.250)
*
* Note:
* - SSDP port must be setup ready before call this function. (lssdp.port > 0)
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_send_msearch(lssdp_ctx * lssdp);
/*
* 06. lssdp_send_notify
*
* send SSDP NOTIFY packet to multicast address (239.255.255.250)
*
* Note:
* - SSDP port must be setup ready before call this function. (lssdp.port > 0)
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_send_notify(lssdp_ctx * lssdp);
/*
* 07. lssdp_neighbor_check_timeout
*
* check neighbor in list is timeout or not. (lssdp.neighbor_timeout)
* the timeout neighbor will be remove from the list.
*
* Note:
* - if neighbor be removed, neighbor_list_changed_callback will be invoked.
*
* @param lssdp
* @return = 0 success
* < 0 failed
*/
int lssdp_neighbor_check_timeout(lssdp_ctx * lssdp);
/*
* 08. lssdp_set_log_callback
*
* setup SSDP log callback. All SSDP library log will be forward to here.
*
* @param callback
*/
void lssdp_set_log_callback(void (* callback)(const char * file, const char * tag, int level, int line, const char * func, const char * message));
/*
* 09. lssdp_get_current_time
*
* helper function to get a platform indepentend timstamp of current time
*
* @return continous time value.
*/
long long lssdp_get_current_time();
#endif