|
a/upmpd/upmpd.cxx |
|
b/upmpd/upmpd.cxx |
|
... |
|
... |
1123 |
#define OPT_D 0x10
|
1123 |
#define OPT_D 0x10
|
1124 |
#define OPT_c 0x20
|
1124 |
#define OPT_c 0x20
|
1125 |
#define OPT_l 0x40
|
1125 |
#define OPT_l 0x40
|
1126 |
#define OPT_f 0x80
|
1126 |
#define OPT_f 0x80
|
1127 |
#define OPT_q 0x100
|
1127 |
#define OPT_q 0x100
|
|
|
1128 |
#define OPT_i 0x200
|
|
|
1129 |
#define OPT_P 0x400
|
1128 |
|
1130 |
|
1129 |
static const char usage[] =
|
1131 |
static const char usage[] =
|
1130 |
"-c configfile \t configuration file to use\n"
|
1132 |
"-c configfile \t configuration file to use\n"
|
1131 |
"-h host \t specify host MPD is running on\n"
|
1133 |
"-h host \t specify host MPD is running on\n"
|
1132 |
"-p port \t specify MPD port\n"
|
1134 |
"-p port \t specify MPD port\n"
|
1133 |
"-d logfilename\t debug messages to\n"
|
1135 |
"-d logfilename\t debug messages to\n"
|
1134 |
"-l loglevel\t log level (0-6)\n"
|
1136 |
"-l loglevel\t log level (0-6)\n"
|
1135 |
"-D \t run as a daemon\n"
|
1137 |
"-D \t run as a daemon\n"
|
1136 |
"-f friendlyname\t define device displayed name\n"
|
1138 |
"-f friendlyname\t define device displayed name\n"
|
1137 |
"-q 0|1 \t if set, we own the mpd queue, else avoid clearing it whenever we feel like it"
|
1139 |
"-q 0|1 \t if set, we own the mpd queue, else avoid clearing it whenever we feel like it\n"
|
|
|
1140 |
"-i iface \t specify network interface name to be used for UPnP"
|
|
|
1141 |
"-P upport \t specify port number to be used for UPnP"
|
1138 |
" \n\n"
|
1142 |
" \n\n"
|
1139 |
;
|
1143 |
;
|
1140 |
static void
|
1144 |
static void
|
1141 |
Usage(void)
|
1145 |
Usage(void)
|
1142 |
{
|
1146 |
{
|
|
... |
|
... |
1158 |
int main(int argc, char *argv[])
|
1162 |
int main(int argc, char *argv[])
|
1159 |
{
|
1163 |
{
|
1160 |
string mpdhost("localhost");
|
1164 |
string mpdhost("localhost");
|
1161 |
int mpdport = 6600;
|
1165 |
int mpdport = 6600;
|
1162 |
string mpdpassword;
|
1166 |
string mpdpassword;
|
1163 |
string upnplogfilename("/tmp/upmpdcli_libupnp.log");
|
|
|
1164 |
string logfilename;
|
1167 |
string logfilename;
|
1165 |
int loglevel(upnppdebug::Logger::LLINF);
|
1168 |
int loglevel(upnppdebug::Logger::LLINF);
|
1166 |
string configfile;
|
1169 |
string configfile;
|
1167 |
string friendlyname(dfltFriendlyName);
|
1170 |
string friendlyname(dfltFriendlyName);
|
1168 |
bool ownqueue = true;
|
1171 |
bool ownqueue = true;
|
1169 |
string upmpdcliuser("upmpdcli");
|
1172 |
string upmpdcliuser("upmpdcli");
|
1170 |
string pidfilename("/var/run/upmpdcli.pid");
|
1173 |
string pidfilename("/var/run/upmpdcli.pid");
|
|
|
1174 |
string iface;
|
|
|
1175 |
unsigned short upport = 0;
|
|
|
1176 |
string upnpip;
|
1171 |
|
1177 |
|
1172 |
const char *cp;
|
1178 |
const char *cp;
|
1173 |
if ((cp = getenv("UPMPD_HOST")))
|
1179 |
if ((cp = getenv("UPMPD_HOST")))
|
1174 |
mpdhost = cp;
|
1180 |
mpdhost = cp;
|
1175 |
if ((cp = getenv("UPMPD_PORT")))
|
1181 |
if ((cp = getenv("UPMPD_PORT")))
|
1176 |
mpdport = atoi(cp);
|
1182 |
mpdport = atoi(cp);
|
1177 |
if ((cp = getenv("UPMPD_FRIENDLYNAME")))
|
1183 |
if ((cp = getenv("UPMPD_FRIENDLYNAME")))
|
1178 |
friendlyname = atoi(cp);
|
1184 |
friendlyname = atoi(cp);
|
1179 |
if ((cp = getenv("UPMPD_CONFIG")))
|
1185 |
if ((cp = getenv("UPMPD_CONFIG")))
|
1180 |
configfile = cp;
|
1186 |
configfile = cp;
|
|
|
1187 |
if ((cp = getenv("UPMPD_UPNPIFACE")))
|
|
|
1188 |
iface = cp;
|
|
|
1189 |
if ((cp = getenv("UPMPD_UPNPPORT")))
|
|
|
1190 |
upport = atoi(cp);
|
1181 |
|
1191 |
|
1182 |
thisprog = argv[0];
|
1192 |
thisprog = argv[0];
|
1183 |
argc--; argv++;
|
1193 |
argc--; argv++;
|
1184 |
while (argc > 0 && **argv == '-') {
|
1194 |
while (argc > 0 && **argv == '-') {
|
1185 |
(*argv)++;
|
1195 |
(*argv)++;
|
|
... |
|
... |
1200 |
loglevel = atoi(*(++argv)); argc--; goto b1;
|
1210 |
loglevel = atoi(*(++argv)); argc--; goto b1;
|
1201 |
case 'p': op_flags |= OPT_p; if (argc < 2) Usage();
|
1211 |
case 'p': op_flags |= OPT_p; if (argc < 2) Usage();
|
1202 |
mpdport = atoi(*(++argv)); argc--; goto b1;
|
1212 |
mpdport = atoi(*(++argv)); argc--; goto b1;
|
1203 |
case 'q': op_flags |= OPT_q; if (argc < 2) Usage();
|
1213 |
case 'q': op_flags |= OPT_q; if (argc < 2) Usage();
|
1204 |
ownqueue = atoi(*(++argv)) != 0; argc--; goto b1;
|
1214 |
ownqueue = atoi(*(++argv)) != 0; argc--; goto b1;
|
|
|
1215 |
case 'i': op_flags |= OPT_i; if (argc < 2) Usage();
|
|
|
1216 |
iface = *(++argv); argc--; goto b1;
|
|
|
1217 |
case 'P': op_flags |= OPT_P; if (argc < 2) Usage();
|
|
|
1218 |
upport = atoi(*(++argv)); argc--; goto b1;
|
1205 |
default: Usage(); break;
|
1219 |
default: Usage(); break;
|
1206 |
}
|
1220 |
}
|
1207 |
b1: argc--; argv++;
|
1221 |
b1: argc--; argv++;
|
1208 |
}
|
1222 |
}
|
1209 |
|
1223 |
|
|
... |
|
... |
1226 |
if (!(op_flags & OPT_h))
|
1240 |
if (!(op_flags & OPT_h))
|
1227 |
config.get("mpdhost", mpdhost);
|
1241 |
config.get("mpdhost", mpdhost);
|
1228 |
if (!(op_flags & OPT_p) && config.get("mpdport", value)) {
|
1242 |
if (!(op_flags & OPT_p) && config.get("mpdport", value)) {
|
1229 |
mpdport = atoi(value.c_str());
|
1243 |
mpdport = atoi(value.c_str());
|
1230 |
}
|
1244 |
}
|
|
|
1245 |
config.get("mpdpassword", mpdpassword);
|
1231 |
if (!(op_flags & OPT_q) && config.get("ownqueue", value)) {
|
1246 |
if (!(op_flags & OPT_q) && config.get("ownqueue", value)) {
|
1232 |
ownqueue = atoi(value.c_str()) != 0;
|
1247 |
ownqueue = atoi(value.c_str()) != 0;
|
1233 |
}
|
1248 |
}
|
1234 |
config.get("mpdpassword", mpdpassword);
|
1249 |
if (!(op_flags & OPT_i)) {
|
|
|
1250 |
config.get("upnpiface", iface);
|
|
|
1251 |
if (iface.empty()) {
|
|
|
1252 |
config.get("upnpip", upnpip);
|
|
|
1253 |
}
|
|
|
1254 |
}
|
|
|
1255 |
if (!(op_flags & OPT_P) && config.get("upnpport", value)) {
|
|
|
1256 |
upport = atoi(value.c_str());
|
|
|
1257 |
}
|
1235 |
}
|
1258 |
}
|
1236 |
|
1259 |
|
1237 |
if (upnppdebug::Logger::getTheLog(logfilename) == 0) {
|
1260 |
if (upnppdebug::Logger::getTheLog(logfilename) == 0) {
|
1238 |
cerr << "Can't initialize log" << endl;
|
1261 |
cerr << "Can't initialize log" << endl;
|
1239 |
return 1;
|
1262 |
return 1;
|
|
... |
|
... |
1287 |
}
|
1310 |
}
|
1288 |
}
|
1311 |
}
|
1289 |
|
1312 |
|
1290 |
// Initialize libupnpp, and check health
|
1313 |
// Initialize libupnpp, and check health
|
1291 |
LibUPnP *mylib = 0;
|
1314 |
LibUPnP *mylib = 0;
|
|
|
1315 |
string hwaddr;
|
1292 |
int libretrysecs = 10;
|
1316 |
int libretrysecs = 10;
|
1293 |
for (;;) {
|
1317 |
for (;;) {
|
1294 |
// Libupnp init fails if we're started at boot and the network
|
1318 |
// Libupnp init fails if we're started at boot and the network
|
1295 |
// is not ready yet. So retry this forever
|
1319 |
// is not ready yet. So retry this forever
|
1296 |
mylib = LibUPnP::getLibUPnP(true);
|
1320 |
mylib = LibUPnP::getLibUPnP(true, &hwaddr, iface, upnpip, upport);
|
1297 |
if (mylib) {
|
1321 |
if (mylib) {
|
1298 |
break;
|
1322 |
break;
|
1299 |
}
|
1323 |
}
|
1300 |
sleep(libretrysecs);
|
1324 |
sleep(libretrysecs);
|
1301 |
libretrysecs = MIN(2*libretrysecs, 120);
|
1325 |
libretrysecs = MIN(2*libretrysecs, 120);
|
|
... |
|
... |
1304 |
if (!mylib->ok()) {
|
1328 |
if (!mylib->ok()) {
|
1305 |
LOGFAT("Lib init failed: " <<
|
1329 |
LOGFAT("Lib init failed: " <<
|
1306 |
mylib->errAsString("main", mylib->getInitError()) << endl);
|
1330 |
mylib->errAsString("main", mylib->getInitError()) << endl);
|
1307 |
return 1;
|
1331 |
return 1;
|
1308 |
}
|
1332 |
}
|
|
|
1333 |
|
|
|
1334 |
//string upnplogfilename("/tmp/upmpdcli_libupnp.log");
|
1309 |
//mylib->setLogFileName(upnplogfilename, LibUPnP::LogLevelDebug);
|
1335 |
//mylib->setLogFileName(upnplogfilename, LibUPnP::LogLevelDebug);
|
1310 |
|
1336 |
|
1311 |
// Initialize MPD client module
|
1337 |
// Initialize MPD client module
|
1312 |
MPDCli mpdcli(mpdhost, mpdport, mpdpassword);
|
1338 |
MPDCli mpdcli(mpdhost, mpdport, mpdpassword);
|
1313 |
if (!mpdcli.ok()) {
|
1339 |
if (!mpdcli.ok()) {
|
1314 |
LOGFAT("MPD connection failed" << endl);
|
1340 |
LOGFAT("MPD connection failed" << endl);
|
1315 |
return 1;
|
1341 |
return 1;
|
1316 |
}
|
1342 |
}
|
1317 |
|
1343 |
|
1318 |
// Create unique ID
|
1344 |
// Create unique ID
|
1319 |
string UUID = LibUPnP::makeDevUUID(friendlyname);
|
1345 |
string UUID = LibUPnP::makeDevUUID(friendlyname, hwaddr);
|
1320 |
|
1346 |
|
1321 |
// Read our XML data to make it available from the virtual directory
|
1347 |
// Read our XML data to make it available from the virtual directory
|
1322 |
string reason;
|
1348 |
string reason;
|
1323 |
unordered_map<string, string> xmlfiles;
|
1349 |
unordered_map<string, string> xmlfiles;
|
1324 |
for (int i = 0; i < xmlfilenamescnt; i++) {
|
1350 |
for (int i = 0; i < xmlfilenamescnt; i++) {
|