1. 程式人生 > >【2018深信服 醒獅計劃】《Linux應用程式設計》學習筆記

【2018深信服 醒獅計劃】《Linux應用程式設計》學習筆記

2018深信服“醒獅計劃”筆記

第4~5周(5.14-5.27)

課程 必修 選修 基本要求
Linux應用程式設計 《Linux 網路程式設計卷一》《Linux 網路程式設計卷二》 《《Linux4.0裝置驅動開發詳解》 主要熟練掌握Linux程序、執行緒模型、各種程序間通訊方式、網路套接字程式設計、基礎後臺模組架構模型

1. 工欲善其事必先利其器.

根據網上大佬們的建議,決定使用Clion+Clang作為開發,為什麼選擇Clion,因為之前有做過Android開發,對JetBrains的一系列產品情有獨鍾,網上大佬各種吐槽gcc

,強推Clang,那我就先這樣試試.不行再換被,反正程式猿就是喜歡折騰.

怎麼配置我就不說了,網上有很多

這裡寫圖片描述

2. 根據checklist設定註釋模板

有很多,自己去改把

檔案頭註釋範例:
/*
  功能:
  日期:${TIME}
  作者:luojie
*/

這裡寫圖片描述

3. 下載本書的原始碼,遇到很多坑

1. 下載完成後檢視README

Execute the following from the src/ directory:

    ./configure    # try to figure out all implementation differences

    cd lib         # build the basic library
that all programs need make # use "gmake" everywhere on BSD/OS systems cd ../libfree # continue building the basic library make cd ../libroute # only if your system supports 4.4BSD style routing sockets make # only if your system supports 4.4BSD style routing sockets cd ../libxti # only if
your system supports XTI make # only if your system supports XTI cd ../intro # build and test a basic client program make daytimetcpcli ./daytimetcpcli 127.0.0.1 If all that works, you're all set to start compiling individual programs. Notice that all the source code assumes tabs every 4 columns, not 8.

2. unpv13e/libfree目錄下 make報錯

inet_ntop.c:60:9: error: argument ‘size’ doesn’t match prototype
  size_t size;

解決方法:將inet_ntop.csize_t改為socklen_t

3. unpv13e/libroute目錄下 make報錯

gcc -I../lib -g -O2 -D_REENTRANT -Wall   -c -o get_rtaddrs.o get_rtaddrs.c
In file included from get_rtaddrs.c:1:0:
unproute.h:3:45: fatal error: net/if_dl.h: No such file or directory
compilation terminated.

解決方法: 建立if_dl.h 放到/usr/include/net/下,下面是if_dl.h原始碼,可以複製建立檔案

/*
 * Copyright (c) 1990, 1993
 *  The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *  This product includes software developed by the University of
 *  California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  @(#)if_dl.h 8.1 (Berkeley) 6/10/93
 */

/* 
 * A Link-Level Sockaddr may specify the interface in one of two
 * ways: either by means of a system-provided index number (computed
 * anew and possibly differently on every reboot), or by a human-readable
 * string such as "il0" (for managerial convenience).
 * 
 * Census taking actions, such as something akin to SIOCGCONF would return
 * both the index and the human name.
 * 
 * High volume transactions (such as giving a link-level ``from'' address
 * in a recvfrom or recvmsg call) may be likely only to provide the indexed
 * form, (which requires fewer copy operations and less space).
 * 
 * The form and interpretation  of the link-level address is purely a matter
 * of convention between the device driver and its consumers; however, it is
 * expected that all drivers for an interface of a given if_type will agree.
 */

/*
 * Structure of a Link-Level sockaddr:
 */
struct sockaddr_dl {
    u_char  sdl_len;    /* Total length of sockaddr */
    u_char  sdl_family; /* AF_DLI */
    u_short sdl_index;  /* if != 0, system given index for interface */
    u_char  sdl_type;   /* interface type */
    u_char  sdl_nlen;   /* interface name length, no trailing 0 reqd. */
    u_char  sdl_alen;   /* link level address length */
    u_char  sdl_slen;   /* link layer selector length */
    char    sdl_data[12];   /* minimum work area, can be larger;
                   contains both if name and ll address */
};

#define LLADDR(s) ((caddr_t)((s)->sdl_data + (s)->sdl_nlen))

#ifndef KERNEL

#include <sys/cdefs.h>

__BEGIN_DECLS
void    link_addr __P((const char *, struct sockaddr_dl *));
char    *link_ntoa __P((const struct sockaddr_dl *));
__END_DECLS

#endif /* !KERNEL */

4. RTAX_MAX變數未宣告

README裡面說BSD的系統才用編譯這個。自己的系統是基於Ubuntu系統,所以不用編譯這個,直接跳到第五步。

get_rtaddrs.c:21:18: error: ‘RTAX_MAX’ undeclared (first use in this function)
  for (i = 0; i < RTAX_MAX; i++) {
                  ^

解決辦法:get_rtaddrs.cRTAX_MAX變數未宣告,在/libroute/unproute.h#define RTAX_MAX 1024

5. Run

linux 現在因為安全問題,各個發行版本預設是不開daytime服務的。

第一個例子實際上是兩個程式客戶端服務端,所以要開個終端

  • 一個執行:
make daytimetcpsrv
sudo ./daytimetcpsrv
  • 另一個執行
./daytimetcpcli 127.0.0.1
  • 返回結果:
Wed May 16 16:37:05 2018

這裡寫圖片描述