📄 apitest.c
字号:
/* $Id: apitest.c,v 1.8 2001/08/04 20:55:56 jm Exp $ * Tool for sending API message - Jari Hautio * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. */#define _GNU_SOURCE#include <stdio.h>#include <string.h>#include <sys/socket.h>#include <sys/un.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include "util.h"#include "agentapi.h"#define DEFAULT_PATH "api_rw"#define MYNAME "api_test_socket"int main(int argc, char *argv[]){ char mypath[256]; int s; struct api_msg msg; struct sockaddr_un addr; socklen_t addrlen; unsigned int i; int w; /* wait for reply ? */ addr.sun_family = AF_LOCAL; dynamics_strlcpy(addr.sun_path, DEFAULT_PATH, sizeof(addr.sun_path)); addrlen = sizeof(addr.sun_family) + strlen(DEFAULT_PATH); if (argc <= 1) { printf("Usage: apitest [-path <api_path>] [-wait] <command> \n" " path - specifies path for agent api socket\n" " wait - Waits for reply from agent.\n" "Available commands are:\n" " connect, locupd, abort, disconnect\n" "Commands can be abbreviated.\n"); return 0; } msg.code = API_DUMMY; msg.length = 0; msg.type = API_CALL_MSG; w = 0; /* process command line */ for (i = 1; i < argc; i++) { if (strncasecmp(argv[i], "-p", 2) == 0) { if (i + 1 < argc) { dynamics_strlcpy(addr.sun_path, argv[i + 1], sizeof(addr.sun_path)); i++; } else { printf("Path name missing.\n"); return 0; } } else if (strncasecmp(argv[i], "c", 1) == 0) { msg.code = API_CONNECT; } else if (strncasecmp(argv[i], "l", 1) == 0) { msg.code = API_UPDATE_LOCATION; if (i + 1 < argc && inet_aton(argv[i + 1], (struct in_addr *) msg.params)) { msg.length = 4; i++; } } else if (strncasecmp(argv[i], "a", 1) == 0) { msg.code = API_CANCEL; } else if (strncasecmp(argv[i], "-w", 2) == 0) { w = 1; } else if (strncasecmp(argv[i], "d", 1) == 0) { msg.code = API_DISCONNECT; } } *mypath = 0; getcwd(mypath, sizeof(mypath)); snprintf(&mypath[strlen(mypath)], sizeof(mypath) - strlen(mypath), "/%s", MYNAME); printf("Sending api call (code=%d). \n\tTo: '%s' \n\tUsing: '%s'.\n", msg.code, addr.sun_path, mypath); unlink(mypath); s = api_open_socket(mypath, NULL, NULL, 0777); if (s < 0) { perror("open_socket"); return 0; } if (msg.code != API_DUMMY) { if (api_send(s, &addr, addrlen, &msg) < 0) { perror("api_send"); } else { printf("Call sent.\n"); } } if (w) { printf("Waiting for reply...\n"); memset(&msg, 0, sizeof(msg)); memset(&addr, 0, sizeof(addr)); i = sizeof(addr); if (api_receive(s, &addr, &i, &msg) < 0) { perror("api receive"); } else { printf("Reply code = %d.\n", (short int) msg.code); } } close(s); unlink(mypath); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -