Back to index

im-sdk  12.3.91
Defines | Functions
udclient.c File Reference
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <stdio.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include "udclient.h"

Go to the source code of this file.

Defines

#define BUFSIZE   (4096)
#define COMMAND   "udclient"
#define VERSION   "0.6"

Functions

static ssize_t ud_transport (int in, int out, char *buf, size_t bufsize)
int ud_cli (FILE *in, FILE *out, int sockfd)
static void print_help (void)
static int proc_opt (int argc, char *argv[])
int main (int argc, char *argv[])

Define Documentation

#define BUFSIZE   (4096)

Definition at line 24 of file udclient.c.

#define COMMAND   "udclient"

Definition at line 26 of file udclient.c.

#define VERSION   "0.6"

Definition at line 27 of file udclient.c.


Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 203 of file udclient.c.

{
       int sockfd;
       int pathname_i;
       struct sockaddr_un svaddr;
       FILE *in, *out;
       int proto = SOCK_STREAM;

       if ((pathname_i = proc_opt(argc, argv)) <= 0) {
              exit(1);
       }

       in = stdin; out      = stdout; 

       /* create unix domain socket and connect server*/
       bzero(&svaddr, sizeof(svaddr));
       svaddr.sun_family = AF_LOCAL;
       strncpy(svaddr.sun_path, argv[pathname_i], sizeof(svaddr.sun_path) - 1);

       if ((sockfd = socket(PF_LOCAL, proto, 0)) < 0) {
              perror("socket");
              exit(1);
       }
       if (connect(sockfd, (struct sockaddr*)&svaddr, SUN_LEN(&svaddr)) < 0) {
              perror("connect");
              close(sockfd);
              exit(1);
       }

       /* transport data */
       if (!ud_cli(in, out, sockfd)){
              close(sockfd);       
              exit(1);
       }

       close(sockfd);
       exit(0);
}

Here is the call graph for this function:

static void print_help ( void  ) [static]

Definition at line 150 of file udclient.c.

{
       fprintf(stderr, "usage: " COMMAND " [-v|-h] path\n"
              "\n"
              "-h\t show this message.\n"
              "-v\t print version.\n"
              "\n"
              "Communicate with server through unix domain socket\n"
              "specified by path argument.\n"
              "Written by Hiroshi Miura <miura@da-cha.org>, Jan. 2005\n"
              );
}

Here is the caller graph for this function:

static int proc_opt ( int  argc,
char *  argv[] 
) [static]

Definition at line 163 of file udclient.c.

{
       int i = 1;
       char c;

       if (argc < 2) {
              fprintf(stderr, "Error: socket pathname is needed.\n\n");
              return 0;
       }

       while (1) {
              if ((c = getopt(argc, argv, "hv")) == -1) 
                     break;

              switch (c) {
              case 'h':
                     print_help();
                     return 0;
              case 'v':
                     fprintf(stderr, COMMAND ": Version " VERSION "\n");
                     return 0;
              default:
                     fprintf(stderr, "Error: unknown option(s).\n\n");
                     print_help();
                     return 0;
              }
       }

       if ( argc == i + 1 )
              return i;
       
       if ( argc > i + 1 ) {
              fprintf(stderr, "Error: too many argument(s).\n\n");
       } else {
              fprintf(stderr, "Error: socket pathname is needed.\n\n");
       }
       print_help();
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ud_cli ( FILE *  in,
FILE *  out,
int  sockfd 
)

Definition at line 107 of file udclient.c.

{
       int maxfd, in_eof;
       fd_set rset;
       char *buf;

       if ((buf = (char *)malloc(sizeof(char)*BUFSIZE)) == NULL)
              exit (1);

       in_eof = 0;
       FD_ZERO(&rset);
       while (1) {
              if (in_eof == 0) {
                     FD_SET(fileno(in), &rset);
              }
              FD_SET(sockfd, &rset);
              maxfd = max(fileno(in), sockfd) + 1;
              select(maxfd, &rset, NULL, NULL, NULL);

              if (FD_ISSET(sockfd, &rset)) { /*socket is readable */
                     if (ud_transport(sockfd, fileno(out), buf, BUFSIZE) == 0) {
                            if (in_eof == 1) { /* normal termination */
                                   break;
                            } else {
                                   perror("ud_cli: server terminated prematurely"); 
                                   free(buf);
                                   return 1;
                            }
                     }
              }
              if (FD_ISSET(fileno(in), &rset)) { /* stdin  is readable */
                     if (ud_transport(fileno(in), sockfd, buf, BUFSIZE) == 0) {
                            in_eof = 1;
                            shutdown(sockfd, SHUT_WR); /* send FIN */
                            FD_CLR(fileno(in), &rset);
                     }
              }
       }
       free(buf);
       return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static ssize_t ud_transport ( int  in,
int  out,
char *  buf,
size_t  bufsize 
) [static]

Definition at line 32 of file udclient.c.

{
       ssize_t len, wlen, llen;
       char *ptr = buf;

       if ((len = read(in, buf, bufsize)) < 0) {
              if (errno == EINTR)
                     return 0;
              else
                     return -1;
       }
       llen = len;
       while (llen > 0) {
              if ((wlen = write(out, ptr, llen)) < 0) {
                     if (errno == EINTR)
                            wlen = 0;
                     else
                            return -1;
              }
              llen -= wlen;
              ptr += wlen;
       }
       return len;
}

Here is the call graph for this function:

Here is the caller graph for this function: