📄 xsupgui_request3.c
字号:
/**
* Licensed under a dual GPL/BSD license. (See LICENSE file for more info.)
*
* \file xsupgui_request3.c
*
* \author chris@open1x.org
*
* $Id: xsupgui_request3.c,v 1.1.2.14 2007/08/17 18:43:15 chessing Exp $
* $Date: 2007/08/17 18:43:15 $
**/
#include <string.h>
#include <libxml/parser.h>
#ifdef WINDOWS
#include "../../src/stdintwin.h"
#endif
#include "xsupgui.h"
#include "../libxsupconfig/xsupconfig_structs.h"
#include "xsupgui_request.h"
#include "xsupgui_xml_common.h"
/**
* \brief Request the SSID in use for a given interface.
*
* @param[in] device The OS specific device name for the device we want to get the SSID
* for.
* @param[out] ssid The SSID that this interface is attempting to use.
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout.
* \retval >299 on other error.
*
* \note This call returns the SSID that the interface is attempting to use. The caller
* shouldn't assume that because there is an SSID value returned that the interface
* is actually associated to the network.
**/
int xsupgui_request_get_ssid(char *device, char **ssid)
{
return xsupgui_request_get_byte_string(device, "Get_SSID", "SSID", "SSID_Name", ssid);
}
/**
* \brief Request the BSSID (MAC address of the AP) in use for a given interface.
*
* @param[in] device The OS specific device name that we want to get the BSSID for.
* @param[out] bssid An ASCII printable string that identifies the BSSID that this
* interface is associated to.
*
* \brief REQUEST_SUCCESS on success
* \brief REQUEST_TIMEOUT on timeout
* \brief >299 on other error.
*
* \note bssid will be returned as a printable string. If you want the hex version,
* you will need to convert it yourself.
**/
int xsupgui_request_get_bssid(char *device, char **bssid)
{
return xsupgui_request_get_byte_string(device, "Get_BSSID", "BSSID", "BSSID_Value", bssid);
}
/**
* \brief Request that the supplicant disassociate an interface.
*
* @param[in] device The OS specific device name that caller would like to send a
* disassociate for.
* @param[in] reason The reason code to return in the disassocate request. (Reason
* codes are defined by the IEEE 802.11 standard.)
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout
* \retval >299 on other error.
**/
int xsupgui_request_set_disassociate(char *device, unsigned char reason)
{
xmlDocPtr doc = NULL;
xmlDocPtr retdoc = NULL;
xmlNodePtr n = NULL, t = NULL;
int done = 0, err = 0;
char strreason[50];
char *temp = NULL;
if (device == NULL) return IPC_ERROR_INVALID_PARAMETERS;
doc = xsupgui_xml_common_build_msg();
if (doc == NULL) return IPC_ERROR_CANT_CREATE_REQ_HDR;
n = xmlDocGetRootElement(doc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_REQ_ROOT_NODE;
goto request_disassoc;
}
t = xmlNewChild(n, NULL, "Request_Disassociate", NULL);
if (t == NULL)
{
done = IPC_ERROR_CANT_CREATE_REQUEST;
goto request_disassoc;
}
xsupgui_xml_common_convert_amp(device, &temp);
if (xmlNewChild(t, NULL, "Interface", temp) == NULL)
{
done = IPC_ERROR_CANT_CREATE_INT_NODE;
free(temp);
goto request_disassoc;
}
free(temp);
sprintf((char *)&strreason, "%d", reason);
if (xmlNewChild(t, NULL, "Reason", strreason) == NULL)
{
done = IPC_ERROR_UNSPEC_REQ_FAILURE;
goto request_disassoc;
}
err = xsupgui_request_send(doc, &retdoc);
if (err != REQUEST_SUCCESS)
{
done = err;
goto request_disassoc;
}
// Otherwise, parse it and see if we got what we wanted.
err = xsupgui_request_check_exceptions(retdoc);
if (err != 0)
{
done = err;
goto request_disassoc;
}
n = xmlDocGetRootElement(retdoc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_RESP_ROOT_NODE;
goto request_disassoc;
}
// If we get here, then we know that the document passed the
// validation tests imposed. So, we need to see if we got the result
// we wanted.
n = n->children;
if (xsupgui_request_find_node(n, "ACK") != NULL)
{
done = REQUEST_SUCCESS;
}
else
{
done = IPC_ERROR_NOT_ACK;
}
request_disassoc:
if (doc != NULL) xmlFreeDoc(doc);
if (retdoc != NULL) xmlFreeDoc(retdoc);
return done;
}
/**
* \brief Request the signal strength of an interface, as a percentage.
*
* @param[in] device The OS specific device name that we wish to get the signal
* strength from.
* @param[out] strength A signal strength as a percentage.
*
* \retval REQUEST_TIMEOUT on timeout
* \retval REQEUST_SUCCESS on success
* \retval >299 on other error.
*
**/
int xsupgui_request_get_signal_strength_percent(char *device, int *strength)
{
return xsupgui_request_get_some_value(device, "Get_Signal_Strength_Percent", "Signal_Strength", "Percent", strength);
}
/**
* \brief Request the length of time a given interface has been in authenticated state.
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout
* \retval >299 on other error.
*
* \note If you call this function while the interface is not in authenticated state,
* you will always get 0! ("Authenticated" state includes S_FORCE_AUTH state!)
**/
int xsupgui_request_get_seconds_authenticated(char *device, long int *seconds)
{
return xsupgui_request_get_long_int(device, "Get_Seconds_Authenticated", "Seconds_Authenticated",
"Seconds", seconds);
}
/**
* \brief Request the association type that is currently in use.
*
* @param[in] device The OS specific device name that we want to get the association type
* from.
* @param[out] assoctype The association type that is in use.
*
* \retval REQUEST_FAILURE on failure
* \retval REQUEST_SUCCESS on success
* \retval >299 on other error.
**/
int xsupgui_request_get_association_type(char *device, int *assoctype)
{
return xsupgui_request_get_some_value(device, "Get_Association_Type", "Association_Type", "Association", assoctype);
}
/**
* \brief Verify that the supplicant is still alive and kicking.
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout
* \retval >299 on other error
**/
int xsupgui_request_ping()
{
xmlDocPtr doc = NULL, retdoc = NULL;
xmlNodePtr n = NULL;
int err = 0;
int done = REQUEST_SUCCESS;
doc = xsupgui_xml_common_build_msg();
if (doc == NULL) return IPC_ERROR_CANT_CREATE_REQ_HDR;
n = xmlDocGetRootElement(doc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_REQ_ROOT_NODE;
goto request_ping_done;
}
if (xmlNewChild(n, NULL, "Ping", NULL) == NULL)
{
done = IPC_ERROR_CANT_CREATE_REQUEST;
goto request_ping_done;
}
err = xsupgui_request_send(doc, &retdoc);
if (err != REQUEST_SUCCESS)
{
done = err;
goto request_ping_done;
}
// Otherwise, parse it and see if we got what we wanted.
err = xsupgui_request_check_exceptions(retdoc);
if (err != 0)
{
done = err;
goto request_ping_done;
}
n = xmlDocGetRootElement(retdoc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_RESP_ROOT_NODE;
goto request_ping_done;
}
// If we get here, then we know that the document passed the
// validation tests imposed. So, we need to see if we got the result
// we wanted.
n = n->children;
if (xsupgui_request_find_node(n, "Pong") != NULL)
{
done = REQUEST_SUCCESS;
}
else
{
done = IPC_ERROR_NOT_PONG;
}
request_ping_done:
if (doc != NULL) xmlFreeDoc(doc);
if (retdoc != NULL) xmlFreeDoc(retdoc);
return done;
}
/**
* \brief Request the username and password for a given connection.
*
* @param[in] conn_name The name of the connection we would like to get the username
* and password for.
* @param[out] username The username that is associated with this connection.
* @param[out] password The password that is associated with this connection.
* @param[out] authtype The authentication type that will be used with this connection.
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout
* \retval >299 on other error.
**/
int xsupgui_request_get_connection_upw(char *conn_name, char **username, char **password, int *authtype)
{
xmlDocPtr doc = NULL;
xmlDocPtr retdoc = NULL;
xmlNodePtr n = NULL, t = NULL;
xmlChar *content = NULL;
int done = REQUEST_SUCCESS;
int err = 0;
char *temp = NULL;
if (conn_name == NULL) return IPC_ERROR_INVALID_PARAMETERS;
doc = xsupgui_xml_common_build_msg();
if (doc == NULL) return IPC_ERROR_CANT_CREATE_REQ_HDR;
n = xmlDocGetRootElement(doc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_REQ_ROOT_NODE;
goto finish_connection_upw;
}
t = xmlNewChild(n, NULL, "Get_Connection_UPW", NULL);
if (t == NULL)
{
done = IPC_ERROR_CANT_CREATE_REQUEST;
goto finish_connection_upw;
}
xsupgui_xml_common_convert_amp(conn_name, &temp);
if (xmlNewChild(t, NULL, "Connection", temp) == NULL)
{
done = IPC_ERROR_UNSPEC_REQ_FAILURE;
free(temp);
goto finish_connection_upw;
}
free(temp);
err = xsupgui_request_send(doc, &retdoc);
if (err != REQUEST_SUCCESS)
{
done = err;
goto finish_connection_upw;
}
// Otherwise, parse it and see if we got what we wanted.
// Check if we got errors.
err = xsupgui_request_check_exceptions(retdoc);
if (err != 0)
{
done = err;
goto finish_connection_upw;
}
n = xmlDocGetRootElement(retdoc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_RESP_ROOT_NODE;
goto finish_connection_upw;
}
// If we get here, then we know that the document passed the
// validation tests imposed. So, we need to see if we got the result
// we wanted.
n = xsupgui_request_find_node(n->children, "Connection_UPW");
if (n == NULL)
{
done = IPC_ERROR_BAD_RESPONSE;
goto finish_connection_upw;
}
t = xsupgui_request_find_node(n->children, "Username");
if (t == NULL)
{
done = IPC_ERROR_BAD_RESPONSE;
goto finish_connection_upw;
}
(*username) = xmlNodeGetContent(t); // It is perfectly reasonable to have a NULL username. (In the case of a WPA(2)-PSK network.) So don't check it!
t = xsupgui_request_find_node(n->children, "Password");
if (t == NULL)
{
done = IPC_ERROR_BAD_RESPONSE;
goto finish_connection_upw;
}
(*password) = xmlNodeGetContent(t);
t = xsupgui_request_find_node(n->children, "Authentication");
if (t == NULL)
{
done = IPC_ERROR_BAD_RESPONSE;
goto finish_connection_upw;
}
content = xmlNodeGetContent(t);
(*authtype) = AUTH_NONE;
if (strcmp(content, "PSK") == 0) (*authtype) = AUTH_PSK;
if (strcmp(content, "EAP") == 0) (*authtype) = AUTH_EAP;
if (content != NULL) free(content);
finish_connection_upw:
if (doc) xmlFreeDoc(doc);
if (retdoc) xmlFreeDoc(retdoc);
return done;
}
/**
* \brief Request the OS specific device name given a device description from the configuration
* file.
*
* @param[in] dev_desc The description that is from the configuration file.
* @param[out] device The OS specific device name that maps to the description from
* the configuration file.
*
* \retval REQUEST_SUCCESS on success
* \retval REQUEST_TIMEOUT on timeout
* \retval >299 on other error.
**/
int xsupgui_request_get_devname(char *dev_desc, char **device)
{
xmlDocPtr doc = NULL, retdoc = NULL;
xmlNodePtr n = NULL, t = NULL;
int err = 0;
int done = REQUEST_SUCCESS;
char *temp = NULL;
if ((dev_desc == NULL) || (device == NULL)) return IPC_ERROR_INVALID_PARAMETERS;
(*device) = NULL;
doc = xsupgui_xml_common_build_msg();
if (doc == NULL) return IPC_ERROR_CANT_CREATE_REQ_HDR;
n = xmlDocGetRootElement(doc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_REQ_ROOT_NODE;
goto request_get_devname_done;
}
t = xmlNewChild(n, NULL, "Get_Device_Name", NULL);
if (t == NULL)
{
done = IPC_ERROR_CANT_CREATE_REQUEST;
goto request_get_devname_done;
}
xsupgui_xml_common_convert_amp(dev_desc, &temp);
if (xmlNewChild(t, NULL, "Device_Description", temp) == NULL)
{
done = IPC_ERROR_UNSPEC_REQ_FAILURE;
free(temp);
goto request_get_devname_done;
}
free(temp);
err = xsupgui_request_send(doc, &retdoc);
if (err != REQUEST_SUCCESS)
{
done = err;
goto request_get_devname_done;
}
// Otherwise, parse it and see if we got what we wanted.
err = xsupgui_request_check_exceptions(retdoc);
if (err != 0)
{
done = err;
goto request_get_devname_done;
}
n = xmlDocGetRootElement(retdoc);
if (n == NULL)
{
done = IPC_ERROR_CANT_FIND_RESP_ROOT_NODE;
goto request_get_devname_done;
}
// If we get here, then we know that the document passed the
// validation tests imposed. So, we need to see if we got the result
// we wanted.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -