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 if ((self = [super initWithConfig:[self setUpConfigForPeer]])) {
75 self.peerID = (__bridge NSString *)(SOSPeerGetID(peer));
76 self.accessGroupRateLimitState = [[NSMutableDictionary alloc] init];
77 self.accessGroupToTimer = [[NSMutableDictionary alloc]init];
78 self.accessGroupToNextMessageToSend = [[NSMutableDictionary alloc]init];
83 -(enum RateLimitState) stateForAccessGroup:(NSString*) accessGroup
85 enum RateLimitState stateForAccessGroup;
86 NSNumber *state = [self.accessGroupRateLimitState objectForKey:accessGroup];
89 //initialize access group state
90 stateForAccessGroup = RateLimitStateCanSend;
91 NSNumber *initialize = [[NSNumber alloc] initWithLong:stateForAccessGroup];
92 [self.accessGroupRateLimitState setObject:initialize forKey:accessGroup];
94 stateForAccessGroup = [state intValue];
96 return stateForAccessGroup;
100 @implementation KeychainItem
102 -(instancetype)initWithAccessGroup:(NSString *)accessGroup
104 if ((self = [super init])) {
105 _accessGroup = accessGroup;