//This is called when the headers have been received and the connection is ready to send
//the result headers and data.
//We need to find the CGI function to call, call it, and dependent on what it returns either
//find the next cgi function, wait till the cgi data is sent or close up the connection.
static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) {
	int r;
	int i=0;
	if (conn->url==NULL) {
		os_printf("WtF? url = NULL\n");
		return; //Shouldn't happen
	}
	//See if we can find a CGI that's happy to handle the request.
	while (1) {
		//Look up URL in the built-in URL table.
		while (builtInUrls[i].url!=NULL) {
			int match=0;
			//See if there's a literal match
			if (os_strcmp(builtInUrls[i].url, conn->url)==0) match=1;
			//See if there's a wildcard match
			if (builtInUrls[i].url[os_strlen(builtInUrls[i].url)-1]=='*' &&
					os_strncmp(builtInUrls[i].url, conn->url, os_strlen(builtInUrls[i].url)-1)==0) match=1;
			if (match) {
				os_printf("Is url index %d\n", i);
				conn->cgiData=NULL;
				conn->cgi=builtInUrls[i].cgiCb;
				conn->cgiArg=builtInUrls[i].cgiArg;
				break;
			}
			i++;
		}
		if (builtInUrls[i].url==NULL) {
			//Drat, we're at the end of the URL table. This usually shouldn't happen. Well, just
			//generate a built-in 404 to handle this.
			os_printf("%s not found. 404!\n", conn->url);
			httpdSend(conn, httpNotFoundHeader, -1);
			xmitSendBuff(conn);
			conn->cgi=NULL; //mark for destruction
			return;
		}
		
		//Okay, we have a CGI function that matches the URL. See if it wants to handle the
		//particular URL we're supposed to handle.
		r=conn->cgi(conn);
		if (r==HTTPD_CGI_MORE) {
			//Yep, it's happy to do so and has more data to send.
			xmitSendBuff(conn);
			return;
		} else if (r==HTTPD_CGI_DONE) {
			//Yep, it's happy to do so and already is done sending data.
			xmitSendBuff(conn);
			conn->cgi=NULL; //mark conn for destruction
			return;
		} else if (r==HTTPD_CGI_NOTFOUND || r==HTTPD_CGI_AUTHENTICATED) {
			//URL doesn't want to handle the request: either the data isn't found or there's no
			//need to generate a login screen.
			i++; //look at next url the next iteration of the loop.
		}
	}
}
LOCAL void ICACHE_FLASH_ATTR start_resolve_dh_server() {
	static ip_addr_t ip;
	const char *server = dhrequest_current_server();
	char host[os_strlen(server) + 1];
	const char *fr = server;
	while(*fr != ':') {
		fr++;
		if(*fr == 0) {
			fr = 0;
			break;
		}
	}
	if(fr) {
		fr++;
		if(*fr != '/')
			fr = 0;
	}
	if (fr) {
		while (*fr == '/')
			fr++;
		int i = 0;
		while (*fr != '/' && *fr != ':' && *fr != 0)
			host[i++] = *fr++;
		// read port if present
		int port = 0;
		if(*fr == ':') {
			unsigned char d;
			fr++;
			while ( (d = *fr - 0x30) < 10) {
				fr++;
				port = port*10 + d;
				if(port > 0xFFFF)
					break;
			}
		}
		if(port && port < 0xFFFF)
			mDHConnector.proto.tcp->remote_port = port;
		else if (os_strncmp(dhrequest_current_server(), "https", 5) == 0)
			mDHConnector.proto.tcp->remote_port = 443; // HTTPS default port
		else
			mDHConnector.proto.tcp->remote_port = 80; //HTTP default port
		host[i] = 0;
		dhdebug("Resolving %s", host);
		err_t r = espconn_gethostbyname(&mDHConnector, host, &ip, resolve_cb);
		if(r == ESPCONN_OK) {
			resolve_cb(host, &ip, NULL);
		} else if(r != ESPCONN_INPROGRESS) {
			dhesperrors_espconn_result("Resolving failed:", r);
			arm_repeat_timer(RETRY_CONNECTION_INTERVAL_MS);
		}
	} else {
		dhdebug("Can not find scheme in server url");
	}
}
Exemple #3
0
char * os_strstr(const char *haystack, const char *needle)
{
	size_t len = os_strlen(needle);
	while (*haystack) {
		if (os_strncmp(haystack, needle, len) == 0)
			return (char *) haystack;
		haystack++;
	}

	return NULL;
}
static int eap_sim_db_open_socket(struct eap_sim_db_data *data)
{
	struct sockaddr_un addr;
	static int counter = 0;

	if (os_strncmp(data->fname, "unix:", 5) != 0)
		return -1;

	data->sock = socket(PF_UNIX, SOCK_DGRAM, 0);
	if (data->sock < 0) {
		wpa_printf(MSG_INFO, "socket(eap_sim_db): %s", strerror(errno));
		return -1;
	}

	os_memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	os_snprintf(addr.sun_path, sizeof(addr.sun_path),
		    "/tmp/eap_sim_db_%d-%d", getpid(), counter++);
	os_free(data->local_sock);
	data->local_sock = os_strdup(addr.sun_path);
	if (data->local_sock == NULL) {
		close(data->sock);
		data->sock = -1;
		return -1;
	}
	if (bind(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		wpa_printf(MSG_INFO, "bind(eap_sim_db): %s", strerror(errno));
		close(data->sock);
		data->sock = -1;
		return -1;
	}

	os_memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	os_strlcpy(addr.sun_path, data->fname + 5, sizeof(addr.sun_path));
	if (connect(data->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		wpa_printf(MSG_INFO, "connect(eap_sim_db): %s",
			   strerror(errno));
		wpa_hexdump_ascii(MSG_INFO, "HLR/AuC GW socket",
				  (u8 *) addr.sun_path,
				  os_strlen(addr.sun_path));
		close(data->sock);
		data->sock = -1;
		unlink(data->local_sock);
		os_free(data->local_sock);
		data->local_sock = NULL;
		return -1;
	}

	eloop_register_read_sock(data->sock, eap_sim_db_receive, data, NULL);

	return 0;
}
static void ICACHE_FLASH_ATTR
tcpserver_recv_callback(void *arg, char *pdata, uint16_t length)
{
    if(os_strncmp(OTA_UPGRADE_COMMAND, pdata, length) == 0)
    {
        remot_info *phost_info = NULL;

        os_printf("Upgrade command received.\r\n");
        espconn_get_connection_info(&tcp_server, &phost_info, 0);
        ota_upgrade(phost_info);
    }
}
Exemple #6
0
static int wpa_supplicant_ctrl_iface_blacklist(
		struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
{
	struct wpa_ssid *ssid;
	u8 bssid[ETH_ALEN];
	struct wpa_blacklist *e;
	char *pos, *end;
	int ret;

	/* cmd: "BLACKLIST [<BSSID>]" */
	if (*cmd == '\0') {
		pos = buf;
		end = buf + buflen;

		e = wpa_s->blacklist;
		while (e) {
			ret = os_snprintf(pos, end-pos,
					  "%02x:%02x:%02x:%02x:%02x:%02x\n",
					  e->bssid[0],
					  e->bssid[1],
					  e->bssid[2],
					  e->bssid[3],
					  e->bssid[4],
					  e->bssid[5]);
			if (ret < 0 || ret >= end - pos)
				return pos - buf;
			pos += ret;
			e = e->next;
		}
		return pos - buf;
	}
	wpa_printf(MSG_DEBUG, "CTRL_IFACE: bssid='%s'", cmd);

	++cmd;
	if (os_strncmp(cmd, "clear", 5) == 0) {
		wpa_blacklist_clear(wpa_s);
		return 0;
	}

	if (hwaddr_aton(cmd, bssid)) {
		wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", cmd);
		return -1;
	}

	/*
	 * Add the BSSID twice, so its count will be 2, causing it to be
	 * skipped when processing scan results.
	 */
	ret = wpa_blacklist_add(wpa_s, bssid);
	if (ret != 0)
		return ret;
	return wpa_blacklist_add(wpa_s, bssid);
}
LOCAL void ICACHE_FLASH_ATTR
user_udp_recv(void *arg, char *pusrdata, unsigned short length)
{
	char DeviceBuffer[40] = { 0 };
	char Device_mac_buffer[60] = { 0 };
	char hwaddr[6];

	struct ip_info ipconfig;

	if (wifi_get_opmode() != STATION_MODE) {
		wifi_get_ip_info(SOFTAP_IF, &ipconfig);
		wifi_get_macaddr(SOFTAP_IF, hwaddr);

		if (!ip_addr_netcmp
		    ((struct ip_addr *)ptrespconn.proto.udp->remote_ip,
		     &ipconfig.ip, &ipconfig.netmask)) {
			//udp packet is received from ESP8266 station
			wifi_get_ip_info(STATION_IF, &ipconfig);
			wifi_get_macaddr(STATION_IF, hwaddr);
		} else {
			//udp packet is received from ESP8266 softAP
		}

	} else {
		//udp packet is received from ESP8266 station
		wifi_get_ip_info(STATION_IF, &ipconfig);
		wifi_get_macaddr(STATION_IF, hwaddr);
	}

	if (pusrdata == NULL)
		return;

	if (length == os_strlen(device_find_request) &&
	    os_strncmp(pusrdata, device_find_request,
		       os_strlen(device_find_request)) == 0) {
		//received device find message

		os_sprintf(DeviceBuffer, "%s" MACSTR " " IPSTR,
			   device_find_response_ok, MAC2STR(hwaddr),
			   IP2STR(&ipconfig.ip));

		os_printf("%s\n", DeviceBuffer);
		length = os_strlen(DeviceBuffer);

		//if received "Are You ESP8266 ?" ,
		//response "Yes,I'm ESP8266!" + ESP8266 mac + ESP8266 ip
		espconn_sent(&ptrespconn, DeviceBuffer, length);

	} else {
		//received some other data
	}

}
static int p2p_wfa_service_adv(struct p2p_data *p2p)
{
	struct p2ps_advertisement *adv;

	for (adv = p2p->p2ps_adv_list; adv; adv = adv->next) {
		if (os_strncmp(adv->svc_name, P2PS_WILD_HASH_STR,
			       os_strlen(P2PS_WILD_HASH_STR)) == 0)
			return 1;
	}

	return 0;
}
Exemple #9
0
static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
	int i;
	char first_line = false;

	if (os_strncmp(h, "GET ", 4)==0) {
		conn->requestType = HTTPD_METHOD_GET;
		first_line = true;
	} else if (os_strncmp(h, "Host:", 5)==0) {
		i=5;
		while (h[i]==' ') i++;
		conn->hostName=&h[i];
	} else if (os_strncmp(h, "POST ", 5)==0) {
		conn->requestType = HTTPD_METHOD_POST;
		first_line = true;
	}

	if (first_line) {
		char *e;

		//Skip past the space after POST/GET
		i=0;
		while (h[i]!=' ') i++;
		conn->url=h+i+1;

		//Figure out end of url.
		e=(char*)os_strstr(conn->url, " ");
		if (e==NULL) return; // ?
		*e=0; //terminate url part

		//Parse out the URL part before the GET parameters.
		conn->getArgs=(char*)os_strstr(conn->url, "?");
		if (conn->getArgs!=0) {
			*conn->getArgs=0;
			conn->getArgs++;
			TESTP("GET args = %s\n", conn->getArgs);
		} else {
			conn->getArgs=NULL;
		}
	}
}
//Parse a line of header data and modify the connection data accordingly.
static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) {
	int i;
//	os_printf("Got header %s\n", h);
	if (os_strncmp(h, "GET ", 4)==0 || os_strncmp(h, "POST ", 5)==0) {
		char *e;
		
		//Skip past the space after POST/GET
		i=0;
		while (h[i]!=' ') i++;
		conn->url=h+i+1;

		//Figure out end of url.
		e=(char*)os_strstr(conn->url, " ");
		if (e==NULL) return; //wtf?
		*e=0; //terminate url part

		os_printf("URL = %s\n", conn->url);
		//Parse out the URL part before the GET parameters.
		conn->getArgs=(char*)os_strstr(conn->url, "?");
		if (conn->getArgs!=0) {
			*conn->getArgs=0;
			conn->getArgs++;
			os_printf("GET args = %s\n", conn->getArgs);
		} else {
			conn->getArgs=NULL;
		}
	} else if (os_strncmp(h, "Content-Length: ", 16)==0) {
		i=0;
		//Skip trailing spaces
		while (h[i]!=' ') i++;
		//Get POST data length
		conn->postLen=atoi(h+i+1);
		//Clamp if too big. Hmm, maybe we should error out instead?
		if (conn->postLen>MAX_POST) conn->postLen=MAX_POST;
		os_printf("Mallocced buffer for %d bytes of post data.\n", conn->postLen);
		//Alloc the memory.
		conn->postBuff=(char*)os_malloc(conn->postLen+1);
		conn->priv->postPos=0;
	}
}
/**
 * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
 * @path: The dbus object path
 * @sep: Separating part (e.g., "Networks" or "PersistentGroups")
 * @item: (out) The part following the specified separator, if any
 * Returns: The object path of the interface this path refers to
 *
 * For a given object path, decomposes the object path into object id and
 * requested part, if those parts exist. The caller is responsible for freeing
 * the returned value. The *item pointer points to that allocated value and must
 * not be freed separately.
 *
 * As an example, path = "/fi/w1/wpa_supplicant1/Interfaces/1/Networks/0" and
 * sep = "Networks" would result in "/fi/w1/wpa_supplicant1/Interfaces/1"
 * getting returned and *items set to point to "0".
 */
char * wpas_dbus_new_decompose_object_path(const char *path, const char *sep,
					   char **item)
{
	const unsigned int dev_path_prefix_len =
		os_strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
	char *obj_path_only;
	char *pos;
	size_t sep_len;

	*item = NULL;

	/* Verify that this starts with our interface prefix */
	if (os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
		       dev_path_prefix_len) != 0)
		return NULL; /* not our path */

	/* Ensure there's something at the end of the path */
	if ((path + dev_path_prefix_len)[0] == '\0')
		return NULL;

	obj_path_only = os_strdup(path);
	if (obj_path_only == NULL)
		return NULL;

	pos = obj_path_only + dev_path_prefix_len;
	pos = os_strchr(pos, '/');
	if (pos == NULL)
		return obj_path_only; /* no next item on the path */

	 /* Separate network interface prefix from the path */
	*pos++ = '\0';

	sep_len = os_strlen(sep);
	if (os_strncmp(pos, sep, sep_len) != 0 || pos[sep_len] != '/')
		return obj_path_only; /* no match */

	 /* return a pointer to the requested item */
	*item = pos + sep_len + 1;
	return obj_path_only;
}
Exemple #12
0
static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
				  struct eap_peer_config *conf)
{
	char *resp, *pos;
	size_t i;

	wpa_printf(MSG_DEBUG,
		   "EAP-SIM: Use result from external SIM processing");

	resp = conf->external_sim_resp;
	conf->external_sim_resp = NULL;

	if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
		wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
		os_free(resp);
		return -1;
	}

	pos = resp + 9;
	for (i = 0; i < data->num_chal; i++) {
		wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
			    data->rand[i], GSM_RAND_LEN);

		if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
			goto invalid;
		wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
				data->kc[i], EAP_SIM_KC_LEN);
		pos += EAP_SIM_KC_LEN * 2;
		if (*pos != ':')
			goto invalid;
		pos++;

		if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
			goto invalid;
		wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
				data->sres[i], EAP_SIM_SRES_LEN);
		pos += EAP_SIM_SRES_LEN * 2;
		if (i + 1 < data->num_chal) {
			if (*pos != ':')
				goto invalid;
			pos++;
		}
	}

	os_free(resp);
	return 0;

invalid:
	wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
	os_free(resp);
	return -1;
}
Exemple #13
0
/*****************************************************************************
  Function:
	WEBFS_HANDLE WEBFSOpen(uint8* cFile)

  Description:
	Opens a file in the WEBFS2 file system.

  Precondition:
	None

  Parameters:
	cFile - a null terminated file name to open

  Returns:
	An WEBFS_HANDLE to the opened file if found, or WEBFS_INVALID_HANDLE
	if the file could not be found or no free handles exist.
  ***************************************************************************/
WEBFS_HANDLE ICACHE_FLASH_ATTR WEBFSOpen(uint8* cFile)
{
	WEBFS_HANDLE hWEBFS;
	uint16 nameHash; 
	int i, len = 0; 
	uint16 hashCache[16];
	uint8 bufname[MAX_FILE_NAME_LEN];
	uint8 *ptr;

	// Make sure WEBFS is unlocked and we got a filename
	if(*cFile == '\0' || isWEBFSLocked == true)
		return WEBFS_INVALID_HANDLE;

	// Calculate the name hash to speed up searching
	for(nameHash = 0, ptr = cFile; *ptr != '\0'; ptr++)
	{
		nameHash += *ptr;
		nameHash <<= 1;
		len++;
	}
	// Find a free file handle to use
	for(hWEBFS = 1; hWEBFS <= MAX_WEBFS_OPENFILES; hWEBFS++)
		if(WEBFSStubs[hWEBFS].addr == WEBFS_INVALID) break;
	if(hWEBFS == MAX_WEBFS_OPENFILES)
		return WEBFS_INVALID_HANDLE;
	// Read in hashes, and check remainder on a match.  Store 8 in cache for performance
	for(i = 0; i < numFiles; i++) {
		// For new block of 8, read in data
		if((i & 0x0F) == 0)	{
			WEBFSStubs[0].addr = 12 + i*2;
			WEBFSStubs[0].bytesRem = 32;
			WEBFSGetArray(0, (uint8*)hashCache, 32);
		}
		// If the hash matches, compare the full filename
		if(hashCache[i&0x0F] == nameHash)
		{
			GetFATRecord(i);
			// filename comparison
			WEBFSStubs[0].addr = fatCache.string;
			WEBFSStubs[0].bytesRem = MAX_FILE_NAME_LEN;
			WEBFSGetArray(0, bufname, MAX_FILE_NAME_LEN);
			if(os_strncmp(cFile, bufname, len) == 0) { // Filename matches, so return true
				WEBFSStubs[hWEBFS].addr = fatCache.data;
				WEBFSStubs[hWEBFS].bytesRem = fatCache.len;
				WEBFSStubs[hWEBFS].fatID = i;
				return hWEBFS;
			}
		}
	}
	// No file name matched, so return nothing
	return WEBFS_INVALID_HANDLE;
}
Exemple #14
0
/******************************************************************************
 * FunctionName : find_json_path
 * Description  : find the JSON format tree's path
 * Parameters   : json -- A pointer to a JSON set up
 *                path -- A pointer to the JSON format tree's path
 * Returns      : A pointer to the JSON format tree
*******************************************************************************/
struct jsontree_value *ICACHE_FLASH_ATTR
find_json_path(struct jsontree_context *json, const char *path)
{
    struct jsontree_value *v;
    const char *start;
    const char *end;
    int len;

    v = json->values[0];
    start = path;

    do {
        end = (const char *)os_strstr(start, "/");

        if (end == start) {
            break;
        }

        if (end != NULL) {
            len = end - start;
            end++;
        } else {
            len = os_strlen(start);
        }

        if (v->type != JSON_TYPE_OBJECT) {
            v = NULL;
        } else {
            struct jsontree_object *o;
            int i;

            o = (struct jsontree_object *)v;
            v = NULL;

            for (i = 0; i < o->count; i++) {
                if (os_strncmp(start, o->pairs[i].name, len) == 0) {
                    v = o->pairs[i].value;
                    json->index[json->depth] = i;
                    json->depth++;
                    json->values[json->depth] = v;
                    json->index[json->depth] = 0;
                    break;
                }
            }
        }

        start = end;
    } while (end != NULL && *end != '\0' && v != NULL);

    json->callback_state = 0;
    return v;
}
Exemple #15
0
void
wrapd_hostapd_ctrl_iface_process(struct wrap_demon *aptr, char *msg, char *child_ifname)
{
	int addr_off;
    char parent_ifname[IFNAMSIZ] = {0};
    u_int32_t flags = 0;

	if (os_strncmp(msg + HOSTAPD_MSG_ADDR_OFF, "AP-STA-CONNECTED ", 17) == 0) {	
        flags |= WRAPD_PSTA_FLAG_MAT ;
        addr_off = HOSTAPD_MSG_ADDR_OFF + 17;

        if (char2addr(msg + addr_off) != 0) {
            wrapd_printf("Invalid MAC addr");	
            return;
        }

        if (dbdc_ifname) {
            os_strncpy(parent_ifname, dbdc_ifname, IFNAMSIZ);
            parent_ifname[IFNAMSIZ - 1] = '\0';
            flags &= ~WRAPD_PSTA_FLAG_MAT;
        } else {
            wrapd_ifname_to_parent_ifname(aptr, child_ifname, parent_ifname);
        }
        
		wrapd_psta_add(aptr, parent_ifname, child_ifname, (u8 *)(msg + addr_off), flags);
        
	} else if (os_strncmp(msg + HOSTAPD_MSG_ADDR_OFF, "AP-STA-DISCONNECTED ", 20) == 0) {
	    addr_off = HOSTAPD_MSG_ADDR_OFF + 20;
        if (char2addr(msg + addr_off) != 0) {
            wrapd_printf("Invalid MAC addr");	
            return;
        }
		wrapd_psta_remove(aptr, (u8 *)(msg + addr_off));

	} else {
		wrapd_printf("Unknow msg(%s)", msg);
	}

}
struct l2_packet_data * l2_packet_init(
        const char *ifname, const u8 *own_addr, unsigned short protocol,
        void (*rx_callback)(void *ctx, const u8 *src_addr,
                            const u8 *buf, size_t len),
        void *rx_callback_ctx, int l2_hdr)
{
        struct l2_packet_data *l2;
        DWORD thread_id;

        l2 = os_zalloc(sizeof(struct l2_packet_data));
        if (l2 == NULL)
                return NULL;
        if (os_strncmp(ifname, "\\Device\\NPF_", 12) == 0)
                os_strlcpy(l2->ifname, ifname, sizeof(l2->ifname));
        else
                os_snprintf(l2->ifname, sizeof(l2->ifname), "\\Device\\NPF_%s",
                            ifname);
        l2->rx_callback = rx_callback;
        l2->rx_callback_ctx = rx_callback_ctx;
        l2->l2_hdr = l2_hdr;

        if (own_addr)
                os_memcpy(l2->own_addr, own_addr, ETH_ALEN);

        if (l2_packet_init_libpcap(l2, protocol)) {
                os_free(l2);
                return NULL;
        }

        l2->rx_avail = CreateEvent(NULL, TRUE, FALSE, NULL);
        l2->rx_done = CreateEvent(NULL, TRUE, FALSE, NULL);
        l2->rx_notify = CreateEvent(NULL, TRUE, FALSE, NULL);
        if (l2->rx_avail == NULL || l2->rx_done == NULL ||
            l2->rx_notify == NULL) {
                CloseHandle(l2->rx_avail);
                CloseHandle(l2->rx_done);
                CloseHandle(l2->rx_notify);
                pcap_close(l2->pcap);
                os_free(l2);
                return NULL;
        }

        eloop_register_event(l2->rx_avail, sizeof(l2->rx_avail),
                             l2_packet_rx_event, l2, NULL);

        l2->running = 1;
        l2->rx_thread = CreateThread(NULL, 0, l2_packet_receive_thread, l2, 0,
                                     &thread_id);

        return l2;
}
Exemple #17
0
void ICACHE_FLASH_ATTR config_parse(serverConnData *conn, char *buf, int len) {
    char *lbuf = (char *)os_malloc(len + 1), **argv;
    uint8_t i, argc;
    // we need a '\0' end of the string
    os_memcpy(lbuf, buf, len);
    lbuf[len] = '\0';
    DBG_MSG("parse %d[%s]\r\n", len, lbuf);

    // remove any CR / LF
    for (i = 0; i < len; ++i)
        if (lbuf[i] == '\n' || lbuf[i] == '\r')
            lbuf[i] = '\0';

    // verify the command prefix
    if (os_strncmp(lbuf, "+++AT", 5) != 0) {
        DBG_MSG("Not a command\r\n");
        return;
    }
    // parse out buffer into arguments
    argv = config_parse_args(&lbuf[5], &argc);
    DBG_MSG("argc: %d, argv[0]: %s.\r\n", argc, argv[0]);

    if (argc == 0) {
        espbuffsentstring(conn, MSG_OK);
    } else {
        argc--; // to mimic C main() argc argv
        for (i = 0; config_commands[i].command; ++i) {
            if (os_strncmp(argv[0], config_commands[i].command, strlen(argv[0])) == 0) {
                config_commands[i].function(conn, argc, argv);
                break;
            }
        }
        if (!config_commands[i].command)
            espbuffsentprintf(conn, "%s - buf: %s\r\n", MSG_INVALID_CMD, argv[0]);
    }
    config_parse_args_free(argc, argv);
    os_free(lbuf);
}
static DBusMessage * properties_handler(DBusMessage *message,
					struct wpa_dbus_object_desc *obj_dsc)
{
	DBusMessageIter iter;
	char *interface;
	const char *method;

	method = dbus_message_get_member(message);
	dbus_message_iter_init(message, &iter);

	if (!os_strncmp(WPA_DBUS_PROPERTIES_GET, method,
			WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
	    !os_strncmp(WPA_DBUS_PROPERTIES_SET, method,
			WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) ||
	    !os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
			WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
		/* First argument: interface name (DBUS_TYPE_STRING) */
		if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		{
			return dbus_message_new_error(message,
						      DBUS_ERROR_INVALID_ARGS,
						      NULL);
		}

		dbus_message_iter_get_basic(&iter, &interface);

		if (!os_strncmp(WPA_DBUS_PROPERTIES_GETALL, method,
				WPAS_DBUS_METHOD_SIGNAL_PROP_MAX)) {
			/* GetAll */
			return properties_get_all(message, interface, obj_dsc);
		}
		/* Get or Set */
		return properties_get_or_set(message, &iter, interface,
					     obj_dsc);
	}
	return dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD,
				      NULL);
}
Exemple #19
0
cmdCallback* ICACHE_FLASH_ATTR
CMD_GetCbByName(char* name) {
  for (uint8_t i = 0; i < MAX_CALLBACKS; i++) {
    //os_printf("CMD_GetCbByName: index %d name=%s cb=%p\n", i, callbacks[i].name,
    //  (void *)callbacks[i].callback);
    // if callback doesn't exist or it's null
    if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0) {
      DBG("CMD_GetCbByName: cb %s found at index %d\n", name, i);
      return &callbacks[i];
    }
  }
  os_printf("CMD_GetCbByName: cb %s not found\n", name);
  return 0;
}
Exemple #20
0
void
wrapd_wpa_s_ctrl_iface_process(struct wrap_demon *aptr, char *msg)
{
	if (os_strncmp(msg + WPA_S_MSG_ADDR_OFF, "CTRL-EVENT-DISCONNECTED ", 24) == 0) {
        aptr->mpsta_conn = 0;
        wrapd_disconn_all(aptr);
        
	} else if (os_strncmp(msg + WPA_S_MSG_ADDR_OFF, "CTRL-EVENT-CONNECTED ", 21) == 0) {
        aptr->mpsta_conn = 1;
        if ((NULL == wrapd_hostapd_conn) || (0 == aptr->do_timer) ){
            wrapd_conn_all(aptr);
        } else {
            if (0 == aptr->in_timer) {
                eloop_register_timeout(1, 0, wrapd_conn_timer, aptr, NULL);
                aptr->in_timer = 1;
            }
        }
        
	} else {
		//wrapd_printf("Unknow msg(%s)", msg);
	}

}
Exemple #21
0
LOCAL int ICACHE_FLASH_ATTR version_get(struct jsontree_context *js_ctx)
{
    const char *path = jsontree_path_name(js_ctx, js_ctx->depth - 1);
    char string[32];
    
    os_printf("got %s \n", path);
    if (os_strncmp(path, "hardware", 8) == 0) {
        os_sprintf(string, "0.1");
    }

    jsontree_write_string(js_ctx, string);

    return 0;
}
/*
 * Parse an URL of the form http://host:port/path
 * <host> can be a hostname or an IP address
 * <port> is optional
 */
void ICACHE_FLASH_ATTR http_post(const char * url, const char * post_data, http_callback user_callback)
{
	// FIXME: handle HTTP auth with http://user:pass@host/
	// FIXME: make https work.
	// FIXME: get rid of the #anchor part if present.

	char hostname[128] = "";
	int port = 80;

	if (os_strncmp(url, "http://", strlen("http://")) != 0) {
		os_printf("URL is not HTTP %s\n", url);
		return;
	}
	url += strlen("http://"); // Get rid of the protocol.

	char * path = os_strchr(url, '/');
	if (path == NULL) {
		path = os_strchr(url, '\0'); // Pointer to end of string.
	}

	char * colon = os_strchr(url, ':');
	if (colon > path) {
		colon = NULL; // Limit the search to characters before the path.
	}

	if (colon == NULL) { // The port is not present.
		os_memcpy(hostname, url, path - url);
		hostname[path - url] = '\0';
	}
	else {
		port = atoi(colon + 1);
		if (port == 0) {
			os_printf("Port error %s\n", url);
			return;
		}

		os_memcpy(hostname, url, colon - url);
		hostname[colon - url] = '\0';
	}


	if (path[0] == '\0') { // Empty path is not allowed.
		path = "/";
	}

	PRINTF("hostname=%s\n", hostname);
	PRINTF("port=%d\n", port);
	PRINTF("path=%s\n", path);
	http_raw_request(hostname, port, path, post_data, user_callback);
}
void ICACHE_FLASH_ATTR init_params(){
	user_esp_platform_load_param(&esp_param);

	char ver[32];
	c_sprintf(ver, "SIZE:%u", sizeof(esp_param));

	if(os_strncmp(esp_param.version, ver, strlen(ver))){
		memset(&esp_param, 0, sizeof(esp_param));
		strcpy(esp_param.version, ver);

		params_save();
	}

}
static char * wpa_supplicant_ctrl_iface_path(struct wpa_supplicant *wpa_s)
{
	char *buf;
	size_t len;
	char *pbuf, *dir = NULL;
	int res;

	if (wpa_s->conf->ctrl_interface == NULL)
		return NULL;

	pbuf = os_strdup(wpa_s->conf->ctrl_interface);
	if (pbuf == NULL)
		return NULL;
	if (os_strncmp(pbuf, "DIR=", 4) == 0) {
		char *gid_str;
		dir = pbuf + 4;
		gid_str = os_strstr(dir, " GROUP=");
		if (gid_str)
			*gid_str = '\0';
	} else
		dir = pbuf;

	len = os_strlen(dir) + os_strlen(wpa_s->ifname) + 2;
	buf = os_malloc(len);
	if (buf == NULL) {
		os_free(pbuf);
		return NULL;
	}

	res = os_snprintf(buf, len, "%s/%s", dir, wpa_s->ifname);
	if (os_snprintf_error(len, res)) {
		os_free(pbuf);
		os_free(buf);
		return NULL;
	}
#ifdef __CYGWIN__
	{
		/* Windows/WinPcap uses interface names that are not suitable
		 * as a file name - convert invalid chars to underscores */
		char *pos = buf;
		while (*pos) {
			if (*pos == '\\')
				*pos = '_';
			pos++;
		}
	}
#endif /* __CYGWIN__ */
	os_free(pbuf);
	return buf;
}
static DBusMessage * msg_method_handler(DBusMessage *message,
					struct wpa_dbus_object_desc *obj_dsc)
{
	const struct wpa_dbus_method_desc *method_dsc = obj_dsc->methods;
	const char *method;
	const char *msg_interface;

	method = dbus_message_get_member(message);
	msg_interface = dbus_message_get_interface(message);

	/* try match call to any registered method */
	while (method_dsc && method_dsc->dbus_method) {
		/* compare method names and interfaces */
		if (!os_strncmp(method_dsc->dbus_method, method,
				WPAS_DBUS_METHOD_SIGNAL_PROP_MAX) &&
		    !os_strncmp(method_dsc->dbus_interface, msg_interface,
				WPAS_DBUS_INTERFACE_MAX))
			break;

		method_dsc++;
	}
	if (method_dsc == NULL || method_dsc->dbus_method == NULL) {
		wpa_printf(MSG_DEBUG, "no method handler for %s.%s on %s",
			   msg_interface, method,
			   dbus_message_get_path(message));
		return dbus_message_new_error(message,
					      DBUS_ERROR_UNKNOWN_METHOD, NULL);
	}

	if (!is_signature_correct(message, method_dsc)) {
		return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
					      NULL);
	}

	return method_dsc->method_handler(message,
					  obj_dsc->user_data);
}
LOCAL int ICACHE_FLASH_ATTR check_rest(HTTP_RESPONSE_STATUS *res, const char *path,
                                       const char *key, HTTP_CONTENT *content_in, HTTP_ANSWER *answer) {
    static const char api[] = "/api";
    if(os_strncmp(path, api, sizeof(api) - 1) == 0) {
        const char *p = &path[sizeof(api) - 1];
        if(p[0] == 0) {
            *res = rest_handle(p, key, content_in, answer);
            return 1;
        } else if(p[0] == '/') {
            *res = rest_handle(&p[1], key, content_in, answer);
            return 1;
        }
    }
    return 0;
}
Exemple #27
0
void mjyun_receive(const char *event_name, const char *event_data)
{
	INFO("RECEIVED: key:value [%s]:[%s]", event_name, event_data);

	if(os_strncmp(event_data, "on", 2) == 0)
	{
		INFO("set switch on\r\n");
		param_set_status(1);
		param_save();
		relay_set_status_and_publish(1);
	}
	if(os_strncmp(event_data, "off", 3) == 0)
	{
		INFO("set switch off\r\n");
		param_set_status(0);
		param_save();
		relay_set_status_and_publish(0);
	}
	if(os_strncmp(event_data, "ota", 3) == 0)
	{
		INFO("OTA: upgrade the firmware!\r\n");
		mjyun_mini_ota_start("ota/dev/minik/files");
	}
}
static int wpa_driver_mediatek_set_country(void *priv, const char *alpha2_arg)
{
    struct i802_bss *bss = priv;
    struct wpa_driver_nl80211_data *drv = bss->drv;
    int ioctl_sock = -1;
    struct iwreq iwr;
    int ret = -1;
    char buf[11];
#ifdef MTK_TC1_FEATURE
    char replace_ifname[IFNAMSIZ+1];

    memset(replace_ifname, 0, IFNAMSIZ+1);
    os_strlcpy(replace_ifname, "wlan0", os_strlen("wlan0")+1);
#endif

    wpa_printf(MSG_DEBUG, "wpa_driver_nl80211_set_country");
    ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
    if (ioctl_sock < 0) {
        wpa_printf(MSG_ERROR, "%s: socket(PF_INET,SOCK_DGRAM)", __func__);
        return -1;
    }
    os_memset(&iwr, 0, sizeof(iwr));
#ifdef MTK_TC1_FEATURE
    // convert 'p2p0' -> 'wlan0' :
    // when iface name is p2p0, COUNTRY driver command doesn't support in MTK solution.
    if (os_strncmp(drv->first_bss->ifname, "p2p0", os_strlen("p2p0")) == 0) {
        wpa_printf(MSG_DEBUG, "Change interface name : p2p0->wlan0");
        os_strlcpy(iwr.ifr_name, replace_ifname, IFNAMSIZ );
    } else {
        os_strlcpy(iwr.ifr_name, drv->first_bss->ifname, IFNAMSIZ);
    }
#else
    os_strlcpy(iwr.ifr_name, drv->first_bss->ifname, IFNAMSIZ);
#endif
    sprintf(buf, "COUNTRY %s", alpha2_arg);
    iwr.u.data.pointer = buf;
    iwr.u.data.length = strlen(buf);
    if ((ret = ioctl(ioctl_sock, 0x8B0C, &iwr)) < 0) {  // SIOCSIWPRIV
        wpa_printf(MSG_DEBUG, "ioctl[SIOCSIWPRIV]: %s", buf);
        close(ioctl_sock);
        return ret;
    }
    else {
        close(ioctl_sock);
        return 0;
    }

}
Exemple #29
0
static uint32_t ICACHE_FLASH_ATTR
CMD_AddCb(char* name, uint32_t cb) {
  for (uint8_t i = 0; i < MAX_CALLBACKS; i++) {
    //os_printf("CMD_AddCb: index %d name=%s cb=%p\n", i, callbacks[i].name,
    //  (void *)callbacks[i].callback);
    // find existing callback or add to the end
    if (os_strncmp(callbacks[i].name, name, CMD_CBNLEN) == 0 || callbacks[i].name[0] == '\0') {
      os_strncpy(callbacks[i].name, name, sizeof(callbacks[i].name));
      callbacks[i].name[CMD_CBNLEN-1] = 0; // strncpy doesn't null terminate
      callbacks[i].callback = cb;
      DBG("CMD_AddCb: cb %s added at index %d\n", callbacks[i].name, i);
      return 1;
    }
  }
  return 0;
}
Exemple #30
0
int wapi_conf_read_certfile(const char *cert_file, unsigned char *cert_buf, int buf_len)
{
    if (NULL == cert_file) {
        wpa_printf(MSG_ERROR, "Error: try to open a null certificate");
        return -1;
    }
#ifdef ANDROID
    if (os_strncmp("keystore://", cert_file, 11) == 0) {
        char value[KEYSTORE_MESSAGE_SIZE];
        const char *key = cert_file + 11;
        int length = keystore_get(key, strlen(key), value);
        if (length <= 0) {
            wpa_printf(MSG_ERROR, "Open Android keystore: %s Error %d", cert_file, length);
            return -1;
        }
        if (length > buf_len) {
            wpa_printf(MSG_ERROR, "cert buf too small, buf_len = %d, keystore_len = %d", buf_len, length);
            return -1;
        }
        wpa_hexdump(MSG_MSGDUMP, cert_file, (const u8*)value, length);
        memcpy(cert_buf, value, length);
        return length;
    } else 
#endif
    {
        int i = 0;
        FILE *file = fopen((const char*)cert_file, "rb");
        if (NULL == file) {
            wpa_printf(MSG_ERROR, "Open file: %s Error", cert_file);
            perror("Open cert file Error");
            return -1;
        }

        while (!feof(file)) {
            cert_buf[i++] = (char)fgetc(file);
            if (i >= buf_len) {
                wpa_printf(MSG_ERROR, "cert buf too small, buf_len = %d, i = %d", buf_len, i);
                fclose(file);
                return -1;
            }
        }
        fclose(file);
        return i;
    }
    return -1;
}