VLCHTTPUploaderController.m 3.5 KB

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