MCPcopy Create free account
hub / github.com/Tencent/Hardcoder / getUidByPort

Method getUidByPort

libapp2sys/src/main/cpp/util.h:259–302  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

257 const static int UDP_PORT = 2;
258
259 static int getUidByPort(const int type, const int port) {
260
261 char szPort[8];
262 snprintf(szPort, sizeof(szPort), "%X", port);
263 perr("port type:%d port:%x hex:%s", type, port, szPort);
264
265 char const *path = NULL;
266 if (type == TCP_PORT) path = "/proc/net/tcp";
267 else if (type == UDP_PORT) path = "/proc/net/udp";
268 else return -2;
269
270
271 FILE *fp = fopen(path, "r");
272 if (!fp) {
273 return -3;
274 }
275
276 char line[4096];
277 while (fgets(line, sizeof(line), fp)) {
278
279 char *saveptr = line;
280 char *token = NULL;
281 int portHitRow = 0;
282 int colIndex = 0;
283 while ((token = strtok_r(saveptr, " ", &saveptr)) != NULL) {
284
285 if (colIndex == 1) {
286 char *saveptr2 = token;
287 char *token2 = NULL;
288 while ((token2 = strtok_r(saveptr2, ":", &saveptr2)) != NULL) {
289 if (!strcmp(szPort, token2)) portHitRow = 1;
290
291 }
292 }
293 if (portHitRow == 1 && colIndex == 7) {
294 fclose(fp);
295 return atoi(token);
296 }
297 colIndex++;
298 }
299 }
300 fclose(fp);
301 return -1;
302 }
303};
304
305#endif //HARDCODER_UTIL_H_H

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected