You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
10 KiB

  1. --- a/main.c
  2. +++ b/main.c
  3. @@ -78,6 +78,7 @@ static void usage(const char *argv0) {
  4. #if IR
  5. " -i [<filename>]\tEnable lirc remote control support (lirc config file ~/.lircrc used if filename not specified)\n"
  6. #endif
  7. + " -I <interface>\t\tNetwork interface used to send discovery\n"
  8. " -m <mac addr>\t\tSet mac address, format: ab:cd:ef:12:34:56\n"
  9. " -M <modelname>\tSet the squeezelite player model name sent to the server (default: " MODEL_NAME_STRING ")\n"
  10. " -n <name>\t\tSet the player name\n"
  11. @@ -233,6 +234,8 @@ int main(int argc, char **argv) {
  12. extern bool pcm_check_header;
  13. char *logfile = NULL;
  14. u8_t mac[6];
  15. + char *iface = NULL;
  16. + in_addr_t bcast_addr = 0;
  17. unsigned stream_buf_size = STREAMBUF_SIZE;
  18. unsigned output_buf_size = 0; // set later
  19. unsigned rates[MAX_SUPPORTED_SAMPLERATES] = { 0 };
  20. @@ -271,6 +274,7 @@ int main(int argc, char **argv) {
  21. int maxSampleRate = 0;
  22. + memset(mac, 0, sizeof(mac));
  23. char *optarg = NULL;
  24. int optind = 1;
  25. int i;
  26. @@ -278,8 +282,6 @@ int main(int argc, char **argv) {
  27. #define MAXCMDLINE 512
  28. char cmdline[MAXCMDLINE] = "";
  29. - get_mac(mac);
  30. -
  31. for (i = 0; i < argc && (strlen(argv[i]) + strlen(cmdline) + 2 < MAXCMDLINE); i++) {
  32. strcat(cmdline, argv[i]);
  33. strcat(cmdline, " ");
  34. @@ -287,7 +289,7 @@ int main(int argc, char **argv) {
  35. while (optind < argc && strlen(argv[optind]) >= 2 && argv[optind][0] == '-') {
  36. char *opt = argv[optind] + 1;
  37. - if (strstr("oabcCdefmMnNpPrs"
  38. + if (strstr("oabcCdefImMnNpPrs"
  39. #if ALSA
  40. "UV"
  41. #endif
  42. @@ -382,6 +384,9 @@ int main(int argc, char **argv) {
  43. case 'f':
  44. logfile = optarg;
  45. break;
  46. + case 'I':
  47. + iface = optarg;
  48. + break;
  49. case 'm':
  50. {
  51. int byte = 0;
  52. @@ -667,6 +672,11 @@ int main(int argc, char **argv) {
  53. winsock_init();
  54. #endif
  55. + if (!(bcast_addr = get_iface_info(log_slimproto, iface, mac))) {
  56. + fprintf(stderr, "Error binding to network or none given\n");
  57. + exit(1);
  58. + }
  59. +
  60. stream_init(log_stream, stream_buf_size);
  61. if (!strcmp(output_device, "-")) {
  62. @@ -710,7 +720,7 @@ int main(int argc, char **argv) {
  63. exit(1);
  64. }
  65. - slimproto(log_slimproto, server, mac, name, namefile, modelname, maxSampleRate);
  66. + slimproto(log_slimproto, server, bcast_addr, mac, name, namefile, modelname, maxSampleRate);
  67. decode_close();
  68. stream_close();
  69. --- a/squeezelite.h
  70. +++ b/squeezelite.h
  71. @@ -403,7 +403,7 @@ typedef enum { EVENT_TIMEOUT = 0, EVENT_
  72. char *next_param(char *src, char c);
  73. u32_t gettime_ms(void);
  74. -void get_mac(u8_t *mac);
  75. +in_addr_t get_iface_info(log_level level, char *iface, u8_t *mac);
  76. void set_nonblock(sockfd s);
  77. int connect_timeout(sockfd sock, const struct sockaddr *addr, socklen_t addrlen, int timeout);
  78. void server_addr(char *server, in_addr_t *ip_ptr, unsigned *port_ptr);
  79. @@ -459,7 +459,7 @@ void buf_init(struct buffer *buf, size_t
  80. void buf_destroy(struct buffer *buf);
  81. // slimproto.c
  82. -void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate);
  83. +void slimproto(log_level level, char *server, in_addr_t bcast_addr, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate);
  84. void slimproto_stop(void);
  85. void wake_controller(void);
  86. --- a/slimproto.c
  87. +++ b/slimproto.c
  88. @@ -119,7 +119,7 @@ void send_packet(u8_t *packet, size_t le
  89. }
  90. }
  91. -static void sendHELO(bool reconnect, const char *fixed_cap, const char *var_cap, u8_t mac[6]) {
  92. +static void sendHELO(bool reconnect, const char *fixed_cap, const char *var_cap, u8_t *mac) {
  93. const char *base_cap = "Model=squeezelite,AccuratePlayPoints=1,HasDigitalOut=1,HasPolarityInversion=1,Firmware=" VERSION;
  94. struct HELO_packet pkt;
  95. @@ -782,7 +782,7 @@ void wake_controller(void) {
  96. wake_signal(wake_e);
  97. }
  98. -in_addr_t discover_server(void) {
  99. +in_addr_t discover_server(in_addr_t bcast_addr) {
  100. struct sockaddr_in d;
  101. struct sockaddr_in s;
  102. char *buf;
  103. @@ -798,7 +798,7 @@ in_addr_t discover_server(void) {
  104. memset(&d, 0, sizeof(d));
  105. d.sin_family = AF_INET;
  106. d.sin_port = htons(PORT);
  107. - d.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  108. + d.sin_addr.s_addr = bcast_addr;
  109. pollinfo.fd = disc_sock;
  110. pollinfo.events = POLLIN;
  111. @@ -829,7 +829,7 @@ in_addr_t discover_server(void) {
  112. #define FIXED_CAP_LEN 256
  113. #define VAR_CAP_LEN 128
  114. -void slimproto(log_level level, char *server, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate) {
  115. +void slimproto(log_level level, char *server, in_addr_t bcast_addr, u8_t mac[6], const char *name, const char *namefile, const char *modelname, int maxSampleRate) {
  116. struct sockaddr_in serv_addr;
  117. static char fixed_cap[FIXED_CAP_LEN], var_cap[VAR_CAP_LEN] = "";
  118. bool reconnect = false;
  119. @@ -849,7 +849,7 @@ void slimproto(log_level level, char *se
  120. }
  121. if (!slimproto_ip) {
  122. - slimproto_ip = discover_server();
  123. + slimproto_ip = discover_server(bcast_addr);
  124. }
  125. if (!slimproto_port) {
  126. @@ -924,7 +924,7 @@ void slimproto(log_level level, char *se
  127. // rediscover server if it was not set at startup
  128. if (!server && ++failed_connect > 5) {
  129. - slimproto_ip = serv_addr.sin_addr.s_addr = discover_server();
  130. + slimproto_ip = serv_addr.sin_addr.s_addr = discover_server(bcast_addr);
  131. }
  132. } else {
  133. --- a/utils.c
  134. +++ b/utils.c
  135. @@ -22,11 +22,11 @@
  136. #include "squeezelite.h"
  137. #if LINUX || OSX || FREEBSD
  138. -#include <sys/ioctl.h>
  139. +#include <sys/types.h>
  140. #include <net/if.h>
  141. -#include <netdb.h>
  142. -#if FREEBSD
  143. #include <ifaddrs.h>
  144. +#include <netdb.h>
  145. +#if FREEBSD || OSX
  146. #include <net/if_dl.h>
  147. #include <net/if_types.h>
  148. #endif
  149. @@ -44,15 +44,11 @@
  150. #if WIN
  151. #include <iphlpapi.h>
  152. #endif
  153. -#if OSX
  154. -#include <net/if_dl.h>
  155. -#include <net/if_types.h>
  156. -#include <ifaddrs.h>
  157. -#include <netdb.h>
  158. -#endif
  159. #include <fcntl.h>
  160. +static log_level loglevel;
  161. +
  162. // logging functions
  163. const char *logtime(void) {
  164. static char buf[100];
  165. @@ -114,58 +110,101 @@ u32_t gettime_ms(void) {
  166. #endif
  167. }
  168. -// mac address
  169. -#if LINUX && !defined(SUN)
  170. -// search first 4 interfaces returned by IFCONF
  171. -void get_mac(u8_t mac[]) {
  172. - char *utmac;
  173. - struct ifconf ifc;
  174. - struct ifreq *ifr, *ifend;
  175. - struct ifreq ifreq;
  176. - struct ifreq ifs[4];
  177. +// Get broadcast address for interface (given or first available)
  178. +// Return MAC address if none given
  179. +#if LINUX || OSX || FREEBSD
  180. - utmac = getenv("UTMAC");
  181. - if (utmac)
  182. - {
  183. - if ( strlen(utmac) == 17 )
  184. - {
  185. - if (sscanf(utmac,"%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx",
  186. - &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) == 6)
  187. - {
  188. - return;
  189. - }
  190. - }
  191. +in_addr_t get_iface_info(log_level level, char *iface, u8_t *mac) {
  192. + struct ifaddrs *addrs, *ifa;
  193. + struct sockaddr *sdl;
  194. + char ifname[16];
  195. + unsigned char *ptr;
  196. + in_addr_t bcast_addr = 0;
  197. + int have_mac = 0, have_ifname = 0;
  198. +
  199. + loglevel = level;
  200. +
  201. + // Check for non-zero MAC
  202. + if ((mac[0] | mac[1] | mac[2]) != 0)
  203. + have_mac = 1;
  204. +
  205. + // Copy interface name, if it was provided.
  206. + if (iface != NULL)
  207. + {
  208. + if( strlen(iface) > sizeof(ifname) )
  209. + return -1;
  210. + strncpy(ifname, iface, sizeof(ifname));
  211. + have_ifname = 1;
  212. }
  213. - mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0;
  214. + if (getifaddrs(&addrs) == 0) {
  215. + //iterate to find corresponding ethernet address
  216. + for (ifa = addrs; ifa; ifa = ifa->ifa_next) {
  217. + // Skip LOOPBACK interfaces, DOWN interfaces and interfaces that
  218. + // don't support BROADCAST.
  219. + if ((ifa->ifa_flags & IFF_LOOPBACK)
  220. + || !(ifa->ifa_flags & IFF_UP)
  221. + || !(ifa->ifa_flags & IFF_BROADCAST))
  222. + {
  223. + continue;
  224. + }
  225. - int s = socket(AF_INET, SOCK_DGRAM, 0);
  226. + if (!have_ifname)
  227. + {
  228. + // We have found a valid interface name. Keep it.
  229. + strncpy(ifname, ifa->ifa_name, sizeof(ifname));
  230. + have_ifname = 1;
  231. + } else {
  232. + if (strncmp(ifname, ifa->ifa_name, sizeof(ifname)) != 0)
  233. + {
  234. + // This is not the interface we're looking for.
  235. + continue;
  236. + }
  237. + }
  238. - ifc.ifc_len = sizeof(ifs);
  239. - ifc.ifc_req = ifs;
  240. - if (ioctl(s, SIOCGIFCONF, &ifc) == 0) {
  241. - ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq));
  242. + // Check address family.
  243. + if ((ifa->ifa_addr->sa_family == AF_INET) &&
  244. + (((struct sockaddr_in *)ifa->ifa_broadaddr)->sin_addr.s_addr != 0))
  245. + {
  246. + // Get broadcast address and MAC address
  247. + bcast_addr = ((struct sockaddr_in *)ifa->ifa_broadaddr)->sin_addr.s_addr;
  248. + break;
  249. + }
  250. + else
  251. + {
  252. + // Address is not IPv4
  253. + if (iface == NULL)
  254. + have_ifname = 0;
  255. + }
  256. +
  257. + }
  258. - for (ifr = ifc.ifc_req; ifr < ifend; ifr++) {
  259. - if (ifr->ifr_addr.sa_family == AF_INET) {
  260. -
  261. - strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
  262. - if (ioctl (s, SIOCGIFHWADDR, &ifreq) == 0) {
  263. - memcpy(mac, ifreq.ifr_hwaddr.sa_data, 6);
  264. - if (mac[0]+mac[1]+mac[2] != 0) {
  265. - break;
  266. - }
  267. + // Find MAC address matching interface
  268. + if (!have_mac && (bcast_addr != 0)) {
  269. + for (ifa = addrs; ifa; ifa = ifa->ifa_next) {
  270. + if ((ifa->ifa_addr->sa_family == PF_PACKET) &&
  271. + (strncmp(ifname, ifa->ifa_name, sizeof(ifname)) == 0)) {
  272. + sdl = (struct sockaddr *)(ifa->ifa_addr);
  273. + ptr = (unsigned char *)sdl->sa_data;
  274. + memcpy(mac, ptr + 10, 6);
  275. + have_mac = 1;
  276. }
  277. }
  278. }
  279. +
  280. + freeifaddrs(addrs);
  281. }
  282. - close(s);
  283. + LOG_INFO("Interface: %s, broadcast: %08X, macaddr = %02x:%02x:%02x:%02x:%02x:%02x",
  284. + ifname, bcast_addr, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  285. +
  286. + return bcast_addr;
  287. }
  288. #endif
  289. +
  290. #if SUN
  291. void get_mac(u8_t mac[]) {
  292. struct arpreq parpreq;
  293. @@ -232,30 +271,6 @@ void get_mac(u8_t mac[]) {
  294. }
  295. #endif
  296. -#if OSX || FREEBSD
  297. -void get_mac(u8_t mac[]) {
  298. - struct ifaddrs *addrs, *ptr;
  299. - const struct sockaddr_dl *dlAddr;
  300. - const unsigned char *base;
  301. -
  302. - mac[0] = mac[1] = mac[2] = mac[3] = mac[4] = mac[5] = 0;
  303. -
  304. - if (getifaddrs(&addrs) == 0) {
  305. - ptr = addrs;
  306. - while (ptr) {
  307. - if (ptr->ifa_addr->sa_family == AF_LINK && ((const struct sockaddr_dl *) ptr->ifa_addr)->sdl_type == IFT_ETHER) {
  308. - dlAddr = (const struct sockaddr_dl *)ptr->ifa_addr;
  309. - base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen];
  310. - memcpy(mac, base, min(dlAddr->sdl_alen, 6));
  311. - break;
  312. - }
  313. - ptr = ptr->ifa_next;
  314. - }
  315. - freeifaddrs(addrs);
  316. - }
  317. -}
  318. -#endif
  319. -
  320. #if WIN
  321. #pragma comment(lib, "IPHLPAPI.lib")
  322. void get_mac(u8_t mac[]) {