2 // SOSPeerRateLimiter.m
3 // SecureObjectSyncServer
6 #import <Foundation/Foundation.h>
7 #import <keychain/ckks/RateLimiter.h>
8 #import "keychain/SecureObjectSync/SOSPeerRateLimiter.h"
10 #include "keychain/SecureObjectSync/SOSPeer.h"
11 #include <utilities/SecCFError.h>
12 #include <utilities/SecCFRelease.h>
13 #include <utilities/SecCFWrappers.h>
16 // RateLimiting Code per Peer
18 @implementation PeerRateLimiter
20 @synthesize peerID = peerID;
22 -(NSDictionary*) setUpConfigForPeer
24 NSData *configData = [@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
25 <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\
26 <plist version=\"1.0\">\
30 <key>maxStateSize</key>\
31 <integer>250</integer>\
32 <key>maxItemAge</key>\
33 <integer>3600</integer>\
34 <key>overloadDuration</key>\
35 <integer>1800</integer>\
45 <string>global</string>\
47 <integer>1000</integer>\
49 <integer>10</integer>\
55 <string>accessGroup</string>\
57 <integer>50</integer>\
59 <integer>900</integer>\
66 " dataUsingEncoding:NSUTF8StringEncoding];
69 return ([NSPropertyListSerialization propertyListWithData:configData options:NSPropertyListImmutable format:nil error:&err]);
72 -(instancetype)initWithPeer:(SOSPeerRef)peer
74 self = [super initWithConfig:[self setUpConfigForPeer]];
76 self.peerID = (__bridge NSString *)(SOSPeerGetID(peer));
77 self.accessGroupRateLimitState = [[NSMutableDictionary alloc] init];
78 self.accessGroupToTimer = [[NSMutableDictionary alloc]init];
79 self.accessGroupToNextMessageToSend = [[NSMutableDictionary alloc]init];
84 -(enum RateLimitState) stateForAccessGroup:(NSString*) accessGroup
86 enum RateLimitState stateForAccessGroup;
87 NSNumber *state = [self.accessGroupRateLimitState objectForKey:accessGroup];
90 //initialize access group state
91 stateForAccessGroup = RateLimitStateCanSend;
92 NSNumber *initialize = [[NSNumber alloc] initWithLong:stateForAccessGroup];
93 [self.accessGroupRateLimitState setObject:initialize forKey:accessGroup];
95 stateForAccessGroup = [state intValue];
97 return stateForAccessGroup;
101 @implementation KeychainItem
103 -(instancetype)initWithAccessGroup:(NSString *)accessGroup
107 _accessGroup = accessGroup;