wps_upnp_web.c
来自「最新的Host AP 新添加了许多pcmcia 的驱动」· C语言 代码 · 共 1,965 行 · 第 1/4 页
C
1,965 行
/* * UPnP WPS Device - Web connections * Copyright (c) 2000-2003 Intel Corporation * Copyright (c) 2006-2007 Sony Corporation * Copyright (c) 2008-2009 Atheros Communications * Copyright (c) 2009, Jouni Malinen <j@w1.fi> * * See wps_upnp.c for more details on licensing and code history. */#include "includes.h"#include <fcntl.h>#include "common.h"#include "base64.h"#include "eloop.h"#include "uuid.h"#include "httpread.h"#include "wps_i.h"#include "wps_upnp.h"#include "wps_upnp_i.h"/*************************************************************************** * Web connections (we serve pages of info about ourselves, handle * requests, etc. etc.). **************************************************************************/#define WEB_CONNECTION_TIMEOUT_SEC 30 /* Drop web connection after t.o. */#define WEB_CONNECTION_MAX_READ 8000 /* Max we'll read for TCP request */#define MAX_WEB_CONNECTIONS 10 /* max simultaneous web connects */static const char *urn_wfawlanconfig = "urn:schemas-wifialliance-org:service:WFAWLANConfig:1";static const char *http_server_hdr = "Server: unspecified, UPnP/1.0, unspecified\r\n";static const char *http_connection_close = "Connection: close\r\n";/* * Incoming web connections are recorded in this struct. * A web connection is a TCP connection to us, the server; * it is called a "web connection" because we use http and serve * data that looks like web pages. * State information is need to track the connection until we figure * out what they want and what we want to do about it. */struct web_connection { /* double linked list */ struct web_connection *next; struct web_connection *prev; struct upnp_wps_device_sm *sm; /* parent */ int sd; /* socket to read from */ struct sockaddr_in cli_addr; int sd_registered; /* nonzero if we must cancel registration */ struct httpread *hread; /* state machine for reading socket */ int n_rcvd_data; /* how much data read so far */ int done; /* internal flag, set when we've finished */};/* * XML parsing and formatting * * XML is a markup language based on unicode; usually (and in our case, * always!) based on utf-8. utf-8 uses a variable number of bytes per * character. utf-8 has the advantage that all non-ASCII unicode characters are * represented by sequences of non-ascii (high bit set) bytes, whereas ASCII * characters are single ascii bytes, thus we can use typical text processing. * * (One other interesting thing about utf-8 is that it is possible to look at * any random byte and determine if it is the first byte of a character as * versus a continuation byte). * * The base syntax of XML uses a few ASCII punctionation characters; any * characters that would appear in the payload data are rewritten using * sequences, e.g., & for ampersand(&) and < for left angle bracket (<). * Five such escapes total (more can be defined but that does not apply to our * case). Thus we can safely parse for angle brackets etc. * * XML describes tree structures of tagged data, with each element beginning * with an opening tag <label> and ending with a closing tag </label> with * matching label. (There is also a self-closing tag <label/> which is supposed * to be equivalent to <label></label>, i.e., no payload, but we are unlikely * to see it for our purpose). * * Actually the opening tags are a little more complicated because they can * contain "attributes" after the label (delimited by ascii space or tab chars) * of the form attribute_label="value" or attribute_label='value'; as it turns * out we do not have to read any of these attributes, just ignore them. * * Labels are any sequence of chars other than space, tab, right angle bracket * (and ?), but may have an inner structure of <namespace><colon><plain_label>. * As it turns out, we can ignore the namespaces, in fact we can ignore the * entire tree hierarchy, because the plain labels we are looking for will be * unique (not in general, but for this application). We do however have to be * careful to skip over the namespaces. * * In generating XML we have to be more careful, but that is easy because * everything we do is pretty canned. The only real care to take is to escape * any special chars in our payload. *//** * xml_next_tag - Advance to next tag * @in: Input * @out: OUT: start of tag just after '<' * @out_tagname: OUT: start of name of tag, skipping namespace * @end: OUT: one after tag * Returns: 0 on success, 1 on failure * * A tag has form: * <left angle bracket><...><right angle bracket> * Within the angle brackets, there is an optional leading forward slash (which * makes the tag an ending tag), then an optional leading label (followed by * colon) and then the tag name itself. * * Note that angle brackets present in the original data must have been encoded * as < and > so they will not trouble us. */static int xml_next_tag(char *in, char **out, char **out_tagname, char **end){ while (*in && *in != '<') in++; if (*in != '<') return 1; *out = ++in; if (*in == '/') in++; *out_tagname = in; /* maybe */ while (isalnum(*in) || *in == '-') in++; if (*in == ':') *out_tagname = ++in; while (*in && *in != '>') in++; if (*in != '>') return 1; *end = ++in; return 0;}/* xml_data_encode -- format data for xml file, escaping special characters. * * Note that we assume we are using utf8 both as input and as output! * In utf8, characters may be classed as follows: * 0xxxxxxx(2) -- 1 byte ascii char * 11xxxxxx(2) -- 1st byte of multi-byte char w/ unicode value >= 0x80 * 110xxxxx(2) -- 1st byte of 2 byte sequence (5 payload bits here) * 1110xxxx(2) -- 1st byte of 3 byte sequence (4 payload bits here) * 11110xxx(2) -- 1st byte of 4 byte sequence (3 payload bits here) * 10xxxxxx(2) -- extension byte (6 payload bits per byte) * Some values implied by the above are however illegal because they * do not represent unicode chars or are not the shortest encoding. * Actually, we can almost entirely ignore the above and just do * text processing same as for ascii text. * * XML is written with arbitrary unicode characters, except that five * characters have special meaning and so must be escaped where they * appear in payload data... which we do here. */static void xml_data_encode(struct wpabuf *buf, const char *data, int len){ int i; for (i = 0; i < len; i++) { u8 c = ((u8 *) data)[i]; if (c == '<') { wpabuf_put_str(buf, "<"); continue; } if (c == '>') { wpabuf_put_str(buf, ">"); continue; } if (c == '&') { wpabuf_put_str(buf, "&"); continue; } if (c == '\'') { wpabuf_put_str(buf, "'"); continue; } if (c == '"') { wpabuf_put_str(buf, """); continue; } /* * We could try to represent control characters using the * sequence: &#x; where x is replaced by a hex numeral, but not * clear why we would do this. */ wpabuf_put_u8(buf, c); }}/* xml_add_tagged_data -- format tagged data as a new xml line. * * tag must not have any special chars. * data may have special chars, which are escaped. */static void xml_add_tagged_data(struct wpabuf *buf, const char *tag, const char *data){ wpabuf_printf(buf, "<%s>", tag); xml_data_encode(buf, data, os_strlen(data)); wpabuf_printf(buf, "</%s>\n", tag);}/* A POST body looks something like (per upnp spec): * <?xml version="1.0"?> * <s:Envelope * xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" * s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> * <s:Body> * <u:actionName xmlns:u="urn:schemas-upnp-org:service:serviceType:v"> * <argumentName>in arg value</argumentName> * other in args and their values go here, if any * </u:actionName> * </s:Body> * </s:Envelope> * * where : * s: might be some other namespace name followed by colon * u: might be some other namespace name followed by colon * actionName will be replaced according to action requested * schema following actionName will be WFA scheme instead * argumentName will be actual argument name * (in arg value) will be actual argument value */static intupnp_get_first_document_item(char *doc, const char *item, char **value){ const char *match = item; int match_len = os_strlen(item); char *tag; char *tagname; char *end; *value = NULL; /* default, bad */ /* * This is crude: ignore any possible tag name conflicts and go right * to the first tag of this name. This should be ok for the limited * domain of UPnP messages. */ for (;;) { if (xml_next_tag(doc, &tag, &tagname, &end)) return 1; doc = end; if (!os_strncasecmp(tagname, match, match_len) && *tag != '/' && (tagname[match_len] == '>' || !isgraph(tagname[match_len]))) { break; } } end = doc; while (*end && *end != '<') end++; *value = os_zalloc(1 + (end - doc)); if (*value == NULL) return 1; os_memcpy(*value, doc, end - doc); return 0;}/* * "Files" that we serve via HTTP. The format of these files is given by * WFA WPS specifications. Extra white space has been removed to save space. */static const char wps_scpd_xml[] ="<?xml version=\"1.0\"?>\n""<scpd xmlns=\"urn:schemas-upnp-org:service-1-0\">\n""<specVersion><major>1</major><minor>0</minor></specVersion>\n""<actionList>\n""<action>\n""<name>GetDeviceInfo</name>\n""<argumentList>\n""<argument>\n""<name>NewDeviceInfo</name>\n""<direction>out</direction>\n""<relatedStateVariable>DeviceInfo</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>PutMessage</name>\n""<argumentList>\n""<argument>\n""<name>NewInMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>InMessage</relatedStateVariable>\n""</argument>\n""<argument>\n""<name>NewOutMessage</name>\n""<direction>out</direction>\n""<relatedStateVariable>OutMessage</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>GetAPSettings</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""<argument>\n""<name>NewAPSettings</name>\n""<direction>out</direction>\n""<relatedStateVariable>APSettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>SetAPSettings</name>\n""<argumentList>\n""<argument>\n""<name>APSettings</name>\n""<direction>in</direction>\n""<relatedStateVariable>APSettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>DelAPSettings</name>\n""<argumentList>\n""<argument>\n""<name>NewAPSettings</name>\n""<direction>in</direction>\n""<relatedStateVariable>APSettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>GetSTASettings</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""<argument>\n""<name>NewSTASettings</name>\n""<direction>out</direction>\n""<relatedStateVariable>STASettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>SetSTASettings</name>\n""<argumentList>\n""<argument>\n""<name>NewSTASettings</name>\n""<direction>out</direction>\n""<relatedStateVariable>STASettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>DelSTASettings</name>\n""<argumentList>\n""<argument>\n""<name>NewSTASettings</name>\n""<direction>in</direction>\n""<relatedStateVariable>STASettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>PutWLANResponse</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""<argument>\n""<name>NewWLANEventType</name>\n""<direction>in</direction>\n""<relatedStateVariable>WLANEventType</relatedStateVariable>\n""</argument>\n""<argument>\n""<name>NewWLANEventMAC</name>\n""<direction>in</direction>\n""<relatedStateVariable>WLANEventMAC</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>SetSelectedRegistrar</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>RebootAP</name>\n""<argumentList>\n""<argument>\n""<name>NewAPSettings</name>\n""<direction>in</direction>\n""<relatedStateVariable>APSettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>ResetAP</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>RebootSTA</name>\n""<argumentList>\n""<argument>\n""<name>NewSTASettings</name>\n""<direction>in</direction>\n""<relatedStateVariable>APSettings</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""<action>\n""<name>ResetSTA</name>\n""<argumentList>\n""<argument>\n""<name>NewMessage</name>\n""<direction>in</direction>\n""<relatedStateVariable>Message</relatedStateVariable>\n""</argument>\n""</argumentList>\n""</action>\n""</actionList>\n""<serviceStateTable>\n""<stateVariable sendEvents=\"no\">\n""<name>Message</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>InMessage</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>OutMessage</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>DeviceInfo</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>APSettings</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"yes\">\n""<name>APStatus</name>\n""<dataType>ui1</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>STASettings</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"yes\">\n""<name>STAStatus</name>\n""<dataType>ui1</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"yes\">\n""<name>WLANEvent</name>\n""<dataType>bin.base64</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>WLANEventType</name>\n""<dataType>ui1</dataType>\n""</stateVariable>\n""<stateVariable sendEvents=\"no\">\n""<name>WLANEventMAC</name>\n""<dataType>string</dataType>\n""</stateVariable>\n"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?