Changeset 1234


Ignore:
Timestamp:
Mar 25, 2009, 6:24:59 PM (9 years ago)
Author:
Daniel Kahn Gillmor
Message:

sendfile test now can send to tcp sockets as well as unix-domain sockets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/sendfile/sendfiletester.c

    r1233 r1234  
     1/* Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net> */
     2
     3/* testing error states from sendfile invocations */
     4
     5/* Usage: to sendfile to a unix-domain socket: */
     6/*   sendfiletester src-file target-socket */
     7/* to sendfile to a TCP socket: */
     8/*   sendfiletester src-file host port */
     9
     10/* why am i interested in this?  http://bugs.debian.org/494768 */
     11
    112#include <sys/sendfile.h>
    213#include <sys/types.h>
    314#include <sys/stat.h>
    415#include <sys/socket.h>
     16#include <netdb.h>
    517#include <sys/un.h>
     18#include <netinet/in.h>
     19#include <arpa/inet.h>
    620#include <limits.h>
    721#include <fcntl.h>
    822#include <stdio.h>
    923#include <errno.h>
     24#include <unistd.h>
     25#include <string.h>
     26#include <stdlib.h>
     27
     28
     29char msg[1024];
     30
     31
     32int unixfd(const char* path) {
     33    struct sockaddr_un sockaddr;
     34    int ret;
     35
     36    sockaddr.sun_family = AF_UNIX;
     37    strncpy(sockaddr.sun_path, path, 108);
     38   
     39    ret = socket(AF_UNIX, SOCK_STREAM, 0);
     40    if (ret == -1) {
     41      snprintf(msg, sizeof(msg), "socket() failed");
     42      perror(msg);
     43      exit(errno);
     44    }
     45    if (connect(ret, (const struct sockaddr*) (&sockaddr), sizeof(sockaddr)) == -1) {
     46      snprintf(msg, sizeof(msg), "socket() failed");
     47      perror(msg);
     48      exit(errno);
     49    }
     50    return ret;
     51}
     52
     53/* adapted from getaddrinfo(3) example */
     54int tcpfd(const char* ip, const char* port) {
     55    struct addrinfo hints;
     56    struct addrinfo *result, *rp;
     57    int sfd, s;
     58
     59
     60    snprintf(msg, sizeof(msg), "ip: %s port: %s\n", ip, port);
     61    perror(msg);
     62    /* Obtain address(es) matching host/port */
     63
     64    memset(&hints, 0, sizeof(struct addrinfo));
     65    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
     66    hints.ai_socktype = SOCK_STREAM; /* Stream socket */
     67    hints.ai_flags = 0;
     68    hints.ai_protocol = 0;          /* Any protocol */
     69
     70    s = getaddrinfo(ip, port, &hints, &result);
     71    if (s != 0) {
     72      snprintf(msg, sizeof(msg), "getaddrinfo:");
     73      perror(msg);
     74      exit(1);
     75    }
     76
     77    /* getaddrinfo() returns a list of address structures.
     78       Try each address until we successfully connect(2).
     79       If socket(2) (or connect(2)) fails, we (close the socket
     80       and) try the next address. */
     81
     82    for (rp = result; rp != NULL; rp = rp->ai_next) {
     83      sfd = socket(rp->ai_family, rp->ai_socktype,
     84                   rp->ai_protocol);
     85      if (sfd == -1)
     86        continue;
     87     
     88      if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
     89        break;                  /* Success */
     90     
     91      close(sfd);
     92    }
     93
     94    if (rp == NULL) {
     95      snprintf(msg, sizeof(msg), "could not connect");
     96      exit(1);
     97    }
     98    freeaddrinfo(result);
     99
     100    return sfd;
     101}
    10102
    11103int main(int argc, char** argv) {
    12104    off_t offset;
    13105    ssize_t ret;
    14     char msg[1024];
    15106    int a, b;
    16107    struct stat statbuf;
    17     struct sockaddr_un sockaddr;
    18108
    19109    offset = 0;
    20     sockaddr.sun_family = AF_UNIX;
    21     strncpy(sockaddr.sun_path, argv[2], 108);
    22110
    23111    if (argc < 2) {
     
    32120      return errno;
    33121    }
    34     b = socket(AF_UNIX, SOCK_STREAM, 0);
    35     if (b == -1) {
    36       snprintf(msg, sizeof(msg), "socket() failed");
    37       perror(msg);
    38       return errno;
    39     }
    40     if (connect(b, (const struct sockaddr*) (&sockaddr), sizeof(sockaddr)) == -1) {
    41       snprintf(msg, sizeof(msg), "socket() failed");
    42       perror(msg);
    43       return errno;
    44     }
     122
     123    if (argc == 3)
     124      b = unixfd(argv[2]);
     125    else if (argc == 4)
     126      b = tcpfd(argv[2], argv[3]);
    45127
    46128    if (fstat(a, &statbuf) != 0) {
Note: See TracChangeset for help on using the changeset viewer.