VLCHTTPUploaderController.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*****************************************************************************
  2. * VLCHTTPUploaderViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Jean-Baptiste Kempf <jb # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Felix Paul Kühne <fkuehne # videolan.org>
  11. * Jean-Romain Prévost <jr # 3on.fr>
  12. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCAppDelegate.h"
  16. #import "VLCHTTPUploaderController.h"
  17. #import "VLCHTTPConnection.h"
  18. #import "HTTPServer.h"
  19. #import <ifaddrs.h>
  20. #import <arpa/inet.h>
  21. #if TARGET_IPHONE_SIMULATOR
  22. NSString *const WifiInterfaceName = @"en1";
  23. #else
  24. NSString *const WifiInterfaceName = @"en0";
  25. #endif
  26. @implementation VLCHTTPUploaderController
  27. - (id)init
  28. {
  29. if (self = [super init]) {
  30. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  31. [center addObserver:self selector:@selector(applicationDidBecomeActive:)
  32. name:UIApplicationDidBecomeActiveNotification object:nil];
  33. [center addObserver:self selector:@selector(applicationDidEnterBackground:)
  34. name:UIApplicationDidEnterBackgroundNotification object:nil];
  35. }
  36. return self;
  37. }
  38. - (void)applicationDidBecomeActive: (NSNotification *)notification
  39. {
  40. [self changeHTTPServerState:[[NSUserDefaults standardUserDefaults] boolForKey:kVLCSettingSaveHTTPUploadServerStatus]];
  41. }
  42. - (void)applicationDidEnterBackground: (NSNotification *)notification
  43. {
  44. [self changeHTTPServerState:NO];
  45. }
  46. - (BOOL)changeHTTPServerState:(BOOL)state
  47. {
  48. if (!state) {
  49. [self.httpServer stop];
  50. return true;
  51. }
  52. // Initialize our http server
  53. _httpServer = [[HTTPServer alloc] init];
  54. [_httpServer setInterface:WifiInterfaceName];
  55. // Tell the server to broadcast its presence via Bonjour.
  56. // This allows browsers such as Safari to automatically discover our service.
  57. [self.httpServer setType:@"_http._tcp."];
  58. // Serve files from the standard Sites folder
  59. NSString *docRoot = [[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"] stringByDeletingLastPathComponent];
  60. APLog(@"Setting document root: %@", docRoot);
  61. [self.httpServer setDocumentRoot:docRoot];
  62. [self.httpServer setPort:80];
  63. [self.httpServer setConnectionClass:[VLCHTTPConnection class]];
  64. NSError *error = nil;
  65. if (![self.httpServer start:&error]) {
  66. if (error.code == 13) {
  67. APLog(@"Port forbidden by OS, trying another one");
  68. [self.httpServer setPort:8888];
  69. if(![self.httpServer start:&error])
  70. return true;
  71. }
  72. /* Address already in Use, take a random one */
  73. if (error.code == 48) {
  74. APLog(@"Port already in use, trying another one");
  75. [self.httpServer setPort:0];
  76. if(![self.httpServer start:&error])
  77. return true;
  78. }
  79. if (error.code != 0)
  80. APLog(@"Error starting HTTP Server: %@", error.localizedDescription);
  81. return false;
  82. }
  83. return true;
  84. }
  85. - (NSString *)currentIPAddress
  86. {
  87. NSString *address = @"";
  88. struct ifaddrs *interfaces = NULL;
  89. struct ifaddrs *temp_addr = NULL;
  90. int success = getifaddrs(&interfaces);
  91. if (!success) {
  92. freeifaddrs(interfaces);
  93. return address;
  94. }
  95. temp_addr = interfaces;
  96. while (temp_addr != NULL) {
  97. if (temp_addr->ifa_addr->sa_family == AF_INET) {
  98. if([@(temp_addr->ifa_name) isEqualToString:WifiInterfaceName])
  99. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  100. }
  101. temp_addr = temp_addr->ifa_next;
  102. }
  103. freeifaddrs(interfaces);
  104. return address;
  105. }
  106. @end