2 * Hotspot 2.0 SPP server - standalone version
3 * Copyright (c) 2012-2013, Qualcomm Atheros, Inc.
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "xml-utils.h"
15 #include "spp_server.h"
18 static void write_timestamp(FILE *f)
26 fprintf(f, "%04u-%02u-%02u %02u:%02u:%02u ",
27 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
28 tm->tm_hour, tm->tm_min, tm->tm_sec);
32 void debug_print(struct hs20_svc *ctx, int print, const char *fmt, ...)
36 if (ctx->debug_log == NULL)
39 write_timestamp(ctx->debug_log);
41 vfprintf(ctx->debug_log, fmt, ap);
44 fprintf(ctx->debug_log, "\n");
48 void debug_dump_node(struct hs20_svc *ctx, const char *title, xml_node_t *node)
52 if (ctx->debug_log == NULL)
54 str = xml_node_to_str(ctx->xml, node);
58 write_timestamp(ctx->debug_log);
59 fprintf(ctx->debug_log, "%s: '%s'\n", title, str);
64 static int process(struct hs20_svc *ctx)
67 xml_node_t *soap, *spp, *resp;
68 char *user, *realm, *post, *str;
70 ctx->addr = getenv("HS20ADDR");
72 debug_print(ctx, 1, "Connection from %s", ctx->addr);
74 user = getenv("HS20USER");
75 if (user && strlen(user) == 0)
77 realm = getenv("HS20REALM");
79 debug_print(ctx, 1, "HS20REALM not set");
82 post = getenv("HS20POST");
84 debug_print(ctx, 1, "HS20POST not set");
88 soap = xml_node_from_buf(ctx->xml, post);
90 debug_print(ctx, 1, "Could not parse SOAP data");
93 debug_dump_node(ctx, "Received SOAP message", soap);
94 spp = soap_get_body(ctx->xml, soap);
96 debug_print(ctx, 1, "Could not get SPP message");
97 xml_node_free(ctx->xml, soap);
100 debug_dump_node(ctx, "Received SPP message", spp);
102 resp = hs20_spp_server_process(ctx, spp, user, realm, dmacc);
103 xml_node_free(ctx->xml, soap);
104 if (resp == NULL && user == NULL) {
105 debug_print(ctx, 1, "Request HTTP authentication");
106 return 2; /* Request authentication */
109 debug_print(ctx, 1, "No response");
113 soap = soap_build_envelope(ctx->xml, resp);
115 debug_print(ctx, 1, "SOAP envelope building failed");
118 str = xml_node_to_str(ctx->xml, soap);
119 xml_node_free(ctx->xml, soap);
121 debug_print(ctx, 1, "Could not get node string");
131 static void usage(void)
134 "hs20_spp_server -r<root directory> [-f<debug log>]\n");
138 int main(int argc, char *argv[])
143 os_memset(&ctx, 0, sizeof(ctx));
145 int c = getopt(argc, argv, "f:r:");
152 ctx.debug_log = fopen(optarg, "a");
153 if (ctx.debug_log == NULL) {
154 printf("Could not write to %s\n", optarg);
159 ctx.root_dir = optarg;
166 if (ctx.root_dir == NULL) {
170 ctx.xml = xml_node_init_ctx(&ctx, NULL);
173 if (hs20_spp_server_init(&ctx) < 0) {
174 xml_node_deinit_ctx(ctx.xml);
179 debug_print(&ctx, 1, "process() --> %d", ret);
181 xml_node_deinit_ctx(ctx.xml);
182 hs20_spp_server_deinit(&ctx);
184 fclose(ctx.debug_log);