+const char *admin_portinfo(int sock, int argc, char *argv[])
+{
+ struct admin_message msg,
+ *m;
+ int i, ii, j;
+ int num;
+ int len;
+ int off;
+
+ /* send state request command */
+ memset(&msg, 0, sizeof(msg));
+ msg.message = ADMIN_REQUEST_STATE;
+ if (write(sock, &msg, sizeof(msg)) != sizeof(msg)) {
+ cleanup_curses();
+ return("Broken pipe while sending command.");
+ }
+
+ /* receive response */
+ if (read(sock, &msg, sizeof(msg)) != sizeof(msg)) {
+ cleanup_curses();
+ return("Broken pipe while receiving response.");
+ }
+
+ if (msg.message != ADMIN_RESPONSE_STATE) {
+ cleanup_curses();
+ return("Response not valid. Expecting state response.");
+ }
+ num = msg.u.s.interfaces + msg.u.s.remotes + msg.u.s.joins + msg.u.s.epoints + msg.u.s.ports;
+ m = (struct admin_message *)MALLOC(num*sizeof(struct admin_message));
+ off=0;
+ if (num) {
+ readagain:
+ if ((len = read(sock, ((unsigned char *)(m))+off, num*sizeof(struct admin_message)-off)) != num*(int)sizeof(struct admin_message)-off) {
+ if (len <= 0) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Broken pipe while receiving state infos.");
+ }
+ if (len < num*(int)sizeof(struct admin_message)) {
+ off+=len;
+ goto readagain;
+ }
+ }
+ }
+ j = 0;
+ i = 0;
+ while(i < msg.u.s.interfaces) {
+ if (m[j].message != ADMIN_RESPONSE_S_INTERFACE) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Response not valid. Expecting interface information.");
+ }
+ i++;
+ j++;
+ }
+ i = 0;
+ while(i < msg.u.s.remotes) {
+ if (m[j].message != ADMIN_RESPONSE_S_REMOTE) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Response not valid. Expecting remote application information.");
+ }
+ i++;
+ j++;
+ }
+ i = 0;
+ while(i < msg.u.s.joins) {
+ if (m[j].message != ADMIN_RESPONSE_S_JOIN) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Response not valid. Expecting join information.");
+ }
+ i++;
+ j++;
+ }
+ i = 0;
+ while(i < msg.u.s.epoints) {
+ if (m[j].message != ADMIN_RESPONSE_S_EPOINT) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Response not valid. Expecting endpoint information.");
+ }
+ i++;
+ j++;
+ }
+ i = 0;
+ while(i < msg.u.s.ports) {
+ if (m[j].message != ADMIN_RESPONSE_S_PORT) {
+ FREE(m, 0);
+ cleanup_curses();
+ return("Response not valid. Expecting port information.");
+ }
+ i++;
+ j++;
+ }
+ // now j is the number of message blocks
+
+ /* output interfaces */
+ i = 0;
+ ii = i + msg.u.s.interfaces;
+ while(i < ii) {
+ if (argc > 2) {
+ if (!!strcmp(argv[2], m[i].u.i.interface_name)) {
+ i++;
+ continue;
+ }
+ }
+ printf("%s:\n", m[i].u.i.interface_name);
+ if (m[i].u.i.portnum < 0)
+ printf("\t port = unknown\n");
+ else
+ printf("\t port = %d \"%s\"\n",m[i].u.i.portnum, m[i].u.i.portname);
+ printf("\t extension = %s\n", (m[i].u.i.extension)?"yes":"no");
+ if (m[i].u.i.block >= 2) {
+ printf("\t status = not loaded\n");
+ } else {
+ if (m[i].u.i.block)
+ printf("\t status = blocked\n");
+ else
+ printf("\t status = unblocked\n");
+ printf("\t mode = %s %s%s%s\n", (m[i].u.i.ntmode)?"NT-mode":"TE-mode", (m[i].u.i.ptp)?"ptp":"ptmp", (m[i].u.i.l1hold)?" l1hold":"", (m[i].u.i.l2hold)?" l2hold":"");
+ if (m[i].u.i.l1link < 0)
+ printf("\t l1 link = unknown\n");
+ else
+ printf("\t l1 link = %s\n", (m[i].u.i.l1link)?"up":"down");
+ if (m[i].u.i.ptp || !m[i].u.i.ntmode) {
+ if (m[i].u.i.l2link < 0)
+ printf("\t l2 link = unknown\n");
+ else
+ printf("\t l2 link = %s\n", (m[i].u.i.l2link)?"up":"down");
+ }
+ printf("\t usage = %d\n", m[i].u.i.use);
+ }
+ i++;
+ }
+
+ /* free memory */
+ FREE(m, 0);
+
+ return(NULL);
+}
+