/** Returns a human-readable string representing this PathMatcherEntry, for easier debugging */ String PathMatcherEntry :: ToString() const { String ret; if (_parser()) ret += _parser()->ToString().Prepend("Parser=[").Append("]"); if (_filter()) { char buf[128]; muscleSprintf(buf, "%sfilter=%p", ret.HasChars()?" ":"", _filter()); ret += buf; } return ret; }
String AbstractReflectSession :: GetSessionDescriptionString() const { uint16 port = _ipAddressAndPort.GetPort(); String ret = GetTypeName(); ret += " "; ret += GetSessionIDString(); ret += (port>0)?" at [":" to ["; ret += _hostName; char buf[64]; muscleSprintf(buf, ":%u]", (port>0)?port:_asyncConnectDest.GetPort()); ret += buf; return ret; }
String HexBytesToString(const uint8 * buf, uint32 numBytes) { String ret; if (ret.Prealloc(numBytes*3) == B_NO_ERROR) { for (uint32 i=0; i<numBytes; i++) { if (i > 0) ret += ' '; char b[32]; muscleSprintf(b, "%02x", buf[i]); ret += b; } } return ret; }
static void WriteOctalASCII(uint8 * b, uint64 val, uint8 fieldSize) { // gotta pad out the file data to the nearest block boundary! char formatStr[16]; muscleStrcpy(formatStr, UINT64_FORMAT_SPEC " "); char * pi = strchr(formatStr, 'u'); if (pi) *pi = 'o'; // gotta use octal here! char tmp[256]; muscleSprintf(tmp, formatStr, val); int numChars = muscleMin((int)fieldSize, ((int)(strlen(tmp)+1))); // include the NUL byte if possible uint8 * dStart = (b+fieldSize)-numChars; memcpy(dStart, tmp, numChars); memset(b, '0', dStart-b); // initial zeros }
String HexBytesToString(const Queue<uint8> & bytes) { const uint32 numBytes = bytes.GetNumItems(); String ret; if (ret.Prealloc(numBytes*3) == B_NO_ERROR) { for (uint32 i=0; i<numBytes; i++) { if (i > 0) ret += ' '; char b[32]; muscleSprintf(b, "%02x", bytes[i]); ret += b; } } return ret; }
AbstractReflectSession :: AbstractReflectSession() : _sessionID(GetNextGlobalID(_sessionIDCounter)) , _connectingAsync(false) , _isConnected(false) , _maxAsyncConnectPeriod(MUSCLE_MAX_ASYNC_CONNECT_DELAY_MICROSECONDS) , _asyncConnectTimeoutTime(MUSCLE_TIME_NEVER) , _reconnectViaTCP(true) , _lastByteOutputAt(0) , _lastReportedQueueSize(0) , _maxInputChunk(MUSCLE_NO_LIMIT) , _maxOutputChunk(MUSCLE_NO_LIMIT) , _outputStallLimit(MUSCLE_TIME_NEVER) , _autoReconnectDelay(MUSCLE_TIME_NEVER) , _reconnectTime(MUSCLE_TIME_NEVER) , _wasConnected(false) , _isExpendable(false) { char buf[64]; muscleSprintf(buf, UINT32_FORMAT_SPEC, _sessionID); _idString = buf; }
status_t SharedMemory :: SetArea(const char * keyString, uint32 createSize, bool returnLocked) { UnsetArea(); // make sure everything is deallocated to start with #if defined(MUSCLE_FAKE_SHARED_MEMORY) if (createSize > 0) { _area = muscleAlloc(createSize); if (_area) { memset(_area, 0, createSize); _areaName = keyString; _areaSize = createSize; _isCreatedLocally = true; _isLocked = returnLocked; _isLockedReadOnly = false; return B_NO_ERROR; } else WARN_OUT_OF_MEMORY; } #elif defined(WIN32) char buf[64]; if (keyString == NULL) { muscleSprintf(buf, INT32_FORMAT_SPEC, GetTickCount()); // No user-supplied name? We'll pick an arbitrary name then keyString = buf; } _areaName = keyString; // For windows we only use a Mutex, because even a Windows semaphore isn't enough to // do shared read locking. When I figure out how to do interprocess shared read locking // under Windows I will implement that, but for now it's always exclusive-locking. :^( _mutex = CreateMutexA(NULL, true, (_areaName+"__mutex")()); if (_mutex != NULL) { bool ok = true; if (GetLastError() == ERROR_ALREADY_EXISTS) ok = (LockAreaReadWrite() == B_NO_ERROR); else { // We created it in our CreateMutex() call, and it's already locked for us _isLocked = true; _isLockedReadOnly = false; } if (ok) { char buf[MAX_PATH]; if (GetTempPathA(sizeof(buf), buf) > 0) { _fileName = _areaName.Prepend(buf)+"__file"; _file = CreateFileA(_fileName(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH|FILE_FLAG_RANDOM_ACCESS, NULL); if (_file != INVALID_HANDLE_VALUE) { _isCreatedLocally = (GetLastError() != ERROR_ALREADY_EXISTS); if (createSize == 0) createSize = GetFileSize(_file, NULL); _areaSize = createSize; // assume the file will be resized automagically for us if (_areaSize > 0) { _map = CreateFileMappingA(_file, NULL, PAGE_READWRITE, 0, createSize, (_areaName+"__map")()); if (_map) { _area = MapViewOfFile(_map, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (_area) { if (returnLocked == false) UnlockArea(); return B_NO_ERROR; } } } } } } } #else key_t requestedKey = IPC_PRIVATE; if (keyString) { requestedKey = (key_t) CalculateHashCode(keyString, (uint32)strlen(keyString)); if (requestedKey == IPC_PRIVATE) requestedKey++; _areaName = keyString; } DECLARE_SEMCTL_ARG(semopts); const int permissionBits = 0777; // Try to create a new semaphore to control access to our area _semID = semget(requestedKey, 1, IPC_CREAT|IPC_EXCL|permissionBits); if (_semID >= 0) { // race condition here!? semopts.val = LARGEST_SEMAPHORE_DELTA; if (semctl(_semID, 0, SETVAL, semopts) < 0) _semID = -1; // oops! } else _semID = semget(requestedKey, 1, permissionBits); // Couldn't create? then get the existing one if (_semID >= 0) { _key = requestedKey; // If we requested a private key, we still need to know the actual key value if (_key == IPC_PRIVATE) { struct semid_ds semInfo = {}; // the braces zero-initialize the struct for us, to keep clang++SA happy semopts.buf = &semInfo; if (semctl(_semID, 0, IPC_STAT, semopts) == 0) { # ifdef __linux__ _key = semInfo.sem_perm.__key; // Mac os-x leopard uses '_key' by default. Both Tiger and Leopard may use _key if the following condition is true, otherwise they use 'key'. # elif (defined(__APPLE__) && (defined(__POSIX_C_SOURCE) || defined(__LP64__))) || __DARWIN_UNIX03 _key = semInfo.sem_perm._key; # else _key = semInfo.sem_perm.key; # endif } _areaName = "private"; // sorry, it's the best I can do short of figuring out how to invert the hash function! } if ((_key != IPC_PRIVATE)&&(LockAreaReadWrite() == B_NO_ERROR)) { _areaID = shmget(_key, 0, permissionBits); if ((_areaID < 0)&&(createSize > 0)) { _areaID = shmget(_key, createSize, IPC_CREAT|IPC_EXCL|permissionBits); _isCreatedLocally = true; } if (_areaID >= 0) { _area = shmat(_areaID, NULL, 0); if ((_area)&&(_area != ((void *)-1))) // FogBugz #7294 { // Now get the stats on our area struct shmid_ds buf; if (shmctl(_areaID, IPC_STAT, &buf) == 0) { _areaSize = (uint32) buf.shm_segsz; if (returnLocked == false) UnlockArea(); return B_NO_ERROR; } } } } } #endif UnsetArea(); // oops, roll back everything! return B_ERROR; }
void App::ArgvReceived(int32 argc, char **argv) { printf("ArgvReceived\n"); Message args; (void) ParseArgs(argc, argv, args); HandleStandardDaemonArgs(args); const char * value; if (args.HasName("help")) { Log(MUSCLE_LOG_INFO, "Usage: muscled [port=%u] [listen=ip:port] [displaylevel=lvl] [filelevel=lvl] [logfile=filename]\n", DEFAULT_MUSCLED_PORT); #ifdef MUSCLE_ENABLE_MEMORY_TRACKING Log(MUSCLE_LOG_INFO, " [maxmem=megs]\n"); #endif Log(MUSCLE_LOG_INFO, " [maxnodespersession=num] [remap=oldip=newip]\n"); Log(MUSCLE_LOG_INFO, " [ban=ippattern] [require=ippattern]\n"); Log(MUSCLE_LOG_INFO, " [privban=ippattern] [privunban=ippattern]\n"); Log(MUSCLE_LOG_INFO, " [privkick=ippattern] [privall=ippattern]\n"); Log(MUSCLE_LOG_INFO, " [maxsendrate=kBps] [maxreceiverate=kBps]\n"); Log(MUSCLE_LOG_INFO, " [maxcombinedrate=kBps] [maxmessagesize=k]\n"); Log(MUSCLE_LOG_INFO, " [maxsessions=num] [maxsessionsperhost=num]\n"); Log(MUSCLE_LOG_INFO, " [localhost=ipaddress] [daemon]\n"); Log(MUSCLE_LOG_INFO, " - port may be any number between 1 and 65536\n"); Log(MUSCLE_LOG_INFO, " - listen is like port, except it includes a local interface IP as well.\n"); Log(MUSCLE_LOG_INFO, " - lvl is: none, critical, errors, warnings, info, debug, or trace.\n"); #ifdef MUSCLE_ENABLE_MEMORY_TRACKING Log(MUSCLE_LOG_INFO, " - maxmem is the max megabytes of memory the server may use (default=unlimited)\n"); #endif Log(MUSCLE_LOG_INFO, " - You may also put one or more ban=<pattern> arguments in.\n"); Log(MUSCLE_LOG_INFO, " Each pattern specifies one or more IP addresses to\n"); Log(MUSCLE_LOG_INFO, " disallow connections from, e.g. ban=192.168.*.*\n"); Log(MUSCLE_LOG_INFO, " - You may put one or more require=<pattern> arguments in.\n"); Log(MUSCLE_LOG_INFO, " If any of these are present, then only IP addresses that match\n"); Log(MUSCLE_LOG_INFO, " at least one of them will be allowed to connect.\n"); Log(MUSCLE_LOG_INFO, " - To assign privileges, specify one of the following:\n"); Log(MUSCLE_LOG_INFO, " privban=<pattern>, privunban=<pattern>,\n"); Log(MUSCLE_LOG_INFO, " privkick=<pattern> or privall=<pattern>.\n"); Log(MUSCLE_LOG_INFO, " privall assigns all privileges to the matching IP addresses.\n"); Log(MUSCLE_LOG_INFO, " - remap tells muscled to treat connections from a given IP address\n"); Log(MUSCLE_LOG_INFO, " as if they are coming from another (for stupid NAT tricks, etc)\n"); Log(MUSCLE_LOG_INFO, " - If daemon is specified, muscled will run as a background process.\n"); } { for (int32 i = 0; (args.FindString("port", i, &value) == B_NO_ERROR); i++) { int16 port = atoi(value); if (port >= 0) listenPorts.PutWithDefault(IPAddressAndPort(invalidIP, port)); } for (int32 i = 0; (args.FindString("listen", i, &value) == B_NO_ERROR); i++) { IPAddressAndPort iap(value, DEFAULT_MUSCLED_PORT, false); if (iap.GetPort() > 0) listenPorts.PutWithDefault(iap); else LogTime(MUSCLE_LOG_ERROR, "Unable to parse IP/port string [%s]\n", value); } } { for (int32 i = 0; (args.FindString("remap", i, &value) == B_NO_ERROR); i++) { StringTokenizer tok(value, ",="); const char * from = tok(); const char * to = tok(); ip_address fromIP = from ? Inet_AtoN(from) : 0; if ((fromIP != invalidIP)&&(to)) { char ipbuf[64]; Inet_NtoA(fromIP, ipbuf); LogTime(MUSCLE_LOG_INFO, "Will treat connections coming from [%s] as if they were from [%s].\n", ipbuf, to); tempRemaps.Put(fromIP, to); } else LogTime(MUSCLE_LOG_ERROR, "Error parsing remap argument (it should look something like remap=192.168.0.1,132.239.50.8).\n"); } } #ifdef MUSCLE_ENABLE_MEMORY_TRACKING if (args.FindString("maxmem", &value) == B_NO_ERROR) { int megs = muscleMax(1, atoi(value)); LogTime(MUSCLE_LOG_INFO, "Limiting memory usage to %i megabyte%s.\n", megs, (megs==1)?"":"s"); maxBytes = megs*1024L*1024L; } #endif if (args.FindString("maxmessagesize", &value) == B_NO_ERROR) { int k = muscleMax(1, atoi(value)); LogTime(MUSCLE_LOG_INFO, "Limiting message sizes to %i kilobyte%s.\n", k, (k==1)?"":"s"); maxMessageSize = k*1024L; } if (args.FindString("maxsendrate", &value) == B_NO_ERROR) { float k = (float) atof(value); maxSendRate = muscleMax((uint32)0, (uint32)(k * 1024.0f)); } if (args.FindString("maxreceiverate", &value) == B_NO_ERROR) { float k = (float) atof(value); maxReceiveRate = muscleMax((uint32)0, (uint32)(k * 1024.0f)); } if (args.FindString("maxcombinedrate", &value) == B_NO_ERROR) { float k = (float) atof(value); maxCombinedRate = muscleMax((uint32)0, (uint32)(k * 1024.0f)); } if (args.FindString("maxnodespersession", &value) == B_NO_ERROR) { maxNodesPerSession = atoi(value); LogTime(MUSCLE_LOG_INFO, "Limiting nodes-per-session to "UINT32_FORMAT_SPEC".\n", maxNodesPerSession); } if (args.FindString("maxsessions", &value) == B_NO_ERROR) { maxSessions = atoi(value); LogTime(MUSCLE_LOG_INFO, "Limiting total session count to "UINT32_FORMAT_SPEC".\n", maxSessions); } if (args.FindString("maxsessionsperhost", &value) == B_NO_ERROR) { maxSessionsPerHost = atoi(value); LogTime(MUSCLE_LOG_INFO, "Limiting session count for any given host to "UINT32_FORMAT_SPEC".\n", maxSessionsPerHost); } if (args.FindString("privatekey", &value) == B_NO_ERROR) { fprivateKeyFilePath = new String(value); //const String * fprivateKeyFilePath = args.GetStringPointer("privatekey"); //LogTime(MUSCLE_LOG_INFO, "Limiting session count for any given host to "UINT32_FORMAT_SPEC".\n", fprivateKeyFilePath); } { for (int32 i = 0; (args.FindString("ban", i, &value) == B_NO_ERROR); i++) { LogTime(MUSCLE_LOG_INFO, "Banning all clients whose IP addresses match [%s].\n", value); bans.AddTail(value); } } { for (int32 i = 0; (args.FindString("require", i, &value) == B_NO_ERROR); i++) { LogTime(MUSCLE_LOG_INFO, "Allowing only clients whose IP addresses match [%s].\n", value); requires.AddTail(value); } } { const char * privNames[] = {"privkick", "privban", "privunban", "privall"}; for (int p = 0; p <= PR_NUM_PRIVILEGES; p++) { // if (p == PR_NUM_PRIVILEGES), that means all privileges for (int32 q=0; (args.FindString(privNames[p], q, &value) == B_NO_ERROR); q++) { LogTime(MUSCLE_LOG_INFO, "Clients whose IP addresses match [%s] get %s privileges.\n", value, privNames[p]+4); char tt[32]; muscleSprintf(tt, "priv%i", p); tempPrivs.AddString(tt, value); } } } }